Take me advice, please

164 views
Skip to first unread message

Константин Комков

unread,
Dec 29, 2019, 2:34:49 PM12/29/19
to web2py-users
Hello!

How can I transfer my variable type of list, which contain my classes to another function in controller. I tried vars and args in URL function but my classes now is str type.

Thank you!

Eric

unread,
Dec 30, 2019, 6:38:51 AM12/30/19
to web2py-users
Have you tried with a session variable?

Massimo Di Pierro

unread,
Dec 30, 2019, 6:41:22 AM12/30/19
to web2py-users
Transfer from where to where? things like objects may have a complex state and cannot be reliably serialized. They can be rebuilt.

What is your use case?

Massimo

Константин Комков

unread,
Dec 30, 2019, 7:28:06 AM12/30/19
to web...@googlegroups.com
I can give more details.
I have nine files with timetable for each faculty in excel. I saved all of them in csv and my task was create parser for read each class (university subject) to database for each group. Parser was written now, and all classes in my variable type of list.
Class structure:
class DoubleClass:
   
def __init__(self, tt_id, dc_number, dc_day, dc_type, subject_id, teacher_id, building_id, class_room_id,
                 subgroup
,  dc_week, dc_queue):
       
self.tt_id = tt_id
       
self.dc_number = dc_number
       
self.dc_day = dc_day
       
self.dc_type = dc_type
       
self.subject_id = subject_id
       
self.teacher_id = teacher_id
       
self.building_id = building_id
       
self.class_room_id = class_room_id
       
self.subgroup = subgroup
       
self.dc_week = dc_week
       
self.dc_queue = dc_queue
default.py
def importTimetable():
#form code
    #parser code
    if form.process().accepted:
        double_class_list = parseExcelCsv(filesLinesList, int(form.vars.HALF_YEAR), int(form.vars.FAC))
        #Example of double_class_list = [DoubleClass(1, 1, 1, 0, ИНОСТР.ЯЗЫК, Англичанин А.Я., 12, 203, 0, 'odd', 1), DoubleClass(1, 2, 1, 0, 1, Програмирование, Комков К.А., 15, 106, 'both', 1), ... ]
        redirect(URL('Timetable', 'default', 'findSubjectsId', vars=dict(double_class_list = double_class_list)))

def findSubjectsId():
#Here I want to get variable
    double_class_list = request.vars.double_class_list

Function findSubjectsId will create forms for each group in parsed file. Max count of groups in file 60.
In that forms user must choose name of subject from database for unique subjects from parsed file with timetable becouse subjects name can be different. For example in database name is 'Иностранный язык', in timetable name is 'ИНОСТР.ЯЗЫК'.

Eric,
I thought about sessions but user will not be in time for send all forms.
1.png

Константин Комков

unread,
Jan 4, 2020, 9:28:56 AM1/4/20
to web...@googlegroups.com
Eric, I tried use sessions but have error, becouse as I understood session can't store user class objects. "Don't store user-defined classes in session" - in book. Then I serialised object to json like that (in parseExcelCsv function): json.dumps(temp_double_class.__dict__) and in my function use that code:
def importTimetable():
    #form code
    #parser code
   
if form.process().accepted:
        filesLinesList = [line.strip().decode() for line in form.vars.ttFile.file]
        double_class_list = parseExcelCsv(filesLinesList, int(form.vars.HALF_YEAR), int(form.vars.FAC))
        session.double_class_list = double_class_list
        redirect
(URL('Timetable', 'default', 'findSubjectsId'))
    return dict(form=form, test=test)

def
findSubjectsId():
   
import json
   
from collections import namedtuple
    double_class_list
= list()
   
for x in session.double_class_list:
        double_class_list
.append(json.loads(x, object_hook=lambda d: namedtuple('DoubleClass', d.keys())(*d.values())))

    test
= ""
    for x in double_class_list:
        test
+= f"tt_id - {x.tt_id}, dc_number - {x.dc_number}, dc_day - {x.dc_day}, dc_type - {x.dc_type}, " \
               
f"subject_id - {x.subject_id}, teacher_id - {x.teacher_id}, building_id - {x.building_id}, " \
               
f"class_room_id - {x.class_room_id}, subgroup - {x.subgroup}, dc_week - {x.dc_week}, " \
               
f"dc_queue - {x.dc_queue};<br>"
    test = XML(test)
   
return dict(test = test)
If somebody know more useful case share it, please.

Val K

unread,
Jan 5, 2020, 4:19:11 AM1/5/20
to web2py-users
You want to keep python object in memory between requests? - it is bad idea. You should transform it to something that can be saved on disk or in db and recreate it in each request

Константин Комков

unread,
Jan 5, 2020, 10:50:11 AM1/5/20
to web...@googlegroups.com
Val K, yes I want transfer class objects in list like variable at first. Then I tried save that variable in session, but it was unsuccess too. And now I serialised all objects in list like that json.dumps(my_object.__dict__) append all of them in list and save in session. 

вс, 5 янв. 2020 г., 12:19 Val K <valq...@gmail.com>:
You want to keep python object in memory  between requests? - it is bad idea. You should transform it to something that can be saved on disk or in db and recreate it in each request

--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/UrR2cxoFpDo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/d1bffc13-b8cc-4c19-8e41-87401c105944%40googlegroups.com.

Val K

unread,
Jan 5, 2020, 11:23:30 AM1/5/20
to web2py-users
Finally, the session is saved on disk or in db or in cookies between requests, web2py uses pickle to do that, so all that you store in the session must be pickable. In your case it is enough to trasform your class object into dict, you don't need to convert it into json

Константин Комков

unread,
Jan 5, 2020, 12:12:34 PM1/5/20
to web2py-users
Val K, thank you now I use list wich contain dicts without json and all work.
Reply all
Reply to author
Forward
0 new messages