Last active
August 29, 2015 13:58
-
-
Save flying-sheep/9929117 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import OrderedDict | |
from collections.abc import Iterable | |
class ModOD(OrderedDict): | |
def __repr__(self): | |
if not len(self): | |
return 'od()' | |
return 'od[' + ', '.join('{}: {}'.format(k, v) for k, v in self.items()) + ']' | |
od_syntax_error = SyntaxError('Allowed sytax: od[<k>: <v>(, <k>: <v>, …)] or od()') | |
class ODSlicer: | |
def __call__(self): | |
return ModOD() | |
def __getitem__(self, keys): | |
if isinstance(keys, slice): | |
keys = [keys] | |
if not isinstance(keys, Iterable): | |
raise od_syntax_error | |
d = ModOD() | |
for key in keys: | |
if not isinstance(key, slice) or key.step is not None: | |
raise od_syntax_error | |
d[key.start] = key.stop | |
return d | |
od = ODSlicer() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment