From 1ef274d455d894660c9c697187d95b7724fe4ff1 Mon Sep 17 00:00:00 2001 From: alex_nanou Date: Sun, 27 Jan 2008 02:58:50 +0000 Subject: [PATCH] *** empty log message *** --- CHANGES | 6 +- PKG-INFO | 2 +- pli/logictypes.py | 48 ++++++-- pli/pattern/mixin/mapping.py | 53 +-------- pli/pattern/mixin/mappingutils.py | 235 ++++++++++++++++++++++++++++++++++++++ pli/pattern/tree/acquisitional.py | 3 +- 6 files changed, 281 insertions(+), 66 deletions(-) create mode 100755 pli/pattern/mixin/mappingutils.py diff --git a/CHANGES b/CHANGES index 8edf6b1..d9f0879 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,10 @@ -# this file was generated on [200801040034] +# this file was generated on [200801270555] pli changes: +version 0.0.219 (200801270511): +- added pli.pattern.mixin.mappingutils module and moved there several mixins form pli.pattern.mixin.mapping... +- added DictChain to pli.logictypes + version 0.0.217 (200801020225): - added tag chain support... (still both incomplete and experimental) still todo: diff --git a/PKG-INFO b/PKG-INFO index 4409804..8f2c460 100644 --- a/PKG-INFO +++ b/PKG-INFO @@ -1,6 +1,6 @@ Metadata-Version: 1.0 Name: pli -Version: 0.0.217 +Version: 0.0.219 Summary: PLI: a utility library for the Python language. Home-page: http://pli.sourceforge.net/ Author: Alex A. Naanou diff --git a/pli/logictypes.py b/pli/logictypes.py index 551bb05..96c4158 100755 --- a/pli/logictypes.py +++ b/pli/logictypes.py @@ -1,7 +1,7 @@ #======================================================================= __version__ = '''0.1.21''' -__sub_version__ = '''20070707174650''' +__sub_version__ = '''20080127040755''' __copyright__ = '''(c) Alex A. Naanou 2003''' __doc__ = '''\ @@ -12,6 +12,7 @@ usage of standard python types. #----------------------------------------------------------------------- import pli.pattern.mixin.mapping as mapping +import pli.pattern.proxy.utils as proxyutils #----------------------------------------------------------------------- @@ -544,25 +545,45 @@ class BasicDictChain(AbstractDictChainMixin, mapping.DictLike): NOTE: this class was designed as a basic base class (atleast the __iterchainmembers__ method should be overloaded). NOTE: do not forget to call the original __iterchainmembers__. - NOTE: this, if used as is will not differ from a dict. + NOTE: this, if used as-is will not differ from a dict. ''' + _dict_data = None + def __init__(self, *p, **n): ''' ''' - self._dct_data = {} + self._dict_data = {} super(BasicDictChain, self).__init__(*p, **n) - def __setitem__(self, name, value): - ''' - ''' - self._dct_data[name] = value - def __delitem__(self, name): + proxyutils.proxymethods(( + '__setitem__', + '__delitem__', + ), '_dict_data') + def __iterchainmembers__(self): ''' ''' - del self._dct_data[name] + yield self._dict_data + + +#-----------------------------------------------------------DictChain--- +class DictChain(BasicDictChain): + ''' + this is a basic dict chain element. + + when a key can not be found in the local data then the request is + forwarded to the .chain_next attribute. + + NOTE: .chain_next can be None, then no other forwarding is done. + NOTE: editing is possible only to the local data. + ''' + chain_next = None + def __iterchainmembers__(self): ''' ''' - yield self._dct_data + for v in super(DictChain, self).__iterchainmembers__(): + yield v + if self.chain_next != None: + yield self.chain_next @@ -631,6 +652,13 @@ if __name__ == '__main__': print isodd(3), isodd(6) + # check the dict chain... + d = DictChain(a=1, b=2, c=3) + print d.todict() + d.chain_next = DictChain(c=4, d=5, e=6) + print d.todict() + d.chain_next.chain_next = dict(e=7, f=8, g=9) + print d.todict() diff --git a/pli/pattern/mixin/mapping.py b/pli/pattern/mixin/mapping.py index 1fb383e..49a17a9 100644 --- a/pli/pattern/mixin/mapping.py +++ b/pli/pattern/mixin/mapping.py @@ -1,7 +1,7 @@ #======================================================================= __version__ = '''0.1.03''' -__sub_version__ = '''20071206152113''' +__sub_version__ = '''20080127042744''' __copyright__ = '''(c) Alex A. Naanou 2003''' @@ -373,56 +373,5 @@ class DictLike(Mapping): -#----------------------------------------------------------------------- -# XXX move this elsewhere... - -import pli.pattern.proxy.utils as proxyutils - - -#---------------------------------------------------Matting2AttrMixin--- -class Mapping2AttrMinimalMixin(Mapping): - ''' - provides a minimal simple drop-in mixin to enable access to the objects - namespace through a mapping interface. - - NOTE: this shadows the already defined functionality. - ''' - proxyutils.proxymethods(( - # minimal set... - '__getitem__', - '__setitem__', - '__delitem__', - '__iter__', - # for speed... - '__contains__', - '__len__', - ), '__dict__') - - -#---------------------------------------------------Matting2AttrMixin--- -class Mapping2AttrMixin(Mapping2AttrMinimalMixin): - ''' - provides a simple drop-in mixin to enable access to the objects - namespace through a mapping interface. - - NOTE: this shadows the already defined functionality. - ''' - proxyutils.proxymethods(( - # other methods... - 'kyes', - 'iterkeys', - 'values', - 'itervalues', - 'items', - 'iteritems', - 'get', - 'pop', - 'update', - 'copy', - ), '__dict__') - - - - #======================================================================= # vim:set ts=4 sw=4 nowrap : diff --git a/pli/pattern/mixin/mappingutils.py b/pli/pattern/mixin/mappingutils.py new file mode 100755 index 0000000..ff065c9 --- /dev/null +++ b/pli/pattern/mixin/mappingutils.py @@ -0,0 +1,235 @@ +#======================================================================= + +__version__ = '''0.0.01''' +__sub_version__ = '''20080127051039''' +__copyright__ = '''(c) Alex A. Naanou 2003''' + + +#----------------------------------------------------------------------- + +import new + +import pli.pattern.mixin.mapping as mapping +import pli.pattern.proxy.utils as proxyutils +import pli.logictypes as logictypes +import pli.objutils as objutils + + +#----------------------------------------------------------------------- +#--------------------------------------------Mapping2AttrMinimalMixin--- +class Mapping2AttrMinimalMixin(mapping.Mapping): + ''' + provides a minimal simple drop-in mixin to enable access to the objects + namespace through a mapping interface. + + NOTE: this shadows the already defined functionality. + ''' + proxyutils.proxymethods(( + # minimal set... + '__getitem__', + '__setitem__', + '__delitem__', + '__iter__', + # for speed... + '__contains__', + '__len__', + ), '__dict__') + + +#---------------------------------------------------Mapping2AttrMixin--- +class Mapping2AttrMixin(Mapping2AttrMinimalMixin): + ''' + provides a simple drop-in mixin to enable access to the objects + namespace through a mapping interface. + + NOTE: this shadows the already defined functionality. + ''' + proxyutils.proxymethods(( + # other methods... + 'kyes', + 'iterkeys', + 'values', + 'itervalues', + 'items', + 'iteritems', + 'get', + 'pop', + 'update', + 'copy', + ), '__dict__') + + +#---------------------------------------------------Attr2MappingMixin--- +# XXX this is a workaround pythons "bug" with __dict__ using the +# C API instead of the py dict interface... +class Attr2MappingMixin(mapping.BasicMapping): + ''' + ''' + # attrs that will always be accessed in the __dict__ + __attr2map_ignore_attrs__ = () + + def __getattr__(self, name): + ''' + ''' + try: + if name not in self.__attr2map_ignore_attrs__: + return self[name] + except KeyError: + pass + # call the next get... + try: + return super(Attr2MappingMixin, self).__getattr__(name) + except KeyError: + raise AttributeError, name + def __setattr__(self, name, value): + ''' + ''' + if name not in self.__attr2map_ignore_attrs__: + self[name] = value + else: + super(Attr2MappingMixin, self).__setattr__(name, value) + def __delattr__(self, name): + ''' + ''' + try: + if name not in self.__attr2map_ignore_attrs__: + del self[name] + return + except KeyError: + pass + # call the next del... + try: + del super(Attr2MappingMixin, self)[name] + except KeyError: + raise AttributeError, name + + + +#----------------------------------------------------------------------- +#-------------------------------------MappingWithItemConstructorMixin--- +# TODO make a version/mixin where constructors are searched in +# parents... +class MappingWithItemConstructorMixin(mapping.BasicMapping): + ''' + ''' + # mapping containing item consturctos... + # a constructor may take any arguments and must return the + # constructed object... + # NOTE: when the constructor is called form the object it must take + # the key for the object as the fist argument folowed bu + # normal constructor args... + __item_constructors__ = None + + def __getattr__(self, name): + ''' + ''' + if name in self.__item_constructors__: + return new.instancemethod(self.__item_constructors__[name], self, self.__class__) + # XXX do we need a try block here??? (may block some errors) + try: + return super(MappingWithItemConstructorMixin, self).__getattr__(name) + except AttributeError: + raise AttributeError, name + + # XXX this might not be pickle safe... + @objutils.classinstancemethod + def regconstructor(self, name, constructor): + ''' + ''' + if self.__item_constructors__ == None: + self.__item_constructors__ = logictypes.DictChain() + # XXX vars might not work in all cases (when the ns interface is + # redefined)... + elif '__item_constructors__' not in vars(self): + p = self.__item_constructors__ + c = self.__item_constructors__ = logictypes.DictChain() + c.chain_next = p + # XXX is this approach pickle-safe??? + def prepare(constructor): + def _construct(self, name, *p, **n): + val = constructor(*p, **n) + self[name] = val + return val + return _construct + self.__item_constructors__[name] = prepare(constructor) + + + +#----------------------------------------------------------------------- +#----------------------------------MappingWithKeyTypeRestrictoinMixin--- +class MappingWithKeyTypeRestrictoinMixin(mapping.BasicMapping): + ''' + ''' + def __check_mapping_key_type__(self, key): + ''' + ''' + raise NotImplementedError + + ##!!! add super call... + def __setitem__(self, name, value): + ''' + ''' + self.__check_mapping_key_type__(value) + super(MappingWithValueTypeRestrictoin, self).__setitem__(name, value) + + +#--------------------------------MappingWithValueTypeRestrictoinMixin--- +class MappingWithValueTypeRestrictoinMixin(mapping.BasicMapping): + ''' + ''' + def __check_mapping_value_type__(self, value): + ''' + ''' + raise NotImplementedError + + ##!!! add super call... + def __setitem__(self, name, value): + ''' + ''' + self.__check_mapping_value_type__(value) + super(MappingWithValueTypeRestrictoin, self).__setitem__(name, value) + + + +#----------------------------------------------------------------------- +if __name__ == '__main__': + +## class C(MappingWithItemConstructorMixin, dict): + class C(Attr2MappingMixin, MappingWithItemConstructorMixin, dict): + ''' + ''' + __attr2map_ignore_attrs__ = ( + '__item_constructors__', + ) + + C.regconstructor('str', str) + C.regconstructor('set', set) + + c = C() + + c.str('a', 124) + c.set('b', '124') + + c.regconstructor('int', int) + + c.str('c', 321) + c.int('d', 124) + + print c + + cc = C() + + cc.str('a', 124) +## cc.int('d', 124) + + print cc + + print cc.a + del cc.a + print cc + + + + +#======================================================================= +# vim:set ts=4 sw=4 nowrap : diff --git a/pli/pattern/tree/acquisitional.py b/pli/pattern/tree/acquisitional.py index 1b3d7b8..a39f5e4 100755 --- a/pli/pattern/tree/acquisitional.py +++ b/pli/pattern/tree/acquisitional.py @@ -1,7 +1,7 @@ #======================================================================= __version__ = '''0.0.01''' -__sub_version__ = '''20050824141124''' +__sub_version__ = '''20080126193432''' __copyright__ = '''(c) Alex A. Naanou 2003''' @@ -13,7 +13,6 @@ import pli.logictypes as logictypes #----------------------------------------------------------------------- -# TODO rename this module to something like acquisitional.py # TODO parant resolution protocol (if not present use attr). # TODO acquisition cut (mignt be done by the above...) # TODO might be good to do a recursive depth iterator... -- 2.11.4.GIT