Hi,
I have been just playing with some new features of Python2.5 in
connection with SA0.3.
Usage of the "with_statement" seems to be particularily promising. I
made a basic test: let's use contextlib.contextmanager and define a
method in metadata:
@contextmanager
def create_table( self, name, **kw ):
_table = xTable( name, self, **kw )
try:
setattr(_table,'triger', True)
yield _table
except Exception, why:
print 'setter fails: %s' % str(why)
del _table
else:
delattr(_table, 'triger') #don't really creating... will wait
for create_all afterwards
setattr(DynamicMetaData, 'create_table', create_table)
and subclass Table:
class xTable(Table):
def __setattr__(self, name, value):
if hasattr(self, 'triger') and name not in self.__dict__:
v = list( value ) # 'value' is tuple, thus, immutable
n = name
t = v.pop(0)
kw = v.pop()
args=tuple(v)
self.append_column( Column( n, t, *args, **kw ) )
else:
object.__setattr__(self,name,value)
These basic modifications enable one to create schemas in a simpler and
more readable way:
if __name__=='__main__':
meta = DynamicMetaData()
db_mem=create_engine('sqlite://')
meta.connect(db_mem, echo=True)
with meta.create_table('test') as t:
t.test_id = Integer, _(primary_key=True) #_=dict
t.description = String(60), _(nullable=False)
with meta.create_table('test2') as t:
t.test2_id = Integer, _(primary_key=True)
t.append_column( Column( 'test', Integer,
ForeignKey('test.test_id')))
# "special" syntax for the special column
try:
ta=meta.tables['test']
tb=meta.tables['test2']
meta.create_all()
except Exception, why:
print "failed: %s" % str(why)
else: print 'ok'
And it worked or, well, seemed so... :)
Of course, all this is pretty rudimental and less helpful. However,
IMHO, adopting novelties of Py2.5 seems really promising.
Is this particluar direction of SA extensions worthy? Any criticism is
welcome.
Kind regards,
Giorgi
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sqlalchemy
-~----------~----~----~----~------~----~------~--~---