Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > An attempt to use a python-based mini declarative language for formdefinition

Reply
Thread Tools

An attempt to use a python-based mini declarative language for formdefinition

 
 
Carlos Ribeiro
Guest
Posts: n/a
 
      09-22-2004
I'm doing some experiments with mini declarative languages (as
explained by David Mertz in
http://www-106.ibm.com/developerwork...y/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: http://www.velocityreviews.com/forums/(E-Mail Removed)
mail: (E-Mail Removed)
 
Reply With Quote
 
 
 
 
Thomas Heller
Guest
Posts: n/a
 
      09-22-2004
Carlos Ribeiro <(E-Mail Removed)> writes:

> I'm doing some experiments with mini declarative languages (as
> explained by David Mertz in
> http://www-106.ibm.com/developerwork...y/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?


No, there is no way. But there is a trick you can use (I've played with
stuff like this, in a different context, in the past): You can use an
instance variable or global in the TextBox callable, that is incremented
on each call. This counter is somehow attached to the object that
TextBox returns, and lets you order these objects afterwards. Makes
sense?

Thomas
 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      09-22-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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm doing some experiments with mini declarative languages (as
> explained by David Mertz in
> http://www-106.ibm.com/developerwork...y/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: (E-Mail Removed)
> mail: (E-Mail Removed)



 
Reply With Quote
 
Carlos Ribeiro
Guest
Posts: n/a
 
      09-22-2004
On Wed, 22 Sep 2004 20:04:21 +0200, Thomas Heller <(E-Mail Removed)> wrote:
> No, there is no way. But there is a trick you can use (I've played with
> stuff like this, in a different context, in the past): You can use an
> instance variable or global in the TextBox callable, that is incremented
> on each call. This counter is somehow attached to the object that
> TextBox returns, and lets you order these objects afterwards. Makes
> sense?


I think that it does. Actually, I had a pretty much more ellaborate
idea that rely on a *lot* of introspection for the same effect. It's
not for the faint of heart

It goes like this: inside the TextBox constructor, raise an exception,
capture the stack frame, and check from where was it called. I think
there's enough information at this point to order the elements. As I
said, not for the faint of heart, and it smells like a terrible hack.

I'll check this and other similar ideas. Thanks again,


--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: (E-Mail Removed)
mail: (E-Mail Removed)
 
Reply With Quote
 
Carlos Ribeiro
Guest
Posts: n/a
 
      09-22-2004
On Wed, 22 Sep 2004 13:10:00 -0500, Larry Bates <(E-Mail Removed)> wrote:
> You may can write your own __setattr__ method.
> That way you can keep track of the order of
> the fields yourself.


Larry,

I may have made a mistake on my own code, but __setattr__ would not
work for me; I'm creating my instances through a metaclass, and I'm
using class attributes for the fields.

In your example the form fields are instance attributes. In this case,
I agree it works. But as I said -- I was testing if I could make it
work using class attributes and metaclasses. I think that the
resulting code is much cleaner (after all black magic is done and
hidden from the user, that is). Compare:

1) using class attributes

class UserForm(Form):
nickname = TextBox(length=15, default="")
password = TextBox(length=10, default="", password=True)
name = TextBox(length=40, default="")

2) using instance atttributes

class UserForm(Form):
def __init__(self):
Form.__init__(self)
self.nickname = TextBox(length=15, default=""))
self.password = TextBox(length=10, default="", password=True))
self.name = TextBox(length=40, default="")

Others may think that's a small difference, but if I could choose
between both approaches, I would surely pick (1).

--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: (E-Mail Removed)
mail: (E-Mail Removed)
 
Reply With Quote
 
Andrew Dalke
Guest
Posts: n/a
 
      09-22-2004
Carlos Ribeiro wrote:
> I may have made a mistake on my own code, but __setattr__ would not
> work for me; I'm creating my instances through a metaclass, and I'm
> using class attributes for the fields.


I tried too, though not using metaclasses.

>>> class StoreOrder(dict):

.... def __init__(self, *args, **kwargs):
.... dict.__init__(self, *args, **kwargs)
.... self["_order"] = []
.... def __setitem__(self, k, v):
.... dict.__setitem__(self, k, v)
.... self["_order"].append(k)
....
>>> x = StoreOrder()
>>> x["a"] = 9
>>> x["b"] = 10
>>> x

{'a': 9, '_order': ['_order', 'a', 'b'], 'b': 10}
>>> class Form:

.... pass
....
>>> Form.__dict__ = StoreOrder()
>>> Form.__dict__

{'_order': ['_order']}
>>> Form.a = 9
>>> Form.__dict__

{'a': 9, '_order': ['_order']}
>>> Form.__dict__["b"] = 10
>>> Form.b

10
>>>


