Hello

I have 4 .py files along with __init__.py in a folder.
I want to create a file which would act as a menu and load classes from the files in that folder.

I have that file outside the folder.

The contents of that file:

from files import *

print "CPU Scheduling Algorithm Simulation\n"
print "Which Algorithm do you want to run? "
print "1.FCFS\n2.SJF\n3.Priority Scheduling\n4.Round Robin Scheduling\n"
choice=input()

if choice==1:
	fc=FCFS()
	fc
elif choice==3:
	pr=Priori
	pr
elif choice==2:
	import sjf #as this file does not have a class
elif choice==4:
	ro=RRobin()
	ro
else:
	print "Wrong choice"
raw_input()

So the folder structure is like this:

CPUSh.py
files [ this is a folder]
|
->__init__.py
->fcfs.py
->priority.py
-> sjf.py
->rr.py

each .py files mentioned above has all the code and functions inside single class(which I am instantiating in the CPUSh.py file)

So how do I make it in such a way that when I enter a particular choice the corresponding file executes?

Thank you

I did a few changes & achieved what I wanted.

This is the changes I made to CPUSh.py:

print "CPU Scheduling Algorithm Simulation\n"
print "Which Algorithm do you want to run? "
print "1.FCFS\n2.SJF\n3.Priority Scheduling\n4.Round Robin Scheduling\n"
choice=input()

if choice==1:
	from files.fcfs import FCFS
	fc=FCFS()
	fc
elif choice==3:
	from files.priority import Priori
	pr=Priori()
	pr

elif choice==2:
	import files.sjf
	
elif choice==4:
	from files.rr import RRobin
	ro=RRobin()
	ro
else:
	print "Wrong choice"

raw_input()

But is this the right way to create modular code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.