Re: [SQLObject] Keeping the fields in order
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Carlos R. <car...@gm...> - 2005-01-29 01:27:04
|
On Fri, 28 Jan 2005 18:53:30 -0600, Ian Bicking <ia...@co...> wrote: > Carlos Ribeiro wrote: > > Humm. A __getitem__/__setitem__ there would not do any harm IMHO. I > > hope ou reconsider :-) But at the same time, an adapter may be enough > > for this purpose. > > A dictionary interface is just too ambiguous. I can appreciate that it > comes with a number of useful methods (like keys(), __contains__, etc) > that feel missing compared to normal attributes. But [] implies a level > of dynamicism that I think isn't good. I'm not sure I understand what do you mean with 'ambiguous'. One possible concern is that access through the [] interface will happily create new (key, item) pairs. In this case, a custom __setitem__ can be designed so it traps it and raises an KeyError instead. > > One potential application for this interface is to build automated > > tools around SQLObject. For instance: report generators, form > > generators, and the kind of scaffolding stuff that Rails is now famed > > for. Of course, one can dive into SQLObject internals... but better > > introspection methods would be helpful. A dict-like interface is > > natural because __getitem__ style access is more convenient in this > > scenario, to retrieve the necessary fields. BTW, I found myself using > > getattr more often than I would care to admit. > > I would agree there should be more clear, public methods for reflection > in SQLObject, but I don't think there's anything wrong with getattr, and > I don't think the dictionary interface is ultimately complete enough or > extensible enough. I think I expressed myself badly; there's nothing wrong with getattr per se... but on the other hand, I believe that access via the __getitem__ interface is still more convenient. But that may be a matter of personal opinion. I think that, while we can overload __getitem__ so it exposes only a few attributes of the object (in SQLObject's case, just the columns), it's more difficult to do the same with a getattr interface, which involves automatic lookup on the class, superclasses, etc. An example may help to clarify things: I would welcome the ability to iterate over the columns of a SQLObject using iteritems() to build a report, or a form, as in: for colname, value in Person.iteritems(): print "%s: %s" % colname, value The main advantage is familiarity: the iteritems() interface is well know. It can return only the columns defined in the object, and that seems to be intuitive enough for users to understand. That said, I think that's a very interesting discussion. Let's the issue rest a little bit; maybe we can gather more opinions... Best regards, and have a nice weekend! -- Carlos Ribeiro Consultoria em Projetos blog: http://rascunhosrotos.blogspot.com blog: http://pythonnotes.blogspot.com mail: car...@gm... mail: car...@ya... |