[sqlalchemy] is True vs ==True

2015-03-30 Thread Jose Soares

Hi all,

While I changed some obsolete syntax as defined in
(https://www.python.org/dev/peps/pep-0008/)
like (is True instead of ==True) also False and None.
I realized that sqlalchemy do not support them
What can I do to avoid this behavior?


--

print  
session.query(Rischio.c.codice).select_from(Rischio).filter(Rischio.c.peso_gruppo
  == True)

SELECT caratteristica_rischio.codice AS caratteristica_rischio_codice
FROM caratteristica_rischio
WHERE caratteristica_rischio.peso_gruppo = true

print 
session.query(Rischio.c.codice).select_from(Rischio).filter(Rischio.c.peso_gruppo
 is True)
SELECT caratteristica_rischio.codice AS caratteristica_rischio_codice
FROM caratteristica_rischio
WHERE false

j

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] How to add custom attribute to the session?

2015-03-30 Thread Simon King
On Mon, Mar 30, 2015 at 1:09 PM, uralbash svintso...@gmail.com wrote:
 Hello,

 I want to set up in each session was my custom attribute, for example:

 from sqlalchemy.orm import scoped_session, sessionmaker

 Session = my_wapper(scoped_session(sessionmaker()))
 session1 = Session()
 session2 = Session()

 hasattr(session1, foo)  # True: What I want
 hasattr(session2, foo)  # True: What I want
 print(session1.foo)

 What is the best way to do it? I have no idea how I can change the object
 from __call__ function. I also did not find that something like
 Session.set_property.


You can define a subclass of the sqlalchemy Session, and ask
sessionmaker to return instances of that class by passing the class_
parameter to sessionmaker():

http://docs.sqlalchemy.org/en/rel_0_9/orm/session_api.html#session-and-sessionmaker

Would that do what you wanted?

Simon

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] How to add custom attribute to the session?

2015-03-30 Thread uralbash
More example in tests 
https://github.com/ITCase/sacrud/blob/master/sacrud/tests/test_sessionmaker.py

def test_create(self):
user = self.session.sacrud(User)\
.create({'name': 'Dzhirad', 'fullname': 'Kiri', 'password': 123})
self.assertEqual(user.name, 'Dzhirad')
db_user = self.session.query(User).get(user.id)
self.assertEqual(user.id, db_user.id)

def test_read(self):
self._add_item(User, 'Vasya', 'Pupkin', 123)
user = self.session.sacrud(User).read(1)
self.assertEqual(user.name, 'Vasya')
self.assertEqual(user.id, 1)

def test_update(self):
self._add_item(User, 'Vasya', 'Pupkin', 123)
user = self.session.sacrud(User).update(1, {'name': 'Bill'})
db_user = self.session.query(User).get(user.id)
self.assertEqual(user.name, 'Bill')
self.assertEqual(user.id, db_user.id)

def test_delete(self):
user = self._add_item(User, 'Volod', 'Khonin', 123)
self.session.sacrud(User).delete(user.id)
db_user = self.session.query(User).filter_by(id=user.id).all()
self.assertEqual(db_user, [])




понедельник, 30 марта 2015 г., 17:34:52 UTC+5 пользователь Simon King 
написал:

 On Mon, Mar 30, 2015 at 1:09 PM, uralbash svint...@gmail.com 
 javascript: wrote: 
  Hello, 
  
  I want to set up in each session was my custom attribute, for example: 
  
  from sqlalchemy.orm import scoped_session, sessionmaker 
  
  Session = my_wapper(scoped_session(sessionmaker())) 
  session1 = Session() 
  session2 = Session() 
  
  hasattr(session1, foo)  # True: What I want 
  hasattr(session2, foo)  # True: What I want 
  print(session1.foo) 
  
  What is the best way to do it? I have no idea how I can change the 
 object 
  from __call__ function. I also did not find that something like 
  Session.set_property. 
  

 You can define a subclass of the sqlalchemy Session, and ask 
 sessionmaker to return instances of that class by passing the class_ 
 parameter to sessionmaker(): 


 http://docs.sqlalchemy.org/en/rel_0_9/orm/session_api.html#session-and-sessionmaker
  

 Would that do what you wanted? 

 Simon 


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] is True vs ==True

