Pickle Ricking

Simple classes for encapsulating Python objects, with functions to write these simple objects to YAML or JSON.

Basic Rick

class pickle_rick.__init__.BasicRick(base: Union[dict, str, _io.TextIOWrapper] = None, deep: bool = False, args: dict = None)

A base class that creates internal structures from embedded structures.

Parameters
  • base (str) – String (YAML or JSON, file path to YAML/JSON file), text IO stream, dict (default = None).

  • deep (bool) – Internalize dictionary structures in lists (default = False).

  • args (dict) – Intended for extended classes to handle overriden _internalize customisation (default = None).

Raises

ValueError – If the given base object can not be handled.

add_attr(name, value)

Add a new attribute member to Rick.

Parameters
  • name (str) – Property name.

  • value (any) – Value of new member.

dict()

Deconstructs the whole object into a Python dictionary.

Returns

of object.

Return type

dict

get(key: str, default=None)

Employs a recursive search of structure and returns the first found key-value pair. Searches with normal, upper, and lower case.

Parameters
  • key (str) – key string being searched.

  • default (any) – Return value if nothing is found.

Returns

value found, or None for nothing found.

Return type

obj

has(key: str, deep=False) → bool

Checks whether the key exists in the object.

Parameters
  • key (str) – key string being searched.

  • deep (bool) – whether to search deeply (default = False).

Returns

if found.

Return type

bool

items()

Iterate through all key value pairs.

Yields

tuple – str, object.

keys()

Gets the higher level keys of the current Rick object.

Returns

of keys.

Return type

list

to_json_file(file_path: str)

Does a self dump to a JSON file.

Parameters

file_path (str) – File path.

to_json_string()

Dumps self to YAML string.

Returns

JSON representation.

Return type

str

to_yaml_file(file_path: str)

Does a self dump to a YAML file.

Parameters

file_path (str) – File path.

to_yaml_string()

Dumps self to YAML string.

Returns

YAML representation.

Return type

str

values()

Gets the higher level values of the current Rick object.

Returns

of objects.

Return type

list

Pickle Rick

An extended version of BasicRick that allows extra easy loading, including Python functions, lambdas, and OS environmental variables. Contains all the same methods in BasicRick.

class pickle_rick.__init__.PickleRick(base: Union[dict, str] = None, deep: bool = False, load_lambda: bool = False)

An extended version of the BasicRick that can load OS environ variables and Python Lambda functions.

Parameters
  • base (str) – String (YAML or JSON, file path to YAML/JSON file), text IO stream, dict.

  • deep (bool) – Internalize dictionary structures in lists.

  • load_lambda (bool) – Load lambda as code or strings.

add_env_variable(name, load, default=None)

Add a new OS ENVIRONMENT VARIABLE to Rick.

Parameters
  • name (str) – Property name.

  • load (str) – ENV var name.

  • default (any) – Default to value (default = None).

add_function(name, load, args: pickle_rick.__init__.PickleRick.dict = None, imports: list = None)

Add a new function to Rick.

Parameters
  • name (str) – Property name.

  • load (str) – Python code containing the function.

  • args (dict) – Key-value pairs of arguments with default values (default = None).

  • imports (list) – Python modules to import (default = None).

Examples

Basic example for adding to a PickleRick:

>> test_rick = PickleRick()

>> load = ‘’’
def tester(x, c):

y = x * 2 + c return math.cos(y)

‘’’

>> args = { ‘x’ : 0.42, ‘c’ : 1.7 }

>> imports = [‘math’]

>> test_rick.add_function(‘tester’,load, args, imports)

>> y = test_rick.tester(x=0.66, c=1.6)

add_lambda(name, load, imports: list = None)

Add a Python lambda to Rick.

Parameters
  • name (str) – Property name.

  • load (str) – Python code containing the lambda.

  • imports (list) – Python modules to import (default = None).

Examples

Basic example for adding to a PickleRick:

>> test_rick = PickleRick()

>> load = “lambda: dd.utcnow().strftime(‘%Y-%m-%d’)”

>> imports = [‘from datetime import datetime as dd’]

>> test_rick.add_lambda(‘date_str’, load, imports)

>> date_string = test_rick.date_str()

dict()

Deconstructs the whole object into a Python dictionary.

Returns

of object.

Return type

dict