ecoxipy.pyxom.indexing - Indexing PyXOM Structures

This package is a foundation for indexing ecoxipy.pyxom structures. The abstract base class Indexer establishes an interface for indexing PyXOM structures.

Two mapping implementations are provided to serve as indexes:

For easier implementation of indexers there are some abstract base classes:

Two ready-to-use indexer implementations are provided:

The IndexDescriptor makes index access and deletion more convenient.

Examples

First we create a test document:

>>> from ecoxipy import MarkupBuilder
>>> b = MarkupBuilder()
>>> test = b.test(
...     b.foo(id='b'),
...     b.foo('Foo Bar', no_id='d'),
...     b.bar(id='c'),
...     id='a'
... )

Here is an example how to use ElementByUniqueAttributeValueIndexer:

>>> indexer = ElementByUniqueAttributeValueIndexer()
>>> index = indexer(test)
>>> len(index) == 3
True
>>> u'a' in index and index[u'a'] is test
True
>>> index[u'b'] is test[0] and index[u'c'] is test[-1]
True
>>> u'd' in index
False
>>> index = indexer(b[:](b.test0(id='test'), b.test1(id='test')))
Traceback (most recent call last):
ValueError: A value for key "test" is already registered

And this is how ElementsByNameIndexer works:

>>> indexer = ElementsByNameIndexer()
>>> index = indexer(test)
>>> len(index) == 3
True
>>> u'test' in index
True
>>> list(index[u'test'])[0] is test
True
>>> u'bar' in index
True
>>> list(index[u'bar'])[0] is test[-1]
True
>>> foo_elements = set(index[u'foo'])
>>> len(foo_elements) == 2
True
>>> foo_elements == {test[0], test[1]}
True
>>> u'unknown' in index
False

Abstract Base Classes

class ecoxipy.pyxom.indexing.Indexer[source]

Abstract base class for ecoxipy.pyxom stucture indexers.

__call__(root_node)[source]

Indexes a ecoxipy.pyxom.XMLNode tree.

Parameters:root_node (ecoxipy.pyxom.ContainerNode) – the node to start indexing on
Returns:an index data structure

The following is done for indexing:

  1. new_index() is called to create the index data structure.
  2. For root_node and its descendants on which node_predicate() returns True:
    1. A single item or an iterator of items is obtained by calling extract_items().
    2. The single item or each item of the iterator is registered on the index by calling register().
  3. The index data structure ist returned.
new_index()[source]

Creates and returns an index data structure.

node_predicate(node)[source]

Determines if node should be used to extract index items from.

Parameters:node (ecoxipy.pyxom.XMLNode) – the node to test
Returns:True if node should be used to extract index items, False otherwise.
extract_items(node)[source]

Extracts index items from node. An index item is a 2-tuple() with the key as first item and value as second item.

Parameters:node (ecoxipy.pyxom.XMLNode) – the node to extract index items from
Returns:A single index item or an iterator of index items.
register(index, key, value)[source]

Registers value under key on index.

Parameters:
  • index – the index data structure
  • key – the identifier for value
  • value – the value registered under key
class ecoxipy.pyxom.indexing.UniqueValueIndexer[source]

An abstract Indexer base class which uses UniqueValueIndex as the index.

new_index()[source]

Creates and returns an UniqueValueIndex instance.

register(index, key, node)[source]

Registers value under key by calling register(key, value) on index.

class ecoxipy.pyxom.indexing.MultiValueIndexer[source]

An abstract Indexer base class which uses MultiValueIndex as the index.

new_index()[source]

Creates and returns a MultiValueIndex instance.

class ecoxipy.pyxom.indexing.AttributeValueIndexer(attribute_name)[source]

An abstract Indexer base class which selects ecoxipy.pyxom.Element nodes that contain an ecoxipy.pyxom.Attribute having a name equal to attribute_name. The index items are those attributes, identified by their value.

Parameters:attribute_name – defines attribute_name
attribute_name[source]

The name of the attributes whose values define the indexing keys.

node_predicate(node)[source]

Returns True if node is an ecoxipy.pyxom.Element instance and has an attribute with name attribute_name, False otherwise.

extract_items(node)[source]

Returns a 2-tuple() with the value of the attribute with name attribute_name on node as first item and the attribute itself as second item.

Table Of Contents

Previous topic

ecoxipy.pyxom.output - Building PyXOM Structures