Context
The Context module is a part of the Koheesio framework and is primarily used for managing the environment configuration where a Task or Step runs. It helps in adapting the behavior of a Task/Step based on the environment it operates in, thereby avoiding the repetition of configuration values across different tasks.
The Context class, which is a key component of this module, functions similarly to a dictionary but with additional features. It supports operations like handling nested keys, recursive merging of contexts, and serialization/deserialization to and from various formats like JSON, YAML, and TOML.
For a comprehensive guide on the usage, examples, and additional features of the Context class, please refer to the reference/concepts/context section of the Koheesio documentation.
koheesio.context.Context #
The Context class is a key component of the Koheesio framework, designed to manage configuration data and shared variables across tasks and steps in your application. It behaves much like a dictionary, but with added functionalities.
Key Features
- Nested keys: Supports accessing and adding nested keys similar to dictionary keys.
- Recursive merging: Merges two Contexts together, with the incoming Context having priority.
- Serialization/Deserialization: Easily created from a yaml, toml, or json file, or a dictionary, and can be converted back to a dictionary.
- Handling complex Python objects: Uses jsonpickle for serialization and deserialization of complex Python objects to and from JSON.
For a comprehensive guide on the usage, examples, and additional features of the Context class, please refer to the reference/concepts/context section of the Koheesio documentation.
Methods:
Name | Description |
---|---|
add |
Add a key/value pair to the context. |
get |
Get value of a given key. |
get_item |
Acts just like |
contains |
Check if the context contains a given key. |
merge |
Merge this context with the context of another, where the incoming context has priority. |
to_dict |
Returns all parameters of the context as a dict. |
from_dict |
Creates Context object from the given dict. |
from_yaml |
Creates Context object from a given yaml file. |
from_json |
Creates Context object from a given json file. |
Dunder methods
- _
_iter__()
: Allows for iteration across a Context. __len__()
: Returns the length of the Context.__getitem__(item)
: Makes class subscriptable.
Inherited from Mapping
items()
: Returns all items of the Context.keys()
: Returns all keys of the Context.values()
: Returns all values of the Context.
Source code in src/koheesio/context.py
add #
contains #
from_dict
classmethod
#
from_json
classmethod
#
Creates Context object from a given json file
Note: jsonpickle is used to serialize/deserialize the Context object. This is done to allow for objects to be stored in the Context object, which is not possible with the standard json library.
Why jsonpickle?
(from https://jsonpickle.github.io/)
Data serialized with python’s pickle (or cPickle or dill) is not easily readable outside of python. Using the json format, jsonpickle allows simple data types to be stored in a human-readable format, and more complex data types such as numpy arrays and pandas dataframes, to be machine-readable on any platform that supports json.
Security
(from https://jsonpickle.github.io/)
jsonpickle should be treated the same as the Python stdlib pickle module from a security perspective.
! Warning !#
The jsonpickle module is not secure. Only unpickle data you trust. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling. Never unpickle data that could have come from an untrusted source, or that could have been tampered with. Consider signing data with an HMAC if you need to ensure that it has not been tampered with. Safer deserialization approaches, such as reading JSON directly, may be more appropriate if you are processing untrusted data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_file_or_str
|
Union[str, Path]
|
Pathlike string or Path that points to the json file or string containing json |
required |
Returns:
Type | Description |
---|---|
Context
|
|
Source code in src/koheesio/context.py
from_toml
classmethod
#
Creates Context object from a given toml file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
toml_file_or_str
|
Union[str, Path]
|
Pathlike string or Path that points to the toml file or string containing toml |
required |
Returns:
Type | Description |
---|---|
Context
|
|
Source code in src/koheesio/context.py
from_yaml
classmethod
#
Creates Context object from a given yaml file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
yaml_file_or_str
|
str
|
Pathlike string or Path that points to the yaml file, or string containing yaml |
required |
Returns:
Type | Description |
---|---|
Context
|
|
Source code in src/koheesio/context.py
get #
Get value of a given key
The key can either be an actual key (top level) or the key of a nested value.
Behaves a lot like a dict's .get()
method otherwise.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
Can be a real key, or can be a dotted notation of a nested key |
required |
default
|
Any
|
Default value to return |
None
|
safe
|
bool
|
Toggles whether to fail or not when item cannot be found |
True
|
Returns:
Type | Description |
---|---|
Any
|
Value of the requested item |
Example
Example of a nested call:
Returns c
Source code in src/koheesio/context.py
get_item #
Acts just like .get
, except that it returns the key also
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
key/value-pair of the requested item |
Example
Example of a nested call:
Returns {'a.b': 'c'}
Source code in src/koheesio/context.py
merge #
Merge this context with the context of another, where the incoming context has priority.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
Context
|
Another Context class |
required |
recursive
|
bool
|
Recursively merge two dictionaries to an arbitrary depth |
False
|
Returns:
Type | Description |
---|---|
Context
|
updated context |
Source code in src/koheesio/context.py
process_value #
Processes the given value, converting dictionaries to Context objects as needed.
Source code in src/koheesio/context.py
to_dict #
Returns all parameters of the context as a dict
Returns:
Type | Description |
---|---|
dict
|
containing all parameters of the context |
Source code in src/koheesio/context.py
to_json #
Returns all parameters of the context as a json string
Note: jsonpickle is used to serialize/deserialize the Context object. This is done to allow for objects to be stored in the Context object, which is not possible with the standard json library.
Why jsonpickle?
(from https://jsonpickle.github.io/)
Data serialized with python's pickle (or cPickle or dill) is not easily readable outside of python. Using the json format, jsonpickle allows simple data types to be stored in a human-readable format, and more complex data types such as numpy arrays and pandas dataframes, to be machine-readable on any platform that supports json.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty
|
bool
|
Toggles whether to return a pretty json string or not |
False
|
Returns:
Type | Description |
---|---|
str
|
containing all parameters of the context |
Source code in src/koheesio/context.py
to_yaml #
Returns all parameters of the context as a yaml string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
clean
|
bool
|
Toggles whether to remove |
False
|
Returns:
Type | Description |
---|---|
str
|
containing all parameters of the context |