[Zope3-dev] Re: ObjectHub
Steve Alexander
steve@cat-box.net
Sat, 07 Dec 2002 12:56:18 +0000
Gary Poster wrote:
> Guido wrote in comments to ObjectHub:
>
> # XXX It would be more convenient if register() returned
> # a bool indicating whether the object is already
> # registered, rather than raising an exception.
> # Then a more useful distinction between real errors
> # and this (common) condition could be made.
>
> To which my reply is: +1. I can do this, or you, Steve. I'm working in
> the vicinity now so would be happy to if you agree and noone else
> disagrees.
I was working alongside Guido when we added that comment.
Now that I think about it more carefully, we can't do exactly what is
suggested there. The 'register' method of IObjectHub is supposed to
return the hubid of the newly registered object. This is meaningful
information, and it is useful information for many clients of the
ObjectHub: you want to know the hubid of the object you just
successfully registered.
So, here are my requirements for the 'register' method:
* Postcondition: the given object's location is registered
* If the postcondition cannot be met, you get an exception
Otherwise...
* You want to know the hubid of the registration
* You want to know whether registration did any work
So, I propose that 'register' return a tuple of
(hubid, is_new_registration)
For example:
>>> print hub.register('/foo/bar/baz')
(23, True)
>>> print hub.register('/foo/bar/baz')
(23, False)
>>> print hub.register('/foo/bar/baz')
(23, False)
For another example:
newly_registered_count = 0
for wrapped_object in iteratorOfSomeObjectHierarchy():
hubid, isregistered = hub.register(wrapped_object)
newly_registered_count += isregistered # True == 1, False == 0
print "I registered %s additional objects" % newly_registered_count
There's a similar issue with 'unregister': it raises a NotFoundError if
the object/location is not registered.
We can change 'unregister' to return a bool. This is fine, as it
currently does not return anything meaningful.
--
Steve Alexander