2015-03-30 Thread Jose Soares
Hmm! in this case we must distinguish between the python syntax and the 
sqlalchemy syntax.:-(

j
On 30/03/2015 12:37, Simon King wrote:

On Mon, Mar 30, 2015 at 10:59 AM, Jose Soares
jose.soa...@sferacarta.com wrote:

Hi all,

While I changed some obsolete syntax as defined in
(https://www.python.org/dev/peps/pep-0008/)
like (is True instead of ==True) also False and None.
I realized that sqlalchemy do not support them
What can I do to avoid this behavior?


--

print
session.query(Rischio.c.codice).select_from(Rischio).filter(Rischio.c.peso_gruppo
== True)

SELECT caratteristica_rischio.codice AS caratteristica_rischio_codice
FROM caratteristica_rischio
WHERE caratteristica_rischio.peso_gruppo = true

print
session.query(Rischio.c.codice).select_from(Rischio).filter(Rischio.c.peso_gruppo
is True)
SELECT caratteristica_rischio.codice AS caratteristica_rischio_codice
FROM caratteristica_rischio
WHERE false


I don't think you can. SQLAlchemy expressions define an __eq__
method to enable expression == value-style constructs. There is no
equivalent hook in Python for the is operator, so there is no way
SQLAlchemy could use it.

Simon



--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] How to add custom attribute to the session?

2015-03-30 Thread uralbash
I'm writing a module (sacrud https://github.com/ITCase/sacrud) that is not 
initially aware of the session. I want to add my session method (sacrud).

from sqlalchemy.orm import scoped_session, sessionmaker
from sacrud import crud_sessionmaker

DBSession = crud_sessionmaker(scoped_session(sessionmaker()))
DBSession.sacrud.mycustommethod


See example there 
https://github.com/ITCase/sacrud/blob/master/sacrud/__init__.py

понедельник, 30 марта 2015 г., 17:34:52 UTC+5 пользователь Simon King 
написал:

 On Mon, Mar 30, 2015 at 1:09 PM, uralbash svint...@gmail.com 
 javascript: wrote: 
  Hello, 
  
  I want to set up in each session was my custom attribute, for example: 
  
  from sqlalchemy.orm import scoped_session, sessionmaker 
  
  Session = my_wapper(scoped_session(sessionmaker())) 
  session1 = Session() 
  session2 = Session() 
  
  hasattr(session1, foo)  # True: What I want 
  hasattr(session2, foo)  # True: What I want 
  print(session1.foo) 
  
  What is the best way to do it? I have no idea how I can change the 
 object 
  from __call__ function. I also did not find that something like 
  Session.set_property. 
  

 You can define a subclass of the sqlalchemy Session, and ask 
 sessionmaker to return instances of that class by passing the class_ 
 parameter to sessionmaker(): 


 http://docs.sqlalchemy.org/en/rel_0_9/orm/session_api.html#session-and-sessionmaker
  

 Would that do what you wanted? 

 Simon 


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] is True vs ==True

2015-03-30 Thread Richard Gerd Kuesters | Pollux

there's this:

* import sqlalchemy as sa**
** sa.sql.null()**
**sqlalchemy.sql.elements.Null object at 0x7f8a70065c50**
** sa.sql.false()**
**sqlalchemy.sql.elements.False_ object at 0x7f8a6fb13050**
** sa.sql.true()**
**sqlalchemy.sql.elements.True_ object at 0x7f8a70065c50**
*
so, you can still use __eq__ and pep-8 will not complain, because 
there's no way to implement is True or is None. then you'll have this:


*session.query(Rischio.c.codice).select_from(Rischio).filter(Rischio.c.peso_gruppo 
== sa.**sql.true()**)*


:)

