orderby='<random>' on Google App Engine

100 views
Skip to first unread message

Albert Abril

unread,
Dec 6, 2010, 5:09:45 PM12/6/10
to web...@googlegroups.com
Hi!

As said on the book, in a DAL select, orderby='<random>' is not supported on GAE.

Someone knows the best way to randomize a Rows dictionary?
(thinking in GAE deployment of course)

Thanks in advance.

mdipierro

unread,
Dec 6, 2010, 5:53:37 PM12/6/10
to web2py-users
import random
...
rows=db(...).select().sort(lambda row: random.random())

On Dec 6, 4:09 pm, Albert Abril <albert.ab...@gmail.com> wrote:
> Hi!
>
> As said on the book, in a DAL select, orderby='<random>' is not supported on
> GAE.http://web2py.com/book/default/chapter/06#orderby,-groupby,-limitby,-...

Albert Abril

unread,
Dec 6, 2010, 6:16:49 PM12/6/10
to web...@googlegroups.com
I didn't know i could use imports in GAE
Thank you Massimo.

mdipierro

unread,
Dec 6, 2010, 6:52:12 PM12/6/10
to web2py-users
and you can put your own modules in web2py/site-packages and they will
be deployed with your app.

On Dec 6, 5:16 pm, Albert Abril <albert.ab...@gmail.com> wrote:
> I didn't know i could use imports in GAE
> Thank you Massimo.
>

cjrh

unread,
Dec 7, 2010, 3:00:06 AM12/7/10
to web2py-users
On Dec 7, 1:16 am, Albert Abril <albert.ab...@gmail.com> wrote:
> I didn't know i could use imports in GAE
> Thank you Massimo.

Book updated to reflect Massimo's answer. I thought it best to
emphasize the technique of using imports in these situations.

blackthorne

unread,
Dec 7, 2010, 8:13:50 AM12/7/10
to web2py-users
I don't see howcome this works as random sorting.
The only random thing I see it's the row chosen for sorting which will
only change the sorting criteria for the number of rows in the table.

Am I wrong?

P.S.:
I couldn't make a quick test because:
isco-Ribeiros-MacBook-Pro-2:web2py blackthorne$ python web2py.py -S
"test"
web2py Enterprise Web Framework
Created by Massimo Di Pierro, Copyright 2007-2010
Version 1.89.5 (2010-11-21 16:03:13)
Database drivers available: SQLite3
application test does not exist, create (y/n)?yes
Traceback (most recent call last):
File "web2py.py", line 23, in <module>
gluon.widget.start(cron=True)
File "/Users/blackthorne/Code/web2py/gluon/widget.py", line 766, in
start
File "/Users/blackthorne/Code/web2py/gluon/shell.py", line 166, in
run
NameError: name 'aidr' is not defined

?

mdipierro

unread,
Dec 7, 2010, 10:09:21 AM12/7/10
to web2py-users
You have an old version. aidr is a type fixed some time ago.

blackthorne

unread,
Dec 7, 2010, 10:28:43 AM12/7/10
to web2py-users
Seems to be 1.89.5 (2010-11-21 16:03:13)..

but what about my comment of the random sorting?

mdipierro

unread,
Dec 7, 2010, 12:13:58 PM12/7/10
to web2py-users
Not sure I understand our comment:

rows=db(...).select().sort(lambda row: random.random())

sorts the rows that have been already fetched at the web2py/python
level. That is all. What is wrong with it?

Richard

unread,
Dec 7, 2010, 5:41:06 PM12/7/10
to web2py-users
I think he's after an database-side implementation. If you used limit
and random together then wouldn't you always get the same set of data,
rather than random?

mdipierro

unread,
Dec 7, 2010, 7:04:10 PM12/7/10
to web2py-users
He asked: "Someone knows the best way to randomize a Rows dictionary?"

The proposed solution randomized the Rows object that you already
fethed form the db. You are correct that it does not fetch random
rows.

Albert Abril

unread,
Dec 8, 2010, 10:37:20 AM12/8/10
to web...@googlegroups.com
Massimo is correct, 
I just wanted to know the more efficient way to randomize a Rows dictionary, instead of using orderby=<random>.
Because as said on the book, orderby=<random> is not supported on GAE.

Just easy as using python random library :)

Thanks again.

Richard

unread,
Dec 8, 2010, 7:45:56 PM12/8/10
to web...@googlegroups.com
ah OK. 
Anyway, how could this be done server-side when also using limit? I want to show some random records without querying all the data.

howesc

unread,
Dec 9, 2010, 4:01:53 PM12/9/10
to web...@googlegroups.com
as far as i know you would have to write some custom big table queries (not using web2py dal).  but in theory you could:
 - write a direct query to get all the keys of a table (and no data)
 - cache those keys
 - randomly choose a set of keys from the full list
 - write a google key query.

google is not really set up for selecting random data...

cfh

Bruno Rocha

unread,
Nov 24, 2011, 4:26:19 PM11/24/11
to web...@googlegroups.com

Isn't random sort suported in GAE ?


rows = db(query).select().sort(lambda row: random.random())
File "/base/data/home/apps/s~movucahq/0-1.354917246392574252/gluon/dal.py", line 5789, in select
return self.db._adapter.select(self.query,fields,attributes)
File "/base/data/home/apps/s~movucahq/0-1.354917246392574252/gluon/dal.py", line 3410, in select
(items, tablename, fields) = self.select_raw(query,fields,attributes)
File "/base/data/home/apps/s~movucahq/0-1.354917246392574252/gluon/dal.py", line 3367, in select_raw
filters = self.expand(query)
File "/base/data/home/apps/s~movucahq/0-1.354917246392574252/gluon/dal.py", line 3249, in expand
return expression.op(expression.first, expression.second)
File "/base/data/home/apps/s~movucahq/0-1.354917246392574252/gluon/dal.py", line 3263, in AND
a = self.expand(first)
File "/base/data/home/apps/s~movucahq/0-1.354917246392574252/gluon/dal.py", line 3249, in expand
return expression.op(expression.first, expression.second)
File "/base/data/home/apps/s~movucahq/0-1.354917246392574252/gluon/dal.py", line 3109, in OR
def OR(self,first,second): raise SyntaxError, "Not supported" 
SyntaxError: Not supported
 

--

howesc

unread,
Nov 25, 2011, 8:46:58 PM11/25/11
to web...@googlegroups.com
looks like the exception there is because your query has an OR condition in it, which is not supported on GAE.  that sort should be in-memory and should work fine.

Bruno Rocha

unread,
Nov 25, 2011, 9:01:51 PM11/25/11
to web...@googlegroups.com


On Fri, Nov 25, 2011 at 11:46 PM, howesc <how...@umich.edu> wrote:
looks like the exception there is because your query has an OR condition in it, which is not supported on GAE.  that sort should be in-memory and should work fine.

I found, the error was because of the use of contains() in query
Reply all
Reply to author
Forward
0 new messages