[storm] python properties and storm properties...
Jason R Briggs
jason at briggs.net.nz
Mon Mar 24 10:24:08 GMT 2008
Thought I had. Must've hit reply rather than reply-all by mistake...
Jamu Kakar wrote:
> Hi Jason,
>
> Please CC the list when you respond to messages so that everyone can
> see the complete thread.
>
> Thanks,
> J.
>
> Jason R Briggs wrote:
>> Thanks for that. I'd already come up with the former solution, but
>> decided it wasn't the right answer since it then requires the column
>> name to be changed -- thus the column is different from the actual
>> property. Not a big deal, but I figured there might be a more
>> elegant way. Your second solution might be the answer, thanks.
>>
>> Jamu Kakar wrote:
>>> Hi Jason,
>>>
>>> Jason R Briggs wrote:
>>> > This might be a bit of a dumb question, but I have to admit I'm
>>> > struggling to find the right answer.
>>>
>>> The only dumb question is the one you don't ask. :)
>>>
>>> > Is there a way to use a Storm property in a similar way to a python
>>> > property? For example, in a normal class I can define a setter and
>>> > getter and then a property which uses those methods:
>>> >
>>> > def set_password(self, password):
>>> > self._password = encrypt_password(self.username, password)
>>> >
>>> > def get_password(self):
>>> > return self._password
>>> >
>>> > password = property(get_password, set_password)
>>> >
>>> > Can I do something similar with a storm property?
>>>
>>> You can do this:
>>>
>>> class Person(Storm):
>>>
>>> __storm_table__ = "person"
>>>
>>> id = Int(primary=True)
>>> password_hash = RawStr(allow_none=False)
>>>
>>> def set_password(self, password):
>>> self.password_hash = encrypt_password(self.username, password)
>>>
>>> def get_password(self):
>>> return self.password_hash
>>>
>>> password = property(get_password, set_password)
>>>
>>> Another option is to create a custom property to do what you want
>>> (useful if you need the behaviour in more than one place and don't
>>> want to repeat the set/get property boilerplate):
>>>
>>> from storm.variables import RawStrVariable
>>> from storm.properties import SimpleProperty
>>>
>>> class PasswordVariable(RawStrVariable):
>>>
>>> def parse_set(self, value, from_db):
>>> return super(PasswordVariable,
>>> self).parse_set(encrypt_password(value),
>>> from_db)
>>>
>>> class Password(SimpleProperty):
>>>
>>> variable_class = PasswordVariable
>>>
>>> And then do:
>>>
>>> class Person(Storm):
>>>
>>> __storm_table__ = "person"
>>>
>>> id = Int(primary=True)
>>> password = Password(allow_none=False)
>>>
>>> I haven't tested either of these solutions, but I believe they will
>>> both work.
>>>
>>> Thanks,
>>> J.
>>>
>>
>
--
Jason R Briggs
[e] jason at briggs.net.nz
[m] +44 7914 641406
[w] http://www.domyinvoice.com
[b] http://www.briggs.net.nz/log
More information about the storm
mailing list