Package genshi :: Module core :: Class Attrs

Class Attrs

object --+    
         |    
     tuple --+
             |
            Attrs

Immutable sequence type that stores the attributes of an element.

Ordering of the attributes is preserved, while access by name is also supported.

>>> attrs = Attrs([('href', '#'), ('title', 'Foo')])
>>> attrs
Attrs([('href', '#'), ('title', 'Foo')])
>>> 'href' in attrs
True
>>> 'tabindex' in attrs
False
>>> attrs.get('title')
'Foo'

Instances may not be manipulated directly. Instead, the operators | and - can be used to produce new instances that have specific attributes added, replaced or removed.

To remove an attribute, use the - operator. The right hand side can be either a string or a set/sequence of strings, identifying the name(s) of the attribute(s) to remove:

>>> attrs - 'title'
Attrs([('href', '#')])
>>> attrs - ('title', 'href')
Attrs()

The original instance is not modified, but the operator can of course be used with an assignment:

>>> attrs
Attrs([('href', '#'), ('title', 'Foo')])
>>> attrs -= 'title'
>>> attrs
Attrs([('href', '#')])

To add a new attribute, use the | operator, where the right hand value is a sequence of (name, value) tuples (which includes Attrs instances):

>>> attrs | [('title', 'Bar')]
Attrs([('href', '#'), ('title', 'Bar')])

If the attributes already contain an attribute with a given name, the value of that attribute is replaced:

>>> attrs | [('href', 'http://example.org/')]
Attrs([('href', 'http://example.org/')])
Instance Methods
bool
__contains__(self, name)
Return whether the list includes an attribute with the specified name.
 
__getitem__(self, i)
Return an item or slice of the attributes list.
 
__getslice__(self, i, j)
Return a slice of the attributes list.
Attrs
__or__(self, attrs)
Return a new instance that contains the attributes in attrs in addition to any already existing attributes.
 
__repr__(self)
repr(x)
Attrs
__sub__(self, names)
Return a new instance with all attributes with a name in names are removed.
object
get(self, name, default=None)
Return the value of the attribute with the specified name, or the value of the default parameter if no such attribute is found.
tuple
totuple(self)
Return the attributes as a markup event.

Inherited from tuple: __add__, __eq__, __ge__, __getattribute__, __getnewargs__, __gt__, __hash__, __iter__, __le__, __len__, __lt__, __mul__, __ne__, __new__, __rmul__, __sizeof__, count, index

Inherited from object: __delattr__, __format__, __init__, __reduce__, __reduce_ex__, __setattr__, __str__, __subclasshook__

Properties

Inherited from object: __class__

Method Details

__contains__(self, name)
(In operator)

 
Return whether the list includes an attribute with the specified name.
Returns: bool
True if the list includes the attribute
Overrides: tuple.__contains__

__getitem__(self, i)
(Indexing operator)

 

Return an item or slice of the attributes list.

>>> attrs = Attrs([('href', '#'), ('title', 'Foo')])
>>> attrs[1]
('title', 'Foo')
>>> attrs[1:]
Attrs([('title', 'Foo')])
Overrides: tuple.__getitem__

__getslice__(self, i, j)
(Slicling operator)

 

Return a slice of the attributes list.

>>> attrs = Attrs([('href', '#'), ('title', 'Foo')])
>>> attrs[1:]
Attrs([('title', 'Foo')])
Overrides: tuple.__getslice__

__or__(self, attrs)
(Or operator)

 
Return a new instance that contains the attributes in attrs in addition to any already existing attributes.
Returns: Attrs
a new instance with the merged attributes

__repr__(self)
(Representation operator)

 
repr(x)
Overrides: object.__repr__
(inherited documentation)

__sub__(self, names)
(Subtraction operator)

 
Return a new instance with all attributes with a name in names are removed.
Parameters:
  • names - the names of the attributes to remove
Returns: Attrs
a new instance with the attribute removed

get(self, name, default=None)

 
Return the value of the attribute with the specified name, or the value of the default parameter if no such attribute is found.
Parameters:
  • name - the name of the attribute
  • default - the value to return when the attribute does not exist
Returns: object
the attribute value, or the default value if that attribute does not exist

totuple(self)

 

Return the attributes as a markup event.

The returned event is a TEXT event, the data is the value of all attributes joined together.

>>> Attrs([('href', '#'), ('title', 'Foo')]).totuple()
('TEXT', '#Foo', (None, -1, -1))
Returns: tuple
a TEXT event