-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
139 lines (95 loc) · 2.74 KB
/
Copy pathutils.py
File metadata and controls
139 lines (95 loc) · 2.74 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from typing import Sequence
def no_effect_decorator(func):
return func
override = no_effect_decorator
noexcept = no_effect_decorator
class Implicit_type_conversion:
__slots__ = ('func', 'types')
def __init__(self, func, *types):
self.func = func
self.types = types
def __call__(self, *args, **kwargs):
lis = []
for i in range(len(args)):
lis.append(self.types[i](args[i]))
return self.func(*lis, **kwargs)
def implicit_type_conversion(*types):
def decorator(func):
return Implicit_type_conversion(func, *types)
return decorator
def idtt(x):
return x
class Logger:
__slots__ = ('_log',)
_end_char: str = '\n'
def __init__(self):
self._log: str = ""
def get_log(self) -> str:
return self._log
def pop_log(self) -> str:
log = self._log
self._log = ""
return log
def write_on_file(self, *, filename: str) -> None:
f = open(filename, 'w')
f.write(self._log)
f.close()
@classmethod
def set_end_char(cls, end_char: str) -> None:
cls._end_char = end_char
def log(self, msg: str, end: str = None) -> None:
if end is None:
end = self._end_char
self._log += str(msg) + end
logger = Logger()
def merge(arr1: Sequence, arr2: Sequence) -> list:
"""
Merge two sorted sequences into a sorted sequence.
"""
p1, p2 = 0, 0
ret = []
while p1 < len(arr1) and p2 < len(arr2):
if arr1[p1] < arr2[p2]:
ret.append(arr1[p1])
p1 += 1
else:
ret.append(arr2[p2])
p2 += 1
if p1 < len(arr1):
ret.extend(arr1[p1:])
if p2 < len(arr2):
ret.extend(arr2[p2:])
return ret
def is_duplicated(sorted_: Sequence) -> bool:
"""
Check if there are duplicated elements in a sorted sequence.
:param sorted_: a sorted sequence
:return: True if there is a duplicated element, False otherwise
"""
for i in range(len(sorted_) - 1):
if sorted_[i] == sorted_[i + 1]:
return True
return False
class DuplicatedError(Exception):
pass
class FnConflictError(Exception):
pass
class VarConflictError(Exception):
pass
class GPErrors:
__slots__ = ()
@staticmethod
def duplicated_error(msg: str) -> DuplicatedError:
return DuplicatedError(msg)
@staticmethod
def fn_conflict_error(msg: str) -> FnConflictError:
return FnConflictError(msg)
@staticmethod
def var_conflict_error(msg: str) -> VarConflictError:
return VarConflictError(msg)
errs = GPErrors()
@implicit_type_conversion(int, int)
def add(a, b):
return a + b
if __name__ == '__main__':
print(add('1', '2'))