[sqlalchemy] sa0.5 __init__ replacement
hi i have class X(object): X.__init__ = setattr_kargs where def setattr_kargs( *args, **kargs): assert len(args)==1 x = args[0] for k,v in kargs.iteritems(): setattr( x, k, v) when SA comes to play, it fails to find a 'self' in __init__ arguments. in format_argspec_plus(), this line self_arg = spec[0] and spec[0][0] or None should never be None: self_arg = spec[0] and spec[0][0] or spec[1]+'[0]' seems to work. why not (args,vargs,kwargs,defaults) instead of spec+indexes? also, the empty defaults is tuple/() and not None, so spec[3] should not be compared to None but just bool of it. Something like: def format_argspec_plus(fn, grouped=True): spec = args,vargs,kwargs,defaults = inspect.getargspec(fn) vkargs = inspect.formatargspec(*spec) self_arg = args and args[0] or vargs+'[0]' apply_pos = inspect.formatargspec( args,vargs,kwargs) defaulted_vals = defaults and args[-len(defaults):] or () apply_kw = inspect.formatargspec( args,vargs,kwargs, defaulted_vals, formatvalue=lambda x: '=' + x) if grouped: return dict(args=vkargs, self_arg=self_arg, apply_pos=apply_pos, apply_kw=apply_kw) else: return dict(args=vkargs[1:-1], self_arg=self_arg, apply_pos=apply_pos[1:-1], apply_kw=apply_kw[1:-1]) the log/printing of functext/funcvars will help a bit, as the functext is not very visible in a stacktrace. and/or, maybe fix/hack with the co_filename and co_firstlineno code-attributes so inspect.getsource( myclass.__init__) works... maybe pointing to orm.attributes._generate_init function. on a general note, this functext-generation+exec... is going to make troubles. i've been generating languages (like 3 level python1 - python2 - python3/C/corba/html/...) for years, but always exposed the full result and tried to avoid it as much as possible - if _can_ be done without generation+exec - or absolute possible minimum of it. it's a _very_ slippery slope, regardless how attractive it may look. debugging it is a nightmare. for example, the query._generative decoration is not really readable, if something breaks there, would be very hard to understand what/why/where. Playing with user's __init__ may be unavoidable but these decorators over ~standartized SA code should work without stringtext+exec. anyway ciao svilen --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: validation and old values
thanks sandro *:-) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] session expire and discard
Hi, is there a way to say to a session to forget all the changes to an object without hitting the database. Expire or refresh do that but at the prize of a new select. Thanks --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] register dml to be triggerred after create_all
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 hi friends, i wish to insert some initial data in a few management tables (like applications groups, roles etc). is there a way to register dml to be done after create_all ends? i'd specifically like it to happen after the entire ddl dust reaches the ground. thanks in advance, alex -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.6 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iQCVAwUBSGeqaNpwN1sq38njAQI0LQP/ZVKlf0hibUVQAy24fd8JFKNaN6C0yVul b104X2nm/5aVzC1/1oSdSkBLpK4G8EYBNEf/r8eIdPTUutBZMUm1zn04iSUF73Kj 0EaNYgyBjJrKTfGlrS0Yk8/uwKDPPdXoTsbKL2Xm/zITqcbEPdIMAk4HzKjXP3QY uh3xq4MbRk8= =JSlq -END PGP SIGNATURE- --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: MySQL 5.0 DateTime initialization during Table creation is failing.
to my knowledge MySQL doesn't support DEFAULT on date/time columns. You can work with the MySQL database directly a little bit to confirm this. On Jun 28, 2008, at 3:18 PM, Gloria W wrote: Hi All, Looking back in these posts, I tried several older variants of MySQL datetime column initialization discussed here, and they're not working. This works in Postgresql: sqlalchemy.Column('date_created', sqlalchemy.DateTime, sqlalchemy.PassiveDefault(sqlalchemy.sql.func.now()), nullable=False) But the MySQL equivalent fails: sqlalchemy.Column('date_created', sqlalchemy.DateTime, sqlalchemy.PassiveDefault(text(CURRENT_TIMESTAMP)), nullable=False) What is the valid syntax? Is it failing for other reasons? Thank you in advance, Gloria --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: session expire and discard
On Jun 29, 2008, at 11:23 AM, sandro dentella wrote: Hi, is there a way to say to a session to forget all the changes to an object without hitting the database. Expire or refresh do that but at the prize of a new select. there is not because for it to be generally useful, it needs to synchronize with the current transaction in progress - which then results in all sorts of nasty questions such as what do we do for elements that were lazily loaded within the transaction, what do we do with elements that had been expired and reloaded within the trans, etc. The fact is that if you've just rolled back a transaction, in a concurrent environment you now have no idea whats in the database or the true state of your objects, unless you go back and re-fetch. The attribute rollback feature also adds a little bit of bookkeeping overhead and of course more complexity to the source code. So for all these reasons, the attribute rollback feature of SA remains in a dev branch. Since expiry works so well we've decided to KISS. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: register dml to be triggerred after create_all
alex bodnaru wrote: hi friends, i wish to insert some initial data in a few management tables (like applications groups, roles etc). is there a way to register dml to be done after create_all ends? i'd specifically like it to happen after the entire ddl dust reaches the ground. MetaData and Tables emit DDL events that you can listen for with .append_ddl_listener. http://www.sqlalchemy.org/docs/04/sqlalchemy_schema.html#docstrings_sqlalchemy.schema_MetaData Here's an example insert-after-CREATE function from the SA test suite: def fixture(table, columns, *rows): Insert data into table after creation. def onload(event, schema_item, connection): insert = table.insert() column_names = [col.key for col in columns] connection.execute(insert, [dict(zip(column_names, column_values)) for column_values in rows]) table.append_ddl_listener('after-create', onload) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Implementation patterns for initializing the model and obtaining the mappers
I am coming from the Zope world and implemented a SA integration layer z3c.sqlalchemy. It basically provides a 'wrapper' exposing a thread-local session as property and as a registry for mappers that can be accessed through a getMapper(mapper_name) method. Now with the declarative layer I am trying to get rid of the approach and basically define the mapper classes on the module level and importing them from the other modules as needed. The problem with module-level initialization is that you have little control about initialization parameter (e.g. I must be able to pass the DSN as parameter). My current code looks a bit like this: main.py: dsn = get_dsn_from_some_config_object(..) import model model.setup(dsn) from model import MyMapper, Session session = Session() rows = session.query(MyMapper).filter_by().all() model.py: - def setup(dsn): engine= create_engine(...) session = scoped_session(sessionmaker(...))) Base = declarative_base(engine) class MyMapper(Base): __table__ = Table('mytable', Base.metadata, autoload=True) for k,v in locals().items(): globals()[k]=v This approach is ugly (because of putting the mapper within the local scope into the global scope (in order to make them importable) and because of this code within main.py: import model model.setup(dsn) from model import MyMapper, Session This there any better pattern for implementing this? Andreas pgp8Sn4MBKtP9.pgp Description: PGP signature
[sqlalchemy] Re: MySQL 5.0 DateTime initialization during Table creation is failing.
Gloria W wrote: Hi All, Looking back in these posts, I tried several older variants of MySQL datetime column initialization discussed here, and they're not working. This works in Postgresql: sqlalchemy.Column('date_created', sqlalchemy.DateTime, sqlalchemy.PassiveDefault(sqlalchemy.sql.func.now()), nullable=False) But the MySQL equivalent fails: sqlalchemy.Column('date_created', sqlalchemy.DateTime, sqlalchemy.PassiveDefault(text(CURRENT_TIMESTAMP)), nullable=False) What is the valid syntax? Is it failing for other reasons? The MySQL TIMESTAMP type is required for that default: from sqlalchemy.databases import mysql sqlalchemy.Column('date_created', mysql.MSDateTime, sqlalchemy.PassiveDefault(text(CURRENT_TIMESTAMP)), nullable=False) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: MySQL 5.0 DateTime initialization during Table creation is failing.
jason kirtland wrote: Gloria W wrote: Hi All, Looking back in these posts, I tried several older variants of MySQL datetime column initialization discussed here, and they're not working. This works in Postgresql: sqlalchemy.Column('date_created', sqlalchemy.DateTime, sqlalchemy.PassiveDefault(sqlalchemy.sql.func.now()), nullable=False) But the MySQL equivalent fails: sqlalchemy.Column('date_created', sqlalchemy.DateTime, sqlalchemy.PassiveDefault(text(CURRENT_TIMESTAMP)), nullable=False) What is the valid syntax? Is it failing for other reasons? The MySQL TIMESTAMP type is required for that default: from sqlalchemy.databases import mysql sqlalchemy.Column('date_created', mysql.MSDateTime, sqlalchemy.PassiveDefault(text(CURRENT_TIMESTAMP)), nullable=False) err, mysql.MSTimeStamp --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: sa0.5 __init__ replacement
[EMAIL PROTECTED] wrote: hi i have class X(object): X.__init__ = setattr_kargs where def setattr_kargs( *args, **kargs): assert len(args)==1 x = args[0] for k,v in kargs.iteritems(): setattr( x, k, v) when SA comes to play, it fails to find a 'self' in __init__ arguments. r4880 now considers args[0] as 'self' when introspecting def(*args): ... --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Implementation patterns for initializing the model and obtaining the mappers
On Jun 29, 2008, at 12:46 PM, Andreas Jung wrote: This approach is ugly (because of putting the mapper within the local scope into the global scope (in order to make them importable) and because of this code within main.py: import model model.setup(dsn) from model import MyMapper, Session This there any better pattern for implementing this? you don't need to pass DSN as a parameter. The engine argument to declarative_base() is optional, and the docs have been revised in recent months to reflect this. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: register dml to be triggerred after create_all
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 thanks a lot jason, now i see the metadata version of the same. alex jason kirtland wrote: | alex bodnaru wrote: | hi friends, | | i wish to insert some initial data in a few management tables (like applications | groups, roles etc). | | is there a way to register dml to be done after create_all ends? | | i'd specifically like it to happen after the entire ddl dust reaches the ground. | | MetaData and Tables emit DDL events that you can listen for with | .append_ddl_listener. | | http://www.sqlalchemy.org/docs/04/sqlalchemy_schema.html#docstrings_sqlalchemy.schema_MetaData | | Here's an example insert-after-CREATE function from the SA test suite: | | def fixture(table, columns, *rows): | Insert data into table after creation. | def onload(event, schema_item, connection): | insert = table.insert() | column_names = [col.key for col in columns] | connection.execute(insert, |[dict(zip(column_names, column_values)) | for column_values in rows]) | table.append_ddl_listener('after-create', onload) | | | | | -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.6 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iQCVAwUBSGgW8tpwN1sq38njAQKUsQP/UmbeNlPKzYGGAnjnk4/axjYtasO8HAUg jRcRp57J9L0t0UFXE9Lyra66wywSM0fg80Q4ajEEcTQFyh8DOwwbuoJT55pQyV+e BJ8lw379eCdVsHhdA/fFg/vIjZF96qFXHfCj6UnFrk9Gsk/mLWuWqUZPSd8dyS3M yUeDOWzs5vI= =7KPc -END PGP SIGNATURE- --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: MySQL 5.0 DateTime initialization during Table creation is failing.
This gives me an error: sqlalchemy.Column('date_created', mysql.MSTimeStamp, sqlalchemy.PassiveDefault(text(CURRENT_TIMESTAMP)), nullable=False)) NameError: global name 'text' is not defined --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: MySQL 5.0 DateTime initialization during Table creation is failing.
Gloria W wrote: This gives me an error: sqlalchemy.Column('date_created', mysql.MSTimeStamp, sqlalchemy.PassiveDefault(text(CURRENT_TIMESTAMP)), nullable=False)) NameError: global name 'text' is not defined from sqlalchemy import text --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Implementation patterns for initializing the model and obtaining the mappers
--On 29. Juni 2008 16:57:16 -0400 Michael Bayer [EMAIL PROTECTED] wrote: On Jun 29, 2008, at 12:46 PM, Andreas Jung wrote: This approach is ugly (because of putting the mapper within the local scope into the global scope (in order to make them importable) and because of this code within main.py: import model model.setup(dsn) from model import MyMapper, Session This there any better pattern for implementing this? you don't need to pass DSN as a parameter. The engine argument to declarative_base() is optional, and the docs have been revised in recent months to reflect this. However the engine is required in order to perform autoloading. Andreas pgpEpALHo4YAD.pgp Description: PGP signature
[sqlalchemy] Columns division: cast() doen't seem to work. Or how to use it ?
Hi, With direct sql statement, dividing 2 columns with CAST provide a good result (-- 1/2 = 0.5 and not 0). SELECT * , CAST(Mytable.colB AS FLOAT) / CAST(Mytable.colC AS FLOAT) AS CALCUL FROM Mytable When using a SA query with add_column, the result is not correct whether we use cast() or not (and seems equivalent to the direct sql query without CAST). In that case, results of the division is erroneous: 1/2 = 0 and not 0.5, no matter you use cast or not. Query with cast(): session.query(Mytable).add_column(cast(Mytable.colB,Float) / cast(Mytable.colC,Float)).all() Direct sql: sql = SELECT *, (Mytable.colB / Mytable.colC) AS CALCUL FROM Mytable Even if the figures are floats, it doesn't seem to use or consider them as floats for the division. 1 / 2 = 1 / 2.0 = 1 / 2.0 all result in 0 Only in this case 1 / 2.01 will it work. Run the attached snippet to check the example. Everything works just like the classic division in C or python , when not using from __future__ import division. Results are the same whether you use this statement or not. Can someone tell me if I'm missing something and in this case how to write the SA query. Or is the cast() function not correctly used or working in certain cases ? Thanks in advance for your answer Dominique #! /usr/bin/env python # -*- coding: utf-8 -*- #from __future__ import division from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.sql import * import time metadata = MetaData() engine = create_engine('sqlite:///:memory:', encoding = 'utf8', echo=False) mytable = Table('mytable', metadata, Column('id', Integer, primary_key=True), Column('colA', Float), Column('colB', Float), Column('colC', Float) ) class Mytable(object): def __init__(self, colA, colB, colC): self.colA = colA self.colB = colB self.colC = colC def __repr__(self): return Mytable('%s','%s', '%s') % (self.colA, self.colB, self.colC) metadata.create_all(engine) mapper(Mytable, mytable) e0=Mytable(0, 0, 0) e1=Mytable(1, 1, 0) e2=Mytable(2, 2, 0) e3=Mytable(3, 0, 10)#0 e4=Mytable(4, 1, 10)#0.1 e5=Mytable(5, 2, 10)#0.2 e6=Mytable(6, 2, 4)#0.5 e7=Mytable(7, 3, 4.1)#0.75 e8=Mytable(8, 3, 8.1)#0.375 e9=Mytable(9, 4, 8)#0.5 e10=Mytable(10, 5, 8.01)#0.625 e11 = Mytable(11, 11, 10)#1.1 e12=Mytable(12,10,10)#1 e13=Mytable(13, 3,10)#0.3 Session = sessionmaker(bind=engine, autoflush=True, transactional=True) session = Session() for i in [e0,e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,e11,e12,e13]: session.save(i) session.commit() mycase = cast(Mytable.colB,Float) / cast(Mytable.colC,Float) Query1 = session.query(Mytable).add_column(mycase).all() print Query1 = ,Query1 for row in Query1: print row sql = SELECT *, (Mytable.colB / Mytable.colC) AS CALCUL FROM Mytable sql2 = SELECT * , CAST(Mytable.colB AS FLOAT) / CAST(Mytable.colC AS FLOAT)AS CALCUL FROM Mytable Query2 = session.execute(sql) print Query2 = ,Query2 for row in Query2: print row session.clear() session.close() --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---