Note that there are some explanatory texts on larger screens.

plurals
  1. PODecorator dynamic superclassing
    primarykey
    data
    text
    <p>I have simple decorators for replacing invalid characters in dict values when they are attempted to be retrieved.</p> <pre><code>import types class ContentInterface(dict): def __getitem__(self, item): raise NotImplementedError class Content(ContentInterface): def __getitem__(self, item): return dict.__getitem__(self, item) class DictDecorator(ContentInterface): def __init__(self, interfaceContent, **config): super(DictDecorator, self).__init__() self._component = interfaceContent self._config = config def _replace(self, text): return text def _check(self, invalidCharacterSet, itemPath): pass def __getitem__(self, name): item = self._component[name] if isinstance(item, types.StringTypes): newText = self._replace(item) invalidCharacterSet = set([char for char in item if char not in newText]) self._check(invalidCharacterSet, name) return newText else: return self.__class__(item, **self._config) class ReplaceCommaDecorator(DictDecorator): def _replace(self, text): return text.replace(",", ' ') class ReplaceDotDecorator(DictDecorator): def _replace(self, text): return text.replace('.', ' ') class ReplaceColonDecorator(DictDecorator): def _replace(self, text): return text.replace(":", ' ') class ReplaceSemicolonDecorator(DictDecorator): def _replace(self, text): return text.replace(";", ' ') </code></pre> <p>I want to use this the following way:</p> <pre><code>dictWithReplacedCharacters =\ ReplaceCommaDecorator( # Empty ReplaceDotDecorator( # Empty ReplaceColonDecorator( # Empty ReplaceSemicolonDecorator( # Empty Content({ # Data '1':u'1A:B;C,D.E', '2':{ '21':u'21A:B;C,D.E', '22':u'22A:B;C,D.E', } }), ), ), ), ) print dictWithReplacedCharacters['2']['21'] </code></pre> <p>I've got 4 redundant dict objects represented decorators for one data dict. </p> <p>I want to force the nested statement above to return ReplaceCommaDecorator object inherited from ReplaceDotDecorator inherited from ReplaceColonDecorator inherited from ReplaceSemicolonDecorator inherited from Content containing data. I guess this can be solved in DictDecorator's __new__ method.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload