I've been trying to DRY some code and found that the following pattern
keeps popping up:

class SomeObject(object):

        def __init__(self, many_keyword_arguments):
                self.y = y
                self.x = x
                self.z = z

From the outside this then is called from a view (where data comes from
a formlib AddForm:

        def create(self, **data)
                return SomeObject(**data)

I'd like to remove the explicit assignments from the constructor and use
a function to do this in a generic way. I didn't find anything that does
this yet, except from a formlib function that requires FormFields
instead of a schema, so I wrote my own:

def set_schema_data(context, schema, data):
    """Update the context's attributes with the given data that belongs
       to the specified schema.
    for name in schema:
        field = schema[name]
        if not zope.schema.interfaces.IField.providedBy(field):
        if name in data:
            field.set(context, data[name])

There is a function around already which comes from zope.formlib and
requires the schema to be specified as a FormFields setup. I'd like to
not have the formlib dependency in this code.

I propose to include the given function in zope.schema or
zope.interface. Eventually this could also be spelled as a method on a
schema (symmetrical to field.set): schema.set(object, data)

I hope I didn't miss any existing functionality. (I somewhat guess I
didn't as formlib implemented this on it's own too.)


gocept gmbh & co. kg - forsterstra├če 29 - 06112 halle/saale - germany
www.gocept.com - [EMAIL PROTECTED] - phone +49 345 122 9889 7 -
fax +49 345 122 9889 1 - zope and plone consulting and development

Attachment: signature.asc
Description: OpenPGP digital signature

Zope3-dev mailing list
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com

Reply via email to