polymatheia.data

The classes in the polymatheia.data package handle data input and output.

class polymatheia.data.LimitingIterator(it, max_records)

The LimitingIterator limits the number of records returned from a wrapped iterator.

__init__(it, max_records)

Create a new LimitingIterator.

Parameters:
  • it – The wrapped iterator.

  • max_records (number) – The maximum number of records to return.

__iter__()

Return this LimitingIterator as the iterator.

__next__()

Return the next value.

Raises:

StopIteration – If no more values are available

__weakref__

list of weak references to the object (if defined)

class polymatheia.data.NavigableDict(*args, **kwargs)

The NavigableDict is a dict subclass that allows access via dot notation.

>>> test = NavigableDict(one='1')
>>> test.one
1
>>> test['one']
1

The NavigableDict works like a dict in any other respect.

__delattr__(key)

Delete the value with the given key.

Raises:

KeyError – If no value exists for key

__getattr__(key)

Retrieve the value with the given key.

Returns:

The value for the key

Raises:

KeyError – If no value exists for key

__init__(*args, **kwargs)

Initialise the NavigableDict, ensuring that data is coerced.

__setattr__(key, value)

Set the value for key.

This method automatically coerces any dict value into NavigableDict.

Parameters:
  • key (string) – The key to set the value for

  • value – The value to set

__str__()

Return a pretty-printed JSON representation.

__weakref__

list of weak references to the object (if defined)

get(path, default=None)

Get the value specified by the path.

The path can either be a str, in which case it is split into its component parts. If it is a list then it is used as is. The following str path structures are supported:

  • x: Get the value with the key 'x'

  • x.y: Get the value with the key 'x' and then within that the key 'y'

  • x.a: Get the list with the key 'x' and in that returns the a-th element in the list

  • x[a]: Get the list with the key 'x' and in that returns the a-th element in the list

In general the list format is only needed if one of the parts used in the path contains a '.', '[', or ']'.

This differs from just using attribute access, in how it handles lists. If a value is a list and the next path element is not a list index, then it will return a list, applying the remainder of the path to each element in the list.

Parameters:
  • path (str or [str, ...]) – The path to get the value for

  • default – The default value to return if the path does not identify a value

Returns:

The value identified by path or the default value

merge(other)

Merge the other values.

Unlike the update() method, this method does not immediately overwrite any existing values. Instead if both the new and existing value are NavigableDict, then the values from the other NavigableDict are merged into the existing NavigableDict. Similarly, if both the new and existing value are list, then the new existing list is extended with the new list. If neither of these conditions hold, then the existing value is overwritten by the new value.

set(path, value)

Set the value at the location specified by path.

The path can either be a str, in which case it is split into its component parts. If it is a list then it is used as is. The following str path structures are supported:

  • x: Set the value with the key 'x'

  • x.y: Set the value with the key 'x' and then within that the key 'y'

  • x.a: Set the list with the key 'x' and in that set the a-th element in the list

  • x[a]: Set the list with the key 'x' and in that set the a-th element in the list

In general the list format is only needed if one of the parts used in the path contains a '.', '[', or ']'.

Parameters:
  • path (str or [str, ...]) – The path to set the value for

  • value – The value to set

update(*args, **kwargs)

Update the content of this NavigableDict.

Any dict passed will be coerced into NavigableDict

class polymatheia.data.NavigableDictIterator(it, mapper=None)

The NavigableDictIterator maps values to NavigableDict.

If the iterator it wraps returns dict objects, then these are simply converted into NavigableDict objects. If a mapper function is provided, then this function is called with each value and it must return a dict object that is then converted into a NavigableDict object. Otherwise a NavigableDict is returned that has a single key value, with the wrapped iterator value.

__init__(it, mapper=None)

Create a new NavigableDictIterator.

Parameters:
  • it – The iterator that provides the values.

  • mapper – An optional mapping function converting a single iterator value into a dict object.

__iter__()

Return this NavigableDictIterator as the iterator.

__next__()

Return the next NavigableDict.

Raises:

StopIteration – If no more NavigableDict are available

__weakref__

list of weak references to the object (if defined)

polymatheia.data.xml_to_navigable_dict(node)

Convert the XML node to a dictionary.

Child nodes are returned as keys of the dictionary. Where a node occurs multiple times, the key returns a list of dictionaries for the children. This means that the ordering information in the original XML document is lost. All attributes are available via the _attrib key. The node‘s text is available via the _text key and any text that directly follows the node is available via the _tail key.

Parameters:

node – The XML node to convert

Returns:

The dictionary representation of the XML node

Return type:

NavigableDict