From 5a1c67a6c6685ee58e026db833a9edc0b23bec47 Mon Sep 17 00:00:00 2001 From: qtdevops Date: Mon, 13 Dec 2021 19:54:15 +0530 Subject: [PATCH 01/16] Added a test to check if numberguessing is working correctly or not --- .../workouts/test/number_guessing.py | 7 +++++++ .../workouts/test/test_number_guessing.py | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 Dec21/UnitTesting/workouts/test/number_guessing.py create mode 100644 Dec21/UnitTesting/workouts/test/test_number_guessing.py diff --git a/Dec21/UnitTesting/workouts/test/number_guessing.py b/Dec21/UnitTesting/workouts/test/number_guessing.py new file mode 100644 index 0000000..086e90e --- /dev/null +++ b/Dec21/UnitTesting/workouts/test/number_guessing.py @@ -0,0 +1,7 @@ +import random + +def guessing_game(): + """ + This function should guess a number between 0 and 100 + """ + return random.randint(0, 100) diff --git a/Dec21/UnitTesting/workouts/test/test_number_guessing.py b/Dec21/UnitTesting/workouts/test/test_number_guessing.py new file mode 100644 index 0000000..5fb82f9 --- /dev/null +++ b/Dec21/UnitTesting/workouts/test/test_number_guessing.py @@ -0,0 +1,21 @@ +import unittest +from number_guessing import guessing_game + +class NumberGuessingTestClass(unittest.TestCase): + """ + This class will be used to test the number guessing game method + """ + + def test_expect_result_to_be_in_valid_range(self): + """ + Here lets test the number_guessing and ensure values are + in the range of 0 to 100 + """ + result_1 = guessing_game() + self.assertTrue( + 0 < result_1 <= 100, + msg="""Number guessing game should + predict values in range of 0 to 100""") + +if __name__ == '__main__': + unittest.main(verbosity=2) From ca062671416f78c997bed3bcc1f234b50c4ae642 Mon Sep 17 00:00:00 2001 From: qtdevops Date: Mon, 13 Dec 2021 20:39:31 +0530 Subject: [PATCH 02/16] Added tests for checking other puzzle --- .../workouts/test/number_guessing.py | 7 ---- Dec21/UnitTesting/workouts/test/puzzle.py | 29 +++++++++++++ .../workouts/test/test_number_guessing.py | 21 ---------- .../UnitTesting/workouts/test/test_puzzle.py | 42 +++++++++++++++++++ 4 files changed, 71 insertions(+), 28 deletions(-) delete mode 100644 Dec21/UnitTesting/workouts/test/number_guessing.py create mode 100644 Dec21/UnitTesting/workouts/test/puzzle.py delete mode 100644 Dec21/UnitTesting/workouts/test/test_number_guessing.py create mode 100644 Dec21/UnitTesting/workouts/test/test_puzzle.py diff --git a/Dec21/UnitTesting/workouts/test/number_guessing.py b/Dec21/UnitTesting/workouts/test/number_guessing.py deleted file mode 100644 index 086e90e..0000000 --- a/Dec21/UnitTesting/workouts/test/number_guessing.py +++ /dev/null @@ -1,7 +0,0 @@ -import random - -def guessing_game(): - """ - This function should guess a number between 0 and 100 - """ - return random.randint(0, 100) diff --git a/Dec21/UnitTesting/workouts/test/puzzle.py b/Dec21/UnitTesting/workouts/test/puzzle.py new file mode 100644 index 0000000..8425932 --- /dev/null +++ b/Dec21/UnitTesting/workouts/test/puzzle.py @@ -0,0 +1,29 @@ +import random + +def guessing_game() -> int: + """ + This function should guess a number between 0 and 100 + """ + return random.randint(0, 100) + +def pig_latin(word: str) -> str: + """ + This method will return the pig_latin + + if the word begins with vowel (a, e, i, o, u) add "way" at the end of the word + if the word begins with any other letter, + then we take the first letter and put it on the end and add `ay` + + + Examples + 1. air => airway + 2. computer => omputercay + 3. python => ythonpay + """ + if len(word) == 0: + return '' + + if word[0] in 'aeiou': + return f"{word}way" + + return f"{word[1:]}{word[0]}ay" diff --git a/Dec21/UnitTesting/workouts/test/test_number_guessing.py b/Dec21/UnitTesting/workouts/test/test_number_guessing.py deleted file mode 100644 index 5fb82f9..0000000 --- a/Dec21/UnitTesting/workouts/test/test_number_guessing.py +++ /dev/null @@ -1,21 +0,0 @@ -import unittest -from number_guessing import guessing_game - -class NumberGuessingTestClass(unittest.TestCase): - """ - This class will be used to test the number guessing game method - """ - - def test_expect_result_to_be_in_valid_range(self): - """ - Here lets test the number_guessing and ensure values are - in the range of 0 to 100 - """ - result_1 = guessing_game() - self.assertTrue( - 0 < result_1 <= 100, - msg="""Number guessing game should - predict values in range of 0 to 100""") - -if __name__ == '__main__': - unittest.main(verbosity=2) diff --git a/Dec21/UnitTesting/workouts/test/test_puzzle.py b/Dec21/UnitTesting/workouts/test/test_puzzle.py new file mode 100644 index 0000000..02b22e3 --- /dev/null +++ b/Dec21/UnitTesting/workouts/test/test_puzzle.py @@ -0,0 +1,42 @@ +import unittest +from puzzle import guessing_game, pig_latin + +class PuzzleTestClass(unittest.TestCase): + """ + This class will be used to test the puzzles + + """ + + def test_guessing_game(self): + """ + Here lets test the number_guessing and ensure values are + in the range of 0 to 100 + """ + result_1 = guessing_game() + self.assertTrue( + 0 < result_1 <= 100, + msg="""Number guessing game should + predict values in range of 0 to 100""") + + + def test_pig_latin(self): + """ + This method will test the pig_latin sequencs + """ + words = ['python', 'air', 'eat', 'computer', '', 'a', 'b'] + expected_results = ['ythonpay', 'airway', 'eatway', 'omputercay', '', 'away', 'bay'] + self.assertEqual(len(words), len(expected_results), msg="Input is wrong") + for index in range(len(words)): + actual_result = pig_latin(words[index]) + self.assertEqual( + actual_result, + expected_results[index], + msg=f"Pig Latin sequence is not working for {words[index]}" + ) + + + + + +if __name__ == '__main__': + unittest.main(verbosity=2) From 2851a9732160e2468abe40a6aea2fd500c05a65b Mon Sep 17 00:00:00 2001 From: qtdevops Date: Mon, 27 Dec 2021 08:34:48 +0530 Subject: [PATCH 03/16] Added basic inventory management structure --- Dec21/InventoryManagement/main.py | 0 Dec21/InventoryManagement/models/__init__.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 Dec21/InventoryManagement/main.py create mode 100644 Dec21/InventoryManagement/models/__init__.py diff --git a/Dec21/InventoryManagement/main.py b/Dec21/InventoryManagement/main.py new file mode 100644 index 0000000..e69de29 diff --git a/Dec21/InventoryManagement/models/__init__.py b/Dec21/InventoryManagement/models/__init__.py new file mode 100644 index 0000000..e69de29 From 59c9fd6b305677da2d4399dda3b02c819f7e54cd Mon Sep 17 00:00:00 2001 From: qtdevops Date: Mon, 27 Dec 2021 09:23:38 +0530 Subject: [PATCH 04/16] Added inventory management --- .../InventoryManagement/inventory/__init__.py | 0 .../inventory/inventory.py | 11 ++++++++ Dec21/InventoryManagement/main.py | 18 ++++++++++++ Dec21/InventoryManagement/models/products.py | 28 +++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 Dec21/InventoryManagement/inventory/__init__.py create mode 100644 Dec21/InventoryManagement/inventory/inventory.py create mode 100644 Dec21/InventoryManagement/models/products.py diff --git a/Dec21/InventoryManagement/inventory/__init__.py b/Dec21/InventoryManagement/inventory/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Dec21/InventoryManagement/inventory/inventory.py b/Dec21/InventoryManagement/inventory/inventory.py new file mode 100644 index 0000000..f5429e4 --- /dev/null +++ b/Dec21/InventoryManagement/inventory/inventory.py @@ -0,0 +1,11 @@ +from models.products import Product + +product_list = [] + + +def add_product(id, name, description,category,mrp): + """ + This function will add the product + """ + product = Product(id, name, description,category,mrp) + product_list.append(product) diff --git a/Dec21/InventoryManagement/main.py b/Dec21/InventoryManagement/main.py index e69de29..be78e66 100644 --- a/Dec21/InventoryManagement/main.py +++ b/Dec21/InventoryManagement/main.py @@ -0,0 +1,18 @@ +from inventory import inventory + + +def get_input_from_user(field_name): + result = input(f"Enter {field_name}: ") + return result + +if __name__ == '__main__': + + id = get_input_from_user('id') + name = get_input_from_user('name') + description = get_input_from_user('description') + category = get_input_from_user('category') + mrp = float(get_input_from_user('mrp')) + + inventory.add_product(id, name,description,category,mrp) + for item in inventory.product_list: + print(item) \ No newline at end of file diff --git a/Dec21/InventoryManagement/models/products.py b/Dec21/InventoryManagement/models/products.py new file mode 100644 index 0000000..85b1b7a --- /dev/null +++ b/Dec21/InventoryManagement/models/products.py @@ -0,0 +1,28 @@ +from datetime import datetime + +class Product: + """ + This class represents the product + """ + def __init__(self, id, name, description,category,mrp): + """ + Initializer for Product + + :param id: The id of the product + :param name: The name of the product + :param description: The description of the product + :param category: The category of the product + :param mrp: The maximum retail price of the product + """ + self.id = id + self.name = name + self.description = description + self.category = category + self.mrp = mrp + self.created_at = datetime.now() + # todo: fix the updated_at to be changed when attributes are changed + self.upated_at = datetime.now() + + def __str__(self): + return(f"{self.id}, {self.name}, {self.description}, {self.category}, {self.mrp}, {self.created_at}") + From cfbfa1412137b912d520e5cca02c984f32adc700 Mon Sep 17 00:00:00 2001 From: qtdevops Date: Mon, 3 Jan 2022 17:58:40 +0530 Subject: [PATCH 05/16] Added code to save the products --- Dec21/InventoryManagement/data/products.csv | 1 + .../InventoryManagement/inventory/inventory.py | 1 + Dec21/InventoryManagement/main.py | 3 +++ Dec21/InventoryManagement/models/products.py | 17 ++++++++++++++++- 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 Dec21/InventoryManagement/data/products.csv diff --git a/Dec21/InventoryManagement/data/products.csv b/Dec21/InventoryManagement/data/products.csv new file mode 100644 index 0000000..7e68df5 --- /dev/null +++ b/Dec21/InventoryManagement/data/products.csv @@ -0,0 +1 @@ +A00001,iphone 12 pro max,pro max series,mobiles,120000.99,2022-01-03 17:57:08.486696 diff --git a/Dec21/InventoryManagement/inventory/inventory.py b/Dec21/InventoryManagement/inventory/inventory.py index f5429e4..a519d6c 100644 --- a/Dec21/InventoryManagement/inventory/inventory.py +++ b/Dec21/InventoryManagement/inventory/inventory.py @@ -8,4 +8,5 @@ def add_product(id, name, description,category,mrp): This function will add the product """ product = Product(id, name, description,category,mrp) + product.save() product_list.append(product) diff --git a/Dec21/InventoryManagement/main.py b/Dec21/InventoryManagement/main.py index be78e66..76e1954 100644 --- a/Dec21/InventoryManagement/main.py +++ b/Dec21/InventoryManagement/main.py @@ -2,6 +2,9 @@ def get_input_from_user(field_name): + """ + This is the reusable method to collect input from user + """ result = input(f"Enter {field_name}: ") return result diff --git a/Dec21/InventoryManagement/models/products.py b/Dec21/InventoryManagement/models/products.py index 85b1b7a..e647409 100644 --- a/Dec21/InventoryManagement/models/products.py +++ b/Dec21/InventoryManagement/models/products.py @@ -1,6 +1,10 @@ from datetime import datetime +import csv class Product: + + file_name = 'data/products.csv' + """ This class represents the product """ @@ -24,5 +28,16 @@ def __init__(self, id, name, description,category,mrp): self.upated_at = datetime.now() def __str__(self): - return(f"{self.id}, {self.name}, {self.description}, {self.category}, {self.mrp}, {self.created_at}") + """ + This is string representation of object + T""" + return(f"{self.id}, {self.name}, {self.description}, {self.category}, {self.mrp}, {self.created_at}") + + def save(self): + """ + This method will save the Current Object to the file + """ + with open(self.file_name, 'w') as file: + writer = csv.writer(file, delimiter = ',') + writer.writerow([self.id, self.name, self.description, self.category, self.mrp,self.created_at ]) From f27c1cf0892288a30ba345bb6724a3ffbf16b290 Mon Sep 17 00:00:00 2001 From: qtdevops Date: Mon, 3 Jan 2022 18:07:26 +0530 Subject: [PATCH 06/16] Added append mode --- Dec21/InventoryManagement/data/products.csv | 3 ++- Dec21/InventoryManagement/models/products.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Dec21/InventoryManagement/data/products.csv b/Dec21/InventoryManagement/data/products.csv index 7e68df5..acb5b56 100644 --- a/Dec21/InventoryManagement/data/products.csv +++ b/Dec21/InventoryManagement/data/products.csv @@ -1 +1,2 @@ -A00001,iphone 12 pro max,pro max series,mobiles,120000.99,2022-01-03 17:57:08.486696 +A000002,OnePlus 9 Pro 5G,Rear Quad Camera Co-Developed by Hasselblad,mobiles,69999.0,2022-01-03 18:03:08.793102 +A00001,iphone 12 pro max,iphone pro max series,mobiles,120000.99,2022-01-03 18:06:05.801684 diff --git a/Dec21/InventoryManagement/models/products.py b/Dec21/InventoryManagement/models/products.py index e647409..71c3874 100644 --- a/Dec21/InventoryManagement/models/products.py +++ b/Dec21/InventoryManagement/models/products.py @@ -37,7 +37,7 @@ def save(self): """ This method will save the Current Object to the file """ - with open(self.file_name, 'w') as file: + with open(self.file_name, 'a') as file: writer = csv.writer(file, delimiter = ',') writer.writerow([self.id, self.name, self.description, self.category, self.mrp,self.created_at ]) From 6966378553256b235f014f66879a88be937f2dfe Mon Sep 17 00:00:00 2001 From: qtdevops Date: Mon, 3 Jan 2022 18:31:21 +0530 Subject: [PATCH 07/16] Added improvements --- Dec21/InventoryManagement/data/products.csv | 4 ++-- Dec21/InventoryManagement/inventory/inventory.py | 11 +++++------ Dec21/InventoryManagement/main.py | 3 ++- Dec21/InventoryManagement/models/products.py | 12 ++++++++++++ 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Dec21/InventoryManagement/data/products.csv b/Dec21/InventoryManagement/data/products.csv index acb5b56..c7e2f2e 100644 --- a/Dec21/InventoryManagement/data/products.csv +++ b/Dec21/InventoryManagement/data/products.csv @@ -1,2 +1,2 @@ -A000002,OnePlus 9 Pro 5G,Rear Quad Camera Co-Developed by Hasselblad,mobiles,69999.0,2022-01-03 18:03:08.793102 -A00001,iphone 12 pro max,iphone pro max series,mobiles,120000.99,2022-01-03 18:06:05.801684 +A00002,OnePlus 9 Pro 5G,Rear Quad Camera Co-Developed by Hasselblad,mobiles,69999.0,2022-01-03 18:03:08.793102 +A00001,iphone 12 pro max,iphone pro max series,mobiles,120000.99,2022-01-03 18:06:05.801684 \ No newline at end of file diff --git a/Dec21/InventoryManagement/inventory/inventory.py b/Dec21/InventoryManagement/inventory/inventory.py index a519d6c..625a526 100644 --- a/Dec21/InventoryManagement/inventory/inventory.py +++ b/Dec21/InventoryManagement/inventory/inventory.py @@ -1,12 +1,11 @@ from models.products import Product -product_list = [] - - def add_product(id, name, description,category,mrp): """ This function will add the product """ - product = Product(id, name, description,category,mrp) - product.save() - product_list.append(product) + if id not in Product.ids() : + product = Product(id, name, description,category,mrp) + product.save() + else: + print(f"Product with id {id} already exists") diff --git a/Dec21/InventoryManagement/main.py b/Dec21/InventoryManagement/main.py index 76e1954..95e5c7c 100644 --- a/Dec21/InventoryManagement/main.py +++ b/Dec21/InventoryManagement/main.py @@ -1,4 +1,5 @@ from inventory import inventory +from models.products import Product def get_input_from_user(field_name): @@ -9,7 +10,7 @@ def get_input_from_user(field_name): return result if __name__ == '__main__': - + id = get_input_from_user('id') name = get_input_from_user('name') description = get_input_from_user('description') diff --git a/Dec21/InventoryManagement/models/products.py b/Dec21/InventoryManagement/models/products.py index 71c3874..00c3d1f 100644 --- a/Dec21/InventoryManagement/models/products.py +++ b/Dec21/InventoryManagement/models/products.py @@ -41,3 +41,15 @@ def save(self): writer = csv.writer(file, delimiter = ',') writer.writerow([self.id, self.name, self.description, self.category, self.mrp,self.created_at ]) + @classmethod + def ids(cls): + """ + This method will read all the existing ids from the csv file + """ + item_ids = [] + with open(cls.file_name, 'r') as file: + reader = csv.reader(file, delimiter = ',') + for row in reader: + if len(row) > 0: + item_ids.append(row[0]) + return item_ids From a53ce8c45503c3ea632a884d9e0c4036cd92473e Mon Sep 17 00:00:00 2001 From: qtdevops Date: Mon, 3 Jan 2022 19:26:07 +0530 Subject: [PATCH 08/16] Added products and stock derived from a common base --- Dec21/InventoryManagement/main.py | 19 ++++++++------ .../models/baseinventory.py | 17 ++++++++++++ Dec21/InventoryManagement/models/products.py | 10 +++---- Dec21/InventoryManagement/models/stock.py | 26 +++++++++++++++++++ 4 files changed, 57 insertions(+), 15 deletions(-) create mode 100644 Dec21/InventoryManagement/models/baseinventory.py create mode 100644 Dec21/InventoryManagement/models/stock.py diff --git a/Dec21/InventoryManagement/main.py b/Dec21/InventoryManagement/main.py index 95e5c7c..e6cc6ca 100644 --- a/Dec21/InventoryManagement/main.py +++ b/Dec21/InventoryManagement/main.py @@ -1,5 +1,6 @@ from inventory import inventory from models.products import Product +from models.stock import Stock def get_input_from_user(field_name): @@ -10,13 +11,15 @@ def get_input_from_user(field_name): return result if __name__ == '__main__': + s1 = Stock(1,10) + print(s1) - id = get_input_from_user('id') - name = get_input_from_user('name') - description = get_input_from_user('description') - category = get_input_from_user('category') - mrp = float(get_input_from_user('mrp')) + # id = get_input_from_user('id') + # name = get_input_from_user('name') + # description = get_input_from_user('description') + # category = get_input_from_user('category') + # mrp = float(get_input_from_user('mrp')) - inventory.add_product(id, name,description,category,mrp) - for item in inventory.product_list: - print(item) \ No newline at end of file + # inventory.add_product(id, name,description,category,mrp) + # for item in inventory.product_list: + # print(item) \ No newline at end of file diff --git a/Dec21/InventoryManagement/models/baseinventory.py b/Dec21/InventoryManagement/models/baseinventory.py new file mode 100644 index 0000000..dbede6e --- /dev/null +++ b/Dec21/InventoryManagement/models/baseinventory.py @@ -0,0 +1,17 @@ +from datetime import datetime + + +class BaseInventoryModel: + """ + This represents the base model + """ + def __init__(self, file_name) -> None: + self.created_at = datetime.now() + self.updated_at = datetime.now() + self.file_name = file_name + + def save(self): + """ + This method represents saving the csv + """ + pass \ No newline at end of file diff --git a/Dec21/InventoryManagement/models/products.py b/Dec21/InventoryManagement/models/products.py index 00c3d1f..8196105 100644 --- a/Dec21/InventoryManagement/models/products.py +++ b/Dec21/InventoryManagement/models/products.py @@ -1,10 +1,8 @@ from datetime import datetime +from models.baseinventory import BaseInventoryModel import csv -class Product: - - file_name = 'data/products.csv' - +class Product(BaseInventoryModel): """ This class represents the product """ @@ -18,14 +16,12 @@ def __init__(self, id, name, description,category,mrp): :param category: The category of the product :param mrp: The maximum retail price of the product """ + super().__init__(file_name='data/products.csv') self.id = id self.name = name self.description = description self.category = category self.mrp = mrp - self.created_at = datetime.now() - # todo: fix the updated_at to be changed when attributes are changed - self.upated_at = datetime.now() def __str__(self): """ diff --git a/Dec21/InventoryManagement/models/stock.py b/Dec21/InventoryManagement/models/stock.py new file mode 100644 index 0000000..6b2b273 --- /dev/null +++ b/Dec21/InventoryManagement/models/stock.py @@ -0,0 +1,26 @@ +from models.baseinventory import BaseInventoryModel + +class Stock(BaseInventoryModel): + """ + This class represents the Stock of the items in the Store + """ + def __init__(self, id, quantity) -> None: + super().__init__(file_name='data/stocks.csv') + self.id = id + self.quantity = quantity + + def save(self): + """ + This method will save the Stock record + """ + pass + + def update(self, new_quantity): + """ + This method will updated the stock of the existing product + """ + pass + + def __str__(self) -> str: + return f"{self.id}, {self.quantity}" + \ No newline at end of file From 051dc8e1af292030da8c024a00320a7dfc651b71 Mon Sep 17 00:00:00 2001 From: qtdevops Date: Mon, 10 Jan 2022 08:16:35 +0530 Subject: [PATCH 09/16] Added base implementation of csv file save --- Dec21/InventoryManagement/data/products.csv | 4 +-- Dec21/InventoryManagement/data/stocks.csv | 3 ++ Dec21/InventoryManagement/main.py | 8 +++-- .../models/baseinventory.py | 33 ++++++++++++++++--- Dec21/InventoryManagement/models/products.py | 13 +++----- Dec21/InventoryManagement/models/stock.py | 13 ++++---- 6 files changed, 49 insertions(+), 25 deletions(-) create mode 100644 Dec21/InventoryManagement/data/stocks.csv diff --git a/Dec21/InventoryManagement/data/products.csv b/Dec21/InventoryManagement/data/products.csv index c7e2f2e..3e08b5d 100644 --- a/Dec21/InventoryManagement/data/products.csv +++ b/Dec21/InventoryManagement/data/products.csv @@ -1,2 +1,2 @@ -A00002,OnePlus 9 Pro 5G,Rear Quad Camera Co-Developed by Hasselblad,mobiles,69999.0,2022-01-03 18:03:08.793102 -A00001,iphone 12 pro max,iphone pro max series,mobiles,120000.99,2022-01-03 18:06:05.801684 \ No newline at end of file +created_at,updated_at,id,name,description,category,mrp +2022-01-10 08:14:06.929317,2022-01-10 08:14:06.929317,1,test,test,test,100.45 diff --git a/Dec21/InventoryManagement/data/stocks.csv b/Dec21/InventoryManagement/data/stocks.csv new file mode 100644 index 0000000..f2946c4 --- /dev/null +++ b/Dec21/InventoryManagement/data/stocks.csv @@ -0,0 +1,3 @@ +created_at,updated_at,id,quantity +2022-01-10 08:11:49.986717,2022-01-10 08:11:49.986717,1,10 +2022-01-10 08:13:55.202320,2022-01-10 08:13:55.202320,2,11 diff --git a/Dec21/InventoryManagement/main.py b/Dec21/InventoryManagement/main.py index e6cc6ca..41056ef 100644 --- a/Dec21/InventoryManagement/main.py +++ b/Dec21/InventoryManagement/main.py @@ -11,8 +11,12 @@ def get_input_from_user(field_name): return result if __name__ == '__main__': - s1 = Stock(1,10) - print(s1) + s1 = Stock(2,11) + s1.save() + + p1 = Product(1, 'test', 'test', 'test', 100.45) + p1.save() + #print(s1) # id = get_input_from_user('id') # name = get_input_from_user('name') diff --git a/Dec21/InventoryManagement/models/baseinventory.py b/Dec21/InventoryManagement/models/baseinventory.py index dbede6e..c34fe6b 100644 --- a/Dec21/InventoryManagement/models/baseinventory.py +++ b/Dec21/InventoryManagement/models/baseinventory.py @@ -1,17 +1,40 @@ from datetime import datetime +import os +import csv class BaseInventoryModel: """ This represents the base model """ - def __init__(self, file_name) -> None: - self.created_at = datetime.now() - self.updated_at = datetime.now() - self.file_name = file_name + + _file_name = "" + + def __init__(self, created_at=None, updated_at=None) -> None: + """ + This is a base initalizer + """ + self.created_at = created_at or datetime.now() + self.updated_at = updated_at or datetime.now() def save(self): """ This method represents saving the csv """ - pass \ No newline at end of file + # check if the folder exists + data_directory = os.path.dirname(self._file_name) + # if path not exists create a directory + if not os.path.exists(data_directory): + os.mkdir(data_directory) + is_csv_file_existing = os.path.exists(self._file_name) + # we need to write these to the csv file + field_dict = self.__dict__ + with open(self._file_name, 'a') as csv_file: + #writng to csv using https://docs.python.org/3/library/csv.html#csv.DictWriter + writer = csv.DictWriter(csv_file, field_dict.keys()) + if not is_csv_file_existing: + writer.writeheader() + writer.writerow(field_dict) + + + diff --git a/Dec21/InventoryManagement/models/products.py b/Dec21/InventoryManagement/models/products.py index 8196105..1daaecc 100644 --- a/Dec21/InventoryManagement/models/products.py +++ b/Dec21/InventoryManagement/models/products.py @@ -3,10 +3,12 @@ import csv class Product(BaseInventoryModel): + _file_name = 'data/products.csv' + """ This class represents the product """ - def __init__(self, id, name, description,category,mrp): + def __init__(self, id, name, description,category,mrp,created_at=None, updated_at=None): """ Initializer for Product @@ -16,7 +18,7 @@ def __init__(self, id, name, description,category,mrp): :param category: The category of the product :param mrp: The maximum retail price of the product """ - super().__init__(file_name='data/products.csv') + super().__init__(created_at, updated_at) self.id = id self.name = name self.description = description @@ -29,13 +31,6 @@ def __str__(self): T""" return(f"{self.id}, {self.name}, {self.description}, {self.category}, {self.mrp}, {self.created_at}") - def save(self): - """ - This method will save the Current Object to the file - """ - with open(self.file_name, 'a') as file: - writer = csv.writer(file, delimiter = ',') - writer.writerow([self.id, self.name, self.description, self.category, self.mrp,self.created_at ]) @classmethod def ids(cls): diff --git a/Dec21/InventoryManagement/models/stock.py b/Dec21/InventoryManagement/models/stock.py index 6b2b273..0882421 100644 --- a/Dec21/InventoryManagement/models/stock.py +++ b/Dec21/InventoryManagement/models/stock.py @@ -1,19 +1,18 @@ from models.baseinventory import BaseInventoryModel class Stock(BaseInventoryModel): + + _file_name='data/stocks.csv' + """ This class represents the Stock of the items in the Store """ - def __init__(self, id, quantity) -> None: - super().__init__(file_name='data/stocks.csv') + def __init__(self, id, quantity, created_at=None, updated_at=None) -> None: + super().__init__(created_at, updated_at) self.id = id self.quantity = quantity + - def save(self): - """ - This method will save the Stock record - """ - pass def update(self, new_quantity): """ From d3074f35bf36a04d4d1d9b4ff4631733d40b5817 Mon Sep 17 00:00:00 2001 From: qtdevops Date: Mon, 10 Jan 2022 08:38:58 +0530 Subject: [PATCH 10/16] Added Base class implementation for reading items from csv file --- Dec21/InventoryManagement/data/products.csv | 2 -- Dec21/InventoryManagement/data/stocks.csv | 3 --- Dec21/InventoryManagement/main.py | 12 ++++++----- .../models/baseinventory.py | 20 +++++++++++++++++++ Dec21/InventoryManagement/models/products.py | 15 ++------------ Dec21/InventoryManagement/models/stock.py | 3 ++- 6 files changed, 31 insertions(+), 24 deletions(-) delete mode 100644 Dec21/InventoryManagement/data/products.csv delete mode 100644 Dec21/InventoryManagement/data/stocks.csv diff --git a/Dec21/InventoryManagement/data/products.csv b/Dec21/InventoryManagement/data/products.csv deleted file mode 100644 index 3e08b5d..0000000 --- a/Dec21/InventoryManagement/data/products.csv +++ /dev/null @@ -1,2 +0,0 @@ -created_at,updated_at,id,name,description,category,mrp -2022-01-10 08:14:06.929317,2022-01-10 08:14:06.929317,1,test,test,test,100.45 diff --git a/Dec21/InventoryManagement/data/stocks.csv b/Dec21/InventoryManagement/data/stocks.csv deleted file mode 100644 index f2946c4..0000000 --- a/Dec21/InventoryManagement/data/stocks.csv +++ /dev/null @@ -1,3 +0,0 @@ -created_at,updated_at,id,quantity -2022-01-10 08:11:49.986717,2022-01-10 08:11:49.986717,1,10 -2022-01-10 08:13:55.202320,2022-01-10 08:13:55.202320,2,11 diff --git a/Dec21/InventoryManagement/main.py b/Dec21/InventoryManagement/main.py index 41056ef..e964188 100644 --- a/Dec21/InventoryManagement/main.py +++ b/Dec21/InventoryManagement/main.py @@ -11,12 +11,14 @@ def get_input_from_user(field_name): return result if __name__ == '__main__': - s1 = Stock(2,11) - s1.save() + #s1 = Stock(2,11) + #s1.save() - p1 = Product(1, 'test', 'test', 'test', 100.45) - p1.save() - #print(s1) + #p1 = Product(1, 'test', 'test', 'test', 100.45) + #p1.save() + items = Product.items() + for item in items: + print(item) # id = get_input_from_user('id') # name = get_input_from_user('name') diff --git a/Dec21/InventoryManagement/models/baseinventory.py b/Dec21/InventoryManagement/models/baseinventory.py index c34fe6b..04feb30 100644 --- a/Dec21/InventoryManagement/models/baseinventory.py +++ b/Dec21/InventoryManagement/models/baseinventory.py @@ -2,6 +2,8 @@ import os import csv +import inventory + class BaseInventoryModel: """ @@ -36,5 +38,23 @@ def save(self): writer.writeheader() writer.writerow(field_dict) + @classmethod + def items(cls): + """ + Returns all the objects by reading the records in the csv file + """ + if not os.path.exists(cls._file_name): + return [] + with open(cls._file_name, 'r') as csv_file: + reader = csv.DictReader(csv_file) + #inventory_items = [] + #for row in reader: + # inventory_item = cls(**row) + # inventory_items.append(inventory_item) + inventory_items = [cls(**row) for row in reader] + return inventory_items + + + diff --git a/Dec21/InventoryManagement/models/products.py b/Dec21/InventoryManagement/models/products.py index 1daaecc..ef25485 100644 --- a/Dec21/InventoryManagement/models/products.py +++ b/Dec21/InventoryManagement/models/products.py @@ -29,18 +29,7 @@ def __str__(self): """ This is string representation of object T""" - return(f"{self.id}, {self.name}, {self.description}, {self.category}, {self.mrp}, {self.created_at}") + return(f"{self.id}, {self.name}, {self.description}, {self.category}, {self.mrp}, {self.created_at}, {self.updated_at}") - @classmethod - def ids(cls): - """ - This method will read all the existing ids from the csv file - """ - item_ids = [] - with open(cls.file_name, 'r') as file: - reader = csv.reader(file, delimiter = ',') - for row in reader: - if len(row) > 0: - item_ids.append(row[0]) - return item_ids + diff --git a/Dec21/InventoryManagement/models/stock.py b/Dec21/InventoryManagement/models/stock.py index 0882421..882daca 100644 --- a/Dec21/InventoryManagement/models/stock.py +++ b/Dec21/InventoryManagement/models/stock.py @@ -7,9 +7,10 @@ class Stock(BaseInventoryModel): """ This class represents the Stock of the items in the Store """ - def __init__(self, id, quantity, created_at=None, updated_at=None) -> None: + def __init__(self, id, product_id, quantity, created_at=None, updated_at=None) -> None: super().__init__(created_at, updated_at) self.id = id + self.product_id = product_id self.quantity = quantity From a274dcacab93c89a1d2edffc3c24a047da54d5f9 Mon Sep 17 00:00:00 2001 From: qtkhajacloud Date: Mon, 14 Feb 2022 20:48:55 +0530 Subject: [PATCH 11/16] Added code --- Feb22/session1/hackerrank_print.py | 6 ++++++ Feb22/session1/othersample.py | 3 +++ Feb22/session1/sample.py | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 Feb22/session1/hackerrank_print.py create mode 100644 Feb22/session1/othersample.py create mode 100644 Feb22/session1/sample.py diff --git a/Feb22/session1/hackerrank_print.py b/Feb22/session1/hackerrank_print.py new file mode 100644 index 0000000..f5022c2 --- /dev/null +++ b/Feb22/session1/hackerrank_print.py @@ -0,0 +1,6 @@ +if __name__ == '__main__': + n = int(input()) + if not 1 <= n <= 150: + exit(1) + for index in range(1, n+1): + print(index, end='') \ No newline at end of file diff --git a/Feb22/session1/othersample.py b/Feb22/session1/othersample.py new file mode 100644 index 0000000..3c8795f --- /dev/null +++ b/Feb22/session1/othersample.py @@ -0,0 +1,3 @@ +from sample import add + +add(100,200) \ No newline at end of file diff --git a/Feb22/session1/sample.py b/Feb22/session1/sample.py new file mode 100644 index 0000000..05565b1 --- /dev/null +++ b/Feb22/session1/sample.py @@ -0,0 +1,19 @@ +def add(number_1, number_2): + """Adds two numbers + + Args: + number_1: This is the first number + number_2: This is the second number + + Returns: + sum of two arguments passed + + """ + return number_1 + number_2 + +if __name__ == "__main__": + num_1 = 10 + num_2 = 20 + result = add(num_1, num_2) + # Lets change the output to something like 10 + 20 => 30 + print(f"{num_1} + {num_2} => {result} ") \ No newline at end of file From 9a678bc584d8777fc370c92017c66829a6f97fc0 Mon Sep 17 00:00:00 2001 From: qtkhajacloud Date: Mon, 21 Feb 2022 19:42:18 +0530 Subject: [PATCH 12/16] Added basic code for session 2 --- Feb22/session2/__init__.py | 0 Feb22/session2/sample.py | 14 ++++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 Feb22/session2/__init__.py create mode 100644 Feb22/session2/sample.py diff --git a/Feb22/session2/__init__.py b/Feb22/session2/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Feb22/session2/sample.py b/Feb22/session2/sample.py new file mode 100644 index 0000000..6bf77b4 --- /dev/null +++ b/Feb22/session2/sample.py @@ -0,0 +1,14 @@ +def add(number1, number2): + """adding two numbers + + Args: + number1: This is the first argument + number2: This is the second argument + Returns: + sum of two arguments + + """ + return number1 + number2 + +if __name__ == "__main__": + print(add(5,6)) \ No newline at end of file From a0fe7848cbf44fe6668faa62e5d82fd5ad07821b Mon Sep 17 00:00:00 2001 From: qtkhajacloud Date: Mon, 21 Feb 2022 20:06:31 +0530 Subject: [PATCH 13/16] Added basic python test --- Feb22/session2/.vscode/settings.json | 7 +++++++ Feb22/session2/__init__.py | 0 Feb22/session2/requirements.txt | Bin 0 -> 326 bytes Feb22/session2/sample.py | 2 -- Feb22/session2/test_sample.py | 10 ++++++++++ 5 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 Feb22/session2/.vscode/settings.json delete mode 100644 Feb22/session2/__init__.py create mode 100644 Feb22/session2/requirements.txt create mode 100644 Feb22/session2/test_sample.py diff --git a/Feb22/session2/.vscode/settings.json b/Feb22/session2/.vscode/settings.json new file mode 100644 index 0000000..3e99ede --- /dev/null +++ b/Feb22/session2/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "python.testing.pytestArgs": [ + "." + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true +} \ No newline at end of file diff --git a/Feb22/session2/__init__.py b/Feb22/session2/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/Feb22/session2/requirements.txt b/Feb22/session2/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..ade3c43e44921af12118fd6600a746df9b4d31d2 GIT binary patch literal 326 zcmZ8d+X})k3_Z_+zcSWsuqQu8ik4w_VOC^6ub!k~bEBjw$+;x`ey&Iu(W8UM8#8ml z4NqKn911jy5@(zs$V5c$Rfn#pN@UE$^$g*c#&4A0VNjQm`Oa5ZJ;cW;W+Kz$!8;H! z7$$qwhKP8v9-_+~ C Date: Mon, 21 Feb 2022 21:08:45 +0530 Subject: [PATCH 14/16] Added solution to problem --- Feb22/session2/picnic.py | 21 +++++++++++++++++++++ Feb22/session2/test_picnic.py | 19 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 Feb22/session2/picnic.py create mode 100644 Feb22/session2/test_picnic.py diff --git a/Feb22/session2/picnic.py b/Feb22/session2/picnic.py new file mode 100644 index 0000000..2238f55 --- /dev/null +++ b/Feb22/session2/picnic.py @@ -0,0 +1,21 @@ +def food_items(*args): + """ This function will solve the following question + Function Call Sample Output + food_items('salad') => You are bringing salad + food_items('salad', 'chips') => You are bringing salad and chips + food_items('salad', 'chips', 'cake') => You are bringing salad, chips and cake + """ + item_punctuaded = "" + if len(args) == 1: + item_punctuaded = args[0] + elif len(args) == 2: + item_punctuaded = f"{args[0]} and {args[1]}" + elif len(args) > 2: + last_two = f"{args[-2]} and {args[-1]}" + other_than_last_two = ", ".join(args[0:-2]) + item_punctuaded = f"{other_than_last_two}, {last_two}" + else: + return "" + + message = f"You are bringing {item_punctuaded}" + return message \ No newline at end of file diff --git a/Feb22/session2/test_picnic.py b/Feb22/session2/test_picnic.py new file mode 100644 index 0000000..85d275b --- /dev/null +++ b/Feb22/session2/test_picnic.py @@ -0,0 +1,19 @@ +from email.utils import formataddr +from picnic import food_items + +def test_food_items(): + """ + This test case will test the food_items function + """ + result = food_items('salad') + assert result == "You are bringing salad" + result = food_items('salad', 'chips') + assert result == "You are bringing salad and chips" + result = food_items('salad', 'chips', 'cake') + assert result == "You are bringing salad, chips and cake" + result = food_items('salad','muffins', 'chips', 'cake') + assert result == "You are bringing salad, muffins, chips and cake" + result = food_items('salad','icecream', 'muffins', 'chips', 'cake') + assert result == "You are bringing salad, icecream, muffins, chips and cake" + result = food_items() + assert result == "" \ No newline at end of file From d649215501e02ac30623c1c56cd9c3375f3a1360 Mon Sep 17 00:00:00 2001 From: qtkhajacloud Date: Mon, 28 Feb 2022 19:51:48 +0530 Subject: [PATCH 15/16] Added helper function --- Feb22/session3/helperfunctions.py | 20 ++++++++++++++++++++ Feb22/session3/requirements.txt | Bin 0 -> 326 bytes 2 files changed, 20 insertions(+) create mode 100644 Feb22/session3/helperfunctions.py create mode 100644 Feb22/session3/requirements.txt diff --git a/Feb22/session3/helperfunctions.py b/Feb22/session3/helperfunctions.py new file mode 100644 index 0000000..38de2c0 --- /dev/null +++ b/Feb22/session3/helperfunctions.py @@ -0,0 +1,20 @@ +from urllib.parse import parse_qs +# This is writing expressions directly +# from urllib.parse import parse_qs +# my_values = parse_qs('name=python&topic=effective&batch=1') +# my_values.get('name')[0] + ' ' + my_values.get('topic')[0] + +def get_first_value(qs_dict, key, default=""): + """ + This method will parse the qs dictionary are returns the first value + """ + all_values = qs_dict.get(key, default) + return all_values[0] + + + +if __name__ == "__main__": + qs = 'name=python&topic=effective&batch=1' + qs_dict = parse_qs(qs) + print(f"{get_first_value(qs_dict, key='name')} {get_first_value(qs_dict, key='topic')}") + diff --git a/Feb22/session3/requirements.txt b/Feb22/session3/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..ade3c43e44921af12118fd6600a746df9b4d31d2 GIT binary patch literal 326 zcmZ8d+X})k3_Z_+zcSWsuqQu8ik4w_VOC^6ub!k~bEBjw$+;x`ey&Iu(W8UM8#8ml z4NqKn911jy5@(zs$V5c$Rfn#pN@UE$^$g*c#&4A0VNjQm`Oa5ZJ;cW;W+Kz$!8;H! z7$$qwhKP8v9-_+~ C Date: Mon, 28 Feb 2022 21:13:35 +0530 Subject: [PATCH 16/16] Added some problems and explanations --- Feb22/session3/exploring.py | 14 ++++++++++++++ Feb22/session3/exploringclass.py | 25 +++++++++++++++++++++++++ Feb22/session3/helperfunctions.py | 1 + Feb22/session3/sampleclass.py | 28 ++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 Feb22/session3/exploring.py create mode 100644 Feb22/session3/exploringclass.py create mode 100644 Feb22/session3/sampleclass.py diff --git a/Feb22/session3/exploring.py b/Feb22/session3/exploring.py new file mode 100644 index 0000000..7415b12 --- /dev/null +++ b/Feb22/session3/exploring.py @@ -0,0 +1,14 @@ +def print_hashes(): + print("############################################################") + + +def welcome(name, func): + func() + print(name) + func() + name() + +if __name__ == "__main__": + welcome("Python From QT", print_hashes) + + diff --git a/Feb22/session3/exploringclass.py b/Feb22/session3/exploringclass.py new file mode 100644 index 0000000..b75ba94 --- /dev/null +++ b/Feb22/session3/exploringclass.py @@ -0,0 +1,25 @@ +class Student: + """ + This class represents Student + """ + # what is self & why we need self + def enter_extra_details(self, key, value): + """ + This method allows us to enter extra details + """ + print(key, value) + + +class PrimarySchoolStudent(Student): + pass + +class HighSchoolStudent(Student): + pass + +if __name__ == "__main__": + hstd = HighSchoolStudent() + pstd = PrimarySchoolStudent() + + hstd.enter_extra_details(1,2) + pstd.enter_extra_details(1,2) + object \ No newline at end of file diff --git a/Feb22/session3/helperfunctions.py b/Feb22/session3/helperfunctions.py index 38de2c0..a77567c 100644 --- a/Feb22/session3/helperfunctions.py +++ b/Feb22/session3/helperfunctions.py @@ -17,4 +17,5 @@ def get_first_value(qs_dict, key, default=""): qs = 'name=python&topic=effective&batch=1' qs_dict = parse_qs(qs) print(f"{get_first_value(qs_dict, key='name')} {get_first_value(qs_dict, key='topic')}") + diff --git a/Feb22/session3/sampleclass.py b/Feb22/session3/sampleclass.py new file mode 100644 index 0000000..bb2a853 --- /dev/null +++ b/Feb22/session3/sampleclass.py @@ -0,0 +1,28 @@ +class BankAccount: + total_count = 0 + + def __init__(self, name, branch): + self.name = name + self.branch = branch + self.account_number = f"{branch}-{self.total_count}" + self.balance = 0 + + + def deposit(self, amount): + self.balance += amount + + def withdraw(self, amount): + self.balance -= amount + + @classmethod + def increment_count(cls): + cls.total_count += 1 + + @staticmethod + def print_hello(): + print("hello") + +if __name__ == '__main__': + BankAccount.increment_count() + b = BankAccount('bhawana', 'ameerpet') + b.deposit(10000)