Skip to content

Instantly share code, notes, and snippets.

View francoiscampbell's full-sized avatar

Francois Campbell francoiscampbell

View GitHub Profile
@francoiscampbell
francoiscampbell / safety.py
Created November 3, 2021 16:08
Python safety
class Safety:
"""Context manager that catches and reports all exceptions.
Use this to catch and report exceptions around non-critical
code.
"""
def __init__(self, reporter=None):
"""
:param reporter A function that gets called with
tg_foo_bar = TargetGroupArn(
"arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/foo-bar/abc123"
)
# generate a new ARN from the first one
tg_baz_quz = TargetGroupArn(
target_group_arn,
name="baz-qux",
internal_id="def456",
)
def describe_target_group(arn: TargetGroupArn):
# at this point, we know "arn" is a valid ARN
return client.describe_target_groups(
TargetGroupArns=[str(arn)]
)
TargetGroupArn("not an ARN") # raises arn.InvalidArnException
from arn.elbv2 import TargetGroupArn
target_group_arn_str = (
"arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/foo-bar/abc123"
)
target_group_arn = TargetGroupArn(target_group_arn_str)
# use the ARN instance's __str__ to format the ARN back into a string
assert str(target_group_arn) == target_group_arn_str
def pk_or_instance(fn):
try:
first_param_name = next(iter(inspect.signature(fn).parameters))
except StopIteration:
raise ValueError(f"Function {fn} needs to take at least one parameter")
type_hints = get_type_hints(fn, fn.__globals__)
model_type = type_hints.get(first_param_name)
if not model_type:
raise ValueError(
(function(){debugger; console.log(window.localStorage)})()
@francoiscampbell
francoiscampbell / agnoster-mods
Last active November 26, 2018 22:18
Moves the cwd to the RPROMPT and only shows the final path component (rprompt_segment taken from https://gist.github.com/rjorgenson/83094662ace4d3b82b95)
prompt_context() {
}
prompt_dir () {
prompt_segment blue black '$'
}
RSEGMENT_SEPARATOR="\ue0b2"
rprompt_segment() {