[Python-3000] Adaptation [was:Re: Iterators for dict keys, values, and items == annoying :)] (original) (raw)
Walter Dörwald walter at livinglogic.de
Mon Apr 3 01:39:06 CEST 2006
- Previous message: [Python-3000] Adaptation [was:Re: Iterators for dict keys, values, and items == annoying :)]
- Next message: [Python-3000] Adaptation [was:Re: Iterators for dict keys, values, and items == annoying :)]
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Alex Martelli wrote:
On Apr 2, 2006, at 7:11 AM, Paul Moore wrote: ... [...]
Say, for example, that protocols are identified (as in my strawman proposal) by unique strings anyway. E.g., if I were to invent a protocol, I could name it 'it.aleax.myprot' -- since I own the aleax.it domain, nobody else could create a name conflict. Saying that each framework has a separate registry is just the same as saying that each protocol "lives" in one specific registry, so that any registration or lookup regarding protocol P must also specify registryof(P).
Why not make the registry identical to the protocol? The protocol is just a convention anyway:
class AdaptionError(Exception): def init(self, adaptor, type): self.adaptor = adaptor self.type = type
def __str__(self):
return "can adapt %r to %r" % (self.type, self.adaptor)
class Adaptor(object): def init(self): self.registry = {}
def register(self, adaptor, *types):
for type in types:
self.registry[type] = adaptor
def __call__(self, obj, *args, **kwargs):
for bases in type(obj).__mro__:
try:
return self.registry[bases](obj, *args, **kwargs)
except KeyError:
pass
raise AdaptionError(self, type(obj))
With this, code would look like:
import adapt
formatter = adapt.Adaptor()
def formatnumber(x): return hex(x)
formatter.register(formatnumber, int, long)
def formatstring(x): return repr(x)
formatter.register(formatstring, basestring)
print formatter(42) print formatter(42L) print formatter("foo") print formatter(u"bar") print formatter(None)
(The last call would raise an AdaptionError).
Hopefully, rather than having to keep this correspondence in our human memory, we're allowed to have a registry of registries which remembers this correspondence for you: we can registerprotocol(P, registry) and we can lookup the registry for a given protocol with function registryof.
No need for that if each registry is self-contained.
[...]
were defined via some "interface" approach (like zope.interfaces and PyProtocols do) then encapsulation is taken care of by uniqueness of types/interfaces. I know interfaces are outside the scope of what's being proposed right now, but one of their benefits is that they do solve this problem. Structured strings naming protocols ("org.python.std.index" or whatever) do this as well, but without language support. I did mention that one issue with my "strawman proposal" was exactly that it performs no error checking: it entirely relies on programers respecting some simple and reasonable conventions, rather than piling up machinery to provide enforcement. Much like, oh, say, Python. Isn't it just wonderful, how the foes of adaptation switch horses on you? First they request a simple-as-dirt, bare-bones "example system" -- then as soon as you provide one they come back at you with all sort of "cruft" to be piled on top.
I think you might be misinterpreting reactions. If the initial reaction was "I don't understand it. Nobody needs this." (at least that was my reaction), you're "strawman proposal" has put us past this. (At least you got two "I finally got it, this seems useful" from me and Brett.)
So now lets answer the questions: How do we implement adaption of subtypes? What is a protocol? How can we make registration as painless as possible? etc.
Bye, Walter Dörwald
- Previous message: [Python-3000] Adaptation [was:Re: Iterators for dict keys, values, and items == annoying :)]
- Next message: [Python-3000] Adaptation [was:Re: Iterators for dict keys, values, and items == annoying :)]
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]