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
Skip Montanaro
2003-11-26
    Martin> SyntaxError: invalid syntax

    Martin> Hence the work-around:

    >>> make_dict(foo=1, bar=2, class_=3)
    {'class_': 3, 'foo': 1, 'bar': 2}

    Martin> All of which looks like a stupidly clever hack to me, and the
    Martin> need to decorate the names with underscores to sidestep keywords
    Martin> highlights the marginality of the gain.  This is not the one
    Martin> [obvious] way to write a dictionary literal!

So handle that in the attrs function:

    def attrs(**kwargs):
        d = {}
        for k in kwargs:
            d[k.rstrip('_')] = kwargs[k]
        return d

You can probably think of a suitable workaround/hack/"oh jeez, protect me
from that" to allow 'xml:lang' as well:

    def attrs(**kwargs):
        d = {}
        for k in kwargs:
            newk = k.rstrip('_').replace('__', ':')
            d[newk] = kwargs[k]
        return d

Does the expected thing:

    >>> attrs(class_='foo', xml__lang='english')
    {'xml:lang': 'english', 'class': 'foo'}

It's a bit ugly, but is it less ugly than the alternatives?  What are the
alternatives?

Skip

reply