The persistence module¶
Class¶
An
Archive
is used to create a record of uncertain numbers for storage.Python pickle is used for the storage mechanism. An archive can be pickled and stored in a file, or a string.
Functions¶
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) >>> a.add(x=x,fred=y)
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)
-
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
-
-
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 tha same file by repeatedly calling this function.
-
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.
-
dumps
(ar, protocol=4)¶ Save an archive pickled in a string
Parameters: - ar – an
Archive
object - protocol – 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.- ar – an