From 968d3c55e6a7d9c8dab2445e076449db25b1492a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C4=8Cuba?= Date: Mon, 10 Jan 2022 21:18:05 +0100 Subject: [PATCH 1/9] gitignore project files and python cache --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..1c9db0cd --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +__pycache__ \ No newline at end of file From 368386f50c9bb98ec8a94daca1d5f43efd7cba9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C4=8Cuba?= Date: Mon, 10 Jan 2022 21:18:23 +0100 Subject: [PATCH 2/9] Base classes for number systems. --- number_system.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 number_system.py diff --git a/number_system.py b/number_system.py new file mode 100644 index 00000000..98ac0298 --- /dev/null +++ b/number_system.py @@ -0,0 +1,57 @@ +from abc import ABC, abstractmethod + +""" + Abstract class to custom number systems. +""" + + +class NumberSystem(ABC): + + """ + Abstract method to convert decimal number into number system given class provides. + """ + @abstractmethod + def convert_decimal(self, number): + pass + + +""" + Class representing binary number system. +""" + + +class BinarySystem(NumberSystem): + + """ + Method to convert decimal number into binary one. + """ + def convert_decimal(self, number): + return bin(number) + + +""" + Class representing octal number system. +""" + + +class OctalSystem(NumberSystem): + + """ + Method to convert decimal number into octal one. + """ + def convert_decimal(self, number): + return oct(number) + + +""" + Class representing hexadecimal number system. +""" + + +class HexSystem(NumberSystem): + + """ + Method to convert decimal number into hex one. + """ + def convert_decimal(self, number): + return hex(number) From 007293eba18310d2514bccbb12a88742823d99db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C4=8Cuba?= Date: Mon, 10 Jan 2022 21:18:47 +0100 Subject: [PATCH 3/9] Application class + init script --- __init__.py | 5 +++ app.py | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 __init__.py create mode 100644 app.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 00000000..014e6bff --- /dev/null +++ b/__init__.py @@ -0,0 +1,5 @@ +from app import App + +if __name__ == "__main__": + application = App() + application.run() \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 00000000..019ac82b --- /dev/null +++ b/app.py @@ -0,0 +1,97 @@ +from number_system import * + +""" +Base App class to run this application. +""" + + +class App: + + """ + Base function to run this app. + """ + + def run(self): + App.print_help() + + input_number = self.parse_input_number() + + if input_number is None: + print("Program end requested. Exiting...") + return + + target_number_system = self.parse_target_number_system() + + print(target_number_system.convert_decimal(input_number)) + + """ + Method to handle user input. Return valid integer or None if program end is wanted. + """ + + def parse_input_number(self): + number = input("Please pass unsigned integer: ") + + try: + if self.is_ending(number): + return None + + number = int(number) + + if number < 0: + raise ValueError + + except ValueError: + print("Not a valid unsigned integer! Please try once again.") + return self.parse_input_number() + + return number + + """ + Method to handle user target system input. + """ + def parse_target_number_system(self): + target_system = input("Please choose target number system: ") + + try: + if self.is_ending(target_system): + return None + + target_system = int(target_system) + + if target_system not in [1, 2, 3]: + raise ValueError + except ValueError: + print("Not a valid target system! Please try once again.") + return self.parse_target_number_system() + + if target_system == 1: + return BinarySystem() + + if target_system == 2: + return OctalSystem() + + return HexSystem() + + """ + Method to check ending string + """ + @staticmethod + def is_ending(char): + if char == "#": + return True + + return False + + """ + Function which prints help text to understand, how to use this app. + """ + + @staticmethod + def print_help(): + print("You can convert numbers from decimal system to binary, octal or hex systems.") + print("At first you need to pass number in decimal system.") + print("Then you need to choose target number system by selecting number.") + print("1. Binary") + print("2. Octal") + print("3. Hex") + print("At final enjoy your result.") From 8872f0cb4454bfa081fcfdbe8f20e920ae5f7cfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C4=8Cuba?= Date: Mon, 10 Jan 2022 21:19:50 +0100 Subject: [PATCH 4/9] remove example --- example/__init__.py | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 example/__init__.py diff --git a/example/__init__.py b/example/__init__.py deleted file mode 100644 index 3afa75c5..00000000 --- a/example/__init__.py +++ /dev/null @@ -1,19 +0,0 @@ -"""This is the "example" module. - -The example module supplies one function: compute(). For example, - ->>> compute(3) -3 -""" - - -def compute(x): - """Functon compute returns evaluation of expression using argument x. - - Args: - - x - Input of the function - - Returns: - - output - Output of the function - """ - return x * x - 2 * x From 6690ab56db10bb060705be9d12cae3ff1a490a66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C4=8Cuba?= Date: Mon, 10 Jan 2022 21:33:05 +0100 Subject: [PATCH 5/9] Base image with deps. --- Dockerfile | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..02c49590 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.8.10-alpine + +WORKDIR /app + +RUN pip install flake8 flake8-docstrings pytest pytest-cov \ No newline at end of file From 4dde5a279b58afd1fcce9c731e008e8d45e0b769 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C4=8Cuba?= Date: Mon, 10 Jan 2022 21:53:13 +0100 Subject: [PATCH 6/9] Install autopep8 to handle some flake8 errors --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 02c49590..ae7ac2d6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,4 +2,4 @@ FROM python:3.8.10-alpine WORKDIR /app -RUN pip install flake8 flake8-docstrings pytest pytest-cov \ No newline at end of file +RUN pip install flake8 flake8-docstrings pytest pytest-cov autopep8 \ No newline at end of file From 40a09dadcfc51bf1a3bda0a13bccafb21fccfa74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C4=8Cuba?= Date: Mon, 10 Jan 2022 21:56:17 +0100 Subject: [PATCH 7/9] autopep8 fixes.. not too many fixes :-( --- __init__.py | 2 +- app.py | 4 +++- number_system.py | 5 ++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/__init__.py b/__init__.py index 014e6bff..5737c786 100644 --- a/__init__.py +++ b/__init__.py @@ -2,4 +2,4 @@ if __name__ == "__main__": application = App() - application.run() \ No newline at end of file + application.run() diff --git a/app.py b/app.py index 019ac82b..635aed43 100644 --- a/app.py +++ b/app.py @@ -49,6 +49,7 @@ def parse_input_number(self): """ Method to handle user target system input. """ + def parse_target_number_system(self): target_system = input("Please choose target number system: ") @@ -88,7 +89,8 @@ def is_ending(char): @staticmethod def print_help(): - print("You can convert numbers from decimal system to binary, octal or hex systems.") + print( + "You can convert numbers from decimal system to binary, octal or hex systems.") print("At first you need to pass number in decimal system.") print("Then you need to choose target number system by selecting number.") print("1. Binary") diff --git a/number_system.py b/number_system.py index 98ac0298..02ecde20 100644 --- a/number_system.py +++ b/number_system.py @@ -1,7 +1,7 @@ from abc import ABC, abstractmethod """ - Abstract class to custom number systems. + Abstract class to custom number systems. """ @@ -25,6 +25,7 @@ class BinarySystem(NumberSystem): """ Method to convert decimal number into binary one. """ + def convert_decimal(self, number): return bin(number) @@ -39,6 +40,7 @@ class OctalSystem(NumberSystem): """ Method to convert decimal number into octal one. """ + def convert_decimal(self, number): return oct(number) @@ -53,5 +55,6 @@ class HexSystem(NumberSystem): """ Method to convert decimal number into hex one. """ + def convert_decimal(self, number): return hex(number) From ccea8d8066f8a0e7177e80a88ffcff39ab96dacd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C4=8Cuba?= Date: Mon, 10 Jan 2022 22:15:57 +0100 Subject: [PATCH 8/9] Fix those damn flake8 code style errors. --- __init__.py | 1 + app.py | 40 ++++++++++++++-------------------------- number_system.py | 44 ++++++++++---------------------------------- 3 files changed, 25 insertions(+), 60 deletions(-) diff --git a/__init__.py b/__init__.py index 5737c786..87d48a16 100644 --- a/__init__.py +++ b/__init__.py @@ -1,3 +1,4 @@ +"""Initialization script for number system conversion app.""" from app import App if __name__ == "__main__": diff --git a/app.py b/app.py index 635aed43..3267f44b 100644 --- a/app.py +++ b/app.py @@ -1,17 +1,13 @@ -from number_system import * +"""Base App class to run this application.""" -""" -Base App class to run this application. -""" +from number_system import BinarySystem, OctalSystem, HexSystem class App: - - """ - Base function to run this app. - """ + """Application class to run this app.""" def run(self): + """Init function to run whole app.""" App.print_help() input_number = self.parse_input_number() @@ -24,11 +20,8 @@ def run(self): print(target_number_system.convert_decimal(input_number)) - """ - Method to handle user input. Return valid integer or None if program end is wanted. - """ - def parse_input_number(self): + """Handle user number input.""" number = input("Please pass unsigned integer: ") try: @@ -46,11 +39,8 @@ def parse_input_number(self): return number - """ - Method to handle user target system input. - """ - def parse_target_number_system(self): + """Handle user target system input.""" target_system = input("Please choose target number system: ") try: @@ -73,26 +63,24 @@ def parse_target_number_system(self): return HexSystem() - """ - Method to check ending string - """ @staticmethod def is_ending(char): + """Check ending string.""" if char == "#": return True return False - """ - Function which prints help text to understand, how to use this app. - """ - @staticmethod def print_help(): + """Print help text.""" print( - "You can convert numbers from decimal system to binary, octal or hex systems.") - print("At first you need to pass number in decimal system.") - print("Then you need to choose target number system by selecting number.") + "You can convert numbers from decimal system to " + + "binary, octal or hex systems.") + print("At first you need to pass number" + + " in decimal system.") + print("Then you need to choose target number " + + "system by selecting number.") print("1. Binary") print("2. Octal") print("3. Hex") diff --git a/number_system.py b/number_system.py index 02ecde20..ec010a4b 100644 --- a/number_system.py +++ b/number_system.py @@ -1,60 +1,36 @@ -from abc import ABC, abstractmethod +"""Module with number system representations.""" -""" - Abstract class to custom number systems. -""" +from abc import ABC, abstractmethod class NumberSystem(ABC): + """Abstract class to number systems.""" - """ - Abstract method to convert decimal number into number system given class provides. - """ @abstractmethod def convert_decimal(self, number): + """Abstract method to convert decimal number.""" pass -""" - Class representing binary number system. -""" - - class BinarySystem(NumberSystem): - - """ - Method to convert decimal number into binary one. - """ + """Class representing binary number system.""" def convert_decimal(self, number): + """Convert decimal number into binary one.""" return bin(number) -""" - Class representing octal number system. -""" - - class OctalSystem(NumberSystem): - - """ - Method to convert decimal number into octal one. - """ + """Class representing octal number system.""" def convert_decimal(self, number): + """Convert decimal number into octal one.""" return oct(number) -""" - Class representing hexadecimal number system. -""" - - class HexSystem(NumberSystem): - - """ - Method to convert decimal number into hex one. - """ + """Class representing hexadecimal number system.""" def convert_decimal(self, number): + """Convert decimal number into hex one.""" return hex(number) From 062bdc98242d37d48f378e12e35c3fe2e6d42e6a Mon Sep 17 00:00:00 2001 From: Filip Reim Date: Thu, 13 Jan 2022 20:48:08 +0100 Subject: [PATCH 9/9] Remove dockerfile because of testing command(shows error). Add tests, use relative imports, add .coverge to vcs ignore --- .gitignore | 3 ++- Dockerfile | 5 ----- __init__.py | 2 +- app.py | 2 +- test_app.py | 51 +++++++++++++++++++++++++++++++++++++++++++ test_number_system.py | 25 +++++++++++++++++++++ 6 files changed, 80 insertions(+), 8 deletions(-) mode change 100644 => 100755 .gitignore delete mode 100644 Dockerfile mode change 100644 => 100755 __init__.py mode change 100644 => 100755 app.py create mode 100644 test_app.py create mode 100644 test_number_system.py diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 index 1c9db0cd..4ce5bde0 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea/ -__pycache__ \ No newline at end of file +__pycache__ +.coverage \ No newline at end of file diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index ae7ac2d6..00000000 --- a/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM python:3.8.10-alpine - -WORKDIR /app - -RUN pip install flake8 flake8-docstrings pytest pytest-cov autopep8 \ No newline at end of file diff --git a/__init__.py b/__init__.py old mode 100644 new mode 100755 index 87d48a16..397ed7ae --- a/__init__.py +++ b/__init__.py @@ -1,5 +1,5 @@ """Initialization script for number system conversion app.""" -from app import App +from .app import App if __name__ == "__main__": application = App() diff --git a/app.py b/app.py old mode 100644 new mode 100755 index 3267f44b..59fcd389 --- a/app.py +++ b/app.py @@ -1,6 +1,6 @@ """Base App class to run this application.""" -from number_system import BinarySystem, OctalSystem, HexSystem +from .number_system import BinarySystem, OctalSystem, HexSystem class App: diff --git a/test_app.py b/test_app.py new file mode 100644 index 00000000..f74820f5 --- /dev/null +++ b/test_app.py @@ -0,0 +1,51 @@ +"""Test module for app class.""" + +import builtins +from .app import App +from .number_system import BinarySystem, OctalSystem, HexSystem + + +class TestApp: + """App class tests.""" + + def test_parse_input_number_ending(self): + """Test if ending input returns proper value.""" + app = App() + builtins.input = lambda x: '#' + + assert app.parse_input_number() is None + + def test_parse_input_valid_input(self): + """Test if valid input returns proper value.""" + app = App() + builtins.input = lambda x: '64' + + assert app.parse_input_number() == 64 + + def test_parse_target_number_system_ending(self): + """Test if ending input returns proper value.""" + app = App() + builtins.input = lambda x: '#' + + assert app.parse_target_number_system() is None + + def test_parse_target_number_system_return_binary_system(self): + """Test if proper input returns proper number system.""" + app = App() + builtins.input = lambda x: '1' + + assert isinstance(app.parse_target_number_system(), BinarySystem) + + def test_parse_target_number_system_return_octal_system(self): + """Test if proper input returns proper number system.""" + app = App() + builtins.input = lambda x: '2' + + assert isinstance(app.parse_target_number_system(), OctalSystem) + + def test_parse_target_number_system_return_hex_system(self): + """Test if proper input returns proper number system.""" + app = App() + builtins.input = lambda x: '3' + + assert isinstance(app.parse_target_number_system(), HexSystem) diff --git a/test_number_system.py b/test_number_system.py new file mode 100644 index 00000000..bdcc77bf --- /dev/null +++ b/test_number_system.py @@ -0,0 +1,25 @@ +"""Test for number system classes.""" + +from .number_system import BinarySystem, OctalSystem, HexSystem + + +class TestNumberSystem: + """Number system testing class.""" + + def test_convert_decimal_to_binary(self): + """Test binary system conversion method.""" + binary_number_system = BinarySystem() + + assert binary_number_system.convert_decimal(64) == '0b1000000' + + def test_convert_decimal_to_octal(self): + """Test octal system conversion method.""" + octal_number_system = OctalSystem() + + assert octal_number_system.convert_decimal(64) == '0o100' + + def test_convert_decimal_to_hex(self): + """Test hex system conversion method.""" + hex_number_system = HexSystem() + + assert hex_number_system.convert_decimal(64) == '0x40'