On 03/30/2015 10:52 AM, Jose Soares wrote:
Hmm! in this case we must distinguish between the python syntax and 
the sqlalchemy syntax.:-(

j
On 30/03/2015 12:37, Simon King wrote:

On Mon, Mar 30, 2015 at 10:59 AM, Jose Soares
jose.soa...@sferacarta.com wrote:

Hi all,

While I changed some obsolete syntax as defined in
(https://www.python.org/dev/peps/pep-0008/)
like (is True instead of ==True) also False and None.
I realized that sqlalchemy do not support them
What can I do to avoid this behavior?


--

print
session.query(Rischio.c.codice).select_from(Rischio).filter(Rischio.c.peso_gruppo 


== True)

SELECT caratteristica_rischio.codice AS caratteristica_rischio_codice
FROM caratteristica_rischio
WHERE caratteristica_rischio.peso_gruppo = true

print
session.query(Rischio.c.codice).select_from(Rischio).filter(Rischio.c.peso_gruppo 


is True)
SELECT caratteristica_rischio.codice AS caratteristica_rischio_codice
FROM caratteristica_rischio
WHERE false


I don't think you can. SQLAlchemy expressions define an __eq__
method to enable expression == value-style constructs. There is no
equivalent hook in Python for the is operator, so there is no way
SQLAlchemy could use it.

Simon





--
Richard Gerd Kuesters
Pollux Automation
rich...@pollux.com.br mailto:rich...@pollux.com.br | www.pollux.com.br 
http://www.pollux.com.br



•Linhas de Montagem
•Inspeção e Testes
•Robótica   •Identificação e Rastreabilidade
•Software para Manufatura

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Inherited class column override

2015-03-30 Thread Pierre B
Hi all,

I'm ultimately trying to have different default values for the same column. 
Following the documentation, the @declared_attr.cacading decorator seems to 
be the best approach.
Here's my code:
class HasSomeAttribute(object):
@declared_attr.cascading
def type(cls):
if has_inherited_table(cls):
if cls.__name__ == 'MySubClass1':
return db.Column(db.Integer, default=1)
else:
return db.Column(db.Integer, default=2)
else:
return db.Column(db.Integer, default=0)

class MyClass(HasSomeAttribute, db.Model):
__tablename__ = 'people4l2'
id = db.Column(db.Integer, primary_key=True)

class MySubClass1(MyClass):
pass

class MySubClass2(MyClass):
pass

I iterated quite a few times over this but I'm systematically getting this 
error:

ArgumentError: Column 'type' on class class '__main__.MySubClass1' conflicts 
with existing column 'people4l2.type'



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] is True vs ==True

2015-03-30 Thread Simon King
On Mon, Mar 30, 2015 at 10:59 AM, Jose Soares
jose.soa...@sferacarta.com wrote:
 Hi all,

 While I changed some obsolete syntax as defined in
 (https://www.python.org/dev/peps/pep-0008/)
 like (is True instead of ==True) also False and None.
 I realized that sqlalchemy do not support them
 What can I do to avoid this behavior?


 --

 print
 session.query(Rischio.c.codice).select_from(Rischio).filter(Rischio.c.peso_gruppo
 == True)

 SELECT caratteristica_rischio.codice AS caratteristica_rischio_codice
 FROM caratteristica_rischio
 WHERE caratteristica_rischio.peso_gruppo = true

 print
 session.query(Rischio.c.codice).select_from(Rischio).filter(Rischio.c.peso_gruppo
 is True)
 SELECT caratteristica_rischio.codice AS caratteristica_rischio_codice
 FROM caratteristica_rischio
 WHERE false


I don't think you can. SQLAlchemy expressions define an __eq__
method to enable expression == value-style constructs. There is no
equivalent hook in Python for the is operator, so there is no way
SQLAlchemy could use it.

Simon

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] How to add custom attribute to the session?

2015-03-30 Thread uralbash
Hello,

I want to set up in each session was my custom attribute, for example:

from sqlalchemy.orm import scoped_session, sessionmaker

Session = my_wapper(scoped_session(sessionmaker()))
session1 = Session()
session2 = Session()

hasattr(session1, foo)  # True: What I want
hasattr(session2, foo)  # True: What I want
print(session1.foo)

What is the best way to do it? I have no idea how I can change the object 
from __call__ function. I also did not find that something like 
Session.set_property.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] is True vs ==True

2015-03-30 Thread Mathieu Leduc-Hamel
why not just called it that way:

filter(Rischio.c.peso_gruppo.is_(True))

And it would be the same thing for None and False

And the opposite would be:

filter(not_(Rischio.c.peso_gruppo.is_(None)))

