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)
The
NavigableDict
is adict
subclass that allows access via dot notation.>>> test = NavigableDict(one='1') >>> test.one 1 >>> test['one'] 1
The
NavigableDict
works like adict
in any other respect.Delete the value with the given
key
.- Raises:
KeyError – If no value exists for
key
Retrieve the value with the given
key
.- Returns:
The value for the
key
- Raises:
KeyError – If no value exists for
key
Initialise the
NavigableDict
, ensuring that data is coerced.
Set the
value
forkey
.This method automatically coerces any
dict
value
intoNavigableDict
.- Parameters:
key (
string
) – The key to set the value forvalue – The value to set
Return a pretty-printed JSON representation.
list of weak references to the object (if defined)
Get the value specified by the
path
.The
path
can either be astr
, in which case it is split into its component parts. If it is alist
then it is used as is. The followingstr
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 thea
-th element in the listx[a]
: Get the list with the key'x'
and in that returns thea
-th element in the list
In general the
list
format is only needed if one of the parts used in thepath
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 fordefault – The default value to return if the
path
does not identify a value
- Returns:
The value identified by
path
or thedefault
value
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 areNavigableDict
, then the values from theother
NavigableDict
are merged into the existingNavigableDict
. Similarly, if both the new and existing value arelist
, 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 the
value
at the location specified bypath
.The
path
can either be astr
, in which case it is split into its component parts. If it is alist
then it is used as is. The followingstr
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 thea
-th element in the listx[a]
: Set the list with the key'x'
and in that set thea
-th element in the list
In general the
list
format is only needed if one of the parts used in thepath
contains a'.'
,'['
, or']'
.- Parameters:
path (
str
or[str, ...]
) – The path to set the value forvalue – The value to set
Update the content of this
NavigableDict
.Any
dict
passed will be coerced intoNavigableDict
The
NavigableDictIterator
maps values toNavigableDict
.If the iterator it wraps returns
dict
objects, then these are simply converted intoNavigableDict
objects. If amapper
function is provided, then this function is called with each value and it must return adict
object that is then converted into aNavigableDict
object. Otherwise aNavigableDict
is returned that has a single keyvalue
, with the wrapped iterator value.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.
Return this
NavigableDictIterator
as the iterator.
Return the next
NavigableDict
.- Raises:
StopIteration – If no more
NavigableDict
are available
list of weak references to the object (if defined)
Convert the XML node to a dictionary.
Child
node
s are returned as keys of the dictionary. Where anode
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. Thenode
‘s text is available via the_text
key and any text that directly follows thenode
is available via the_tail
key.- Parameters:
node – The XML node to convert
- Returns:
The dictionary representation of the XML node
- Return type: