-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclip.py
More file actions
39 lines (34 loc) · 1.15 KB
/
clip.py
File metadata and controls
39 lines (34 loc) · 1.15 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
def clip(text, max_len=80):
"""Return text clipped at the last space before or after max_len """
end = None
if len(text) > max_len:
space_before = text.rfind(' ', 0, max_len)
if space_before >= 0:
end = space_before
else:
space_after = text.rfind(' ', max_len)
if space_after >= 0:
end = space_after
if end is None: # no spaces were found
end = len(text)
return text[:end].rstrip()
def clip(text:str, max_len:'int > 0'=80) -> str:
"""Return text clipped at the last space before or after max_len """
end = None
if len(text) > max_len:
space_before = text.rfind(' ', 0, max_len)
if space_before >= 0:
end = space_before
else:
space_after = text.rfind(' ', max_len)
if space_after >= 0:
end = space_after
if end is None: # no spaces were found
end = len(text)
return text[:end].rstrip()
print(clip.__defaults__)
print(clip.__code__)
print(clip.__code__.co_argcount)
print(clip.__code__.co_varnames)
print(clip('a bc', max_len=10))
print(clip('a bc', max_len=1))