Le lun. 30 mars 2015 à 11:10, Jonathan Vanasco jonat...@findmeon.com a
écrit :


 If you're just worried about warnings in Flake8, you can turn them off
 line-by-line:

 filter(
 Rischio.c.peso_gruppo == True  # noqa
 )\


  --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/d/optout.


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] is True vs ==True

2015-03-30 Thread Jose Soares

Yeah, this is a good solution. Thanks for the tip.
j

On 30/03/2015 16:33, Richard Gerd Kuesters | Pollux wrote:

there's this:

* import sqlalchemy as sa**
** sa.sql.null()**
**sqlalchemy.sql.elements.Null object at 0x7f8a70065c50**
** sa.sql.false()**
**sqlalchemy.sql.elements.False_ object at 0x7f8a6fb13050**
** sa.sql.true()**
**sqlalchemy.sql.elements.True_ object at 0x7f8a70065c50**
*
so, you can still use __eq__ and pep-8 will not complain, because 
there's no way to implement is True or is None. then you'll have this:


*session.query(Rischio.c.codice).select_from(Rischio).filter(Rischio.c.peso_gruppo 
== sa.**sql.true()**)*


:)

On 03/30/2015 10:52 AM, Jose Soares wrote:
Hmm! in this case we must distinguish between the python syntax and 
the sqlalchemy syntax.:-(

j
On 30/03/2015 12:37, Simon King wrote:

On Mon, Mar 30, 2015 at 10:59 AM, Jose Soares
jose.soa...@sferacarta.com wrote:

Hi all,

While I changed some obsolete syntax as defined in
(https://www.python.org/dev/peps/pep-0008/)
like (is True instead of ==True) also False and None.
I realized that sqlalchemy do not support them
What can I do to avoid this behavior?


--

print
session.query(Rischio.c.codice).select_from(Rischio).filter(Rischio.c.peso_gruppo 


== True)

SELECT caratteristica_rischio.codice AS caratteristica_rischio_codice
FROM caratteristica_rischio
WHERE caratteristica_rischio.peso_gruppo = true

print
session.query(Rischio.c.codice).select_from(Rischio).filter(Rischio.c.peso_gruppo 


is True)
SELECT caratteristica_rischio.codice AS caratteristica_rischio_codice
FROM caratteristica_rischio
WHERE false


I don't think you can. SQLAlchemy expressions define an __eq__
method to enable expression == value-style constructs. There is no
equivalent hook in Python for the is operator, so there is no way
SQLAlchemy could use it.

Simon





--
Richard Gerd Kuesters
Pollux Automation
rich...@pollux.com.br mailto:rich...@pollux.com.br | 
www.pollux.com.br http://www.pollux.com.br



•Linhas de Montagem
•Inspeção e Testes
•Robótica   •Identificação e Rastreabilidade
•Software para Manufatura

--
You received this message because you are subscribed to the Google 
Groups sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to sqlalchemy+unsubscr...@googlegroups.com 
mailto:sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com 
mailto:sqlalchemy@googlegroups.com.

Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Inherited class column override

2015-03-30 Thread Michael Bayer


Pierre B rocambolesque...@gmail.com wrote:

 Hi all,
 
 I'm ultimately trying to have different default values for the same column. 
 Following the documentation, the @declared_attr.cacading decorator seems to 
 be the best approach.
 Here's my code:
 class HasSomeAttribute(object):
 @declared_attr.cascading
 def type(cls):
 if has_inherited_table(cls):
 if cls.__name__ == 'MySubClass1':
 return db.Column(db.Integer, default=1)
 else:
 return db.Column(db.Integer, default=2)
 else:
 return db.Column(db.Integer, default=0)
 
 class MyClass(HasSomeAttribute, db.Model):
 __tablename__ = 'people4l2'
 id = db.Column(db.Integer, primary_key=True)
 
 class MySubClass1(MyClass):
 pass
 
 class MySubClass2(MyClass):
 pass
 
 I iterated quite a few times over this but I'm systematically getting this 
 error:
 ArgumentError: Column 'type' on class class '__main__.MySubClass1' 
 conflicts with existing column 'people4l2.type’

this mapping illustrates MySubClass1 and MySubClass2 as both sharing the
same table “people4l2”, as they have no __tablename__ attribute, so there
can only be one “type” column. So in this case it is not appropriate to use
cascading in exactly this way, as MyClass already has a “type” column, and
that gets attached to the “people4l2” table and that’s it; there can be no
different “type” column on MySubClass1/MySubClass2.

If you’d like “type” to do something different based on which class is being
instantiated, this is an ORM-level differentiation. Use either the
constructor __init__() to set it or use the init() event
(http://docs.sqlalchemy.org/en/rel_0_9/orm/events.html?highlight=event%20init#sqlalchemy.orm.events.InstanceEvents.init).

OTOH if “type” is actually the “polymoprhic discriminator”, which is what
this looks like, then you’d be looking to just set up “type” as the
“polymorphic_on” column and set up the “1”, “2”, “0” as the polymorphic
identity (see
http://docs.sqlalchemy.org/en/rel_0_9/orm/inheritance.html#single-table-inheritance
for a simple example).

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] How to add custom attribute to the session?

2015-03-30 Thread Simon King
Can't you write it like this (untested)?

#
class CRUDSession(sqlalchemy.orm.Session):
def sacrud(self, cls):
return CRUD(self, cls)

Session = scoped_session(sessionmaker(class_=CRUDSession))
#

Simon

On Mon, Mar 30, 2015 at 3:15 PM, uralbash svintso...@gmail.com wrote:
 More example in tests
 https://github.com/ITCase/sacrud/blob/master/sacrud/tests/test_sessionmaker.py

 def test_create(self):
 user = self.session.sacrud(User)\
 .create({'name': 'Dzhirad', 'fullname': 'Kiri', 'password':
 123})
 self.assertEqual(user.name, 'Dzhirad')
 db_user = self.session.query(User).get(user.id)
 self.assertEqual(user.id, db_user.id)

 def test_read(self):
 self._add_item(User, 'Vasya', 'Pupkin', 123)
 user = self.session.sacrud(User).read(1)
 self.assertEqual(user.name, 'Vasya')
 self.assertEqual(user.id, 1)

 def test_update(self):
 self._add_item(User, 'Vasya', 'Pupkin', 123)
 user = self.session.sacrud(User).update(1, {'name': 'Bill'})
 db_user = self.session.query(User).get(user.id)
 self.assertEqual(user.name, 'Bill')
 self.assertEqual(user.id, db_user.id)

 def test_delete(self):
 user = self._add_item(User, 'Volod', 'Khonin', 123)
 self.session.sacrud(User).delete(user.id)
 db_user = self.session.query(User).filter_by(id=user.id).all()
 self.assertEqual(db_user, [])




 понедельник, 30 марта 2015 г., 17:34:52 UTC+5 пользователь Simon King
 написал:

 On Mon, Mar 30, 2015 at 1:09 PM, uralbash svint...@gmail.com wrote:
  Hello,
 
  I want to set up in each session was my custom attribute, for example:
 
  from sqlalchemy.orm import scoped_session, sessionmaker
 
  Session = my_wapper(scoped_session(sessionmaker()))
  session1 = Session()
  session2 = Session()
 
  hasattr(session1, foo)  # True: What I want
  hasattr(session2, foo)  # True: What I want
  print(session1.foo)
 
  What is the best way to do it? I have no idea how I can change the
  object
  from __call__ function. I also did not find that something like
  Session.set_property.
 

 You can define a subclass of the sqlalchemy Session, and ask
 sessionmaker to return instances of that class by passing the class_
 parameter to sessionmaker():


 http://docs.sqlalchemy.org/en/rel_0_9/orm/session_api.html#session-and-sessionmaker

 Would that do what you wanted?

 Simon

 --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] is True vs ==True

2015-03-30 Thread Jonathan Vanasco

If you're just worried about warnings in Flake8, you can turn them off 
line-by-line:

filter(
Rischio.c.peso_gruppo == True  # noqa
)\


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] How to add custom attribute to the session?

