Skip to content

Instantly share code, notes, and snippets.

View TySkby's full-sized avatar

Tyler Hendrickson TySkby

View GitHub Profile
@TySkby
TySkby / class_hierarchy_builder.py
Created June 15, 2017 19:17
Build class hierarchies (and print them)
"""Utilities to build and view class hierarchy represntations in Python"""
def build_class_hierarchy(base_class, dot_paths=False):
"""
Given any (base) class, produces a nested dictionary
representation of the hierarchy formed by all descendents
of the class, with the given `base_class` forming the root
of the hierarchy.
:param base_class: The parent-most class on which the
hierarchy will be based
@TySkby
TySkby / minimum_time.py
Last active October 27, 2016 23:29
Decorator that ensures a function takes at least X seconds to execute
"""
Simple decorator that prevents a function from finishing faster than a given number of seconds.
Useful if you're fairly confident that your function should finish in a certain amount of time,
but you want to make the return time constant (eg. to prevent constant-time attacks).
For example, if I know that my password reset function always takes about 1 second to execute
if the given email address is valid but if the email address is invalid, it usually finishes much faster,
I can ensure that it always takes 2 seconds to execute whenever it's called.
The usefulness here goes out the window if your function's normal execution time is subject
@TySkby
TySkby / timeout.py
Last active March 9, 2023 04:24
Timeout decorator/context manager using signals (for Python 3)
#!/usr/bin/env python3
"""Easily put time restrictions on things
Note: Requires Python 3.x
Usage as a context manager:
```
with timeout(10):
something_that_should_not_exceed_ten_seconds()
```