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.
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 class for ecoxipy.pyxom stucture indexers.
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:
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. |
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. |
An abstract Indexer base class which uses UniqueValueIndex as the index.
Creates and returns an UniqueValueIndex instance.
An abstract Indexer base class which uses MultiValueIndex as the index.
Creates and returns a MultiValueIndex instance.
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 |
---|
Returns True if node is an ecoxipy.pyxom.Element instance and has an attribute with name attribute_name, False otherwise.
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.