2015-03-30 Thread uralbash
Yes, it works. Thank you so much.

понедельник, 30 марта 2015 г., 19:58:40 UTC+5 пользователь Simon King 
написал:

 Can't you write it like this (untested)? 

 # 
 class CRUDSession(sqlalchemy.orm.Session): 
 def sacrud(self, cls): 
 return CRUD(self, cls) 

 Session = scoped_session(sessionmaker(class_=CRUDSession)) 
 # 

 Simon 

 On Mon, Mar 30, 2015 at 3:15 PM, uralbash svint...@gmail.com 
 javascript: wrote: 
  More example in tests 
  
 https://github.com/ITCase/sacrud/blob/master/sacrud/tests/test_sessionmaker.py
  
  
  def test_create(self): 
  user = self.session.sacrud(User)\ 
  .create({'name': 'Dzhirad', 'fullname': 'Kiri', 'password': 
  123}) 
  self.assertEqual(user.name, 'Dzhirad') 
  db_user = self.session.query(User).get(user.id) 
  self.assertEqual(user.id, db_user.id) 
  
  def test_read(self): 
  self._add_item(User, 'Vasya', 'Pupkin', 123) 
  user = self.session.sacrud(User).read(1) 
  self.assertEqual(user.name, 'Vasya') 
  self.assertEqual(user.id, 1) 
  
  def test_update(self): 
  self._add_item(User, 'Vasya', 'Pupkin', 123) 
  user = self.session.sacrud(User).update(1, {'name': 'Bill'}) 
  db_user = self.session.query(User).get(user.id) 
  self.assertEqual(user.name, 'Bill') 
  self.assertEqual(user.id, db_user.id) 
  
  def test_delete(self): 
  user = self._add_item(User, 'Volod', 'Khonin', 123) 
  self.session.sacrud(User).delete(user.id) 
  db_user = self.session.query(User).filter_by(id=user.id).all() 
  self.assertEqual(db_user, []) 
  
  
  
  
  понедельник, 30 марта 2015 г., 17:34:52 UTC+5 пользователь Simon King 
  написал: 
  
  On Mon, Mar 30, 2015 at 1:09 PM, uralbash svint...@gmail.com wrote: 
   Hello, 
   
   I want to set up in each session was my custom attribute, for 
 example: 
   
   from sqlalchemy.orm import scoped_session, sessionmaker 
   
   Session = my_wapper(scoped_session(sessionmaker())) 
   session1 = Session() 
   session2 = Session() 
   
   hasattr(session1, foo)  # True: What I want 
   hasattr(session2, foo)  # True: What I want 
   print(session1.foo) 
   
   What is the best way to do it? I have no idea how I can change the 
   object 
   from __call__ function. I also did not find that something like 
   Session.set_property. 
   
  
  You can define a subclass of the sqlalchemy Session, and ask 
  sessionmaker to return instances of that class by passing the class_ 
  parameter to sessionmaker(): 
  
  
  
 http://docs.sqlalchemy.org/en/rel_0_9/orm/session_api.html#session-and-sessionmaker
  
  
  Would that do what you wanted? 
  
  Simon 
  
  -- 
  You received this message because you are subscribed to the Google 
 Groups 
  sqlalchemy group. 
  To unsubscribe from this group and stop receiving emails from it, send 
 an 
  email to sqlalchemy+...@googlegroups.com javascript:. 
  To post to this group, send email to sqlal...@googlegroups.com 
 javascript:. 
  Visit this group at http://groups.google.com/group/sqlalchemy. 
  For more options, visit https://groups.google.com/d/optout. 


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] is it possible to turn off DELETE cascades globally for a session?

