Looking good. I prefer the generic repr that I posted earlier, though.
def __repr__(self):
L = ['%s=%s' % (a, repr(getattr(self, a))) for a in dir(self) if not callable(getattr(self, a)) and not a.startswith('_')]
return '%s(%s)' % (self.__class__.__name__, ','.join(L))
I suppose you could split that genexp into multiple lines if you like, though. :)
Michael, my first reaction when reading the docs was to write a function like this, too. I ended up deciding that I was probably going to want to customize my classes more than would be convenient with this, so I didn't, but for quick and dirty, maybe something like this would be useful.
-J
On 1/5/06, limodou <[EMAIL PROTECTED]> wrote:
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&opclick
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users
--
Jonathan Ellis
http://spyced.blogspot.com