Skip to content

Instantly share code, notes, and snippets.

@jbasko
jbasko / context_entering_decorator.py
Last active January 27, 2018 10:05
Generator-aware decorator factory
def context_entering_decorator(context_manager):
"""
Creates a context-entering decorator that does not fail to do its job
if the underlying function is actually a generator function.
"""
def decorator(func):
if inspect.isgeneratorfunction(func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
@jbasko
jbasko / universal_decorator.py
Created January 12, 2018 11:17
universal decorator
class universal_decorator:
wrappers = []
def __init__(self, method):
self.original_method = method
self.method = self.original_method
self.apply_decorators()
def apply_decorators(self):
for wrapper in self.wrappers:
@jbasko
jbasko / list_functions_in_module_ordered.py
Created April 14, 2017 16:29
List names of functions declared in a Python module in the order of declaration
"""
Gets list of functions in a module, preserve declaration order.
This cannot be done with inspect.getmembers() or dir() because they lose the order.
Have to construct AST with ast.
"""
import ast
import importlib
import os.path
import re
class _ValueNotSet(object):
pass
ValueNotSet = _ValueNotSet()
@jbasko
jbasko / logbook_example.py
Created November 15, 2016 15:08
Logbook setup example
"""
API request logger:
One record per each API request received
1) Logs to a file in logstash format.
App logger:
Your usual logger that just writes down what the code is doing.
Mostly quiet on production except when errors happen.
Includes stack traces.