2015-03-30 Thread Jonathan Vanasco
I'm running a migration script, and the deletes via the ORM were creating a 
performance issue.  (trying to load 4 relationships 9million times adds up)

I couldn't find any docs for this (just some references on update cascades) 
so i sidestepped the issue by just running the Engine's delete on the table 
directly.I'm just wondering if I missed any docs.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Re: Is there any potential problem to store datetime as strings in one column?

2015-03-30 Thread Bao Niu


On Monday, March 23, 2015 at 7:53:31 AM UTC-7, Jonathan Vanasco wrote:



 On Sunday, March 22, 2015 at 7:01:35 PM UTC-4, Bao Niu wrote:

 Also because sql datetime datatype doesn't persist timezone information. 
 Therefore, I tried to store all the time information as strings.


 If your database doesn't support timezones, I think it would be easiest to 
 convert everything to UTC as a Datetime and then add a second INT column 
 with the time zone offset.You can create a property method on 
 SqlAlchemy models to create a new 'timestamp_display' out of both columns. 
  (you might even be able to write a custom column type)

 That approach will let you do all the in-database sorting you need, and 
 then use the offset to customize the time for display.  If you store as 
 strings, you'll lose all the benefits of the database's datetime 
 sorting/searching/miscellaneous functions and have to write custom 
 compatibility in SQL/SqlAlchemy.  that is a lot more work than splitting 
 the data into 2 columns.




