lib5c.core.mixins module

class lib5c.core.mixins.Annotatable[source]

Mixin class for storing and accessing arbitrary annotation information on object instances.

data

Dict to store arbitrary annotation data. Typically, the keys will be strings.

Type:dict

Notes

This mixin requires initialization. Subclasses must explicitly call

Annotatable.__init__(self)

somewhere in their constructor.

When a subclass’s bound functions return a new instance, the following guidelines are recommended:

  • addition/summing operations: create a new dict and update it
  • other operations: copy a reference to data into the new instance
get_data()[source]

Get this instance’s data attribute.

Returns:This instance’s data attribute.
Return type:dict
get_value(key)[source]

Get the value of some key in data. This is equivalent to a get-or-None function for data[key].

Parameters:key (str) – The key to search for in this instance’s data.
Returns:The value of data[key], or None if the key does not exist.
Return type:any

Examples

>>> from lib5c.core.loci import Locus
>>> locus = Locus('chr3', 34109023, 34113109, num_genes=10)
>>> locus.get_value('num_genes')
10
>>> locus.get_value('num_ctcf_sites') is None
True
set_data(data)[source]

Overwrite this instance’s data attribute with a passed dict.

Parameters:data (dict) – The dict to put in this instance’s data attribute.
set_value(key, value)[source]

Set the value for a specific key in this instance’s data attribute.

Parameters:
  • key (str) – The key to set.
  • value (any) – The value to store.
class lib5c.core.mixins.Loggable[source]

Mixin class for supporting object event logging.

log

Each string in the list describes an event in this object’s history.

Type:list of str

Notes

This mixin requires initialization. Subclasses must explicitly call

Loggable.__init__(self)

somewhere in their constructor.

When a subclass’s bound functions return a new instance, the following guidelines are recommended:

  • addition/summing operations: use the empty log of the new instance
  • other operations: copy a reference to log into the new instance
get_log()[source]

Get this instance’s log.

Returns:This instance’s log.
Return type:list of str
log_event(event)[source]

Add an event to the log.

Parameters:event (str) – A string describing the event.
print_log()[source]

Print this instance’s log to the console.

class lib5c.core.mixins.Picklable[source]

Mixin class for providing easy reading and writing of instances to disk via the pickle module.

classmethod from_pickle(filename)[source]

Create a new instance of the class from a pickle file.

Parameters:filename (str) – String reference to a pickle file to read from.
Returns:Unpickled instance.
Return type:cls
to_pickle(filename)[source]

Write this instance to a pickle file.

Parameters:filename (str) – String reference to the file to write this instance to.