there's nothing magic about klass.__name__ being the same as the identifier you assign it to
>>> class Foo: pass
...
>>> Bar = Foo
>>> Bar.__name__
'Foo'
>>> isinstance(Bar(), Foo)
True
>>> isinstance(Foo(), Bar)
True
... so, you could just make the class name be "autogenerated_for_<tablename>" or whatever.
...
>>> Bar = Foo
>>> Bar.__name__
'Foo'
>>> isinstance(Bar(), Foo)
True
>>> isinstance(Foo(), Bar)
True
... so, you could just make the class name be "autogenerated_for_<tablename>" or whatever.
Hacking globals is a Bad Thing; what if I wanted to do this inside a function and have it be local to that function?
On 1/5/06, limodou <[EMAIL PROTECTED]> wrote:
2006/1/5, Jonathan Ellis <[EMAIL PROTECTED]>:
> instead of messing with globals, it would be cleaner to have a function that
> returned the new class (typically named klass instead of class_, btw) and
> write
>
> TransCataDesc = create_class(trans_cata_desc)
>
I use globals() there because I want to make the class object to be
able imported in global namespace automaticly. Sometimes I may use
assign_class() in a function, and I don't want to write global
statement,:
def init(dbfile='test.db'):
#global Test I don't want to write this statement
test = Table('test', engine, ...)
Test = assign_class('Test', test)
I'm a lazy man.
And I want to know: is there a way to get a class object which class
name is what you want, just like:
TransCataDesc = create_class(trans_cata_desc)
class name is 'TransCataDesc'?
because new.classobj need a class name, so the function could be written as:
TransCataDesc = create_class(TransCataDesc, trans_cata_desc)
If you don't like globals(), the new version is:
def assign_class(class_name, table, **kwargs):
import new
klass = new.classobj(class_name, (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
This time I add a __repr__ method for the class.
--
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users
--
Jonathan Ellis
http://spyced.blogspot.com

