[storm] Type checking

Stuart Bishop stuart at stuartbishop.net
Wed Feb 18 04:54:16 GMT 2009


On Wed, Feb 18, 2009 at 2:24 AM, Vernon Cole <vernondcole at gmail.com> wrote:
> Dear Group:
>
> Okay, I'm sold. Type conversions should not be implicit. What's the neatest
> way to make them explicit?
>
> I am initiating the Vernon's Storm Programming Challenge
>
> (The winner will receive immortal glory and a gift certificate for a Big
> Mac.)
>
> challenge I
>
> I have a new class, inspired by PEP 313, useful for movie copyright dates
> and major American football games.
> It is a subclass of int and works like this:
>
>>>> import roman
>>>> print roman.roman('II') + roman.roman('Vii')
> IX
>>>> roman.toRoman(52)
> u'LII'
>>>> roman.fromRoman('MCMXLXI')
> 1951
>>>>
>>>> roman.toRoman('this is garbage')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "roman.py", line 97, in toRoman
>     n = int(N)
> ValueError: invalid literal for int() with base 10: 'this is garbage'
>
> In a sports database, I want to make a column 'superbowl' which gets and
> gives data of class 'roman'.
>
> How do I implement it in storm?

Class MyTable(Storm):
    __storm_table__ = 'MyTable'

    id = Int(primary=True)
    value = Int()

    def _get_roman_value():
        return roman.toRoman(self.value)

    def _set_roman_value(roman_value):
        self.value = roman.fromRoman(roman_value)

    roman_value = property(_get_roman_value, _set_roman_value)


All nice and explicit.


-- 
Stuart Bishop <stuart at stuartbishop.net>
http://www.stuartbishop.net/



More information about the storm mailing list