Skip to content

Commit be25b4d

Browse files
committed
Adding required files
0 parents  commit be25b4d

33 files changed

+1124
-0
lines changed

code/argv_list.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import sys
2+
print('sys.argv is', sys.argv)

code/arith.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sys
2+
3+
4+
def main():
5+
assert len(sys.argv) == 4, 'Need exactly 3 arguments'
6+
7+
operator = sys.argv[1]
8+
assert operator in ['add', 'subtract', 'multiply', 'divide'], (
9+
'Operator is not one of add, subtract, multiply, or divide: '
10+
'bailing out')
11+
try:
12+
operand1, operand2 = float(sys.argv[2]), float(sys.argv[3])
13+
except ValueError:
14+
print('Cannot convert input to a number: bailing out')
15+
return
16+
17+
do_arithmetic(operand1, operator, operand2)
18+
19+
20+
def do_arithmetic(operand1, operator, operand2):
21+
22+
if operator == 'add':
23+
value = operand1 + operand2
24+
elif operator == 'subtract':
25+
value = operand1 - operand2
26+
elif operator == 'multiply':
27+
value = operand1 * operand2
28+
elif operator == 'divide':
29+
value = operand1 / operand2
30+
print(value)
31+
32+
33+
if __name__ == '__main__':
34+
main()

code/check.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
import numpy
3+
4+
5+
def main():
6+
script = sys.argv[0]
7+
filenames = sys.argv[1:]
8+
if len(filenames) <= 1: # nothing to check
9+
print('Only 1 file specified on input')
10+
else:
11+
nrow0, ncol0 = row_col_count(filenames[0])
12+
print('First file %s: %d rows and %d columns' % (
13+
filenames[0], nrow0, ncol0))
14+
for f in filenames[1:]:
15+
nrow, ncol = row_col_count(f)
16+
if nrow != nrow0 or ncol != ncol0:
17+
print('File %s does not check: %d rows and %d columns'
18+
% (f, nrow, ncol))
19+
else:
20+
print('File %s checks' % f)
21+
return
22+
23+
24+
def row_col_count(filename):
25+
try:
26+
nrow, ncol = numpy.loadtxt(filename, delimiter=',').shape
27+
except ValueError:
28+
# This occurs if the file doesn't have same number of rows and columns,
29+
# or if it has non-numeric content
30+
nrow, ncol = (0, 0)
31+
return nrow, ncol
32+
33+
34+
if __name__ == '__main__':
35+
main()

code/count_stdin.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import sys
2+
3+
count = 0
4+
for line in sys.stdin:
5+
count += 1
6+
7+
print(count, 'lines in standard input')

