[Zope-dev] make zope.component.registry.Components inherit from dict?

Chris McDonough chrism at plope.com
Tue Nov 24 01:39:04 EST 2009


Chris McDonough wrote:
> Thanks.  Yup. I would be +1 on this if the registry itself implemented 
> IDictInterface.
> 
> If that was untenable, if all the above code lived in the zope.component 
> package itself, and you had an API that manifested an IDictInterface object 
> when you asked for a "utilities" attribute, that would also be acceptable, as 
> long as you could do:
> 
> reg.utils['root_factory'] = RootFactory
> 
> Where the registry implementation would implement a propery for "utils":
> 
> class Components(object):
>      @property
>      def utils(self, name):
>          api = self.queryAdapter(IDictInterface, None)
>          if api is None:
>              api = DictInterface(self)
>          return api
> 
> In reality, this will be slow, and nobody is going to override IDictInterface, 
> so it would probably be better as:
> 
> class Components(object):
>      def __init__(self, name='', bases=()):
>          self.utils = DictInterface(self)

OK after rereading this, I think we may be massively overthinking this.  The 
above is getting kinda silly.  I can't think of a use case where being able to 
alternate between:

   reg.utils['root_factory']

and

   reg.getUtility(IAnonymousUtility, name='root_factory')

... is at all useful.  I may be wrong.  Can you think of one?

If not, it's a lot easier to document:

"The utils attribute of a registry is a dictionary that may contain arbitrary 
key/value pairs."

Than it is to document:

"The utils attribute of a registry is a DictInterface which exposes the Python 
dictionary API which does unnamed utility registrations using the 
IAnonymousUtility interface as the utility interface and the key value passed 
to the various API methods as the utility name...... <and so on>"


Could maybe we instead just do:

  class Components(object):
       def __init__(self, name='', bases=()):
           self.utils = {}


This would be faster, simpler to document, and would require exactly one line 
of code.

- C


More information about the Zope-Dev mailing list