parsimony: Base classes and utilities

Classes

class parsimony.ParsimonyException(message)

Base exception for parsimony.

Extend this for defining exceptions.

Functions

parsimony.generate(key, function, **parameters)

Generates the key value using the function and supplied parameters.

Parameters:
  • key – Parsimony key
  • function – Callable
  • parameters – Key value parameters to the function
Returns:

value generated by the function

parsimony.mark_dirty(key)

Mark the key for mandatory regeneration.

This can be done at any time before or after a generator is executed. The value for the generator will be removed from any underlying, attached, stores and caches if needed then regenerated upon a generate() call

Parameters:key
Returns:

parsimony.configuration: Sets the implementations used by parsimony.

Functions

parsimony.configuration.callable_wrapper(key, function, **parameters)

Gets the configured call wrapper. Currently, a default of PickledCallableWrapper is chosen.

Return callable_wrapper:
 CallableWrapper Generator object
parsimony.configuration.obfuscator()

Gets the configured obfuscator. Currently, a default of SHA512Obfuscator is chosen.

Return obfuscator:
 Obfuscator object
parsimony.configuration.store(key)

Gets the configured default store. Currently, a default of PickledStore is chosen.

Return store:Store object
parsimony.configuration.cache()

Returns the configured cache. Currently, a default of MemCache is returned

Returns:
parsimony.configuration.parsimony_directory()

Return the directory to store parsimony data in.

Returns:.parsimony
parsimony.configuration.context_name()

Get the context name

Returns:Current context name
parsimony.configuration.set_context(new_context_name)

Set the context name. This should be something that can be a directory name.

parsimony.generators: Definition of generator functionality and some basic generators.

Classes

class parsimony.generators.Generator(key, obfuscator=None, cache=None, store=None, **parameters)

Generator abstract base class. This is the core object for cached generation.

dump(value)

Internal method to store the value in storage.

This method may be overriden in subclasses. Do not use externally.

Returns:generated value
generate()

Generate the object referred to by the key.

If the object has never been generated before, the necessary work to create it is performed and the object is cached in a ParameterStore.

If the object has been generated and the parameters have not been changed, the Generator checks for an in memory
version of the object first and returns it. If not in memory, the generated object is retrieved from a ParameterStore and cached in memory, then returned.

If the object has been generated, but the parameters have changed, the object is regenerated and the ParameterStore is overwritten.

Return generated_value:
 The value for this generator’s key.
get_parameter(parameter_key)

Return a parameter based on key.

Parameters:parameter_key
Returns:parameter matching the parameter key
key()

Get the key of this Generator

Return key:string
load()

Internal method to load the value from storage.

This method may be overriden in subclasses. Do not use externally.

Returns:generated value
rebuild()

Internal method to regenerate the value based on current parameters.

This method must be overriden in subclasses. Do not use externally.

Returns:generated value
up_to_date()

Indicate if the generated value is fresh or stale with respect to non-parmeter changes. In the case of completely parameter driven values this will always be true. In the case of external objects or persisted data, it must be checked appropriately.

This may be overridden in subclasses. :return: True if the value is fresh.

class parsimony.generators.StoredCallableWrapper(key, function, **parameters)

Generator for callables using that stores results.

rebuild()

Call the callable with parameters to generate the value

Returns:generated value
class parsimony.generators.PathMonitor(key, file_path)

Monitors the path for changes. This is primarily useful as a parameter key

dump(value)

No need to deal with persistence since this generator is completely defined by it’s parameters.

load()

No need to deal with persistence since this generator is completely defined by it’s parameters. :return: the path

rebuild()

Returns the path :return: the path

up_to_date()

Determines if the file has been modified.

Returns:If file timestamp has changed or not
class parsimony.generators.TextFile(key, file_path)

Allows a text file to be treated as a generateable object.

The mechanism is to load the file once and any time the file has been subsequently modified.

dump(value)

This is by definition already stored. Do nothing.

load()

Reads all of the file contents into a string.

Returns:text file contents
rebuild()

Loads the file data.

Returns:self.load()

parsimony.persistence: Storage and retrieval mechanisms

Classes

class parsimony.persistence.DataObfuscator

Interface for data hashers

Obfuscation is the method to make easily comparable, storage efficent, potentially secure versions of the data.

obfuscate(data)

Create the hashed data representation

This is method must be overridden by subclasses.

Parameters:data – data to obfuscate
Returns:obfuscated representation such as a hash
class parsimony.persistence.SHA512Obfuscator

DataObfuscator that uses the SHA512 hashing mechanism.

obfuscate(data)

Generate SHA512 of the hashable data representation

Parameters:data – data to obfuscate
Returns:hash
class parsimony.persistence.Store(key)

A store is an object that implements read and write capabilities for key-ed values

read()

Method to retrieve the persisted value of this store.

Must be overridden by subclasses.

Returns:the saved value from this store
write(value)

Method to persist a value in this store

Must be overridden by subclasses.

class parsimony.persistence.PickleStore(key, base_directory=None)

Store that uses the local file system and pickle as the underlying mechanism.

read()

Pickle load the stored value

write(value)

Dump the value to the pickle store

Parameters:value – value to dump
class parsimony.persistence.Cache

Interface of a Cache object. Cache’s are responsible for persisting parameter values and determining parameter equality.

compare(value, parameter_key)

Compare the stored value to the given value

Must be overridden by subclasses.

Parameters:
  • value – value comparable to stored value
  • parameter_key – key of value to compare to
Returns:

equality test

parameter_keys(key)

Get the parameter sub-keys for the given generator key

Must be overridden by subclasses.

Parameters:key – Generator key
Returns:list of key
update(key, value, parameter_keys=None)

Store new value for the parameter.

Must be overridden by subclasses.

Parameters:
  • key – key of object to store
  • value – value of object to store
  • parameter_keys – keys of parameters for generator parameter values
class parsimony.persistence.MemCache(store)

Parameter Cache that will bring parameters from a store into local memory if it exists and persist updates to the store.

update(key, value, parameter_keys=None)

Update the memory cache and store

Parameters:
  • key – key of object to store
  • value – value of object to store
  • parameter_keys – keys for generator parameters