forked from mCodingLLC/VideosSampleCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoveprefix_removesuffix.py
59 lines (45 loc) · 1.83 KB
/
removeprefix_removesuffix.py
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
from typing import TypeVar
T = TypeVar('T', str, bytes, bytearray)
def removeprefix(s: T, prefix: T) -> T:
if s.startswith(prefix):
return s[len(prefix):]
else:
return s[:]
def removesuffix(s: T, suffix: T) -> T:
if suffix and s.endswith(suffix):
return s[:-len(suffix)]
else:
return s[:]
def test_removeprefix():
assert removeprefix('abctest', 'abc') == 'test'
assert removeprefix('abctest', 'def') == 'abctest'
assert removeprefix('abctest', '') == 'abctest'
assert removeprefix('', 'def') == ''
assert removeprefix('', '') == ''
assert removeprefix(b'abctest', b'abc') == b'test'
assert removeprefix(b'abctest', b'def') == b'abctest'
assert removeprefix(b'abctest', b'') == b'abctest'
assert removeprefix(b'', b'def') == b''
assert removeprefix(b'', b'') == b''
assert removeprefix(bytearray(b'abctest'), bytearray(b'abc')) == bytearray(b'test')
assert removeprefix(bytearray(b'abctest'), bytearray(b'def')) == bytearray(b'abctest')
assert removeprefix(bytearray(b'abctest'), bytearray(b'')) == bytearray(b'abctest')
assert removeprefix(bytearray(b''), bytearray(b'def')) == bytearray(b'')
assert removeprefix(bytearray(b''), bytearray(b'')) == bytearray(b'')
x = bytearray(b'123')
y = removeprefix(x, bytearray(b'abc'))
assert y == x
assert y is not x
print('passed')
if __name__ == '__main__':
# removesuffix files example
files: list[str] = ['paint.exe', 'word.exe', 'virus.exe', 'not-modified-example']
#files = [x.removesuffix('.exe') for x in files]
print(files)
# bytes Response example
print(b'Response: DATA'.removeprefix(b'Response: '))
# NOT rstrip and lstrip
files = [x.rstrip('.exe') for x in files]
print(files)
# sample implementation, tests first!
test_removeprefix()