[Zope-dev] adapting to None
Chris Withers
chris at simplistix.co.uk
Mon Dec 15 11:56:44 EST 2008
Marius Gedminas wrote:
> doesn't fail with an exception, I can assume that
>
> ISomeInterface.providedBy(adapter)
...which in this case should return True, as None does indeed implement
the interface in question.
>> *That's* what I'm looking for help with, not judgement on whether
>> adapting to None is a good idea or not ;-)
>
> Could you please describe the real problem you're solving?
I have a generic IFieldType (well, could arguably be called IFieldValue)
adapter, which subclasses, eg:
class IFieldType(Interface): pass
class number(IFieldType): pass
class text(IFieldType): pass
class date(IFieldType): pass
class empty(IFieldType): pass
...etc..
The idea being that you can do things like:
text(anything)
...and if there's an adapter for it, it gets called and you get back
something that implements IFieldType.
This couples nicely with, for example:
classImplements(unicode,text_type)
classImplements(int,number_type)
classImplements(float,number_type)
classImplements(date,date_type)
classImplements(type(None),empty)
def str_to_date(str):
return parse(str,dayfirst=True)
provideAdapter(str_to_date,(str,),date)
Now, you could, for example, then do:
IFieldType([])
...which should return None.
cheers,
Chris
--
Simplistix - Content Management, Zope & Python Consulting
- http://www.simplistix.co.uk
More information about the Zope-Dev
mailing list