in python dict keys are not sorted. sorted(dict(...)) only gives you a list 
of sorted keys, not a dict with sorted keys. What you want is replace dict 
with collections.OrderedDict

On Saturday, 18 June 2016 07:47:42 UTC-5, DNeuman wrote:
>
> Hi - first time posting here! First let me say thanks to Professor Di 
> Pierro and all the people who have done a great job on Web2Py. I am new to 
> web development, and Python, so I really appreciate all the work you did to 
> make this subject approachable. Now for my question:
>
> I am having some difficulty passing a list of tuples from a controller 
> using  'return locals()'  sometimes it hangs the web2py server.
>
> I have spent a week narrowing this down, and here is what I have; some 
> works, some does not...
>
> If I create a simple list named bob = [(2L, 0.2075327747768832), (16L, 
> 0.19108106970048253), (3L, 0.13288897732583363), (15L, 
> 0.11511204575971237), (12L, 0.10369170755981243), (7L, 0.1033849215142939), 
> (8L, 0.0957849868092478), (5L, 0.05052350890092832)]
>
> then return locals()  I can see this in response._vars  in the debugger 
> and {{=BEAUTIFY(response._vars)}} works fine.          (thats good)
>
>
> However, if I create the same exact list as an output of a sorting 
> operation from a dictionary, it hangs the web2py server when I use return 
> locals() and {{=BEAUTIFY(response._vars)}}       (but only sometimes)
>
>
> Here is what the code is used to generate the sorted list:
>
> def make_list():
>     from operator import itemgetter
>     #dbg.set_trace() # stop here!
>
>     test_dict ={}
>
>     test_dict[2L] = 0.773
>     test_dict[5L] = 0.003
>     test_dict[1L] = 0.31
>     test_dict[9L] = 0.0402
>     test_dict[12L] = 0.12
>     test_dict[7L] = 0.214
>
>     test_list = sorted(test_dict.iteritems(), key=itemgetter(1), 
> reverse=True)
>
>     return test_list             #this should be a list of tuples 
> [(x,y),(w,z)...()]
>
>
> But here is the weird part - if I have this test case standalone, it 
> always works. If I use sorted() in a larger program, it exhibits the hang. 
> Very consistent. standalone test = no problem. larger problem = broken.
>
> More about the server hang:
>
> passing bob through locals() causes a hang, whether BEAUTIFY(bob) or 
> BEAUTIFY(response._vars)    as long as bob is generated from the sorted() 
> function. Never hangs when generated from simply creating a list of tuples.
>
>
> passing bob through locals() doesn’t hang unless BEAUTIFY(bob).  So, if I 
> don't use bob in the view, it doesn't hang. There is something about 
> passing this through locals, AND using BEAUTIFY AND creating the list from 
> sorted() AND being in a larger program.
>
>
> making bob from sorted() in a large program = hang:
>
> use return locals() and BEAUTIFY(response._vars)      -- hang
>
> use return locals() and BEAUTIFY(bob)                        -- hang
>
>
> works:
>
> return locals() and BEAUTIFY(qlist)                                       
>          -- no hang; qlist is some other variable
>
> return locals() and use view {{=bob[0]   =bob[1]   =bob[2] }}           -- 
> no hang; here I simply don't use BEAUTIFY and it all works
>
> bob created without using sorted()                                         
>          -- no hang; can do whatever I like and it works.
>
>
>
> So I can only guess that I am using sorted() wrong, or using BEAUTIFY 
> wrong, or there is some very mysterious aspect of the list that is not 
> visible in the debugger which is confusing BEAUTIFY.
>
>
> when I examine response._vars in cases where there is a hang it is 
> identical to cases where there is no hang.
>
>
> details:
>
> Mac OS X El Capitan  10.11.5
>
> Web2py 2.14.2-stable+timestamp.2016.03.24.22.47.49
>
> modules installed: numpy
>
> browser: Safari
>
>
> Much appreciate any pointers here! Thanks.
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to