[merge] Registry

Richard Wilbur richard.wilbur at gmail.com
Tue Oct 10 09:51:29 BST 2006


On Wed, 2006-09-27 at 17:47 -0500, John Arbash Meinel wrote:
> Robert Collins wrote:
> > On Thu, 2006-09-21 at 16:28 -0500, John Arbash Meinel wrote:
> > 
> > John and I talked on IRC and we are mostly in agreement now... sometime
> > soon should see a new patch :)
> > 
> > -Rob
> 
> Attached is an updated Registry, which defers to helper classes for
> handling whether to lazy import or not.
> 
> And it lessens how much Registry tries to look like a dictionary.
> 
> John
> =:->
> 

John,

I'd say it looks pretty good.  I do have a few questions and comments:

First, my lack of context is showing:
  i.  Is this replacing some other concept of "Registry"?
  ii.  I'm wondering how "it lessens how much Registry tries to look
like a dictionary"?


=== added file 'bzrlib/registry.py'
--- bzrlib/registry.py  1970-01-01 00:00:00 +0000
+++ bzrlib/registry.py  2006-09-27 21:29:00 +0000
@@ -0,0 +1,199 @@

[...]
+class _LazyObjectGetter(_ObjectGetter):
+    """Keep a record of a future object, and on request load it."""
+
+    __slots__ = ['_module_name', '_member_name', '_imported']
+
+    def __init__(self, module_name, member_name):
+        self._module_name = module_name
+        self._member_name = member_name
+        self._imported = False
+        super(_LazyObjectGetter, self).__init__(None)
+
+    def get_obj(self):
+        """Get the referenced object.
+
+        If not imported yet, this will import the requested module.
+        Further requests will just return the already imported object.
+        """
+        if not self._imported:
+            self._do_import()
+        return super(_LazyObjectGetter, self).get_obj()
+
+    def _do_import(self):
+        obj = __import__(self._module_name, globals(), locals(),
+                         [self._member_name])
+        if member_name:
+            obj = getattr(obj, member_name)
+        self._obj = obj
+        self._imported = True

^-- Did you intend 'member_name' as a keyword argument as in
'_do_import(self, member_name=None):'?  Otherwise it is out of scope as
far as I can tell.  The only other reference to that name is the final
parameter of __init__() which makes it out of scope here.

It looks like you were thinking of being able to satisfy a lazy import
of the form 'from <module_name> import <member_name>', although it
doesn't seem to be supported by the rest of the framework, yet.

[...]

=== modified file 'NEWS'
--- NEWS        2006-09-27 20:44:31 +0000
+++ NEWS        2006-09-27 22:47:02 +0000
@@ -3,6 +3,12 @@
   IMPROVEMENTS:
     * ``bzr help commands`` output is now shorter (Aaron Bentley)
 
+    * New Registry class to provide name-to-object registry-like
support,
+      for example for schemes where plugins can register new classes to
+      do certain tasks (e.g. log formatters). Also provides lazy
registration
+      to allow modules to be oladed on request. (John Arbash Meinel,
Adeodato
+      Simó)
+
   INTERNALS:

^--Spelling: 'oladed' --> 'loaded'

[...]

My guess is that this code is intended to help with scalability issues.
Speeds start up in the presence of a large number of plug-ins by
registering their existence and only loading/importing the actual code
when deemed necessary.  Preserves access to the help text for all
modules on more immediate recall.

In light of this, I appreciate your usage of __slots__ to minimize
overhead.  I also like the use of callbacks for validation of
default_key assignment.

Do we have any benchmarks that could show the benefits of strict versus
lazy import?  They would most likely make a strong case for using lazy
imports--especially as such things as plug-ins multiply.

Richard

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 191 bytes
Desc: This is a digitally signed message part
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20061010/68d1b727/attachment.pgp 


More information about the bazaar mailing list