Copy Select/Query

29 views
Skip to first unread message

Sharoon Thomas

unread,
Oct 10, 2014, 9:13:12 AM10/10/14
to pytho...@googlegroups.com
Hello everyone,

Is it possible to copy a query ? 

My use case is to have a query pass on to a pagination object which creates multiple versions of 
the query for each page by changing the LIMIT and OFFSET. Without copying if I set the limit
and offset, it would update the same on the existing Select instance.

On trying to use copy.copy or copy.deepcopy here is the stack trace I get:

>>> sel2 = deepcopy(sel)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 174, in deepcopy
    y = copier(memo)
TypeError: 'Column' object is not callable

Thanks & Regards

Sharoon Thomas

Cédric Krier

unread,
Oct 10, 2014, 9:41:43 AM10/10/14
to pytho...@googlegroups.com
On 10 Oct 06:13, Sharoon Thomas wrote:
> Hello everyone,
>
> Is it possible to copy a query ?

It will be a great feature.

> My use case is to have a query pass on to a pagination object which creates
> multiple versions of
> the query for each page by changing the LIMIT and OFFSET. Without copying
> if I set the limit
> and offset, it would update the same on the existing Select instance.
>
> On trying to use copy.copy or copy.deepcopy here is the stack trace I get:
>
> >>> sel2 = deepcopy(sel)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File
> "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py",
> line 174, in deepcopy
> y = copier(memo)
> TypeError: 'Column' object is not callable

I think it is because FromItem override __getattr__ and so copy method
thinks there is a __deepcopy__ or __copy__ method.
Probably we should add those method to FromItem.

--
Cédric Krier - B2CK SPRL
Email/Jabber: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

Cédric Krier

unread,
Oct 10, 2014, 1:00:26 PM10/10/14
to pytho...@googlegroups.com
On 10 Oct 15:41, Cédric Krier wrote:
> On 10 Oct 06:13, Sharoon Thomas wrote:
> > Hello everyone,
> >
> > Is it possible to copy a query ?
>
> It will be a great feature.
>
> > My use case is to have a query pass on to a pagination object which creates
> > multiple versions of
> > the query for each page by changing the LIMIT and OFFSET. Without copying
> > if I set the limit
> > and offset, it would update the same on the existing Select instance.
> >
> > On trying to use copy.copy or copy.deepcopy here is the stack trace I get:
> >
> > >>> sel2 = deepcopy(sel)
> > Traceback (most recent call last):
> > File "<stdin>", line 1, in <module>
> > File
> > "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py",
> > line 174, in deepcopy
> > y = copier(memo)
> > TypeError: 'Column' object is not callable
>
> I think it is because FromItem override __getattr__ and so copy method
> thinks there is a __deepcopy__ or __copy__ method.
> Probably we should add those method to FromItem.

A quick test shows the even with such patch:

diff -r 6a49ad9f9e97 sql/__init__.py
--- a/sql/__init__.py Sun Aug 03 19:58:34 2014 +0200
+++ b/sql/__init__.py Fri Oct 10 18:58:00 2014 +0200
@@ -166,6 +166,8 @@
return AliasManager.get(self)

def __getattr__(self, name):
+ if name.startswith('__') and name.endswith('__'):
+ raise AttributeError
return Column(self, name)

def __add__(self, other):

copy doesn't work.
I guess __copy__ and __deepcopy__ should be implemented for each object.

Cédric Krier

unread,
Oct 10, 2014, 1:10:52 PM10/10/14
to pytho...@googlegroups.com
On 10 Oct 19:00, Cédric Krier wrote:
> I guess __copy__ and __deepcopy__ should be implemented for each object.

For the record: https://code.google.com/p/python-sql/issues/detail?id=22

Cédric Krier

unread,
Oct 11, 2014, 4:47:11 PM10/11/14
to pytho...@googlegroups.com
On 10 Oct 19:10, Cédric Krier wrote:
> On 10 Oct 19:00, Cédric Krier wrote:
> > I guess __copy__ and __deepcopy__ should be implemented for each object.
>
> For the record: https://code.google.com/p/python-sql/issues/detail?id=22

And the patch: https://codereview.appspot.com/153350043/
Reply all
Reply to author
Forward
0 new messages