From dd4140f687749acbd70a5e84cc8fea1275a682b4 Mon Sep 17 00:00:00 2001 From: llnu Date: Mon, 1 Jun 2020 16:55:04 +0200 Subject: [PATCH] * balazs/python-calculator/calc.py: --- balazs/orgfiles/cdistweek_05-11.org | 22 +++++++++++ balazs/{ => orgfiles}/notes-05-01 | 0 balazs/{ => orgfiles}/todo | 0 balazs/python-calculator/calc.py | 60 +++++++++++++++++++++++++++++ balazs/python-calculator/notes.org | 44 +++++++++++++++++++++ 5 files changed, 126 insertions(+) create mode 100644 balazs/orgfiles/cdistweek_05-11.org rename balazs/{ => orgfiles}/notes-05-01 (100%) rename balazs/{ => orgfiles}/todo (100%) create mode 100644 balazs/python-calculator/calc.py create mode 100644 balazs/python-calculator/notes.org diff --git a/balazs/orgfiles/cdistweek_05-11.org b/balazs/orgfiles/cdistweek_05-11.org new file mode 100644 index 0000000..7979fe7 --- /dev/null +++ b/balazs/orgfiles/cdistweek_05-11.org @@ -0,0 +1,22 @@ +*** cdist #5: Generating Code & Exploring +**** Lecture content +***** Objective + - Understand how to *generate code* and when to use it + - Exploring explorers +***** Code generation steps + - Modify the previously created type *__my_nginx_site* + - Read about cdist messaging + - If there was a change in the nginx configuration file, reload nginx +***** Explorer steps + - Modify the *__my_nginx_site* type to use the *os explorer* + - Adjust your type to work on Alpine Linux and Debian (or two + other Linux distributions of your choice) + - Set the nginx configuration directory accordingly +***** Documentation + - Explain the following in your cdist.org file + - What is the difference between gencode-remote and the remote code? + - What is the difference between gencode-local and gencode-remote? + - Locate a type that comes with upstream cdist that uses + gencode-local - which one is it? Why does it need gencode-local? + +**** Client notes diff --git a/balazs/notes-05-01 b/balazs/orgfiles/notes-05-01 similarity index 100% rename from balazs/notes-05-01 rename to balazs/orgfiles/notes-05-01 diff --git a/balazs/todo b/balazs/orgfiles/todo similarity index 100% rename from balazs/todo rename to balazs/orgfiles/todo diff --git a/balazs/python-calculator/calc.py b/balazs/python-calculator/calc.py new file mode 100644 index 0000000..0ed38e0 --- /dev/null +++ b/balazs/python-calculator/calc.py @@ -0,0 +1,60 @@ +import sys +filename = sys.argv + +def input_and_calculate_one_line(): + printable_list = [] + + input_numbers = input("""Enter the numbers for summing (seperated by space), or enter "q" to exit. +""") +# print (input_numbers) + + if input_numbers == "q": + return "" + else: + splitting_converting_summing_printing(input_numbers) + +def splitting_converting_summing_printing(input_numbers): + split_numbers = input_numbers.split() + print (split_numbers) + + converted_list = list(map(int, split_numbers)) + print (converted_list) + + summed_numbers = sum(converted_list) + print (summed_numbers) + + + printable_list = intersperse(converted_list, "+") + + list_with_strings = convertListElementsToStr(printable_list) + + converted_to_strings_list = listToString(list_with_strings) + print (converted_to_strings_list, "=", summed_numbers) + + +# for i in converted_list: + +# printable_list.append(i) +# printable_list.append("+") +# printable_list += str(i) +# printable_list += '+' + +def intersperse(lst, item): + result = [item] * (len(lst) * 2 - 1) + result[0::2] = lst + return result + +def convertListElementsToStr(list): +# how_long = len +# result = [] + result = map(str, list) +# result.append[str(i) for i in list] + return result + +def listToString(s): + str1 = " " + return (str1.join(s)) + + +input_and_calculate_one_line() + diff --git a/balazs/python-calculator/notes.org b/balazs/python-calculator/notes.org new file mode 100644 index 0000000..334a10e --- /dev/null +++ b/balazs/python-calculator/notes.org @@ -0,0 +1,44 @@ +*** Python applying learnings from 1..6 +**** Lecture notes + - Previous topics covered: + - Printing + - Formatting + - Variables + - Escape Sequences + - Inputting text + - Reading arguments / using argv + - Reading files + - Defining methods + - Boolean logic + - Branching using if/else/elif + - Loops: for/while + - Today we write a calculator that saves results in a file in python + - How it works in general + You read the input until you read a line that only contains a + "q". Every input line consists of numbers separated by a + space. For instance "4 5 9". You will need to .split() the + input. + - Steps + - Create a python script named "calc.py" + - It takes 1 command line argument (argv), which is the filename + - We will store the calculations *and* results in this file + - Create a method named "input_and_calculate_one_line" + - It does not have any arguments + - It reads one line via *input* + - It splits the input (let's say "4 5 9" => [ "4", "5", "9") ]) + - It calculates the result (f.i. 4+5+9 = 18) and stores it in + a variable (use *sum* over the *list*) + - It returns a string of the format "4 + 5 + 9 = 18" + - If the line only contains a "q" it return "" (an empty string) + - Create a method named "editor" that takes a filename as an argument + - It opens the file for writing + - It uses input_and_calculate_one_line in a while loop + - while the return result is not "", we append the string to + the file + - When the return result is "", the function exits + +*** State: +input_and_calculate_one_line is complete, however it's probably a garbage implementation. +Spent too much time on converting back and forth and getting the output right. + +Editor (file io) is not complete yet