What happens is that class assignment sets the
class __dict__ via PyDict_SetItem and not
through the generic mapping interface. In
essence it does

dict.__setitem__(self.__dict__, "new class var", val)
instead of
setitem(self.__dict__, "new class var", val)

Therefore I don't think it's possible to do what
you want. There's no way to interpose your own
code in class variable assignment.

Here's a way to implement the suggestion of
Thomas Heller's.

import itertools
order_gen = itertools.count().next

class Counted(object):
def __init__(self):
self._order = order_gen()

class TextBox(Counted):
def __init__(self, length, default, password = False):
Counted.__init__(self)
self.length = length
self.default = default
self.password = password

class Form:
nickname = TextBox(length=15, default="")
password = TextBox(length=10, default="swordfish", password=True)

>>> Form.nickname._order

0
>>> Form.password._order

1
>>>


Not as clever as checking the stack trace, but probably
for the better.


Andrew
(E-Mail Removed)
 
Reply With Quote
 
WenChen
Guest
Posts: n/a
 
      09-23-2004
Hi

I have done scripts to generate web form and the rest xml, content
template at once

here is the url
http://newped.auckland.ac.nz/python/idevice_template

Currently, the save function only generate files in a template directory

That editor's goal is to allow none techie people to generate their
instructional devices( form ) on their own will and can be plugged into
eXe for use without further coding needs.

Don't know is this somewhat similar to what you want
the code itself is nothing special, it uses javascript dom and python
text processing.

************************************************** ************************
Later, we will put a help box & extra info ( for advanced user to put
style, tags info) for a field,

This is a tool we will use in an opensource project
https://eduforge.org/projects/exe/

still at pre-planning stage, but there is a proof-of-concept to play
around -- see the idevices list, those can be generated by the idevice
editor
http://newped.auckland.ac.nz/python/eXe/start.pyg

************************************************** ************************




> >>There should be some way to pass the "location". If you solve this,
> >>you got your answer (of course, passing the "location" is not trivial,
> >> that's why every GUI toolkit has it own approach).

> >
> >
> > That's *exaclty* the root of my problems. I don't want to specify
> > positioning; I only want to use *relative positioning* for everything,
> > working as automatically as possible. Will it work for all situations?
> > I'm not sure. But I think it's worth investigating.
> >
> > What I intend to do is to borrow some of the layout techniques defined
> > by CSS+DHTML. It is *much* more complex that this, but it basically
> > works by formatting "block level" and "inline elements" elements
> > relatively to each other. You can give hints as to the relative
> > positioning -- absolute, relative, floating to the left, floating to
> > the right, and stuff like that. The layout engine positions everything
> > according to the constraints. If the engine doesn't support advanced
> > layout, it simply falls back to a simple sequence of entries according
> > to the original text flow.
> >
> > There are two reasons behnd my choice:
> >
> > -- there is a lot of knowledge today about how to make good interfaces
> > using CSS. It's faster to develop and is more flexible regarding
> > different engines and platforms than to rely on absolute positioning
> > (as conventional GUI builders do)
> >
> > -- it makes *much* easier to use the same form definition on native
> > GUIs and web-based ones. My goal is to be able to build a wxPython
> > dialog or a web form from the same description.
> >
> > That's why I can't give positioning hints, at least for now. But I
> > could include relative ordering information; for example, an arbitrary
> > tag number. But doing this I would defeat my objective of clarity of
> > design.
> >

>


 
Reply With Quote
 
Carlos Ribeiro
Guest
Posts: n/a
 
      09-23-2004
On 23 Sep 2004 14:18:25 +1200, WenChen <(E-Mail Removed)> wrote:
> Hi
>
> I have done scripts to generate web form and the rest xml, content
> template at once
>
> here is the url
> http://newped.auckland.ac.nz/python/idevice_template


Yes, its closely related to what I'm trying to do, but still its
different, because I want to be able to write the specification of the
data entry form as a Python class that can in turn be converted into a
HTML representation. BTW, I solved most of the issues that were
barring my progress, and I think I may be able to have something
working quite fast now.

Thanks for the pointer,

--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: (E-Mail Removed)
mail: (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declarative use of sqlDataSource in ASP.net 2.0 and Exception Hand =?Utf-8?B?SmVmZkRvdE5ldA==?= ASP .Net 1 02-02-2006 05:35 AM
Mini Dual Layers? Mini dl ScubaSteve7979@gmail.com DVD Video 0 10-25-2005 07:52 PM
Can't locate object method "first" via package "attempt" (perhaps you forgot to load "attempt"?) at .... GMI Perl Misc 3 06-19-2005 10:44 PM
Mini Network attempt! Me-GT Computer Support 1 09-07-2003 03:59 PM



Advertisments