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 / 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 / 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 / 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 / decorator_reporter.py
Created October 3, 2017 21:18
A decorator function that will "report."
def decorator_reporter(report_string="Running...", period=1, reporting_function=None):
"""
A decorator function that will "report."
This function returns 'function_wrapper.'
:param report_string: The string to "report."
:param period: The number of seconds between reports.
:param reporting_function: The reporting function to use instead of 'print.'
This could be a logging function like 'logging.debug' for instance.
This must be capable of taking no more than one string argument.
@saintsGrad15
saintsGrad15 / addAlias.bash
Created October 5, 2017 14:30
A function to add an alias declaration to ~/.bash_profile
function addAlias()
{
echo "alias $1='$2'" >> ~/.bash_profile
. ~/.bash_profile #reload .bash_profile
}