I am currently working on an inventory system that I have mocked up so far.
This is the following code:
import csv
class start_store:
def __init__(self):
self.name = self.ask()
self.options = self.ask_options()
def ask(self):
while 1:
name = input("What is your name?\n")
if name == "":
print("Ha! You have to enter a name!")
else:
print("Welcome to the Shepherdstown Bake Shop " name)
return name
def ask_options(self):
while 1:
option = input('''What would you like to do? \n1. Add a Item: \n2. Delete a Item:\n3. Edit an Item: \n4. View Inventory \n5. End Program\n''')
if option == "4":
print("Here is the following inventory we have " self.name)
items = CsvReader()
items.make_dict_items()
items.show_available()
break
else:
print("You have to enter a valid option " self.name)
and the second class:
class CsvReader:
def __init__(self):
self.result = []
def make_dict_items(self):
with open("Items2.csv") as fp:
reader = csv.reader(fp)
labels = next(reader, None)
result = []
for row in reader:
row[0] = int(row[0])
row[1] = float(row[1])
row[2] = int(row[2])
pairs = zip(labels, row)
self.result.append(dict(pairs))
def show_available(self):
for item in self.result:
print(item)
obj = start_store() # create the instance
while 1:
obj.ask_options() # use the instance
From what I understand, I am able to run my code in IDLE based upon the last three lines in my second class, which initialize, and run. The program runs from : asking the user their name -> asking the user what they would like to do -> performing said task-> looping back to ask what to do.
I am confused on how I would implement a main method that runs my current program in that order? I understand how to make a main method, it being if __name__ == '__main__':
, but what would I put under it? Would I have it run ask() like in the bottom of my lines? How do I add a main method that runs my program in this order in the sense that it is able to run now in IDLE without a main method?
I apologize if I am overlooking something, I am fairly new to Python and OOP.
CodePudding user response:
Create 3 files: one file called csv_reader.py
, one file called start_store.py
, and a third called main.py
. Place your CsvReader
and start_store
classes in the appropriate files, and in main.py
write this:
from start_store import start_store
from csv_reader import CsvReader
if __name__ == "__main__" :
obj = start_store() # create the instance
while 1:
obj.ask_options() # use the instance
In your csv_reader.py
file you should remove the code which creates an obj and the while loop - this should only be in the main.py
file! This new file will include both of your classes (start_store
and CsvReader
) and run your main loop. You can run this with python main.py
. It is worth noting that the file does not need to be called main.py
(it can be called anything), and both classes could be included in the same file as the __main__
function, but this is just a simple example of how to separate your classes and then run them in a main loop.
CodePudding user response:
With python whether you should have multiple classes in a file is a matter of opinion rather than a rule. If the classes are small then why not. So all you need to do is put your if name == 'main': ahead of obj = start_store() ...
If the script is run from the command line to be able to cancel with Ctrl C I like to use ...
def main()
obj = start_store() etc ...
return 0
if __name__ == "__main__":
try: # handle Ctrl C
sys.exit(main())
except KeyboardInterrupt:
sys.exit(0)