The persistence module

Functions

Functions for storing and retrieving archive files using JSON format are

Functions for storing and retrieving an archive as a JSON-formatted string are

Functions for storing and retrieving archive files using XML format are

Functions for storing and retrieving an archive as an XML-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.

Changed in version 1.5.0.

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 ‘wt’:

>>> pr.dump_json(f, a)
>>> f.close()
static copy(ar)

A copy constructor for archive objects.

A new Archive object is returned containing a copy of the data in ar.

The returned archive can have more uncertain numbers added, and can be stored.

Parameters:

ar (Archive) – an archive object.

Added in version 1.5.0.

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 ‘rt’ mode:

>>> a = pr.load_json(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_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.

Added in version 1.3.0.

dump_xml(file, ar, indent=None, prefix=None, **kw)

Save an archive in a file in XML format.

Parameters:
  • file – a file name or a file-like object that can be written to.

  • ar – an Archive object.

  • indent (int or None) – the indentation to apply between XML elements so that the XML document is in a pretty-printed format. The indent value must be a non-negative integer.

  • prefix (str or None) – The prefix to use for the XML namespace.

Keyword arguments will be passed to ElementTree.write().

Only one archive can be saved in a file.

Added in version 1.5.0.

dumps_json(ar, **kw)

Convert an archive to a JSON string

Parameters:

ar – an Archive object

Keyword arguments will be passed to json.dumps()

Added in version 1.3.0.

dumps_xml(ar, indent=None, prefix=None, **kw)

Convert an archive to an XML document bytestring (or string).

Parameters:
  • ar – an Archive object.

  • indent (int or None) – the indentation to apply between XML elements so that the XML document is in a pretty-printed format. The indent value must be a non-negative integer.

  • prefix (str or None) – The prefix to use for the XML namespace.

Keyword arguments will be passed to ElementTree.tostring().

The return type, bytes or str, depends on whether an encoding keyword argument is specified and what its value is. The default return type is bytes.

Added in version 1.5.0.

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()

Added in version 1.3.0.

load_xml(file)

Load an Archive from a file in XML format.

Parameters:

file – a file name or a file-like object that can be read.

Added in version 1.5.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()

Added in version 1.3.0.

loads_xml(s)

Return an Archive object by converting an XML string.

Parameters:

s (bytes or str) – a string created by dumps_xml().

Added in version 1.5.0.