Skip to content

Commit d3074f3

Browse files
committed
Added Base class implementation for reading items from csv file
1 parent 051dc8e commit d3074f3

6 files changed

Lines changed: 31 additions & 24 deletions

File tree

Dec21/InventoryManagement/data/products.csv

Lines changed: 0 additions & 2 deletions
This file was deleted.

Dec21/InventoryManagement/data/stocks.csv

Lines changed: 0 additions & 3 deletions
This file was deleted.

Dec21/InventoryManagement/main.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ def get_input_from_user(field_name):
1111
return result
1212

1313
if __name__ == '__main__':
14-
s1 = Stock(2,11)
15-
s1.save()
14+
#s1 = Stock(2,11)
15+
#s1.save()
1616

17-
p1 = Product(1, 'test', 'test', 'test', 100.45)
18-
p1.save()
19-
#print(s1)
17+
#p1 = Product(1, 'test', 'test', 'test', 100.45)
18+
#p1.save()
19+
items = Product.items()
20+
for item in items:
21+
print(item)
2022

2123
# id = get_input_from_user('id')
2224
# name = get_input_from_user('name')

Dec21/InventoryManagement/models/baseinventory.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import os
33
import csv
44

5+
import inventory
6+
57

68
class BaseInventoryModel:
79
"""
@@ -36,5 +38,23 @@ def save(self):
3638
writer.writeheader()
3739
writer.writerow(field_dict)
3840

41+
@classmethod
42+
def items(cls):
43+
"""
44+
Returns all the objects by reading the records in the csv file
45+
"""
46+
if not os.path.exists(cls._file_name):
47+
return []
48+
with open(cls._file_name, 'r') as csv_file:
49+
reader = csv.DictReader(csv_file)
50+
#inventory_items = []
51+
#for row in reader:
52+
# inventory_item = cls(**row)
53+
# inventory_items.append(inventory_item)
54+
inventory_items = [cls(**row) for row in reader]
55+
return inventory_items
56+
57+
58+
3959

4060

Dec21/InventoryManagement/models/products.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,7 @@ def __str__(self):
2929
"""
3030
This is string representation of object
3131
T"""
32-
return(f"{self.id}, {self.name}, {self.description}, {self.category}, {self.mrp}, {self.created_at}")
32+
return(f"{self.id}, {self.name}, {self.description}, {self.category}, {self.mrp}, {self.created_at}, {self.updated_at}")
3333

3434

35-
@classmethod
36-
def ids(cls):
37-
"""
38-
This method will read all the existing ids from the csv file
39-
"""
40-
item_ids = []
41-
with open(cls.file_name, 'r') as file:
42-
reader = csv.reader(file, delimiter = ',')
43-
for row in reader:
44-
if len(row) > 0:
45-
item_ids.append(row[0])
46-
return item_ids
35+

Dec21/InventoryManagement/models/stock.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ class Stock(BaseInventoryModel):
77
"""
88
This class represents the Stock of the items in the Store
99
"""
10-
def __init__(self, id, quantity, created_at=None, updated_at=None) -> None:
10+
def __init__(self, id, product_id, quantity, created_at=None, updated_at=None) -> None:
1111
super().__init__(created_at, updated_at)
1212
self.id = id
13+
self.product_id = product_id
1314
self.quantity = quantity
1415

1516

0 commit comments

Comments
 (0)