-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathodictliteral.py
More file actions
55 lines (41 loc) · 1.24 KB
/
odictliteral.py
File metadata and controls
55 lines (41 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict
try:
from reprlib import recursive_repr
except ImportError:
# don't cope with recursive repr calls in py2
def recursive_repr(fillvalue='...'):
return (lambda f: f)
try:
from collections import Iterable
except ImportError:
try:
from collections.abc import Iterable
except ImportError:
Iterable = tuple
__all__ = ["odict"]
__version__ = '1.0.1'
class odictType(type):
syntax_error = SyntaxError("Allowed syntax: odict[<k>: <v>(, <k>: <v>...)]")
def __getitem__(self, keys):
if isinstance(keys, slice):
keys = (keys,)
if not isinstance(keys, Iterable):
raise self.syntax_error
od = self()
for k in keys:
if not isinstance(k, slice) or k.step is not None:
raise self.syntax_error
od[k.start] = k.stop
return od
@recursive_repr(fillvalue="odict[...]")
def odict_repr(self):
if not self:
return "odict()"
return "odict[%s]" % (", ".join(
"%r: %r" % (k, v)
for k, v in self.items()
),)
odict = odictType(str('odict'), (OrderedDict,), {"__repr__": odict_repr})