Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data-proc: Add contents for "python" section #43

Open
wants to merge 2 commits into
base: data-proc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
743 changes: 742 additions & 1 deletion chapters/data-proc/python.rst

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions chapters/data-proc/support/python/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python

"""
Print lines in file.
"""

import sys
import os


def usage(argv0):
print("Usage: {} <file>".format(argv0), file=sys.stderr)


def main():
if len(sys.argv) != 2:
usage(sys.argv[0])
sys.exit(1)

fname = sys.argv[1]
if not os.path.isfile(fname):
print("Argument {} is not a file.".format(fname), file=sys.stderr)
sys.exit(1)

with open(fname) as f:
lines = f.readlines()
for l in lines:
print(l.rstrip())


if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions chapters/data-proc/support/python/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env python3

print("Hello, World!")
13 changes: 13 additions & 0 deletions chapters/data-proc/support/python/hello_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python3

"""
Simple stupid program: print "Hello, World!" at standard output.
"""


def main():
print("Hello, World!")


if __name__ == "__main__":
main()
32 changes: 32 additions & 0 deletions chapters/data-proc/support/python/num_lines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python

"""
Print number of lines in file.
"""

import sys
import os


def usage(argv0):
print("Usage: {} <file>".format(argv0), file=sys.stderr)


def main():
if len(sys.argv) != 2:
usage(sys.argv[0])
sys.exit(1)

fname = sys.argv[1]
if not os.path.isfile(fname):
print("Argument {} is not a file.".format(fname), file=sys.stderr)
sys.exit(1)

with open(fname) as f:
lines = f.readlines()
num = len(lines)
print("Number of lines in {} is {}.".format(fname, num))


if __name__ == "__main__":
main()
7 changes: 7 additions & 0 deletions chapters/data-proc/support/python/sum100.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python

sum = 0
for i in range(1, 101):
sum += i

print("sum(1,100):", sum)
29 changes: 29 additions & 0 deletions chapters/data-proc/support/python/sum_n_arg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python

import sys


def usage(argv0):
print("Usage: {} <num>".format(argv0), file=sys.stderr)


def main():
if len(sys.argv) != 2:
usage(sys.argv[0])
sys.exit(1)

sum = 0
try:
n = int(sys.argv[1])
except ValueError:
print("Argument is not an integer.", file=sys.stderr)
sys.exit(1)

for i in range(1, n+1):
sum += i

print("sum(1,{}): {}".format(n, sum))


if __name__ == "__main__":
main()
32 changes: 32 additions & 0 deletions chapters/data-proc/support/python/tac.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python

"""
Print lines in file in reverse.
"""

import sys
import os


def usage(argv0):
print("Usage: {} <file>".format(argv0), file=sys.stderr)


def main():
if len(sys.argv) != 2:
usage(sys.argv[0])
sys.exit(1)

fname = sys.argv[1]
if not os.path.isfile(fname):
print("Argument {} is not a file.".format(fname), file=sys.stderr)
sys.exit(1)

with open(fname) as f:
lines = f.readlines()[::-1]
for l in lines:
print(l.rstrip())


if __name__ == "__main__":
main()