-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.py
More file actions
executable file
·57 lines (42 loc) · 1.54 KB
/
diff.py
File metadata and controls
executable file
·57 lines (42 loc) · 1.54 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
#!/usr/bin/env python
import sys
def deriv(filename, normalize = False, unit = 1.0, percent = False):
file = open(filename,"r")
Start = True
for f in file:
number = float(f)/unit
if Start:
Start = False
FirstElement = number
LastElement = number
else:
if normalize:
print number - FirstElement
elif percent:
print (number - LastElement)/LastElement
LastElement = number
else:
print number - LastElement
LastElement = number
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-n", "--norm", dest="normalize",
help="subtract the first element (instead of differencing)",
action = "store_true", default = False)
parser.add_option("-p", "--percent", dest="percent",
help="Print the percent change (this-last)/last (use this option only)",
action = "store_true", default = False)
parser.add_option("-u", "--unit", dest="unit",
help="divide by unit",
action = "store", type = "float", default = "1.0")
(options, args) = parser.parse_args()
if len( sys.argv ) < 2:
print "diff.py file"
print "differentiates a list of numbers."
else:
filename = sys.argv[1]
norm = False
if len( sys.argv ) > 2:
norm = sys.argv[2]
deriv(filename,normalize=options.normalize, unit = options.unit, percent = options.percent)
#end