Need help with Python class idioms

Mike C. Fletcher mcfletch at rogers.com
Wed Jan 21 16:21:32 EST 2004


Tad Marko wrote:

>Howdy!
>
>I'm trying to get my head around Python and I've come to realize that
>there are a lot of idioms in Python that are a bit different than in
>other languages. I have a class similar to what I've included below.
>The only difference is that in the real class, I have even more
>mandatory attributes. Too many to specify at once on a constructor and
>they don't always exist at construction time anyway, so the accessor
>idiom must remain.
>
>Can anyone suggest a more Pythony way to do the following?
>  
>
Note: shameless plug follows...

With basicproperty, it would look something like this:

class A( propertied.Propertied ):   
    mandatoryVar1 = common.IntegerProperty(
       "mandatoryVar1", """Property that's mandatory 'cause we're cool""",
    )
    defaultVar1 = common.IntegerProperty(
       "defaultVar1", """Property that's got a default value""",
       defaultValue = 32,
    )
    defaultVar2 = common.StringProperty(
       "defaultVar2", """Property that's got a default function instead""",
       defaultFunction = lambda prop, client: client.__class__.__name__,
    )

a = A( mandatoryVar1 = 3 )
a.defaultVar2
a.defaultVar1

however, you continue on to show you're using a database, so you'd 
probably prefer something like pytable (which is built on top of 
basicproperty, but uses database schemas to control the creation and 
operation of the properties):

from pytable.schemabuilder import *
schema = table(
    "my_table",
    comment = """Table for storing whatevers""",
    fields = (
        field(
            'mandatoryVar1', 'integer',0, # name, sql data-type, size
            constraints = (
                notNull(),
            ),
        ),
        field(
            'defaultVal1', 'integer',0, # name, sql data-type, size
            defaultValue = 32, # sql default value
            constraints = (
                notNull(),
            ),
        ),
        field(
            'Var2', 'timestamptz',0, # name, sql data-type, size
            defaultValue = '"timestamp"(\'now\'::text)', # sql default value
            constraints = (
                notNull(),
            ),
        ),
    ),
    indices = (
        index( primary=1, fields=('mandatoryVar1', 'defaultVal1')),
    ),
)

print schema.itemClass
help( schema.itemClass )

row = schema.itemClass( mandatoryVar1 = 33 )
row.insertQuery( my_db_connection ) # get one from pytable
row.mandatoryVar1 = 34
row.updateQuery( my_db_connection )
row.deleteQuery( my_db_connection )

BasicProperty:
    http://basicproperty.sourceforge.net/
PyTable RDBMS Wrapper:
    http://pytable.sourceforge.net/

In a more general sense, for DB operations, use a database wrapper 
module, there's lots of them around.  And even more generally, 
properties are *very* good at describing (common) property-based systems 
of objects such as you're apparently working with.  Properties are new 
with Python 2.2, so underused sometimes, but they are very good at 
certain types of domain modelling.

Enjoy yourself,
Mike

_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/







More information about the Python-list mailing list