Hi Jonathan,

To make sure that I understand it right, did you mean hybrid attribute 
methods when you mentioned property methods here? Thanks

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Re: Is there any potential problem to store datetime as strings in one column?

2015-03-30 Thread Jonathan Vanasco


On Monday, March 30, 2015 at 9:06:48 PM UTC-4, Bao Niu wrote:

 To make sure that I understand it right, did you mean hybrid attribute 
 methods when you mentioned property methods here? Thanks


No, just simple property methods.   A super-simple first Minimum Viable 
Product/iteration might be:

class Foo(object):
  utc_timestamp = Datetime() 
  utc_timezone = Integer()

  def as_local_time(self):
 call this to generate a timezone aware object from the 
database time
 return some_function(self.utc_timestamp, utc_timezone)

Over time, you could add in getter/setters or look into other ways of 
handling this:

or You could integrate SqlAlchemy's Hybird Attribute Methods
or you could use composite columns 
(http://docs.sqlalchemy.org/en/latest/orm/composites.html)
i think you might be able to use a TypeDecorator too 
( http://docs.sqlalchemy.org/en/latest/core/custom_types.html)

A few of the sqlalchemy approaches can even let you generate the SQL the db 
would use on the backend for comparisons.  Until you get to that point 
though, it's pretty simple to have a v1 that is very flexible.

The general point is that there are a lot of ways to handle your situation, 
and keep actual time objects in the database.  


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Custom Type - two classes (object + TypeDecorator) or one (TypeDecorator)?

2015-03-30 Thread Jeffrey McLarty
Hello,

First - amazing work on SQLAlchemy, what an amazing body of work.

Second, my question.

I'm attempting to convert a pure-python object, into a custom type for 
column definition. My question is, do I need to keep my original 
MyObj(object) and use it inside the process_bind_param/process_result_value 
of MyObjType(TypeDecorator), or should I be able to get this to work with 
just one class?

More:

I'm confused about the *arg and **kw passed to typeobj, in the 
to_instance() function, from type_api.py...when those might conflict with 
my object constructor's parameters.  My pure-python object can be entirely 
represented by a single Integer.  Before I started converting my object 
to a TypeDecorator, my object already had a constructor, which used the 
first non-optional argument to instantiate it's value.  I inherited from 
TypeDecorator, added a impl = Integer before my constructor, and self.impl 
= Integer() inside the constructor, plus some one-line 
process_bind_param(), process_result_value() and copy() functions.

...but I can't get it to work.

I can get it to build a table object, with my MyObj and MyObjType, but when 
I try to assign a new instance of my object to the table's column 
attribute, it doesn't want to commit.

...if anybody wants to have a look at my code, it is on github, organized 
into a PR, https://github.com/Equitable/trump/pull/15 with links to the 
files and line numbers.

Thanks for reading, and any tips.

BTW - this is my first project using an ORM!  Still getting up the curve.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] is it possible to turn off DELETE cascades globally for a session?

2015-03-30 Thread Michael Bayer
it’s not an option available at the moment, though there might be ways to avoid 
the collection loads (artificially zeroing them out w/o history).

if you don’t need the relationship features then you might as well just use 
query.delete().



Jonathan Vanasco jonat...@findmeon.com wrote:

 I'm running a migration script, and the deletes via the ORM were creating a 
 performance issue.  (trying to load 4 relationships 9million times adds up)
 
 I couldn't find any docs for this (just some references on update cascades) 
 so i sidestepped the issue by just running the Engine's delete on the table 
 directly.I'm just wondering if I missed any docs.
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.