ROS Code
 All Classes Namespaces Files Functions Variables
string.py
Go to the documentation of this file.
1 def catwalk(text):
2  """
3 
4  Replace multiple spaces with a single space
5  For example replace 'this is a long sentence' with 'this is a long sentence'
6 
7  text:
8  Specify the text to fix
9 
10  """
11 
12  return ' '.join(text.split())
13 
14 
15 def converttabs(text, spaces=4):
16  """
17 
18  Convert all the tabs to a specific amount of spaces
19 
20  text:
21  The text to convert tabs to spaces on
22 
23  spaces:
24  The amount of spaces to replace tabs to. Default is 4.
25 
26  """
27 
28  return text.replace('\t', ' ' * spaces)
29 
30 
31 def shortentext(text, minlength, placeholder='...'):
32  """
33 
34  Shorten some text by replacing the last part with a placeholder (such as '...')
35 
36  text:
37  The text to shorten
38 
39  minlength:
40  The minimum length before a shortening will occur
41 
42  placeholder:
43  The text to append after removing protruding text. Default is '...'.
44 
45  """
46 
47  return textwrap.shorten(text, minlength, placeholder=str(placeholder))
48 
49 
50 def wraptext(text, maxlength):
51  """
52 
53  Wrap text around the execution window according to a given size
54 
55  text:
56  The text to be wraped
57 
58  maxlength:
59  The amount of text until a wrap will be added
60 
61  """
62 
63  return textwrap.wrap(text, maxlength)
64 
65 
66 def unindent(text):
67  """
68 
69  Remove indention for some text
70 
71  text:
72  The text to unindent
73 
74  """
75 
76  return textwrap.dedent(text)
77 
78 
79 def paraspace(paragraphspaces=1):
80  """
81 
82  Print 1 or more paragraph spaces in the terminal output
83 
84  paragraphspaces:
85  The amount of paragraph spaces to print. Default is 1.
86 
87  """
88 
89  for _ in range(paragraphspaces):
90  print('', end='\n')
91 
92 
93 # Choose A Random Item From A List
94 
95 
96 def randomstr(valuelist):
97  try:
98  return random.choice(valuelist)
99  except IndexError:
100  raise RuntimeError('An Error Has Occured: List Not Specified (0018)')
101 
102 
103 def case(text, format='sentence'):
104  """
105 
106  Change the casing of some text
107 
108  text:
109  The text to change the casing of
110 
111  format:
112  The format of casing to apply to the text. Default is sentence.
113 
114  """
115  if format == 'uppercase':
116  return str(text.upper())
117  elif format == 'lowercase':
118  return str(text.lower())
119  elif format == 'sentence':
120  return str(text[0].upper()) + str(text[1:])
121  elif format == 'caterpillar':
122  return str(text.lower().replace(" ", "_"))
def unindent
Definition: string.py:66
def shortentext
Definition: string.py:31
def case
Definition: string.py:103
def wraptext
Definition: string.py:50
def converttabs
Definition: string.py:15
def paraspace
Definition: string.py:79
def catwalk
Definition: string.py:1
def randomstr
Definition: string.py:96