This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compose(...funcs) | |
{ | |
function _compose(...args) | |
{ | |
let result = args; | |
for (func of funcs) | |
{ | |
if ( !Array.isArray(result) ) | |
result = [result]; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
OlderNewer