Skip to content

Instantly share code, notes, and snippets.

@flying-sheep
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flying-sheep/9929117 to your computer and use it in GitHub Desktop.
Save flying-sheep/9929117 to your computer and use it in GitHub Desktop.
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