Mercurial > genshi > mirror
comparison markup/core.py @ 170:6b265e02d099 trunk
Allow initialization of `Attributes` with keyword arguments.
author | cmlenz |
---|---|
date | Fri, 18 Aug 2006 12:40:55 +0000 |
parents | 7b1f07496bf7 |
children | 7fcf8e04514e |
comparison
equal
deleted
inserted
replaced
169:dc6676d3b697 | 170:6b265e02d099 |
---|---|
183 the list: | 183 the list: |
184 | 184 |
185 >>> attrs.set(u'accesskey', 'k') | 185 >>> attrs.set(u'accesskey', 'k') |
186 >>> attrs | 186 >>> attrs |
187 [(u'href', '#'), (u'accesskey', 'k')] | 187 [(u'href', '#'), (u'accesskey', 'k')] |
188 | |
189 An `Attributes` instance can also be initialized with keyword arguments: | |
190 | |
191 >>> attrs = Attributes(href='#', title='Foo') | |
192 >>> attrs.get('href') | |
193 '#' | |
194 >>> attrs.get('title') | |
195 'Foo' | |
188 """ | 196 """ |
189 __slots__ = [] | 197 __slots__ = [] |
190 | 198 |
191 def __init__(self, attrib=None): | 199 def __init__(self, attrib=None, **kwargs): |
192 """Create the `Attributes` instance. | 200 """Create the `Attributes` instance. |
193 | 201 |
194 If the `attrib` parameter is provided, it is expected to be a sequence | 202 If the `attrib` parameter is provided, it is expected to be a sequence |
195 of `(name, value)` tuples. | 203 of `(name, value)` tuples. |
196 """ | 204 """ |
197 if attrib is None: | 205 if attrib is None: |
198 attrib = [] | 206 attrib = [] |
199 list.__init__(self, [(QName(name), value) for name, value in attrib]) | 207 list.__init__(self, [(QName(name), value) for name, value in attrib]) |
208 for name, value in kwargs.items(): | |
209 self.set(name, value) | |
200 | 210 |
201 def __contains__(self, name): | 211 def __contains__(self, name): |
202 """Return whether the list includes an attribute with the specified | 212 """Return whether the list includes an attribute with the specified |
203 name. | 213 name. |
204 """ | 214 """ |
232 value of the existing entry is updated. Otherwise, a new attribute is | 242 value of the existing entry is updated. Otherwise, a new attribute is |
233 appended to the end of the list. | 243 appended to the end of the list. |
234 """ | 244 """ |
235 for idx, (attr, _) in enumerate(self): | 245 for idx, (attr, _) in enumerate(self): |
236 if attr == name: | 246 if attr == name: |
237 self[idx] = (attr, value) | 247 self[idx] = (QName(attr), value) |
238 break | 248 break |
239 else: | 249 else: |
240 self.append((QName(name), value)) | 250 self.append((QName(name), value)) |
241 | 251 |
242 def totuple(self): | 252 def totuple(self): |