pg_utils.pg_model.base.LabeledCollection
- class pg_utils.pg_model.base.LabeledCollection(names: List[str], **fields)[source]
Bases:
object
Abstract base class for collections to be used in PG model
LabeledCollection is a base class that defines following behaviours
indexing by integer; in other words, it is sorted;
indexing by string; in other words, it is labeled;
the elements can be accessed as attributes.
In addition, LabeledCollection supports the following operations
iterator: the object can be traversed as an iterator;
subcollection: a subset can be extracted from it.
- __init__(names: List[str], **fields) None [source]
Initialization
- Parameters:
names (List[str]) – names of the fields, list of names to be used as field attributes
**fields – keyword arguments to be initiated as attributes; the keys must be part of the names, otherwise the key-value pair is ignored; fields in names not in fields will be initiated as None; keys in fields not in names raises an error.
Methods
__init__
(names, **fields)Initialization
apply
(fun[, inplace, metadata])Apply a function iteratively to all collection items
as_empty
()Return an object with the same configuration of attributes but with fields initiated as None.
copy
()Returns a deep copy of itself
deserialize
(obj[, parser])Deserialize an object
generate_collection
(index_array)Generate a new collection based on indices
load_json
(fp[, parser])Load LabeledCollection object from json
save_json
(fp[, serializer])Serialize the object in string format and save to json file
serialize
([serializer])Serialize the object
subs
(sub_map[, inplace])Substitute variables iteratively in all fields.
Attributes
iter_filter: bool determines whether None fields are skipped.
iter_name: bool determines whether the string of the field is also returned.
- apply(fun: Callable[[...], Any], inplace: bool = False, metadata: bool = False) LabeledCollection [source]
Apply a function iteratively to all collection items
- Parameters:
fun (Callable) – determines how the collection entries are processed. The signature of the function should take the form
fun(type(self[i]))
when metadata is False, and the formfun(str, type(self[i]))
when metadata is Trueinplace (bool) – whether to write changes in situ.
metadata (bool) – whether to pass field name to the function
- Returns:
Updated object. If inplace, then the current object itself is returned.
- Return type:
- as_empty() LabeledCollection [source]
Return an object with the same configuration of attributes but with fields initiated as None. May be slightly faster than copy?
- copy() LabeledCollection [source]
Returns a deep copy of itself
- static deserialize(obj: ~typing.List[tuple], parser: ~typing.Callable[[str], ~typing.Any] = <function LabeledCollection.<lambda>>) LabeledCollection [source]
Deserialize an object
- Parameters:
- Returns:
LabeledCollection, that is deserialized from the input object
Warning
Sanitized input needed. Unexpected behaviour if input is not a legitimate serialized object
- generate_collection(index_array: List[bool]) LabeledCollection [source]
Generate a new collection based on indices
- Parameters:
index_array (List[bool]) – array of booleans indicating whether a field is to be included in the new collection
- Returns:
new collection with elements specified by index_array
- property iter_name[source]
iter_name: bool determines whether the string of the field is also returned.
- static load_json(fp: ~typing.TextIO, parser: ~typing.Callable[[str], ~typing.Any] = <function LabeledCollection.<lambda>>) LabeledCollection [source]
Load LabeledCollection object from json
convenient wrapper for
deserialize
- save_json(fp: ~typing.TextIO, serializer: ~typing.Callable[[~typing.Any], str] = <class 'str'>) None [source]
Serialize the object in string format and save to json file
- Parameters:
fp (TextIO) – file handle of the output file
serializer (Callable[[Any], str]) – a callable that maps an element to a string. Default is the
str
method.
- serialize(serializer: ~typing.Callable[[~typing.Any], str] = <class 'str'>) List[tuple] [source]
Serialize the object
- Parameters:
serializer (Callable[[Any],str]) – a callable that maps an element to a string. Default is the
str
method.- Returns:
a list of serialized objects, in the format [(fname_1, serialized_element_1), (fname_2, serialized_element_2), …]
- subs(sub_map: dict, inplace: bool = False)[source]
Substitute variables iteratively in all fields. This utility is for collections with sympy.Expr elements. See warning below.
- Parameters:
Warning
To use this method, all elements in the collection need to have implemented the
subs
method. This is intended for a collection of which all elements aresympy.Expr
type. Then, this method callsapply
internally to substitute all the variables.