From 1db776ff5b109248dfdf4d2d3a0b3930d817a3fe Mon Sep 17 00:00:00 2001 From: Youngjin Han Date: Sat, 23 May 2020 00:10:26 +0900 Subject: [PATCH] Learning Circle : python #3 - ex15 ~ ex21 --- youngjin.han/learning-node02-2020.org | 6 ++ youngjin.han/python-the-hard-way/ex15.py | 19 ++++++ .../python-the-hard-way/ex15_sample.txt | 3 + youngjin.han/python-the-hard-way/ex16.py | 41 ++++++++++++ .../python-the-hard-way/ex16_file_write.txt | 3 + .../ex16_file_write_multiple_line.txt | 3 + youngjin.han/python-the-hard-way/ex17.py | 28 +++++++++ .../python-the-hard-way/ex17_new2_test.txt | 1 + .../python-the-hard-way/ex17_new3_test.txt | 1 + .../python-the-hard-way/ex17_new_test.txt | 1 + .../python-the-hard-way/ex17_test.txt | 1 + youngjin.han/python-the-hard-way/ex18.py | 23 +++++++ youngjin.han/python-the-hard-way/ex19.py | 63 +++++++++++++++++++ youngjin.han/python-the-hard-way/ex20.py | 35 +++++++++++ .../python-the-hard-way/ex20_test.txt | 3 + youngjin.han/python-the-hard-way/ex21.py | 33 ++++++++++ youngjin.han/python-the-hard-way/ex22.py | 22 +++++++ youngjin.han/python-the-hard-way/test.txt | 3 + youngjin.han/python.org | 30 ++++++++- 19 files changed, 318 insertions(+), 1 deletion(-) create mode 100644 youngjin.han/python-the-hard-way/ex15.py create mode 100644 youngjin.han/python-the-hard-way/ex15_sample.txt create mode 100644 youngjin.han/python-the-hard-way/ex16.py create mode 100644 youngjin.han/python-the-hard-way/ex16_file_write.txt create mode 100644 youngjin.han/python-the-hard-way/ex16_file_write_multiple_line.txt create mode 100644 youngjin.han/python-the-hard-way/ex17.py create mode 100644 youngjin.han/python-the-hard-way/ex17_new2_test.txt create mode 100644 youngjin.han/python-the-hard-way/ex17_new3_test.txt create mode 100644 youngjin.han/python-the-hard-way/ex17_new_test.txt create mode 100644 youngjin.han/python-the-hard-way/ex17_test.txt create mode 100644 youngjin.han/python-the-hard-way/ex18.py create mode 100644 youngjin.han/python-the-hard-way/ex19.py create mode 100644 youngjin.han/python-the-hard-way/ex20.py create mode 100644 youngjin.han/python-the-hard-way/ex20_test.txt create mode 100644 youngjin.han/python-the-hard-way/ex21.py create mode 100644 youngjin.han/python-the-hard-way/ex22.py create mode 100644 youngjin.han/python-the-hard-way/test.txt diff --git a/youngjin.han/learning-node02-2020.org b/youngjin.han/learning-node02-2020.org index 687a0c3..2e3e91b 100644 --- a/youngjin.han/learning-node02-2020.org +++ b/youngjin.han/learning-node02-2020.org @@ -1,3 +1,9 @@ +* 2020-05-22 +**** TODO Lecture content + - Same structure as "Python #2" + - Exercises 15-22 +**** Lecture material + - Available on https://cloud.ungleich.ch/s/435FyfrQyEq6oF3 * 2020-05-20 *** Python #2: **** DONE Lecture content diff --git a/youngjin.han/python-the-hard-way/ex15.py b/youngjin.han/python-the-hard-way/ex15.py new file mode 100644 index 0000000..0104af1 --- /dev/null +++ b/youngjin.han/python-the-hard-way/ex15.py @@ -0,0 +1,19 @@ +from sys import argv # include argv + +script, filename = argv # define a argument + +txt = open(filename) # open the file + +print(f"Here's your file {filename}:") # print a script +print(txt.read()) # read and print the text from the file + +txt.close() + +print("Type the filename again:") # print a script +file_again = input("> ") # input from stdin + +txt_again = open(file_again) # open the file + +print(txt_again.read()) # read and print the text from the file + +txt_again.close() diff --git a/youngjin.han/python-the-hard-way/ex15_sample.txt b/youngjin.han/python-the-hard-way/ex15_sample.txt new file mode 100644 index 0000000..f4a96d5 --- /dev/null +++ b/youngjin.han/python-the-hard-way/ex15_sample.txt @@ -0,0 +1,3 @@ +This is stuff I typed into a file. +It is really cool stuff. +Lots and lots of fun to have in here. diff --git a/youngjin.han/python-the-hard-way/ex16.py b/youngjin.han/python-the-hard-way/ex16.py new file mode 100644 index 0000000..52f01f6 --- /dev/null +++ b/youngjin.han/python-the-hard-way/ex16.py @@ -0,0 +1,41 @@ +from sys import argv + +script, filename = argv + +print(f"We're going to erase {filename}.") +print("If you don't want that, hit CTRL-C (^C).") +print("If you do want that, hit RETURN.") + +input("?") + +print("Opening the file...") +target = open(filename, 'w') + +print("Truncating the file. Goodbye!") +target.truncate() + +print("Now I'm going to ask you for three lines.") + +line1 = input("line 1: ") +line2 = input("line 2: ") +line3 = input("line 3: ") + +print("I'm going to write these to the file.") + +#target.write(line1) +#target.write("\n") +#target.write(line2) +#target.write("\n") +#target.write(line3) +#target.write("\n") +target.write(f"""{line1} +{line2} +{line3} +""" +) + +print("And finally, we close it.") +target.close() + +txt = open(filename) +print(txt.read()) diff --git a/youngjin.han/python-the-hard-way/ex16_file_write.txt b/youngjin.han/python-the-hard-way/ex16_file_write.txt new file mode 100644 index 0000000..274ed65 --- /dev/null +++ b/youngjin.han/python-the-hard-way/ex16_file_write.txt @@ -0,0 +1,3 @@ +1234 +5678 +90-= diff --git a/youngjin.han/python-the-hard-way/ex16_file_write_multiple_line.txt b/youngjin.han/python-the-hard-way/ex16_file_write_multiple_line.txt new file mode 100644 index 0000000..83db48f --- /dev/null +++ b/youngjin.han/python-the-hard-way/ex16_file_write_multiple_line.txt @@ -0,0 +1,3 @@ +line1 +line2 +line3 diff --git a/youngjin.han/python-the-hard-way/ex17.py b/youngjin.han/python-the-hard-way/ex17.py new file mode 100644 index 0000000..905a372 --- /dev/null +++ b/youngjin.han/python-the-hard-way/ex17.py @@ -0,0 +1,28 @@ +from sys import argv +from os.path import exists +import shutil + +script, from_file, to_file = argv + +print(f"Copying from {from_file} to {to_file}") + +#open(to_file, 'w').write(open(from_file).read()) +shutil.copyfile(from_file, to_file) + +# we could do these two on one line, how? +#in_file = open(from_file) +#indata = in_file.read() + +#print(f"The input file is {len(indata)} bytes long") + +#print(f"Does the output file exist? {exists(to_file)}") +#print("Ready, hit RETURN to continue, CTRL-C to abort.") +#input() + +#out_file = open(to_file, 'w') +#out_file.write(indata) + +print("Alright, all done.") + +#out_file.close() +#in_file.close() diff --git a/youngjin.han/python-the-hard-way/ex17_new2_test.txt b/youngjin.han/python-the-hard-way/ex17_new2_test.txt new file mode 100644 index 0000000..6de7b8c --- /dev/null +++ b/youngjin.han/python-the-hard-way/ex17_new2_test.txt @@ -0,0 +1 @@ +This is a test file. diff --git a/youngjin.han/python-the-hard-way/ex17_new3_test.txt b/youngjin.han/python-the-hard-way/ex17_new3_test.txt new file mode 100644 index 0000000..6de7b8c --- /dev/null +++ b/youngjin.han/python-the-hard-way/ex17_new3_test.txt @@ -0,0 +1 @@ +This is a test file. diff --git a/youngjin.han/python-the-hard-way/ex17_new_test.txt b/youngjin.han/python-the-hard-way/ex17_new_test.txt new file mode 100644 index 0000000..6de7b8c --- /dev/null +++ b/youngjin.han/python-the-hard-way/ex17_new_test.txt @@ -0,0 +1 @@ +This is a test file. diff --git a/youngjin.han/python-the-hard-way/ex17_test.txt b/youngjin.han/python-the-hard-way/ex17_test.txt new file mode 100644 index 0000000..6de7b8c --- /dev/null +++ b/youngjin.han/python-the-hard-way/ex17_test.txt @@ -0,0 +1 @@ +This is a test file. diff --git a/youngjin.han/python-the-hard-way/ex18.py b/youngjin.han/python-the-hard-way/ex18.py new file mode 100644 index 0000000..fd0d0d3 --- /dev/null +++ b/youngjin.han/python-the-hard-way/ex18.py @@ -0,0 +1,23 @@ +# this one is like your scripts with argv +def print_two(*args): + arg1, arg2 = args + print(f'arg1: {arg1}, arg2: {arg2}') + +# ok, that *args is actually pointless, we can just do this +def print_two_again(arg1, arg2): + print(f"arg1: {arg1}, arg2: {arg2}") + +# this just takes one argument +def print_one(arg1): + print(f"arg1: {arg1}") + +# this one takes no arguments +def print_none(): + print("I got nothin'.") + + +print_two("Zed","Shaw") +print_two_again("Zed","Shaw") +print_one("First!") + +print_none() diff --git a/youngjin.han/python-the-hard-way/ex19.py b/youngjin.han/python-the-hard-way/ex19.py new file mode 100644 index 0000000..693b332 --- /dev/null +++ b/youngjin.han/python-the-hard-way/ex19.py @@ -0,0 +1,63 @@ +def cheese_and_crackers(cheese_count, boxes_of_crackers): # define a function + print(f"You have {cheese_count} cheeses!") # print cheese_count + print(f"You have {boxes_of_crackers} boxes of crackers!") # print boxex_of_crackers + print("Man that's enough for a party!") # print a script + print("Get a blanket.\n") # print a script + +def my_f(arg1, arg2): + total = arg1 + arg2 + print(f"arg1: {arg1}, arg2: {arg2}, total: {total}") + +print("We can just give the function numbers directly:") # print a script +cheese_and_crackers(20, 30) # call the function + + +print("OR, we can use variables from our script:") # print a script +amount_of_cheese = 10 # define a value +amount_of_crackers = 50 # define a value + +cheese_and_crackers(amount_of_cheese, amount_of_crackers) # call the function + + +print("We can even do math inside too:") # print a script +cheese_and_crackers(10 + 20, 5 + 6) # call the function + + +print("And we can combine the two, variables and math:") # print a script +cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000) # call the function + +my_f(1, 2) +my_f(2*4, 8/2) +my_f("2*4", "8/2") +my_f("not", "bad") + +n_arg1 = 1 +n_arg2 = 2 + +my_f(n_arg1, n_arg2) + +s_arg1 = "good" +s_arg2 = "luck" + +my_f(s_arg1, s_arg2) + +t_arg1 = 8/2+3 +t_arg2 = 8%2 + +my_f(t_arg1, t_arg2) + + +m_a1 = """line1 +line2 +line3 +""" +m_a2 = """line4 +line5 +line6 +""" +my_f(m_a1, m_a2) + +my_f(open("test.txt").read(), open("test.txt").read()) + +f = "{} {}" +my_f(f.format(1, 2), f.format(3, 4)) diff --git a/youngjin.han/python-the-hard-way/ex20.py b/youngjin.han/python-the-hard-way/ex20.py new file mode 100644 index 0000000..8f27b84 --- /dev/null +++ b/youngjin.han/python-the-hard-way/ex20.py @@ -0,0 +1,35 @@ +from sys import argv # import argv + +script, input_file = argv # define argv + +def print_all(f): # define a function + print(f.read()) # print all + +def rewind(f): # define a function + f.seek(0) # jump the starting point of a file + +def print_a_line(line_count, f): # define a function + print(line_count, f.readline()) # pinrt a line on the file + +current_file = open(input_file) # open a file + +print("First let's print the whole file:\n") # print a script + +print_all(current_file) # call the function + +print("Now let's rewind, kind of like a tape.") # print a script + +rewind(current_file) # call the function + +print("Let's print three lines:") # print a script + +current_line = 1 # define a value +print_a_line(current_line, current_file) # call the function + +#current_line = current_line + 1 # define a value +current_line += 1 +print_a_line(current_line, current_file) # call the function + +#current_line = current_line + 1 # define a value +current_line += 1 +print_a_line(current_line, current_file) # call the function diff --git a/youngjin.han/python-the-hard-way/ex20_test.txt b/youngjin.han/python-the-hard-way/ex20_test.txt new file mode 100644 index 0000000..e97bf0c --- /dev/null +++ b/youngjin.han/python-the-hard-way/ex20_test.txt @@ -0,0 +1,3 @@ +This is line 1 +This is line 2 +This is line 3 diff --git a/youngjin.han/python-the-hard-way/ex21.py b/youngjin.han/python-the-hard-way/ex21.py new file mode 100644 index 0000000..c7cfd05 --- /dev/null +++ b/youngjin.han/python-the-hard-way/ex21.py @@ -0,0 +1,33 @@ +def add(a, b): + print(f"ADDING {a} + {b}") + return a + b + +def subtract(a, b): + print(f"SUBTRACTING {a} - {b}") + return a - b + +def multiply(a, b): + print(f"MULTIPLYING {a} * {b}") + return a * b - b + +def divide(a, b): + print(f"DIVIDING {a} / {b}") + return a / b + + +print("Let's do some math with just functions!") + +age = add(30, 5) +height = subtract(78, 4) +weight = multiply(90, 2) +iq = divide(100, 2) + +print(f"Age: {age}, Height: {height}, Weight: {weight}, IQ: {iq}") + + +# A puzzle for the extra credit, type it in anyway. +print("Here is a puzzle.") + +what = add(age, subtract(height, multiply(weight, divide(iq, 2)))) + +print("That becomes: ", what, "Can you do it by hand?") diff --git a/youngjin.han/python-the-hard-way/ex22.py b/youngjin.han/python-the-hard-way/ex22.py new file mode 100644 index 0000000..3a9d696 --- /dev/null +++ b/youngjin.han/python-the-hard-way/ex22.py @@ -0,0 +1,22 @@ +import sys +script, input_encoding, error = sys.argv + +def main(language_file, encoding, errors): +line = language_file.readline() + +if line: +print_line(line, encoding, errors) +return main(language_file, encoding, errors) + + +def print_line(line, encoding, errors): +next_lang = line.strip() +raw_bytes = next_lang.encode(encoding, errors=errors) +cooked_string = raw_bytes.decode(encoding, errors=errors) + +print(raw_bytes, "<===>", cooked_string) + + +languages = open("languages.txt", encoding="utf-8") + +main(languages, input_encoding, error) diff --git a/youngjin.han/python-the-hard-way/test.txt b/youngjin.han/python-the-hard-way/test.txt new file mode 100644 index 0000000..e3188be --- /dev/null +++ b/youngjin.han/python-the-hard-way/test.txt @@ -0,0 +1,3 @@ +Mary had a little lamb +Its fleece was white as snow +It was also tasty diff --git a/youngjin.han/python.org b/youngjin.han/python.org index e13dc10..65342a9 100644 --- a/youngjin.han/python.org +++ b/youngjin.han/python.org @@ -1,3 +1,32 @@ +* 2020-05-22 +** note + - ex15.py + - The argument method is better than using stdin. becouse bash-completion could be used. + - python3.8 shell + - >>> filename=input() + - ex15_sample.txt + - >>> txt = open(filename) + - >>> txt_script = txt.read() + - >>> print(txt_script) + - This is stuff I typed into a file. + - It is really cool stuff. + - Lots and lots of fun to have in here + - ex16.py + - 'w' is to open a file on write mode. + - If a file is openned with 'w', target.truncate() should not be need. the 'w' mode always overwrite a file. + - ex17.py + - cat is concatenate files to stdout + - If file.close() is not called, the file which is openned by python chould not be modified on orther programs. + - ex18.py + - none + - ex19.py + - none + - ex20.py + - file.seek is to move to new file posotion + - ex21.py + - + - ex22.py + - * 2020-05-20 ** note - ex8.py @@ -15,7 +44,6 @@ - none - ex14.py - none - * 2020-05-18 ** note - ex1.py