forked from abetlen/llama-cpp-python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
Latest commit
101 lines (85 loc) · 2.64 KB
/
Copy pathutil.py
File metadata and controls
101 lines (85 loc) · 2.64 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
ANSI_COLOR_RESET="\x1b[0m"
ANSI_COLOR_YELLOW="\x1b[33m"
ANSI_BOLD="\x1b[1m"
ANSI_COLOR_GREEN="\x1b[32m"
CONSOLE_COLOR_DEFAULT=ANSI_COLOR_RESET
CONSOLE_COLOR_PROMPT=ANSI_COLOR_YELLOW
CONSOLE_COLOR_USER_INPUT=ANSI_BOLD+ANSI_COLOR_GREEN
# Iterative search
# Actively searches and prevents a pattern from being returned
classIterSearch:
def__init__(self, pattern):
self.pattern=list(pattern)
self.buffer= []
def__call__(self, char):
self.buffer+= [char]
ifself.pattern[: len(self.buffer)] ==self.buffer:
iflen(self.buffer) >=len(self.pattern):
self.buffer.clear()
return []
_tmp=self.buffer[:]
self.buffer.clear()
return_tmp
classCircle:
def__init__(self, size, default=0):
self.list= [default] *size
self.maxsize=size
self.size=0
self.offset=0
defappend(self, elem):
ifself.size<self.maxsize:
self.list[self.size] =elem
self.size+=1
else:
self.list[self.offset] =elem
self.offset= (self.offset+1) %self.maxsize
def__getitem__(self, val):
ifisinstance(val, int):
if0>valorval>=self.size:
raiseIndexError("Index out of range")
return (
self.list[val]
ifself.size<self.maxsize
elseself.list[(self.offset+val) %self.maxsize]
)
elifisinstance(val, slice):
start, stop, step=val.start, val.stop, val.step
ifstepisNone:
step=1
ifstartisNone:
start=0
ifstopisNone:
stop=self.size
ifstart<0:
start=self.size+start
ifstop<0:
stop=self.size+stop
indices=range(start, stop, step)
return [
self.list[(self.offset+i) %self.maxsize]
foriinindices
ifi<self.size
]
else:
raiseTypeError("Invalid argument type")
if__name__=="__main__":
c=Circle(5)
c.append(1)
print(c.list)
print(c[:])
assertc[0] ==1
assertc[:5] == [1]
foriinrange(2, 5+1):
c.append(i)
print(c.list)
print(c[:])
assertc[0] ==1
assertc[:5] == [1, 2, 3, 4, 5]
foriinrange(5+1, 9+1):
c.append(i)
print(c.list)
print(c[:])
assertc[0] ==5
assertc[:5] == [5, 6, 7, 8, 9]
# assert c[:-5] == [5,6,7,8,9]
assertc[:10] == [5, 6, 7, 8, 9]