Code Tracing

This module is used to preserve the state of a passed object without requiring the original source code. Unlike pickle, this module doesn’t store the attributes of the object, but rather the code which is needed to instantiate the object. The downside of relying only on pickle is that when the original source code is lost or modified, the reconstruction (or unpickling) process fails. This is because pickle only preserves the objects attributes rather than it’s source code definition. The Code module scans and traces the given object, and attempts to save it’s definition such that the object is always available. Naturally, this module can be used in tandem with pickle, where the module is used to reconstruct the object definition and pickle reconstruct the object state.

class Code(instance input = None)
Params instance input:

An instance of the object to target.


Returns a boolean value indicating whether the input object is also a code object.

AddDependency(list[code_t] inpt)

Expects a list of code_t dictionary like content (see Data Types and Dictionary Mapping.)

  • InstantiateObject (Object) – Returns a wrapped code type object of the original instance. During the instantiation process, if the instance is a class, then an additional attribute is added to the object called code. This attribute is a struct of type code_t, and allows for calling the source code in different contexts (e.g. within a thread).

  • hash (str) – Returns a string which is derived from hashing the source code.

  • fx (callable) – Returns the input function.

  • is_class (bool) – Returns a boolean indicating whether the input was a class

  • is_function (bool) – Returns a boolean indicating whether the input was a function

  • is_callable (bool) – Returns a boolean indicating whether the input function can be called, e.g. name()

  • is_initialized (bool) – Returns a boolean indicating whether the input function was already initialized. This can be particularly useful when the input has input requirements before being called, e.g. __init__(self, var1).

  • function_name (str) – Returns a string of the function name if the input was a function, else an empty string is returned.

  • class_name (str) – Returns a string of the class name if the input was a class, else an empty string is returned.

  • source_code (str) – Returns the original source code of the file from which the object originated from.

  • object_code (str) – Returns the object definition source code.

  • co_vars (list[str]) – Returns a list of the input variables.

  • input_params (dict[str, str]) – Returns a dictionary of the co-variables with their default values.

  • param_space (dict) – An empty placeholder used to add additional parameters to the object. This can be useful when the instantiated object requires some input, which is not always available during instantiation.

  • defaults (list) – Returns a list of default input variable values.

  • trace (None) – Returns a dictionary outlining the dependency tree of the object and any external imports.

  • extern_imports (dict) – A dictionary of external libraries that the code relies on.