From 12f3b2382f19aad9a4b9dd4229cfc23921676af6 Mon Sep 17 00:00:00 2001 From: IgorZakrocki Date: Tue, 19 Dec 2023 01:39:55 +0100 Subject: [PATCH] main.py and functions.py corrected --- README.md | 14 ++++++++++++++ file157.py | 30 ------------------------------ file87.py | 35 ----------------------------------- functions.py | 42 ++++++++++++++++++++++++++++++++++++++++++ main.py | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 65 deletions(-) delete mode 100644 file157.py delete mode 100644 file87.py create mode 100644 functions.py create mode 100644 main.py diff --git a/README.md b/README.md index 5fdbab8..231625c 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,17 @@ Your task is to clone this repository, create a new branch, fix all the issues and bad practises, add file with list of all the changes and explanations why they are wrong. Then create a pull request with your branch, add your teacher as reviewer. (As this project is full of bad practises please do not use it as an example. It's for educational purposes only.) + +1. Global variables in file87.py +2. Import in weird places in file157.py +3. Too long and unreadable variable names in the 157.py file, which describe too detailed the operation of the variables +4. Unintuitive files name +5. Variable name like "b = 0, c = 1" can only by if we provide a descriptions. +6. Unnesserly fragment: yield 'finished' +7. "__init__" in class PiContainer have to by declarate in this way: + "def __init__(self, a = None): + self.a = a if a is not None else []" +, because we will by able to create new memory space for the new object. +8. More description, the best to use English language +9. When we imprt something the best way is to define what we use from the library +10. Use 'pathlib' to manipulate paths, as this will reduce the chance of errors when opening our program in another OS diff --git a/file157.py b/file157.py deleted file mode 100644 index 649c872..0000000 --- a/file157.py +++ /dev/null @@ -1,30 +0,0 @@ -def enumerate(pi): - for hello in pi.a: - print(f'Next element: {hello}') -from file87 import * -try: - my_pi = pi_container() - pi_gen = foo(5) - my_pi.mth(pi_gen.__next__()) - my_pi.mth(pi_gen.__next__()) - my_pi.mth(pi_gen.__next__()) - my_pi.mth(pi_gen.__next__()) - my_pi_2 = pi_container() - my_pi.mth(pi_gen.__next__()) - my_pi.mth(pi_gen.__next__()) - my_pi.mth(pi_gen.__next__()) -except: - print('something went horribly wrong :(') -pIgEn3 = foo(194) -for the_variable_that_contains_next_approximations_of_pi_from_generator in range(23): - my_pi_2.mth(next(pIgEn3)) -my_pi_3 = pi_container() -pi_gen = foo(6) -my_pi_3.mth([i for i in list(pi_gen)]) -print('my first pi') -enumerate(my_pi) -print('my second pi') -enumerate(my_pi_2) -new_file = open('some-file.txt', 'w') -new_file.write(f'my best pi: {my_pi_3.a[-1]}') -new_file.close() diff --git a/file87.py b/file87.py deleted file mode 100644 index 40e209e..0000000 --- a/file87.py +++ /dev/null @@ -1,35 +0,0 @@ -b = 0 -c = 0 - - -class pi_container: - def __init__(self, a=list()): - self.a = a - - def mth(self, x): - if type(x) == list: - self.a += x - else: - self.a.append(x) - - -def foo(x): - global b - global c - b = 0 - c=1 - for hello in range(x): - if hello % 2 == 0: - b += 4 / c#this is a very important operation in calculateing pi according to documentation that is provided in a seperate file in this repository, please analyse this file before using! - else: - b -= 4 / c - c += 2 - yield b - yield 'finished' - -def enumerate(pi: pi_container): - for hello in pi.a: - print(hello) - - -print('All functions are defined') diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..28b4130 --- /dev/null +++ b/functions.py @@ -0,0 +1,42 @@ +class PiContainer: + def __init__(self, a = None): + self.a = a if a is not None else [] + + def mth(self, x): + if isinstance(x, list): + self.a += x + else: + self.a.append(x) + +# Calculate pi value +def calculatePi(x): + + ''' + Wzór: + b: Opis zmiennej b + c: Opis zmiennej c + + miejsce na wzór + + dodatkowe informacje + ''' + + b = 0 + c = 1 + for hello in range(x): + if hello % 2 == 0: + b += 4 / c + else: + b -= 4 / c + c += 2 + yield b + +# Printing element with preamble +def printElements(pi): + for x in pi.a: + print(f"Next element: {x}") + +# Printing only values +def enumarate(pi): + for x in pi.a: + print(x) diff --git a/main.py b/main.py new file mode 100644 index 0000000..4e3a755 --- /dev/null +++ b/main.py @@ -0,0 +1,36 @@ +from functions import calculatePi, PiContainer, enumarate, printElements +import pathlib as Path + +if __name__ == "__main__": + try: + myPi01 = PiContainer() + myPi02 = PiContainer() + myPi03 = PiContainer() + piGen01 = calculatePi(5) + + for _ in range(5): + myPi01.mth(next(piGen01)) + + except Exception as e: + print(f'Something went horribly wrong: {e}') + + piGen02 = calculatePi(194) + + for _ in range(23): + myPi02.mth(next(piGen02)) + + piGen03 = calculatePi(6) + myPi03.mth([i for i in list(piGen03)]) + + print(f'My first pi: {printElements(myPi01)}') + print(f'My second pi: {enumarate(myPi02)}') + print(f'My third pi: {myPi03.a}') + + # Specify the file path + file_path = Path('some-file.txt') + + # Writing to a file + with file_path.open('w') as new_file: + new_file.write(f'My best pi: {myPi03.a[-2]}') + +