Wrap — Creating an object around the checker

Wrap

The Wrap object is not derived from XCheck, but wraps a regular Python object around an Element object.

class xcheck.Wrap(ch, elem=None)

Wrap(checker, element) Creates a object Wrapper around an element that must validate to the checker object.

Parameters:
  • checker (XCheck) – an XCheck instance. Should not be an instance of a sub-class
  • element (ElementTree.Element, a string representation, or None) – Data to be wrapped

The instance has a custom __getattr__ method. The results could be a string, a list of strings, a list of wrapped objects, or None.

If the element is a singleton with data, the text is returned.

_add_elem(tag_name, text, attrib=None)

Adds a child element in the appropriate place in the tree. Raises an IndexError if the checker does not allow an addition child of tag_name.

_get_att(att_name, normalize=True)

_get_att(name, [normalize=True] Return the value of the node attribute

_get_child_wrap(tag_name, nth=0)

Returns a wrap object for the nth child node

_get_elem_att(tag, att)

returns the attribute value for the given tag.

_get_elem_value(tag_name, nth=0, normalize=True)

get_list_elem_text(tag_name, nth, normalize) Return the text value of the nth occurence of the element tag_name. nth is a zero-based index.

Uses the specific xcheck object, so an IntCheck checker will return a normalized (i.e., integer) value

If normalize is False, returns the text value as it appears

_set_elem_att(tag, att, value, nth=0)

Sets the attribute value for the nth occurance given element tag. Raises a ValueError if any of the following are true:

* The tag name does not appear in the checker definition
* The attribute name does not appear in the checker definition
* The attribute is not an attribute of the given tag
* The value is not acceptable according to the checker definition
_set_elem_value(self, tag_name, value, nth = 0)

Sets the nth occurance of element tag_name.text to value

Value will be converted to a string.

tokens()

returns a list of checker tokens

The Wrap class is more useful when subclassed.

import xcheck

item_definition="""<xcheck name="item">
<children>
    <text name="name" min_length="1" />
    <email name="email" max_occurs="4">
        <attributes>
            <selection name="type" values="home, work, personal"/>
        </attributes>
    </email>
</children>
</xcheck>
"""

item = xcheck.load_checker(item_definition)

class Item(xcheck.Wrap):
    def __init__(self, node=None):
        xcheck.Wrap.__init__(self, item, node)

    @property
    def name(self):
        return self._get_elem_value('name')

    @name.setter
    def name(self, value):
        return self._set_elem_value('name', value)


some_person = """<item>
<name>John Doe</name>
<email type="work">lost@fox.com</email>
<email type="personal">knowitall@seattle.net</email>
</item>"""

John = Item(some_person)

print John.name # prints "John Doe"
John.name = 'Jane Doe'
print John.name # prints "Jane Doe"

Table Of Contents

Previous topic

Data Type Checkers

Next topic

Definition Nodes

This Page