Re: What am I doing wrong with my pydo.Field class?
Jacob Smullyan <[email protected]> Sat, 8 Oct 2005 18:56:11 -0400
| Newsgroups | gmane.comp.web.skunkweb |
|---|---|
| Message-ID | <[email protected]> |
On Fri, Oct 07, 2005 at 10:37:48PM +0100, Hamish Lawson wrote:
> I've defined a class derived from pydo.Field as follows:
>
> class XMLPara(pydo.Field):
>
> def __get__(self, obj, type_):
> """descriptor method"""
> if obj is None:
> return None
> return tidy(obj[self.name])
>
> I then use it in my schema as follows:
>
> class Researcher(ResExpTable):
> fields = (
> pydo.Unique('USERNAME'),
> XMLPara('RES_EXP'),
> ....
>
> However a print statement in XMLPara.__get__ indicates that 'obj' is
> always None. Am I defining and using XMLPara correctly?
I believe that 'obj' would be None if you call the descriptor through
the class, rather than through an instance. Are you doing that?
(This would be true of pydo.Field as well.) If you are calling it
through an instance, though, then I'm puzzled, and would like to see
more of the code.
However, if you want to introduce data transformation, I wonder if you
should be doing it this way. You've now introduced a difference
between the values returned by obj['RES_EXP'] and obj.RES_EXP, which I
think is asking for trouble. If you want an attribute that returns a
transformed value (without actually persisting that value), I think it
would be better to use a property with a different name, rather than
putting the magic in the field. If you want to persist the
transformed (in this case, tidied) value, you can do that by
overriding PyDO.onUpdate.
Cheers,
js
--
Jacob Smullyan
signature.asc
(application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFDSE6LuqamFyFXXLIRAoO5AJ49wpJph91fQAZPbLcFz+QR9ZFylwCfQ9CE rcmUxC+LeIx6wPWe7XhR+sE= =iUK/ -----END PGP SIGNATURE-----