2006/1/6, limodou <[EMAIL PROTECTED]>:
> 2006/1/6, Jonathan Ellis <[EMAIL PROTECTED]>:
> > That would work, but it's bad if you later want to introspect on __name__
> > and do something useful with it.
> >
> > Better to pull out table.name and use something based on that as the class
> > name.
> >
> Good idea! Thanks.
>

new version:

def assign_class(table, **kwargs):
    import new
    klass = new.classobj('Class' + table.name.capitalize(), (object,), {})
    def __init__(self, **kwargs):
        for key, value in kwargs.items():
            self.__dict__[key] = value
    def __repr__(self):
        s = []
        for k in self.__class__.c.keys():
            value = getattr(self, k, '')
            if isinstance(value, unicode):
                value = value.encode('gbk')
            s.append("%s=%s" % (k, value))
        return ','.join(s)
    klass.__init__ = __init__
    klass.__repr__ = __repr__
    klass.table = table
    assign_mapper(klass, table, **kwargs)
    return klass

Usage:

A = assign_class(tableA)
B = assign_class(tableB, order_by=tableB.c.name)

--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_idv37&alloc_id865&op=click
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to