Skip to content

Instantly share code, notes, and snippets.

View saintsGrad15's full-sized avatar

John Carrell saintsGrad15

View GitHub Profile
@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}
@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 / 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 / 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 / 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 / context_manager_heartbeater.py
Last active October 3, 2017 21:16
A heartbeater class that implements the context manager interface.
from threading import Thread
from time import sleep
class Heartbeater(Thread):
"""
A class that adheres to the Thread and Context Manager interfaces.
"""
def __init__(self, work=None, work_args=None, work_kwargs=None, period=1, daemon=False):
"""
@saintsGrad15
saintsGrad15 / getObjectValueFromDottedNotation.js
Created April 14, 2017 20:03
A function to retrieve the value inside an object at a dot-delimited path.
function getObjectValueFromDottedNotation(object, dottedString)
{
/*
* Given an object 'object' and a string 'dottedString' that contains a dot-delimited path
* down the object's members, return the value at that path or false if the entire path does not exist.
*
* For example:
* obj = {
* "person": {
* "name": "john",
@saintsGrad15
saintsGrad15 / compose.js
Created June 15, 2017 14:04
A composition function that takes N functions, runs them in order, passing arguments to each other and returning the final result.
function compose(...funcs)
{
function _compose(...args)
{
let result = args;
for (func of funcs)
{
if ( !Array.isArray(result) )
result = [result];
@saintsGrad15
saintsGrad15 / Compose.py
Created June 30, 2017 16:54
A composition class for Python
class Compose(object):
"""
Return a function that, when called, will execute each function in 'functions'
in order, passing the return value of each to the next one
and returning the result of the last one.
NOTE: Generator functions cannot be used.
:param functions: A list of functions.
NOTE: Ensure that the output of one function makes sense
as an input to the next function. A list of values can be
@saintsGrad15
saintsGrad15 / Reporter.py
Last active October 6, 2017 16:59
A Heartbeater offspring that reports periodically.
class Reporter(Heartbeater):
"""
Behaves like a Heartbeater but specifically designed to print statements periodically.
"""
def __init__(self, report_string, period=1, reporting_function=None):
"""
Every 'period' seconds 'report_string' will be passed to 'reporting_function.'
If 'reporting_function' is None it will print 'report_string' instead.