[py-dev] handling of 'class' attribute by py.xml.html

holger krekel hpk at trillke.net
Wed Jun 8 23:59:28 CEST 2005


On Wed, Jun 08, 2005 at 16:18 -0500, William McVey wrote:
> I needed to be able to construct html elements with the 'class'
> attribute, in order to properly style the element with a set of
> pre-existing stylesheets. Ordinarily, you would think that this could be
> something as simple as:
> 
> 	from py.xml import html
> 	html.div(class="somestyleclass")
>
> This obviously doesn't work because 'class' is a reserved word in
> python. 

However, 

    html.div(class_="somestyleclass") 

does produce '<div class="somestyleclass"></div>' as expected. 

> I "solved" this using a quick hack in my local copy of the
> library by adding a quick hack that maps named parameter 'Class' to
> attribute 'class'.  ... 
> RCS file: /cvs/report/lib/python/py/xmlobj/html.py,v
> retrieving revision 1.4
> diff -c -3 -r1.4 html.py
> *** html.py	27 Apr 2005 19:47:34 -0000	1.4
> --- html.py	8 Jun 2005 21:17:17 -0000
> ***************
> *** 20,25 ****
> --- 20,26 ----
>       class Style(object): 
>           def __init__(self, **kw): 
>               for x, y in kw.items():
> +                 if x == "Class": x = "class"
>                   x = x.replace('_', '-')
>                   setattr(self, x, y) 

Here you are modifying the html.Style class which 
does not refer to the example you gave above.  
However, a Style object should not contain a css 'class' 
anyway.  Can you post a more real-life example to show
the problem you are hitting? 

thanks & cheers, 

    holger



More information about the Pytest-dev mailing list