The persistence module¶
Functions¶
Functions for storing and retrieving archive files using Python pickle format are
Functions for storing and retrieving pickled archive strings are
Functions for storing and retrieving archive files using JSON format are
Functions for storing and retrieving an archive as a JSON-formatted string are
Module contents¶
- class Archive¶
An
Archive
helps to store and retrieve uncertain numbers, so that they can be used in later calculations.A particular
Archive
object can either be used to prepare a record of uncertain numbers for storage, or to retrieve a stored record.- add(**kwargs)¶
Add entries to an archive.
Each entry is given a name that identifies it within the archive.
Example
>>> a = pr.Archive() >>> x = ureal(1,1) >>> y = ureal(2,1) >>> z = ureal(20,1) >>> a.add(x=x,fred=y) # Entries can also be added using the name as a key >>> a['z'] = z
Here
f
is a file stream opened in mode ‘wb’:>>> pr.dump(f, a) >>> f.close()
- extract(*args)¶
Extract uncertain numbers by name
- Parameters
args – names of uncertain numbers stored in the archive
If just one name is provided, a single uncertain number is returned. Otherwise a sequence of uncertain numbers is returned.
Example
Continuing the example in
add()
, but in a different Python session,f
is now a file stream opened in ‘rb’ mode:>>> a = pr.load(f) >>> f.close() >>> a.extract('fred') ureal(2.0,1.0,inf) >>> x, fred = a.extract('x','fred') >>> x ureal(1.0,1.0,inf) # Entries can also be extracted using the name as a key >>> a['z'] ureal(20.0,1.0,inf)
- items()¶
Return a list of name -to- uncertain-number pairs
- iteritems()¶
Return an iterator of name -to- uncertain-number pairs
- iterkeys()¶
Return an iterator for names
- itervalues()¶
Return an iterator for uncertain numbers
- keys()¶
Return a list of names
- values()¶
Return a list of uncertain numbers
- dump(file, ar)¶
Save an archive in a file
- Parameters
file – a file object opened in binary mode (with ‘wb’)
ar – an
Archive
object
Several archives can be saved in the same file by repeated use of this function.
- dump_json(file, ar, **kw)¶
Save an archive in a file in JSON format
- Parameters
file – a file object opened in text mode (with ‘w’)
ar – an
Archive
object
Keyword arguments will be passed to
json.dump()
Only one archive can be saved in a file.
New in version 1.3.0.
- dumps(ar, protocol=5)¶
Save an archive pickled in a string
- Parameters
ar – an
Archive
objectprotocol – encoding type
Possible values for protocol are described in the Python documentation for the
pickle
module.protocol=0
creates an ASCII string, but note that many (special) linefeed characters are embedded.
- dumps_json(ar, **kw)¶
Convert an archive to a JSON string
- Parameters
ar – an
Archive
object
Keyword arguments will be passed to
json.dumps()
New in version 1.3.0.
- load(file)¶
Load an archive from a file
- Parameters
file – a file object opened in binary mode (with ‘rb’)
Several archives can be extracted from the same file by repeatedly calling this function.
- load_json(file, **kw)¶
Load an archive from a file
- Parameters
file – a file created by
dump_json()
Keyword arguments will be passed to
json.load()
New in version 1.3.0.
- loads_json(s, **kw)¶
Return an archive object by converting a JSON string
- Parameters
s – a string created by
dumps_json()
Keyword arguments will be passed to
json.loads()
New in version 1.3.0.