Skip to content

Instantly share code, notes, and snippets.

View saintsGrad15's full-sized avatar

John Carrell saintsGrad15

View GitHub Profile
@saintsGrad15
saintsGrad15 / multisplit.py
Last active October 25, 2016 20:49
Will split 'string' using all of the additional arguments to multisplit.
def multisplit(string, *args):
"""
Will split 'string' using all of the additional arguments to multisplit.
Will then filter out any instances of empty strings in the resultant list
For example
result = multisplit("a, b c,d e", ",", " ") # Split on both commas and spaces
result == ["a", "b", "c", "d", "e"]
@saintsGrad15
saintsGrad15 / sanitize_dict_keys.py
Last active June 20, 2016 15:24
Inspect every key in an arbitrarily nested dict ('dictionary') for the existence of any of the keys in 'character_replacement_dict.' Each instance will be replaced by the corresponding value in 'character_replacement_dict.'
def sanitize_dict_keys(dictionary, character_replacement_dict):
"""
Inspect every key in an arbitrarily nested dict ('dictionary')
for the existence of any of the keys in 'character_replacement_dict.'
Each instance will be replaced by the corresponding value in 'character_replacement_dict.'
NOTE: sanitize_dict_keys() will properly sanitize an arbitrarily nested dict
with values that are or inherit from either primitives, dicts, lists, tuples or sets.
This type check is done using the 'isinstance()' function.
@saintsGrad15
saintsGrad15 / verify_dict_path.py
Created March 21, 2016 18:18
A function to verify the ordered nesting of dict members
def verify_dict_path(dictionary, *args):
"""
Verify that 'dictionary' contains nested members consistent with the additional arguments to 'verify_dict_path' in order
e.g. For the_dict = {"a" : {"b" : {"c" : 3}}}
verify_dict_path(the_dict, "a", "b", "c") == True
verify_dict_path(the_dict, "a", "b") == True
verify_dict_path(the_dict, "a", "b", "c", "d") == False
verify_dict_path(the_dict, "a", "b", "e") == False
:param dictionary: A dictionary
@saintsGrad15
saintsGrad15 / memoize.py
Last active July 26, 2019 14:27
A memoize wrapper for use as a decorator
# Author : John Carrell
#
# A memoize function to use as a decorator
#
# Memoization is caching the arguments to and result of a function.
# Then, the next time that function is called with the same arguments the result is taken from the cache rather invoking the function.
# Memoization is effective when executing the logic of a particularly function is expensive.
# For instance, expensive calculations, network I/O or file access would be effective things to avoid if the yielded value won't or doesn't need to change.
#
# A detministic function is one where for a given input the same output is always produced.
@saintsGrad15
saintsGrad15 / convert_dot_to_bracket.js
Last active March 17, 2016 16:16
Convert a dot notated object path to a square bracket notated object path
/**
* Takes a dot notated object string ("root.child1.child2.child3") and converts it to square bracket notation ("root[child1][child2][child3]").
*
* @param dot_string {string}
* A string in dot notation.
* * Must be of a non-zero length
* * Must include at least one "."
* * Must have more than one token
*
* @param include_single_quotes {boolean}