code/gen_inflammation.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Generate pseudo-random patient inflammation data for use in Python lessons.
5+
"""
6+
7+
import random
8+
9+
n_patients = 60
10+
n_days = 40
11+
n_range = 20
12+
13+
middle = n_days / 2
14+
15+
for p in range(n_patients):
16+
vals = []
17+
for d in range(n_days):
18+
upper = max(n_range - abs(d - middle), 0)
19+
vals.append(random.randint(upper/4, upper))
20+
print(','.join([str(v) for v in vals]))

code/line_count.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import sys
2+
3+
4+
def main():
5+
"""
6+
print each input filename and the number of lines in it,
7+
and print the sum of the number of lines
8+
"""
9+
filenames = sys.argv[1:]
10+
sum_nlines = 0 # initialize counting variable
11+
12+
if len(filenames) == 0: # no filenames, just stdin
13+
sum_nlines = count_file_like(sys.stdin)
14+
print('stdin: %d' % sum_nlines)
15+
else:
16+
for f in filenames:
17+
n = count_file(f)
18+
print('%s %d' % (f, n))
19+
sum_nlines += n
20+
print('total: %d' % sum_nlines)
21+
22+
23+
def count_file(filename):
24+
"""count the number of lines in a file"""
25+
f = open(filename, 'r')
26+
nlines = len(f.readlines())
27+
f.close()
28+
return(nlines)
29+
30+
31+
def count_file_like(file_like):
32+
"""count the number of lines in a file-like object (eg stdin)"""
33+
n = 0
34+
for line in file_like:
35+
n = n+1
36+
return n
37+
38+
39+
if __name__ == '__main__':
40+
main()

code/my_ls.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import glob
2+
import sys
3+
4+
5+
def main():
6+
"""prints names of all files with sys.argv as suffix"""
7+
assert len(sys.argv) >= 2, "Argument list cannot be empty"
8+
# NB: behaviour is not as you'd expect if sys.argv[1] is *
9+
suffix = sys.argv[1]
10+
glob_input = '*.' + suffix # construct the input
11+
glob_output = glob.glob(glob_input) # call the glob function
12+
for item in glob_output: # print the output
13+
print(item)
14+
return
15+
16+
17+
if __name__ == '__main__':
18+
main()

code/readings_01.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import sys
2+
import numpy
3+
4+
5+
def main():
6+
script = sys.argv[0]
7+
filename = sys.argv[1]
8+
data = numpy.loadtxt(filename, delimiter=',')
9+
for m in data.mean(axis=1):
10+
print(m)

code/readings_02.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import sys
2+
import numpy
3+
4+
5+
def main():
6+
script = sys.argv[0]
7+
filename = sys.argv[1]
8+
data = numpy.loadtxt(filename, delimiter=',')
9+
for m in data.mean(axis=1):
10+
print(m)
11+
12+
13+
if __name__ == '__main__':
14+
main()

code/readings_03.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import sys
2+
import numpy
3+
4+
5+
def main():
6+
script = sys.argv[0]
7+
for filename in sys.argv[1:]:
8+
data = numpy.loadtxt(filename, delimiter=',')
9+
for m in data.mean(axis=1):
10+
print(m)
11+
12+
13+
if __name__ == '__main__':
14+
main()

code/readings_04.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import sys
2+
import numpy
3+
4+
5+
def main():
6+
script = sys.argv[0]
7+
action = sys.argv[1]
8+
filenames = sys.argv[2:]
9+
10+
for f in filenames:
11+
data = numpy.loadtxt(f, delimiter=',')
12+
13+
if action == '--min':
14+
values = data.min(axis=1)
15+
elif action == '--mean':
16+
values = data.mean(axis=1)
17+
elif action == '--max':
18+
values = data.max(axis=1)
19+
20+
for m in values:
21+
print(m)
22+
23+
24+
if __name__ == '__main__':
25+
main()

code/readings_05.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
import numpy
3+
4+
5+
def main():
6+
script = sys.argv[0]
7+
action = sys.argv[1]
8+
filenames = sys.argv[2:]
9+
assert action in ['--min', '--mean', '--max'], (
10+
'Action is not one of --min, --mean, or --max: ' + action)
11+
for f in filenames:
12+
process(f, action)
13+
14+
15+
def process(filename, action):
16+
data = numpy.loadtxt(filename, delimiter=',')
17+
18+
if action == '--min':
19+
values = data.min(axis=1)
20+
elif action == '--mean':
21+
values = data.mean(axis=1)
22+
elif action == '--max':
23+
values = data.max(axis=1)
24+
25+
for m in values:
26+
print(m)
27+
28+
29+
if __name__ == '__main__':
30+
main()

code/readings_06.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
import numpy
3+
4+
5+
def main():
6+
script = sys.argv[0]
7+
action = sys.argv[1]
8+
filenames = sys.argv[2:]
9+
assert action in ['--min', '--mean', '--max'], (
10+
'Action is not one of --min, --mean, or --max: ' + action)
11+
if len(filenames) == 0:
12+
process(sys.stdin, action)
13+
else:
14+
for f in filenames:
15+
process(f, action)
16+
17+
18+
def process(filename, action):
19+
data = numpy.loadtxt(filename, delimiter=',')
20+
21+
if action == '--min':
22+
values = data.min(axis=1)
23+
elif action == '--mean':
24+
values = data.mean(axis=1)
25+
elif action == '--max':
26+
values = data.max(axis=1)
27+
28+
for m in values:
29+
print(m)
30+
31+
32+
if __name__ == '__main__':
33+
main()

code/readings_07.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
import numpy
3+
4+
5+
def main():
6+
script = sys.argv[0]
7+
action = sys.argv[1]
8+
filenames = sys.argv[2:]
9+
assert action in ['-n', '-m', '-x'], (
10+
'Action is not one of -n, -m, or -x: ' + action)
11+
if len(filenames) == 0:
12+
process(sys.stdin, action)
13+
else:
14+
for f in filenames:
15+
process(f, action)
16+
17+
18+
def process(filename, action):
19+
data = numpy.loadtxt(filename, delimiter=',')
20+
21+
if action == '-n':
22+
values = data.min(axis=1)
23+
elif action == '-m':
24+
values = data.mean(axis=1)
25+
elif action == '-x':
26+
values = data.max(axis=1)
27+
28+
for m in values:
29+
print(m)
30+
31+
32+
if __name__ == '__main__':
33+
main()

code/readings_08.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import sys
2+
import numpy
3+
4+
5+
def main():
6+
script = sys.argv[0]
7+
if len(sys.argv) == 1: # no arguments, so print help message
8+
print('Usage: python readings_08.py action filenames\n'
9+
'action must be one of --min --mean --max\n'
10+
'if filenames is blank, input is taken from stdin;\n'
11+
'otherwise, each filename in the list of arguments\n'
12+
'is processed in turn')
13+
return
14+
15+
action = sys.argv[1]
16+
filenames = sys.argv[2:]
17+
assert action in ['--min', '--mean', '--max'], (
18+
'Action is not one of --min, --mean, or --max: ' + action)
19+
if len(filenames) == 0:
20+
process(sys.stdin, action)
21+
else:
22+
for f in filenames:
23+
process(f, action)
24+
25+
26+
def process(filename, action):
27+
data = numpy.loadtxt(filename, delimiter=',')
28+
29+
if action == '--min':
30+
values = data.min(axis=1)
31+
elif action == '--mean':
32+
values = data.mean(axis=1)
33+
elif action == '--max':
34+
values = data.max(axis=1)
35+
36+
for m in values:
37+
print(m)
38+
39+
40+
if __name__ == '__main__':
41+
main()

0 commit comments

Comments
 (0)