1 |
#!/usr/bin/env python
|
2 |
|
3 |
import math
|
4 |
|
5 |
#function to replace special characters in a cut name
|
6 |
def plainTextString(inputString):
|
7 |
|
8 |
inputString.replace('<','lt')
|
9 |
inputString.replace('>','gt')
|
10 |
inputString.replace('(','')
|
11 |
inputString.replace(')','')
|
12 |
inputString.replace('=','eq')
|
13 |
|
14 |
# we have to handle whitespace separately
|
15 |
# there can be special characters that don't get recognized by just replacing " "
|
16 |
stringList = list(inputString)
|
17 |
inputString = ""
|
18 |
for char in stringList:
|
19 |
if char.isspace():
|
20 |
inputString = inputString + "_"
|
21 |
else:
|
22 |
inputString = inputString + char
|
23 |
|
24 |
return inputString
|
25 |
|
26 |
|
27 |
#function to put spaces between every 3 digits in a number
|
28 |
def formatNumber(inputNumber):
|
29 |
inputList = list(inputNumber)
|
30 |
decimalIndex = inputNumber.find(".")
|
31 |
if decimalIndex is not -1:# found decimal point
|
32 |
numDigitsAboveOne = decimalIndex
|
33 |
numDigitsBelowOne = len(inputList) - numDigitsAboveOne - 1
|
34 |
else: # didn't find a decimal point
|
35 |
numDigitsAboveOne = len(inputList)
|
36 |
numDigitsBelowOne = 0
|
37 |
|
38 |
outputString = "$"
|
39 |
|
40 |
for index in range(numDigitsAboveOne): #print digits > 1
|
41 |
outputString = outputString + inputList[index]
|
42 |
if (numDigitsAboveOne - index - 1) % 3 is 0 and (numDigitsAboveOne - index - 1) is not 0:
|
43 |
outputString = outputString + "\;"
|
44 |
if decimalIndex is not -1: #print "." if needed
|
45 |
outputString = outputString + "."
|
46 |
for index in range(numDigitsBelowOne): #print digits < 1
|
47 |
outputString = outputString + inputList[index+numDigitsAboveOne+1]
|
48 |
if (index+1) % 3 is 0 and (numDigitsBelowOne - index -1) is not 0:
|
49 |
outputString = outputString + "\;"
|
50 |
|
51 |
outputString = outputString + "$"
|
52 |
|
53 |
if outputString.find('$$') is not -1:
|
54 |
outputString = '$0$'
|
55 |
|
56 |
return outputString
|
57 |
|
58 |
|
59 |
#function to round to a certain number of significant digits
|
60 |
def round_sigfigs(num, sig_figs):
|
61 |
if num != 0:
|
62 |
return round(num, -int(math.floor(math.log10(abs(num))) - (sig_figs - 1)))
|
63 |
else:
|
64 |
return 0 # Can't take the log of 0
|