-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence.py
More file actions
executable file
·71 lines (55 loc) · 1.9 KB
/
sequence.py
File metadata and controls
executable file
·71 lines (55 loc) · 1.9 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
s = Sequence('window', 'w', 'i', 'n', 'd', 'o', 'w'), # Args
s = Sequence(name='window', path=('w', 'i', 'n', 'd', 'o', 'w')), # kwrgs
s = Sequence('window') # only args
s = Sequence(path='window') # only kwarg path
"""
class Path(tuple):
def __new__ (cls, *a):
# return super(Path, cls).__new__(cls, a)
return super().__new__(cls, a)
def __init__(self, *a):
# print('Init', a)
super().__init__()
class Sequence(object):
"""
Sequence('window')
Sequence(path='window')
Sequence('window', Path('w', 'i', 'n', 'd', 'o', 'w'))
Sequence(name='window', path=('w', 'i', 'n', 'd', 'o', 'w'))
Sequence('w', 'i', 'n', 'd', 'o', 'w', name='window')
"""
def __init__(self, iterable=None, *extras, name=None, path=None):
rname, rpath = self.construct(iterable, *extras, name=name, path=path)
self.path = rpath
self.name = rname
def __getitem__(self, item):
return self.path[item]
def __len__(self):
return len(self.path)
def construct(self, iterable=None, *extras, name=None, path=None):
args = iterable
has_extra = len(extras) > 0
if has_extra:
args = (iterable, ) + extras
iterable = (iterable, ) + extras
if path is None:
# reverse unpack args
path = args
am1 = args[-1]
if type(am1) is Path:
path = am1
name = args[0]
rpath = Path(*path)
if name is None:
name = args or path
return name, rpath
def __iter__(self):
return iter(self.path)
def stringify(self, path):
return str(path)
def __str__(self):
return self.name
def __repr__(self):
c = self.__class__.__name__
return f'<{c} "{self.name}">'