new convenience methods

Jacob Smullyan <[email protected]> Mon, 1 Aug 2005 12:39:36 -0400
Newsgroups gmane.comp.web.skunkweb
Message-ID <[email protected]>
Dear followers of PyDO SVN:

In svn HEAD I've added two things that I hope will be timesavers,
ForeignKey and OneToMany:

  class foo(PyDO):
     fields=(Sequence('id'),
             'name')

  class bar(PyDO):
    fields=(Sequence('id'),
            'x',
            'foo_id')

    Foo=ForeignKey('foo_id', 'id', foo)

  # adding these outside the class body because of circularity
  foo.getBar=OneToMany('id', 'foo_id', bar)
  # getBar takes getSome()-like methods
  res=foo.getBar(LT(FIELD('x'), 500))
  # the ForeignKey property is setable
  res[0].Foo=foo.new(name='aardvark')


The purpose here is solely to avoid writing boilerplate; there is
nothing mysterious being done here that you couldn't do before.  Both
ForeignKey and OneToMany deal with multi-column foreign keys as well
(just use tuples and make sure they are in the same order and have the
same length).

I added these to HEAD because 1) I needed them myself, and 2) there
are no backwards compatibility issues, so I felt I could justify it
without creating a branch, despite being in beta.  If people have very
different ideas about what could be done here and it looks like this
won't reach stability soon, I can move it out to a branch.

A similar shortcut for many-to-many would be obvious to do and will
follow.  

I didn't see a single obvious pattern for 1-to-many mutator methods --
sometimes a setter that takes a sequence would make sense, sometimes
add()/remove() is the pattern you'd want to follow -- so I punted.  A
possibility would be, rather than directly returning an accessor
method from OneToMany, to return an object that would have get(),
set(), add(), and remove() methods.  I find that mildly irritating for
some reason, but maybe it is OK.

Any ideas?

Cheers,

js


-- 
Jacob Smullyan
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFC7lBIuqamFyFXXLIRAqsDAJwIV8den99EX9fE+5MPSqPqxY6v1gCg07kW
dYQyBCbx7BwTtwlxy3nEvEo=
=bfMe
-----END PGP SIGNATURE-----