-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathodict.py
More file actions
36 lines (28 loc) · 817 Bytes
/
odict.py
File metadata and controls
36 lines (28 loc) · 817 Bytes
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
"""
>>> odict['a': 1, 'b': 2, 'c': 3]
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> odict['a': 1]
OrderedDict([('a', 1)])
>>> odict('a', 1, 'b', 2, 'c', 3)
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> odict('a', 1)
OrderedDict([('a', 1)])
>>> odict()
OrderedDict()
"""
from collections import OrderedDict
from magic import magic
@magic
class odict:
"""OrderedDict builder with nice syntax."""
def __getitem__(self, key):
try:
# Assume key is a tuple of slices
items = [(s.start, s.stop) for s in key]
except TypeError:
# Assume key is a single slice
items = [(key.start, key.stop)]
return OrderedDict(items)
def __call__(self, *kv):
items = zip(kv[::2], kv[1::2]) if kv else []
return OrderedDict(items)