[storm] Bool database type
James Henstridge
james at jamesh.id.au
Mon Aug 6 10:05:08 BST 2007
On 05/08/07, Drew Smathers <drew.smathers at gmail.com> wrote:
> For Bool-type columns in a class it looks as if this directly maps to
> the equivalent boolean type (surprise) for the underlying database.
> My problem is with tables where the boolean type is not used and
> instead a single character (either '0'/'1', or 'Y'/'N') is used used
> for this. It seems like the only way to achieve this now with storm
> is to write a custom property/variable like so:
>
> YesNoBoolVariable(Variable):
> @staticmethod
> def _parse_set(value, from_db):
> ...
> return ('N', 'Y')[bool(value)]
>
> YesNoBool(SimpleProperty):
> variable_class = YesNoBoolVariable
>
> class Foo:
> ...
> ready = YesNoBool()
>
> Is the above the only way to do this?
You could probably use the Enum() column type. Something like this:
class Foo:
ready = Enum(map={False: 'N', True: 'Y'})
This should handle getting values from the database as booleans as
well as setting them. You could potentially use a helper function or
class if you have many columns that encode the data like this.
James.
More information about the storm
mailing list