This feed contains pages in the "python" category.
For work recently I've been doing some
Django-related tasks that involve talking to an external API with
POSTed forms. Django forms objects are declared by creating a class that
inherits from django.forms.Form, with the fields of the
form declared by declaring attributes of that class. Which works well
and is clean and easy to remember—unless the API you're working
with requires a field with the same name as a Python keyword, such as
return. You can't declare a field like this as an
attribute; it will trigger a syntax error.
I spent some time scratching my head over this, and came up with this as
a workaround after source-diving to find out how Form
objects actually work:
from django import forms class ExampleForm(forms.Form): def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, initial=None, errorclass=ErrorList, label_suffix=':', empty_permitted=False, return_url=None): forms.Form.__init__(self, data, files, auto_id, prefix, initial, errorclass, label_suffix, empty_permitted) if return_url is not None: self.fields['return'] = forms.CharField(widget=forms.HiddenInput, initial=return_url)
It turns out that the attribute declaration is just syntactic sugar for
creating a dictionary of key/value pairs, which is then stored in the
fields attribute. So we can monkeypatch in extra values after
the translation. Which is somewhat more awkward and ugly, but works in a pinch.
Note that I haven't extensively tested what interactions this may cause with other forms code, so use with some caution.
A python example in ipython:
In [1]: for i in range(10):
...: print "i in loop:", i
...:
...:
i in loop: 0
i in loop: 1
i in loop: 2
i in loop: 3
i in loop: 4
i in loop: 5
i in loop: 6
i in loop: 7
i in loop: 8
i in loop: 9
In [2]: print "i out of loop:", i
i out of loop: 9
This bit me last night while writing some code for a digital communications lab assignment. I typed the wrong variable name, which was from an inner loop when I meant to use the element from the outer loop. Is there actually a sane reason for a loop variable not to go out of scope when the loop ends? Tell me there's a good reason for it. It took me completely by surprise.
Nice to see that everyone's favourite python/gstreamer-based audio player is still alive and kicking despite slow development over the past while. And while I dislike seeing more things move into Google's cloud, it sure as hell is nice to have an upstream issue tracker that is not a mailing list again. (After how long?)