The Wrap object is not derived from XCheck, but wraps a regular Python object around an Element object.
Wrap(checker, element) Creates a object Wrapper around an element that must validate to the checker object.
Parameters: |
|
---|
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.
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(name, [normalize=True] Return the value of the node attribute
Returns a wrap object for the nth child node
returns the attribute value for the given tag.
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
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
Sets the nth occurance of element tag_name.text to value
Value will be converted to a string.
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"