[Zope3-dev] Applying schema data on an object

Christian Theune ct at gocept.com
Wed Dec 13 11:19:19 EST 2006


Hi,

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):
            continue
        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.)

Christian

-- 
gocept gmbh & co. kg - forsterstraße 29 - 06112 halle/saale - germany
www.gocept.com - ct at gocept.com - phone +49 345 122 9889 7 -
fax +49 345 122 9889 1 - zope and plone consulting and development


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: OpenPGP digital signature
Url : http://mail.zope.org/pipermail/zope3-dev/attachments/20061213/ceda2132/signature.bin


More information about the Zope3-dev mailing list