[Zope3-Users] mapSchemasToDict

ksmith93940-dev at yahoo.com ksmith93940-dev at yahoo.com
Fri Oct 13 00:10:20 EDT 2006


Perhaps this already exists somewhere, but I couldn't find it. Feed this function an object and it returns a dictionary of the associated schema key/value pairs including those that are PersistentList's of objects. Handy for view classes. Pointers and improvements welcome.
 
Kevin Smith

def mapSchemasToDict( context ):
    """ Map schemas to dict

    Provide data for demonstration::

        >>> from zope import interface, schema
        >>> from persistent import Persistent
        >>> from persistent.list import PersistentList
        >>> class IPerson( interface.Interface ):
        ...     first = schema.TextLine( title=u'First name' )
        ...     last = schema.TextLine( title=u'Last name' )
        ...
        >>> class Person:
        ...     interface.implements(IPerson)
        ...     def __init__( self, first, last ):
        ...         self.first = first
        ...         self.last = last
        ...
        >>> p1 = Person( "Jim", "Fulton" )
        >>> p1.first
        'Jim'

    create addressbook content::

        >>> class IAddressBook( interface.Interface ):
        ...     title = schema.TextLine( title=u'Title' )
        ...     persons = schema.Object( IPerson, title=u'Persons')
        ...
        >>> class AddressBook(Persistent):
        ...     interface.implements(IAddressBook)
        ...     def __init__(self, title=''):
        ...          self.title = title
        ...     title = ''
        ...     persons = PersistentList()
        ...
        >>> a1 = AddressBook( title="My Address Book" )
        >>> a1.title
        'My Address Book'
        >>> a1.persons.append( p1 )
        >>> a1.persons.append( p1 )
        >>> a1.persons
        [<...Person instance at ...>, <...Person instance at ...>]
        >>> d = mapSchemasToDict( a1 )
        >>> d
        {'persons': [{'last': 'Fulton', 'first': 'Jim'}, {'last': 'Fulton', 'first':
        'Jim'}], 'title': 'My Address Book'}
    """

    listofinterfaces = list( interface.providedBy( context ) )
    results = {}
    for oneinterface in listofinterfaces:
        fields = schema.getFieldNames( oneinterface )
        if fields:
            for field in fields:
                fieldvalue = getattr( oneinterface( context ), field )
                if isinstance( fieldvalue, PersistentList ):
                    subfieldvalue = []
                    for subfield in fieldvalue:
                        subfieldvalue.append( mapSchemasToDict( subfield ) )
                    fieldvalue = subfieldvalue
                results[field] = fieldvalue
    return results


USAGE:

class MyView( object ):
    ...
    @property
    def fields( self ):
        return mapSchemasToDict( self.context )


template.pt
    ...
    <span tal:replace="view/fields" />

      ...or as in the doctest example...

    <span tal:replace="view/fields/title" /> .... 
   
       returns...

            My Address Book





More information about the Zope3-users mailing list