An attempt to use a python-based mini declarative language for formdefinition
Larry Bates
lbates at swamisoft.com
Wed Sep 22 14:10:00 EDT 2004
You may can write your own __setattr__ method.
That way you can keep track of the order of
the fields yourself.
class UserForm(Form):
def __init__(self):
self.fields=[]
self.next_index=0 # Index pointer for next method
self.nickname = TextBox(length=15, default=""))
self.password = TextBox(length=10, default="", password=True))
self.name = TextBox(length=40, default="")
return
def __setattr__(self, fieldname, object):
self.fields.append(object)
self.__dict__[fieldname]=object
return
def __iter__(self):
return self
def next(self):
#
# Try to get the next route
#
try: FIELD=self.fields[self.next_index]
except:
self.next_index=0
raise StopIteration
#
# Increment the index pointer for the next call
#
self.next_index+=1
return FIELD
self.fields list will contain the fields in the
order they were defined. self.__dict__ contains
them in dictionary that __getattr__ will reference
for indexed lookup. I added __iter__ and next
methods so you can easily loop over all the fields.
Not tested and just one of
many methods.
Larry Bates
"Carlos Ribeiro" <carribeiro at gmail.com> wrote in message
news:mailman.3731.1095874585.5135.python-list at python.org...
> I'm doing some experiments with mini declarative languages (as
> explained by David Mertz in
> http://www-106.ibm.com/developerworks/library/l-cpdec.html) in Python,
> with the intention to use it as the mechanism to define data entry
> forms. My final goal is to have a simple approach to automatic
> generation of visual interfaces. The complete framework is rather big,
> so let's us focus at this specific problem.
>
> -- I would like to describe my data entry forms with plain Python
> code. I don't want to use XML, dicts or other data-driven solution;
> not because I don't like it, not because I don't know about it, only
> because I want to try a different approach.
>
> -- This is a simple code snippet of the intended form declaration:
>
> class UserForm(Form):
> nickname = TextBox(length=15, default="")
> password = TextBox(length=10, default="", password=True)
> name = TextBox(length=40, default="")
>
> It's actually based to some extent on Ian Bicking's sqlobject library,
> that uses a similar approach to build entity definitions. But there's
> a catch: the class constructor receives a dict, and has no way to tell
> the original ordering of the attributes in the original class. The
> field names are passed in an arbitrary ordering, due to the use of the
> dict mapping.
>
> -- I've tried using metaclasses or other similar magic; I've read the
> tutorials, tried some code, and read sqlobject own implementation.
> This is not a problem for sqlobject, because the order of the columns
> in the database is totally isolated from the object representation.
> But in my case, it is a problem, because I need the fields to be in
> the correct order in the display.
>
> My question is, there is any way to retrieve the class attributes in
> the order they were declared? I could not find any; __setattr__ won't
> work because the dict is constructed using the native dict type before
> __new__ has a chance at it. Is there anything that I'm overlooking?
>
>
> --
> Carlos Ribeiro
> Consultoria em Projetos
> blog: http://rascunhosrotos.blogspot.com
> blog: http://pythonnotes.blogspot.com
> mail: carribeiro at gmail.com
> mail: carribeiro at yahoo.com
More information about the Python-list
mailing list