Trouble populating a dictionary of dictionaries
Hi All, I'm trying to implement a computer booking system for staff members here. At the moment I have a table with date, timeslot (eg 8:00 - 9:00), resource (eg. PC 1) and person. I want to draw a table showing the bookings for the day looking something like this: |Time| PC 1 | PC 2 | PC 3 | +----+------+------+------+ |8:00| AH | | | +----+------+------+------+ |9:00| | BC | MD | +----+------+------+------+ etc... I thought the best way to do it would be use the table to populate a dictionary of dictionaries like: booking['8:00']['PC 1'] = 'AH' Then when I'm rendering the table using ZPT I can use a nested loop to look through the timeslots and resources and fill table cells with the booking[slot][resource] value if it exists. After playing around for too long on it, I can't seem to get it to work. I even tried pre-filling the d-o-d with empty strings. I just don't understand what I am doing wrong. Thanks, Anton Code: ===== #parameters: booking_date # Import a standard function, and get the HTML request and response objects. from Products.PythonScripts.standard import html_quote request = container.REQUEST RESPONSE = request.RESPONSE bookings = {} # declare the bookings dictionary resource_hash = {} #declare the resource dictionary #populate bookings dictionary with timeslot keys for row in context.resources(): resource = row['name'] resource_hash[resource] = '' #populate bookings dictionary with timeslot keys for row in context.timeslots(): slot = row['slot_name'] bookings[slot] = resource_hash #fill the bookings dictionary of arrays with actual bookings for row in context.getBookings(on_date=booking_date): slot = row['timeslot'] resource = row['resource'] - 1 person = ['person'] bookings[slot][resource] = person return bookings Error: ====== Error Type: TypeError Error Value: unsupported operand type(s) for - Traceback (innermost last): File D:\ongaku\lib\python\ZPublisher\Publish.py, line 150, in publish_module File D:\ongaku\lib\python\ZPublisher\Publish.py, line 114, in publish File D:\ongaku\lib\python\Zope\__init__.py, line 159, in zpublisher_exception_hook (Object: bookings) File D:\ongaku\lib\python\ZPublisher\Publish.py, line 98, in publish File D:\ongaku\lib\python\ZPublisher\mapply.py, line 88, in mapply (Object: render_bookings) File D:\ongaku\lib\python\ZPublisher\Publish.py, line 39, in call_object (Object: render_bookings) File D:\ongaku\lib\python\Shared\DC\Scripts\Bindings.py, line 252, in __call__ (Object: render_bookings) File D:\ongaku\lib\python\Shared\DC\Scripts\Bindings.py, line 283, in _bindAndExec (Object: render_bookings) File D:\ongaku\lib\python\Products\PythonScripts\PythonScript.py, line 302, in _exec (Object: render_bookings) (Info: ({'script': <PythonScript instance at 01707050>, 'context': <PortalFolder instance at 01CFFEE0>, 'container': <PortalFolder instance at 01CFFEE0>, 'traverse_subpath': []}, ('9/9/02',), {}, None)) File Script (Python), line 23, in render_bookings TypeError: (see above) Anton Hughes Data Administrator Childhood Determinants of Adult Health Project Menzies Centre for Population Health Research GPO Box 252-23, Hobart Tasmania 7001 Email: anton.hughes@utas.edu.au Web: http://www.menzies.utas.edu.au/cohort/CDAH.htm Phone: +61 (0) 3 6226 7761 =+=+=+===+++=====+++++=========+++++++++ Sattinger's Law: It works better if you plug it in.
After playing around for too long on it, I can't seem to get it to work. I even tried pre-filling the d-o-d with empty strings. I just don't understand what I am doing wrong.
Its really rather hard to say if you dont show us some code ;) -- Andy McKay Agmweb Consulting http://www.agmweb.ca
Hmmm..... I posted code on the end of that message. I don't know what happened to it. Anyway, I'll post the code here: #parameters: booking_date # Import a standard function, and get the HTML request and response objects. from Products.PythonScripts.standard import html_quote request = container.REQUEST RESPONSE = request.RESPONSE bookings = {} # declare the bookings dictionary resource_hash = {} #declare the resource dictionary #populate bookings dictionary with timeslot keys for row in context.resources(): resource = row['name'] resource_hash[resource] = '' #populate bookings dictionary with timeslot keys for row in context.timeslots(): slot = row['slot_name'] bookings[slot] = resource_hash #fill the bookings dictionary of arrays with actual bookings for row in context.getBookings(on_date=booking_date): slot = row['timeslot'] resource = row['resource'] - 1 person = ['person'] bookings[slot][resource] = person return bookings Error: ====== Error Type: TypeError Error Value: unsupported operand type(s) for - Traceback (innermost last): File D:\ongaku\lib\python\ZPublisher\Publish.py, line 150, in publish_module File D:\ongaku\lib\python\ZPublisher\Publish.py, line 114, in publish File D:\ongaku\lib\python\Zope\__init__.py, line 159, in zpublisher_exception_hook (Object: bookings) File D:\ongaku\lib\python\ZPublisher\Publish.py, line 98, in publish File D:\ongaku\lib\python\ZPublisher\mapply.py, line 88, in mapply (Object: render_bookings) File D:\ongaku\lib\python\ZPublisher\Publish.py, line 39, in call_object (Object: render_bookings) File D:\ongaku\lib\python\Shared\DC\Scripts\Bindings.py, line 252, in __call__ (Object: render_bookings) File D:\ongaku\lib\python\Shared\DC\Scripts\Bindings.py, line 283, in _bindAndExec (Object: render_bookings) File D:\ongaku\lib\python\Products\PythonScripts\PythonScript.py, line 302, in _exec (Object: render_bookings) (Info: ({'script': <PythonScript instance at 01707050>, 'context': <PortalFolder instance at 01CFFEE0>, 'container': <PortalFolder instance at 01CFFEE0>, 'traverse_subpath': []}, ('9/9/02',), {}, None)) File Script (Python), line 23, in render_bookings TypeError: (see above) Thanks again, Anton -----Original Message----- From: Andy McKay [mailto:andy@agmweb.ca] Sent: Friday, 6 September 2002 3:12 PM To: Anton Hughes; zope-dev@zope.org Subject: Re: [Zope-dev] Trouble populating a dictionary of dictionaries
After playing around for too long on it, I can't seem to get it to work. I even tried pre-filling the d-o-d with empty strings. I just don't understand what I am doing wrong.
Its really rather hard to say if you dont show us some code ;) -- Andy McKay Agmweb Consulting http://www.agmweb.ca
Error Type: TypeError Error Value: unsupported operand type(s) for -
resource = row['resource'] - 1
You should check that row['resource'] is a data type that supports subtraction. If for example row['resource'] is a string, this will raise the error. Try: resource = int(row['resource']) - 1 -- Andy McKay Agmweb Consulting http://www.agmweb.ca
Further to my earlier question about creating a dictionary of dictionaries (which is working now - thanks very much!), I am now doing the ZPT to draw it. But I'm not sure how to make this d-o-d, which is returned by a PythonScript in the same directory, available. This is what I have so far: <table border="1" width="100%"> <tr> <th>Time</th> <th align="center" tal:repeat="resource container/resources"> <div tal:content="resource/name">Resource</div> </th> </tr> <tr tal:repeat="slot container/timeslots"> <td align="center" tal:content="slot/slot_name">#</td> <td align="center" tal:repeat="resource container/resources"> <div>***</div> </td> </tr> </table> (resources and timeslots are ZSQLMethods) *** this is where I want 'bookings[slot/slot_name][resources/name]' to return a person's name (or not). The PythonScript accepts one parameter (a date) and is called render_bookings. Can someone please get me that final 5% of the way there? Thanks Anton
Anton Hughes writes:
... *** this is where I want 'bookings[slot/slot_name][resources/name]' to Please read the ZPT documentation (--> Zope Book), especially about TALES expressions, the "python:" type and the "path" function.
Moreover, this type of questions is inadequate for "zope-dev" (the mailing list for the development of (and not with) Zope). Please post them to "zope@zope.org" or "zpt@zope.org"... Dieter
From: "Anton Hughes" <Anton.Hughes@utas.edu.au>
After playing around for too long on it, I can't seem to get it to work. I even tried pre-filling the d-o-d with empty strings. I just don't understand what I am doing wrong.
Your not thinking object oriented enough. Encapsulate, encapsulate, encapsulate. :-) Make objects of everything instead, and thank me later. :-) Then you index the objects in a ZCatalog so you can do searches. Best Regards Lennart Regebro, Torped http://www.easypublisher.com/
participants (4)
-
Andy McKay -
Anton Hughes -
Dieter Maurer -
Lennart Regebro