1. Rickle Picking¶
Table of Contents
Simple classes for encapsulating Python objects, with functions to write these simple objects to YAML or JSON.
1.1. Basic Rickle¶
- class rickled.__init__.BaseRickle(base: Optional[Union[dict, str, _io.TextIOWrapper, list]] = None, deep: bool = False, **init_args)¶
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).
- 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(serialised: bool = False)¶
Deconstructs the whole object into a Python dictionary.
- Parameters
serialised (bool) – Give a Python dictionary in serialised (True) form or deserialised (default = False).
Notes
Functions and lambdas are always given in serialised form.
- Returns
of object.
- Return type
dict
- get(key: str, default=None, do_recursive: bool = False)¶
Acts as a regular get from a dictionary but can employ a recursive search of structure and returns the first found key-value pair.
- Parameters
key (str) – key string being searched.
default (any) – Return value if nothing is found.
do_recursive (bool) – Search recursively until first match is found (default = False).
- 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
- search_path(key: str) list ¶
Search the current Rickle for all paths that match the search key. Returns empty list if nothing is found.
- Parameters
key (str) – The key to search.
- Returns
all paths found.
- Return type
list
- to_json_file(file_path: str, serialised: bool = True)¶
Does a self dump to a JSON file.
- Parameters
file_path (str) – File path.
serialised (bool) – Give a Python dictionary in serialised (True) form or deserialised (default = True).
Notes
Functions and lambdas are always given in serialised form.
- to_json_string(serialised: bool = True)¶
Dumps self to YAML string.
- Parameters
serialised (bool) – Give a Python dictionary in serialised (True) form or deserialised (default = True).
Notes
Functions and lambdas are always given in serialised form.
- Returns
JSON representation.
- Return type
str
- to_yaml_file(file_path: str, serialised: bool = True)¶
Does a self dump to a YAML file.
- Parameters
file_path (str) – File path.
serialised (bool) – Give a Python dictionary in serialised (True) form or deserialised (default = True).
Notes
Functions and lambdas are always given in serialised form.
- to_yaml_string(serialised: bool = True)¶
Dumps self to YAML string.
- Parameters
serialised (bool) – Give a Python dictionary in serialised (True) form or deserialised (default = True).
Notes
Functions and lambdas are always given in serialised form.
- Returns
YAML representation.
- Return type
str
- values()¶
Gets the higher level values of the current Rick object.
- Returns
of objects.
- Return type
list
1.2. Rickle Pick¶
An extended version of BaseRickle that allows extra easy loading, including Python functions, lambdas, and OS environmental variables. Contains all the same methods in BasicRick.
- class rickled.__init__.Rickle(base: Optional[Union[dict, str, _io.TextIOWrapper, list]] = None, deep: bool = False, load_lambda: bool = False, **init_args)¶
An extended version of the BasicRick that can load OS environ variables and Python Lambda functions.
- Parameters
base (str, list) – String (YAML or JSON, file path to YAML/JSON file) or list of file paths, text IO stream, dict.
deep (bool) – Internalize dictionary structures in lists.
load_lambda (bool) – Load lambda as code or strings.
- add_api_json_call(name, url: str, http_verb: str = 'GET', headers: Optional[rickled.__init__.Rickle.dict] = None, params: Optional[rickled.__init__.Rickle.dict] = None, body: Optional[rickled.__init__.Rickle.dict] = None, load_as_rick: bool = False, deep: bool = False, load_lambda: bool = False, expected_http_status: int = 200, hot_load: bool = False)¶
Load a JSON response from a URL and create a Rick from it. This opens up dynamic possibility, but with that it also opens up extreme security vulnerabilities. Only ever load JSON objects from trusted sources. Important note: Even with ``deep`` and ``load_lambda`` set to False, further API calls could be found within the source that loads lambda functions. Important note: Be careful to never self-reference an API call, i.e. don’t load the same API from within itself to avoid infinte looping.
- Parameters
name (str) – Property name.
url (str) – URL to load from.
http_verb (str) – Either ‘POST’ or ‘GET’ allowed (default = ‘GET’).
headers (dict) – Key-value pair for headers (default = None).
params (dict) – Key-value pair for parameters (default = None).
body (dict) – Key-value pair for data (default = None).
load_as_rick (bool) – If true, loads and creates Rick from source, else loads the contents as dictionary (default = False).
deep (bool) – Internalize dictionary structures in lists (default = False).
load_lambda (bool) – Load lambda as code or strings (default = False).
expected_http_status (int) – Should a none 200 code be expected (default = 200).
hot_load (bool) – Load the data on calling or load it only once on start (cold) (default = False).
- add_base64(name, load)¶
Add Base 64 encoded byte string data.
- Parameters
name (str) – Property name.
load (str) – Base 64 encoded data.
- add_class_definition(name, attributes, imports: Optional[list] = None)¶
Adds a class definition, with attributes such as functions and lambdas.
- Parameters
name (str) – Property name.
attributes (dict) – Standard items or Rickle function definitions.
imports (list) – Python modules to import (default = None).
- add_csv_file(name, file_path: str, fieldnames: Optional[list] = None, load_as_rick: bool = False, encoding: str = 'utf-8')¶
Adds the ability to load CSV data as lists or even a list of Ricks where the column names are the properties.
- Parameters
name (str) – Property name.
file_path (str) – File path to load from.
fieldnames (list) – Column headers (default = None).
load_as_rick (bool) – If true, loads and creates Rick from source, else loads the contents as text (default = False).
encoding (str) – If text, encoding can be specified (default = ‘utf-8’).
- 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_from_file(name, file_path: str, load_as_rick: bool = False, deep: bool = False, load_lambda: bool = False, is_binary: bool = False, encoding: str = 'utf-8', hot_load: bool = False)¶
Adds the ability to further load Ricks from other YAML or JSON files, or alternatively load a text file. This opens up dynamic possibility, but with that it also opens up extreme security vulnerabilities. Only ever load files from trusted sources. Important note: Even with ``deep`` and ``load_lambda`` set to False, further file or API calls could be found within the source that loads lambda functions. Important note: Be careful to never self-reference a file, i.e. don’t load the same file from within itself to avoid infinte looping.
- Parameters
name (str) – Property name.
file_path (str) – File path to load from.
load_as_rick (bool) – If true, loads and creates Rick from source, else loads the contents as text (default = False).
deep (bool) – Internalize dictionary structures in lists (default = False).
load_lambda (bool) – Load lambda as code or strings (default = False).
is_binary (bool) – If the file is a binary file (default = False).
encoding (str) – If text, encoding can be specified (default = ‘utf-8’).
hot_load (bool) – Load the data on calling or load it only once on start (cold) (default = False).
- add_function(name, load, args: Optional[rickled.__init__.Rickle.dict] = None, imports: Optional[list] = None, return_function: bool = False, is_method: bool = False)¶
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).
return_function (bool) – Add to rickle or return the function (default = False).
is_method (bool) – Indicates whether class method source includes self (default = False).
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_html_page(name, url: str, headers: Optional[rickled.__init__.Rickle.dict] = None, params: Optional[rickled.__init__.Rickle.dict] = None, expected_http_status: int = 200, hot_load: bool = False)¶
Loads HTML page as property.
- Parameters
name (str) – Property name.
url (str) – URL to load from.
headers (dict) – Key-value pair for headers (default = None).
params (dict) – Key-value pair for parameters (default = None).
expected_http_status (int) – Should a none 200 code be expected (default = 200).
hot_load (bool) – Load the data on calling or load it only once on start (cold) (default = False).
- add_lambda(name, load, imports: Optional[list] = None, return_lambda: bool = False, is_method: bool = False)¶
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).
return_lambda (bool) – Add to rickle or return the lambda (default = False).
is_method (bool) – Add self param (default = False).
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()
- add_module_import(name, imports: list)¶
Add global Python module imports.
- Parameters
name – Name of import list.
imports (list) – List of strings of Python module names.
- dict(serialised: bool = False)¶
Deconstructs the whole object into a Python dictionary.
- Parameters
serialised (bool) – Give a Python dictionary in serialised (True) form or deserialised (default = False).
Notes
Functions and lambdas are always given in serialised form.
- Returns
of object.
- Return type
dict
1.3. Object Rickler¶
The ObjectRickler attempts to convert Python objects to Rickle objects for YAML/JSON export, and then reconstructing the YAML/JSON files to the Python objects.
- class rickled.__init__.ObjectRickler¶
A class to convert Python objects to Rickle objects, deconstruct objects, create objects from Rickle objects.
Notes
tuple types are deconstructed as lists
- deconstruct(obj, include_imports: bool = False, include_class_source: bool = False)¶
Takes (almost) any Python object and deconstructs it into a dict.
- Parameters
obj – Any object.
include_imports (bool) – Add a list of modules to import as is imported in current env (default = False).
include_class_source (bool) – Add the source of the object’s class (default = False).
- Returns
Deconstructed object in typical Rickle dictionary format.
- Return type
dict
- from_rickle(rickle, cls: rickled.__init__.T, **args) rickled.__init__.T ¶
Takes a Rickle and initialises the class and updates attributes with the ones from the Rickle.
- Parameters
rickle (Rickle) – Rickle to create from.
cls (type) – The class to initialise from.
- Returns
Initiliased cls.
- Return type
object
- to_json_file(file_path, obj)¶
Dumps the object to file.
- Parameters
file_path – Filename.
obj – Any object.
- to_json_string(obj)¶
Dumps the object to string.
- Parameters
obj – Any object.
- Returns
Dumped object.
- Return type
str
- to_rickle(obj, deep: bool = False, load_lambda: bool = False)¶
Transforms a Python object into a Rickle.
- Parameters
obj – Any initialised Python object.
deep (bool) – Internalize dictionary structures in lists (default = False).
load_lambda (bool) – Load python code as code or strings (default = False).
- Returns
A constructed Rickle object.
- Return type
- to_yaml_file(file_path, obj)¶
Dumps the object to file.
- Parameters
file_path – Filename.
obj – Any object.
- to_yaml_string(obj)¶
Dumps the object to string.
- Parameters
obj – Any object.
- Returns
Dumped object.
- Return type
str