Re: [Zope] Lists and external methods

2000-07-05 Thread Tino Wildenhain

Hi,

Oleg Broytmann wrote:
 
 On Tue, 4 Jul 2000, Pieter Claerhout wrote:
  I want the external method to return this in a list, where I can iterate over 
using the
  dtml-in tag, so that I can reference the different fields in each record with a 
name.
 
  What should the list look like so that I can accomplish this?
 
dtml-in eats a list of objects or a list of dictionaries.
I usually use the following helper-class:

class generic_datarecord:
" This class provides an overall mapping of value pairs "
def __init__(self,**data):
self.data=data
def __len__(self):
return(len(self.data.keys()))
def __getitem__(self,key):
return(self.data[key])
def __getattr__(self,key):
return self.__getitem__(key)
def keys(self):
return(self.data.keys())
def values(self):
return(self.data.values())

if you use generic_datarecord(column1=value1,coulumn2=value2) for each
row,
you can alterate over a list of such datarecord_objects with
dtml-in using dtml-var column1 and dtml-var column2

HTH

Tino Wildenhain
PS: __len__() is optional, but for easy use in all circumstances
(e.g. dtml-in over a single datarecord)

___
Zope maillist  -  [EMAIL PROTECTED]
http://lists.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://lists.zope.org/mailman/listinfo/zope-announce
 http://lists.zope.org/mailman/listinfo/zope-dev )




Re: [Zope] Lists and external methods

2000-07-04 Thread Oleg Broytmann

On Tue, 4 Jul 2000, Pieter Claerhout wrote:
 I want the external method to return this in a list, where I can iterate over using 
the
 dtml-in tag, so that I can reference the different fields in each record with a 
name.
 
 What should the list look like so that I can accomplish this?

   dtml-in eats a list of objects or a list of dictionaries.

Example 1.

class Item:
   pass

   Your external methods should return:

l = []

item1 = Item()
item1.date = "21/12/67"
item1.foo = "bar"
l.append(item1)

item2 = Item()
item2.date = "1/1/2000"
item2.foo = "baz"
l.append(item2)

   ...etc...

return l

   In the DTML use the list: dtml-in thelistdtml-var date/dtml-in thelist


Example 2 (dictionaries).

   Your external methods should return:

l = []

item1 = {}
item1["date"] = "21/12/67"
item1["foo"] = "bar"
l.append(item1)

item2 = {}
item2["date"] = "1/1/2000"
item2["foo"] = "baz"
l.append(item2)

   ...etc...

return l

   In the DTML use the list: dtml-in thelist mappingdtml-var date/dtml-in thelist
  ^^^
   dtml-in needs to be notified these are dicttionaries.

Oleg.(All opinions are mine and not of my employer)
 
Oleg Broytmann  Foundation for Effective Policies  [EMAIL PROTECTED]
   Programmers don't die, they just GOSUB without RETURN.


___
Zope maillist  -  [EMAIL PROTECTED]
http://lists.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://lists.zope.org/mailman/listinfo/zope-announce
 http://lists.zope.org/mailman/listinfo/zope-dev )