From d8b114d2be65981647159889159bd09aa815f588 Mon Sep 17 00:00:00 2001 From: rharris-nitro Date: Mon, 29 Apr 2024 11:04:39 +0100 Subject: [PATCH 1/2] outside file initialisation --- Taskfile.yml | 19 ++++++++--------- app/data.py | 36 +++++++++++++++++++++++++++++++ app/input/integers.txt | 1 + app/main.py | 48 +++++++++++++++++++++--------------------- docker-compose.yml | 2 +- 5 files changed, 71 insertions(+), 35 deletions(-) create mode 100644 app/data.py create mode 100644 app/input/integers.txt diff --git a/Taskfile.yml b/Taskfile.yml index 4836028..a0252d7 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -1,5 +1,8 @@ version: "3" +vars: + RUN_DEBUGPY: '/tmp/debugpy --listen 0.0.0.0:5673 --wait-for-client' + tasks: build: cmds: @@ -7,19 +10,15 @@ tasks: up: env: - BASE_COMMAND: '{{if eq .USE_DEBUGPY "true"}}/tmp/debugpy --listen 0.0.0.0:5673 --wait-for-client app/main.py{{else}}{{end}}' - cmds: - - docker-compose -f docker-compose.yml up - - build-test: - cmds: - - docker-compose -f docker-compose.test.yml build + BASE_COMMAND: '{{if eq .USE_DEBUGPY "true"}}{{.RUN_DEBUGPY}} -m app.main{{else}}{{end}}' + BASE_ARGS: '{{.CLI_ARGS}}' + cmd: docker-compose -f docker-compose.yml up up-test: env: - BASE_COMMAND: '{{if eq .USE_DEBUGPY "true"}}/tmp/debugpy --listen 0.0.0.0:5673 --wait-for-client tests/test_main.py{{else}}{{end}}' - cmds: - - docker-compose -f docker-compose.test.yml up + BASE_ENTRYPOINT: '/bin/sh' + BASE_COMMAND: '{{if eq .USE_DEBUGPY "true"}}-c "python {{.RUN_DEBUGPY}} -m pytest"{{else}}-c "coverage run -m pytest && coverage report && coverage html"{{end}}' + cmd: docker-compose -f docker-compose.yml up build-all: deps: [build, build-test] diff --git a/app/data.py b/app/data.py new file mode 100644 index 0000000..2306f9e --- /dev/null +++ b/app/data.py @@ -0,0 +1,36 @@ +""" +This module defines the Data class, which is responsible for initializing and managing +an array of integers. It provides methods to print the array and potentially other +operations related to the array. +""" + + +class Data: + """ + A class representing a data structure that holds an array of integers. + + Attributes: + array (list): An array of integers. + """ + + def __init__(self, integer_array=None): + """ + Initializes the Data object with a predefined array of integers + or an empty array if none is provided. + """ + if integer_array is None: + self.__integer_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + else: + self.__integer_array = integer_array + + def print_array(self): + """ + Prints the array of integers to the console. + """ + print(self.__integer_array) + + def get_array(self): + """ + Returns the initialized array. + """ + return self.__integer_array diff --git a/app/input/integers.txt b/app/input/integers.txt new file mode 100644 index 0000000..db5114f --- /dev/null +++ b/app/input/integers.txt @@ -0,0 +1 @@ +1 2 3 4 5 6 7 8 9 10 diff --git a/app/main.py b/app/main.py index 8c0a22a..1512023 100644 --- a/app/main.py +++ b/app/main.py @@ -3,38 +3,38 @@ of integers and prints it. """ +import argparse -class Data: - """ - Main class for handling integer arrays. - """ - - def __init__(self): - """ - Initializes an array of integers with predefined values. - """ - self.integer_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +from app.data import Data - def print_array(self): - """ - Prints the initialized array to the screen. - """ - print(self.integer_array) - def get_array(self): - """ - Returns the initialized array. - """ - return self.integer_array +def read_file(file_path): + """ + Reads a file and returns an array of integers. + """ + with open(file_path, "r", encoding="utf-8") as file: + return [int(x) for x in file.read().split()] -def main(): +def main(file_path): """ - Main function for creating Main instance and printing the array. + Main function for creating a Data instance and printing the array. + + This function demonstrates the basic usage of the Data class by creating an instance, + initializing it with the contents of a file, and then printing this array. """ - data = Data() + integer_array = read_file(file_path) + data = Data(integer_array) data.print_array() if __name__ == "__main__": - main() + parser = argparse.ArgumentParser(description="Process a file containing integers.") + parser.add_argument( + "file_path", + default="app/input/integers.txt", + type=str, + help="Path to the file containing integers", + ) + args = parser.parse_args() + main(args.file_path) diff --git a/docker-compose.yml b/docker-compose.yml index 907b7fb..b653fc6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,4 +11,4 @@ services: volumes: - ./app:/pythonExercise1/app - ./tests:/pythonExercise1/tests - command: ${BASE_COMMAND:- app/main.py} + command: ${BASE_COMMAND} ${BASE_ARGS} From 7699448af11c6d177c27801cc9ede135bef679e3 Mon Sep 17 00:00:00 2001 From: rharris-nitro Date: Tue, 30 Apr 2024 12:17:26 +0100 Subject: [PATCH 2/2] adds proper variable pointer --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 2b791b3..d8d802d 100644 --- a/app/main.py +++ b/app/main.py @@ -31,7 +31,7 @@ def main(file_path): if __name__ == "__main__": parser = argparse.ArgumentParser(description="Process a file containing integers.") parser.add_argument( - "file_path", + "--file_path", default="app/input/integers.txt", type=str, help="Path to the file containing integers",