durusmail: quixote-users: Generalizing form/widget API a bit
Generalizing form/widget API a bit
2003-11-25
2003-11-25
2003-11-25
2003-11-25
2003-11-25
2003-11-26
2003-11-26
2003-11-26
2003-11-26
2003-11-26
2003-11-26
2003-11-26
2003-11-26
2003-11-26
2003-11-26
2003-11-26
2003-11-29
2003-11-30
2003-11-26
2003-11-26
2003-11-26
Patch: add html_attrs keyword arg to widget classes
2003-11-30
Patch: add html_attrs keyword arg to widget classes
2003-12-01
Patch: add html_attrs keyword arg to widget classes
2003-12-01
Patch: add html_attrs keyword arg to widget classes
2003-12-01
2003-12-02
2003-12-03
2003-12-02
Patch: add html_attrs keyword arg to widget classes
2003-12-01
Patch: add html_attrs keyword arg to widget classes
2003-12-01
Generalizing form/widget API a bit
Neil Schemenauer
2003-11-25
On Mon, Nov 24, 2003 at 09:49:49PM -0500, Greg Ward wrote:
> --- form2/widget.py.orig0     2003-11-16 16:09:37.000000000 -0500
> +++ form2/widget.py   2003-11-24 21:44:41.000000000 -0500
> @@ -31,14 +31,18 @@
>        name : string
>        value : any
>        error : string
> +      attrs : { string : any }
> +        arbitrary HTML attribute values; will be added to the tag
> +        used to render this widget
>
>      Feel free to access these directly; to set them, use the 'set_*()'
>      modifier methods.
>      """
>
> -    def __init__(self, name, value=None):
> +    def __init__(self, name, value=None, **attrs):


I think the general idea is good but I'm worried about name
collisions.  With this approach you can't set an attribute if the
widget takes an argument with the same name.

Maybe something like this:

    def __init__(self, name, value=None, attributes=None, **kwattributes):
        ...
        self.attributes = {}
        if attributes:
            self.attributes.update(attributes)
        if kwattributes:
            self.attributes.update(kwattributes)

Not exactly elegant.  Maybe a helper class would do the trick:

    class Attrs(UserDict):
        def __init__(self, **attrs):
            self.data = {}
            for key, val in attrs.items():
                # XML is case sensitive but SGML is not.  This
                # allows attributes like 'CLASS'.
                self.data[key.lower()] = val

    class Widget:
        ...
        def __init__(self, name, value=None, attrs=None):
            ...
            self.attrs = attrs or {}
            ...

Need to think about it more, I guess.

  Neil

reply