1,105,427 Community Members

Help creating modules

Member Avatar
bharatk
Light Poster
33 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
bharatk
Light Poster
33 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: