This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() | |
``` |