[sqlalchemy] can a SqlAlchemy session be extracted from an object ?

2013-05-02 Thread Jonathan Vanasco
i'm trying to deal with some old code , and need to 'log' a change.  in the 
current code block, I do not have a SqlAlchemy session object - i merely 
have an ORM object that exists in a given session.

is it possible to extract the session object from that ORM object, so I can 
just add a new ORM object to it?  doing the correct thing, and getting the 
session object into the code block properly will take more time than I have 
right now.

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sqlalchemy] can a SqlAlchemy session be extracted from an object ?

2013-05-02 Thread Gunnlaugur Thor Briem
Yep: object_session(obj)

http://docs.sqlalchemy.org/en/rel_0_8/orm/session.html#sqlalchemy.orm.session.Session.object_session

Regards,

Gulli



On Thu, May 2, 2013 at 4:47 PM, Jonathan Vanasco jonat...@findmeon.comwrote:

 i'm trying to deal with some old code , and need to 'log' a change.  in
 the current code block, I do not have a SqlAlchemy session object - i
 merely have an ORM object that exists in a given session.

 is it possible to extract the session object from that ORM object, so I
 can just add a new ORM object to it?  doing the correct thing, and getting
 the session object into the code block properly will take more time than I
 have right now.

 --
 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?hl=en.
 For more options, visit https://groups.google.com/groups/opt_out.




-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sqlalchemy] can a SqlAlchemy session be extracted from an object ?

2013-05-02 Thread Michael Bayer
and in 0.8 its this:

from sqlalchemy import inspect
session = inspect(obj).session



On May 2, 2013, at 12:50 PM, Gunnlaugur Thor Briem gunnlau...@gmail.com wrote:

 Yep: object_session(obj)
 
 http://docs.sqlalchemy.org/en/rel_0_8/orm/session.html#sqlalchemy.orm.session.Session.object_session
 
 Regards,
 
 Gulli
 
 
 
 On Thu, May 2, 2013 at 4:47 PM, Jonathan Vanasco jonat...@findmeon.com 
 wrote:
 i'm trying to deal with some old code , and need to 'log' a change.  in the 
 current code block, I do not have a SqlAlchemy session object - i merely have 
 an ORM object that exists in a given session.
 
 is it possible to extract the session object from that ORM object, so I can 
 just add a new ORM object to it?  doing the correct thing, and getting the 
 session object into the code block properly will take more time than I have 
 right now.
 
 -- 
 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?hl=en.
 For more options, visit https://groups.google.com/groups/opt_out.
  
  
 
 
 -- 
 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?hl=en.
 For more options, visit https://groups.google.com/groups/opt_out.
  
  

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sqlalchemy] can a SqlAlchemy session be extracted from an object ?

2013-05-02 Thread Gunnlaugur Thor Briem
On Thu, May 2, 2013 at 5:08 PM, Michael Bayer mike...@zzzcomputing.comwrote:

 and in 0.8 its this:

 from sqlalchemy import inspect
 session = inspect(obj).session


Is this preferred over object_session in 0.8? object_session is still
there, and docs don't say it's deprecated or that inspect(obj).session is
preferred.

Gulli

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sqlalchemy] can a SqlAlchemy session be extracted from an object ?

2013-05-02 Thread Michael Bayer
ultimately I'm hoping the inspect() system is just easier for everyone, but I 
haven't deprecated the old functions.   I doubt I'd ever remove them at this 
point, and they are still used internally since there's a tad less overhead to 
them.




On May 2, 2013, at 1:31 PM, Gunnlaugur Thor Briem gunnlau...@gmail.com wrote:

 On Thu, May 2, 2013 at 5:08 PM, Michael Bayer mike...@zzzcomputing.com 
 wrote:
 and in 0.8 its this:
 
 from sqlalchemy import inspect
 session = inspect(obj).session
 
 Is this preferred over object_session in 0.8? object_session is still there, 
 and docs don't say it's deprecated or that inspect(obj).session is preferred.
 
 Gulli
 
 
 -- 
 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?hl=en.
 For more options, visit https://groups.google.com/groups/opt_out.
  
  

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sqlalchemy] Are sqlalchemy queries a generator?

2013-05-02 Thread Claudio Freire
On Fri, Apr 26, 2013 at 8:59 PM, Michael Bayer mike...@zzzcomputing.com
wrote:
 All attributes have to be expire-able and act as proxies for a
database connection so I'm not really sure where to go with that.I'm
not too thrilled about proposals to build in various alternate
performance behaviors as the library starts to try to act in many
different ways that the vast majority of users aren't even aware of, it
increases complexity internally, produces vast amounts of new use cases to
test and maintain, etc.I'm always willing to look at patches that are
all winning, of course, so if you have some way to speed things up without
breaking usage contracts and without major new complexity/brittleness I'd
love to look at a pull request.

 I know, it's just a probe to see what kind of a speedup could be
 obtained by not having that getter's interference. You know... simply
 implementing InstrumentedAttribute in C could do the trick...


 In fact... I'm gonna try that...

 feel free!  though you might be surprised, a C function that just calls
out to all the same Python operations anyway is often only negligibly
faster, not enough to make the extra complexity worth it.


Ok, I got around to profiling this. The C extension saves 20s from 800s, as
noted before, most of those 800s are SA-unrelated application logic, and
the app has been greatly optimized to avoid attribute access, so average
speedup would most likely be far more.

