Skip to content

Instantly share code, notes, and snippets.

View Bl41r's full-sized avatar

David Smith Bl41r

View GitHub Profile
@Bl41r
Bl41r / gist:93a89e3efc6c86cb6edd80e0b41a401d
Created February 18, 2018 02:18
Decorator to create a deep copy of a module
def create_copy_of_module(module, new_name):
"""Decorator to copy a module for mock testing without side effects.
module and new_name are strings of the module to copy, and the name that
the copy will be called so that it can then be imported.
"""
def wrap(f):
def wrapped_f(*args):
orig_spec = importlib.util.find_spec(module)
new = importlib.util.module_from_spec(orig_spec)
@Bl41r
Bl41r / codechallenge2.py
Created August 19, 2016 16:34
code challenge 2
def smart_substr(in_str, terminate='...'):
if len(in_str) <= 30:
return in_str + terminate
else:
if in_str[30] != ' ':
return ' '.join(in_str[0:30].split()[:-1]) + terminate
else:
return in_str[0:30] + terminate