Skip to content

Instantly share code, notes, and snippets.

@tyong920
tyong920 / trace.py
Created May 11, 2017 02:09
A decorator that is helpful when debugging a stack of function calls from a recursive function.
from functools import wraps
def trace(func):
@wraps
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
print('%s(%r, %r) -> %r' %
(func.__name__, args, kwargs, result))
return result
@tyong920
tyong920 / str_bytes_helper.py
Created May 5, 2017 09:35
Helper functions to convert between bytes and str for Python 3(unicode and str in Python 2)
# Python 3
def to_str(bytes_or_str):
if isinstance(bytes_or_str, bytes):
value = bytes_or_str.decode('utf-8')
else:
value = bytes_or_str
return value # Instance of str
def to_bytes(bytes_or_str):