As expected, the getter disappears from profiles (it's seen as the calling
function's time now). I've got a function that makes some unavoidable
access to instrumented attributes. It's get_params, it's called around 7M
times, so while small, the overhead does add up. I made sure expiration
works btw.

Without the C extension:
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
 20811734   27.8290.000   27.8550.000 attributes.py:171(__get__)
  7631984   13.5320.000   31.8510.000 ruby.py:86(get_param)

With the C extension:
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  7631984   19.5140.000   21.0510.000 ruby.py:86(get_param)

Notice how the C extension saves a total of 10s (cumtime, sum of internal
and external time).

There's no DB access when hitting those arguments, as everything has been
eagerly loaded. It's all function call overhead.

Assuming an application makes heavy use of attributes, as get_param does
(expectable of straightforward code I'd think), that's a 30% speedup of
CPU-bound code.

As soon as I get GC right I'll post the patch.

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sqlalchemy] Tricky situation

2013-05-02 Thread Richard Gerd Kuesters

Thank you Mariano.

I tested your solution, but it didn't work as well as your previous one 
- since I use a lot of self referential, polymorphic entities; they may 
not like union_all :-(


But it does provides a simplier approach if you're not using it the way 
I do :)



Thanks again!

Best regards,
Richard.


On 04/29/2013 02:57 PM, Mariano Mara wrote:


On 04/25/2013 11:17 AM, Richard Gerd Kuesters wrote:
Well. I'm pretty interested :) I did find your solution very 
flexible, thou.


Thanks a lot,
Richard.




For completeness, here is a pure sqlalchemy version of the same 
recursive ideas:




def _hierarchy():
# TODO: check if object has self referential key
sq1 = S.query(self.model)
sq1 = sq1.add_columns(*(label(level, literal(1, 
type_=Integer)),
label(cycle, literal(False, 
type_=Boolean)),

label(connect_path,
literal_column(ARRAY[id], 
ARRAY(Integer)


sq1 = sq1.filter(self.model.parent_id==None)
sq1 = sq1.cte(name=sq1, recursive=True)
sq1_alias = aliased(sq1, name=pr)
sq2_alias = aliased(self.model, name=p)
sq1 = sq1.union_all(
S.query(sq2_alias,
label(level, sq1_alias.c.level+1),
label(cycle, Any(sq2_alias.id, 
sq1_alias.c.connect_path)),

label(connect_path,
func.array_append(sq1_alias.c.connect_path,
  sq2_alias.id))
).filter(and_(sq2_alias.parent_id==sq1_alias.c.id,
  ~sq1_alias.c.cycle)))
q = S.query(sq1)
q = q.add_columns(*(label(is_leaf,
case([(
sq1.c.connect_path.contained_by(
over(func.lead(
sq1.c.connect_path, 
1),order_by=sq1.c.connect_path))==True,

False)], else_=True)),))
return q.all()



It could use some testing (something I am about to do) and you still 
need to cast to your object but all the ingredients are here. As usual 
the model needs a self referential key (in this case id and 
parent_id). I try not to use the textual ARRAY[id] to generate the 
first connect_path but I couldn't make it without errors (and the time 
was pressing):   If you have a suggestion on how to do so, I will 
surely appreciate it.


Mariano


On 04/25/2013 11:08 AM, Mariano Mara wrote:

On 04/25/2013 10:22 AM, Richard Gerd Kuesters wrote:

Hi all,

I've been playing with sqla_hierarchy from
https://github.com/marplatense/sqla_hierarchy .



That code of that sqla_hierarchy was written to provide a limited
support for cte, from the time when sqalchemy didn't have cte.
Since sqlalchemy has cte now I would recommend to migrate to pure
sqlalchemy code which will open more possibilities for you to interact
with your objects and provide support in the long term.

I am currently trying to do so (and once I solve how to get *level*,
*is_leaf* and *connect_path* attributes as part of the results, I can
surely share it through the list for anyone who's interested).

Mariano





--
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




[sqlalchemy] Re: can a SqlAlchemy session be extracted from an object ?

2013-05-02 Thread Jonathan Vanasco
this is WONDERFUL.  thanks!

On Thursday, May 2, 2013 12:47:25 PM UTC-4, Jonathan Vanasco wrote:

 i'm trying to deal with some old code , and need to 'log' a change.  in 
 the current code block, I do not have a SqlAlchemy session object - i 
 merely have an ORM object that exists in a given session.

 is it possible to extract the session object from that ORM object, so I 
 can just add a new ORM object to it?  doing the correct thing, and getting 
 the session object into the code block properly will take more time than I 
 have right now.


-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




[sqlalchemy] Using UniqueConstraint or unique=True

2013-05-02 Thread Paradox

I am trying to ensure that my table doesn't allow duplicate rows.

The table is defined (in SqlAlchemy 0.8):

class User(Base):
 __tablename__ = 'user'

 id = Column(Integer, primary_key=True)
 lname = Column(String)
 fname = Column(String)
 email = Column(String)
 dateentered = Column(DateTime, default=datetime.now())

 UniqueConstraint('lname','fname',name='full_name')

 ~ etc. 

I have tried with using the above and with adding the unique constraint
to the relevant field definitions.  Using the unique constraint on
individual fields makes a table that requires both the lname and the
fname field to be unique. Using the code above there seems to be no
constraints whatsoever, a session.commit() simply adds the duplicate
rows.  What I am going for is something like this (using an sqlite3
database):

CREATE TABLE user (lname string, fname string, email string,
unique(lname, fname) ON CONFLICT REPLACE);

This will allow me to add multiple rows with the same lname as long as
the fnames are different for each.  Is there a way to define such unique
constraints in SqlAlchemy?

thomas

--
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.