merlin.functions

functions.py is a module of generalized, reusable functions

merlin.functions.chexists(dictionary, keys, check_fn)[source]

applies check_fn against dictionary minus keys then ensures the items returned from check_fn exist in dictionary[keys]

Parameters:
  • dictionary (dict) – {key: [v1, v3, v2]}
  • keys (sequence) – A sequence of keys in dictionary
  • check_fn (function) – Function that accepts dict and returns sequence of items or Exception
Returns:

A sequence of items that are returned from check_fn and exist in dictionary[keys] or Exception

merlin.functions.cqlstr(string)[source]

Makes a string safe to use in Cassandra CQL commands

Parameters:string – The string to use in CQL
Returns:A safe string replacement
Return type:str
merlin.functions.denumpify(arg)[source]

Converts numpy datatypes to python datatypes

bool_ and bool8 are converted to Python bool float64, float32 and float16’s are converted to Python float() intc, intp, int8, int16, int32, int64, uint8, uint16, uint32 and uint64 are converted to Python int() complex_, complex64 and complex128 are converted to Python complex() None is returned as None numpy ndarrays are returned as list()

Python lists, maps, sets and tuples are returned with all values converted to Python types (recursively).

If there is no implemented converter, returns arg.

Parameters:arg – A (possibly numpy) data structure
Returns:A Python data structure
merlin.functions.deserialize(string)[source]

Reconstitues datastructure from a string.

Parameters:string – A serialized data structure
Returns:Data structure represented by string parameter
merlin.functions.difference(a, b)[source]

Subtracts items in b from items in a.

Parameters:
  • a – sequence a
  • b – sequence b
Returns:

items that exist in a but not b

Return type:

set

merlin.functions.extract(sequence, elements)[source]

Given a sequence (possibly with nested sequences), extract the element identifed by the elements sequence.

Parameters:
  • sequence – A sequence of elements which may be other sequences
  • elements – Sequence of nested element indicies (in sequence parameter) to extract
Returns:

The target element

Example

>>> inputs = [1, (2, 3, (4, 5)), 6]
>>> extract(inputs, [0])
>>> 1
>>> extract(inputs, [1])
>>> (2, 3, (4, 5))
>>> extract(inputs, [1, 0])
>>> 2
>>> extract(inputs, [1, 1])
>>> 3
>>> extract(inputs, [1, 2])
>>> (4, 5)
>>> extract(inputs, [1, 2, 0])
>>> 4

merlin.functions.flatten(iterable)[source]

Reduce dimensionality of iterable containing iterables

Parameters:iterable – A multi-dimensional iterable
Returns:A one dimensional iterable
merlin.functions.flip_keys(dods)[source]

Accepts a dictionary of dictionaries and flips the outer and inner keys. All inner dictionaries must have a consistent set of keys or key Exception is raised.

Parameters:dods – dict of dicts
Returns:dict of dicts with inner and outer keys flipped

Example

>>> dods = {"reds":   {(0, 0): [110, 110, 234, 664],
                       (0, 1): [23, 887, 110, 111]},
            "greens": {(0, 0): [120, 112, 224, 624],
                       (0, 1): [33, 387, 310, 511]},
            "blues":  {(0, 0): [128, 412, 244, 654],
                       (0, 1): [73, 987, 119, 191]},
>>> flip_keys(dods)
{(0, 0): {"reds":   [110, 110, 234, 664],
          "greens": [120, 112, 224, 624],
          "blues":  [128, 412, 244, 654], ... },
 (0, 1), {"reds":   [23, 887, 110, 111],
          "greens": [33, 387, 310, 511],
          "blues":  [73, 987, 119, 191], ... }}
merlin.functions.insert_into_every(dods, key, value)[source]

Insert key:values into every subdictionary of dods.

Parameters:
  • dods – dictionary of dictionaries
  • key – key to hold values in subdictionaires
  • value – value to associate with key
Returns:

dictionary of dictionaries with key:values inserted into each

Return type:

dict

merlin.functions.intersection(items)[source]

Returns the intersecting set contained in items

Parameters:items – Two dimensional sequence of items
Returns:Intersecting set of items

Example

>>> items = [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
>>> intersection(items)
{3}
merlin.functions.isnumeric(value)[source]

Does a string value represent a number (positive or negative?)

Parameters:value (str) – A string
Returns:True or False
Return type:bool
merlin.functions.issubset(a, b)[source]

Determines if a exists in b

Parameters:
  • a – sequence a
  • b – sequence b
Returns:

True or False

Return type:

bool

merlin.functions.md5(string)[source]

Computes and returns an md5 digest of the supplied string

Parameters:string – string to digest
Returns:digest value
merlin.functions.represent(item)[source]

Represents callables and values consistently

Parameters:item – The item to represent
Returns:Item representation
merlin.functions.rsort(iterable, key=None)[source]

Reverse sorts an iterable

merlin.functions.seqeq(a, b)[source]

Determine if two unordered sequences are equal.

Parameters:
  • a – sequence a
  • b – sequence b
Returns:

True or False

Return type:

bool

merlin.functions.serialize(arg)[source]

Converts datastructure to json, computes digest

Parameters:dictionary – A python dict
Returns:(digest,json)
Return type:tuple
merlin.functions.sha256(string)[source]

Computes and returns a sha256 digest of the supplied string

Parameters:string (str) – string to digest
Returns:digest value
merlin.functions.sort(iterable, key=None)[source]

Sorts an iterable

merlin.functions.timed(f)[source]

Timing wrapper for functions. Prints start and stop time to INFO along with function name, arguments and keyword arguments.

Parameters:f (function) – Function to be timed
Returns:Wrapped function
Return type:function