-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtruncate.py
More file actions
81 lines (70 loc) · 3.12 KB
/
Copy pathtruncate.py
File metadata and controls
81 lines (70 loc) · 3.12 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
#!/usr/bin/env python
"""Truncation beautifier function
This simple function attempts to intelligently truncate a given string
"""
def trunc(s, min_pos=0, max_pos=75, ellipsis=True):
"""Return a nicely shortened string if over a set upper limit
(default 75 characters)
What is nicely shortened? Consider this line from Orwell's 1984...
0---------1---------2---------3---------4---------5---------6---------7---->
When we are omnipotent we shall have no more need of science. There will be
If the limit is set to 70, a hard truncation would result in...
When we are omnipotent we shall have no more need of science. There wi...
Truncating to the nearest space might be better...
When we are omnipotent we shall have no more need of science. There...
The best truncation would be...
When we are omnipotent we shall have no more need of science...
Therefore, the returned string will be, in priority...
1. If the string is less than the limit, just return the whole string
2. If the string has a period, return the string from zero to the first
period from the right
3. If the string has no period, return the string from zero to the first
space
4. If there is no space or period in the range return a hard truncation
In all cases, the string returned will have ellipsis appended unless
otherwise specified.
Parameters:
s = string to be truncated as a String
min_pos = minimum character index to return as Integer (returned
string will be at least this long - default 0)
max_pos = maximum character index to return as Integer (returned
string will be at most this long - default 75)
ellipsis = returned string will have an ellipsis appended to it
before it is returned if this is set as Boolean
(default is True)
Returns:
Truncated String
Throws:
ValueError exception if min_pos > max_pos, indicating improper
configuration
Usage:
short_string = trunc(some_long_string)
or
shorter_string = trunc(some_long_string,max_pos=15,ellipsis=False)
"""
# Sentinel value -1 returned by String function rfind
NOT_FOUND = -1
# Error message for max smaller than min positional error
ERR_MAXMIN = 'Minimum position cannot be greater than maximum position'
# If the minimum position value is greater than max, throw an exception
if max_pos < min_pos:
raise ValueError(ERR_MAXMIN)
# Change the ellipsis characters here if you want a true ellipsis
if ellipsis:
suffix = '...'
else:
suffix = ''
# Case 1: Return string if it is shorter (or equal to) than the limit
length = len(s)
if length <= max_pos:
return s + suffix
else:
# Case 2: Return it to nearest period if possible
try:
end = s.rindex('.', min_pos, max_pos)
except ValueError:
# Case 3: Return string to nearest space
end = s.rfind(' ', min_pos, max_pos)
if end == NOT_FOUND:
end = max_pos
return s[0:end] + suffix