Advertisement

09.02.2008 at 02:50PM PDT, ID: 23697606
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.3

How can I make this python coding more elegant..and shorter

Asked by optimum_ in Python Scripting Language

Tags:

I want to make this coding more elegant..or shorter, but still provides the same functions. I want to know how a trained programmer would make this? Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from time import *
 
queue = []
abc = 0
 
 
 
 
#classes and functions
 
 
 
 
 
class Patient(object):
    
    
        '''
        Program creating an object with the patient class with all the information
        of the patient is stored as attributes of the object.
        The queue is stored as a global of the objects called "queue" '''
 
 
 
 
 
    def queue_patient(patid, forename, initial, surname):
 
        '''function of the patient queue which creates a object of the patient class
        which then adds an object to the tail..-1 if there is same id..or 0 if there
        is no error with the id
        '''
 
        a = Patient()
        a.id = patid
        a.forename = forename
        a.initial = initial
        a.surname = surname
 
        x = 0
 
        for patient in queue:
            if patient.id == patid:
                return -1
 
        queue.append(a)
        return 0
        
 
 
#Reads the queue
 
    def read_queue(filename):
 
        '''  Calls this function when the program starts.
        Function is to read the queue in the parameter filename'''
 
        fin = open(filename, "r")
        for line in fin:
            s = line
            nonewline = s.replace("\n","")
            record = nonewline.split(" ")        
            patid = int(record[0])
            forename = record[1]
            initial = record[2]
            surname = record[3]
            queue_patient(patid, forename, initial, surname)
 
 
 
 
 
 
 
 
#Next patient
            
    def treat_next_patient():
 
        '''This function deletes head of the queue or it returns -1 if there
        is an error or no patients'''
 
        try:
            del queue[0]
        except IndexError:
            print "There are no patients in queue."
            print
 
 
 
 
 
 
#Writes to queue
 
    def write_queue(filename):
 
        '''This function writes to the queue in the parameter filename
        using the same format specified by def read_queue'''
 
        fin = open(filename, "w")
 
        for patient in queue:
            s = str(str(patient.id) + " " + patient.forename + " " \
                + patient.initial + " " + patient.surname + "\n")
            fin.write(s)
 
        fin.close()
 
 
 
 
 
 
#Patient detail
 
    def list_patient(index):
 
        '''This function prints one row of text giving all the details
        stored in queue[index]. Returns -1 if index
        does not point at a record in the queue, 0 oth-
        erwise.'''
 
        try:
            pdetails = queue[index]
        
            print "ID: %-10i Forename: %-10s Initial: %-10s Surname: %s" % \
                  (pdetails.id, pdetails.forename, pdetails.initial,
                   pdetails.surname)
 
        except IndexError:
            print "There are no patient in index %i" % (index)
 
 
 
 
 
 
#View list(queue)
 
    def list_queue():
 
            '''This function print a list of the queue, by calling list _patient'''
            
            for patient in queue:
            print "ID: %-10i Forename: %-10s Initial: %-10s Surname: %s" % \
                      (patient.id, patient.forename, patient.initial,
                       patient.surname)
 
 
 
#Moves patient
 
 
    def move_patient(index1,index2):
 
            '''This function moves the patient at queue position index1 to
            queue position index2'''
            
        try:
            move1 = queue.pop(index1)
            move2 = queue.insert(index2, move1)
        except IndexError:
            print "The Index out of range."
 
 
 
 
#Deletes patient
 
    def delete_patient(index):
 
        '''This function deletes the patient referred to by
        queue[index]'''
 
        try:
            del queue[index]
        except IndexError:
            print "There are no patient found in index ", index
 
 
 
 
 
#Find patient
            
    def find_patient_id(patid):
 
        '''Find the patient by ID otherwise it returns -1 '''
 
        for patient in queue:
            if patient.id == patid:
                print "ID: %-10i Forename: %-10s Initial: %-10s Surname: %s" % \
                      (patient.id, patient.forename, patient.initial,
                       patient.surname)
                return
        return -1
 
 
 
 
 
 
#Edit detail
 
    def edit(index, detail):
 
        '''Added an extra function, so it makes the program more user friendly
        Edits the detail of the current patient'''
 
        a = queue[index]
        if detail == 'id':
            patid = input("Please enter new ID: ")
            for patient in queue:
                if patient.id == patid:
                    print "Patient already exists with that ID..."
                    return
                else:
                    a.id = patid
            
        elif detail == 'forename':
            a.forename = raw_input("Please enter a new forename: ")
        elif detail == 'initial':
            a.initial = raw_input("Please enter a  new initial: ")
        elif detail == 'surname':
            a.surname = raw_input("Please enter a new surname: ")
        else:
            print 'Invalid Selection'
            return
        print "Editing Details..."
        sleep(10)
        print "....Edited"
        print
 
 
 
#=======END OF FUNCTIONS========
 
 
 
 
 
#======Program startup======
        
    '''Loads up the menu and starts calling all the function to make the program
    work'''
 
    try:
        read_queue(raw_input("What filename would You like to open: "))
        
    except IOError:
        print "There is no such file..."
        print "Loading default file.."
        read_queue("filename")
               
    print
    print
    print "NHS Queue"
    while abc == 0:
        
        print "\n                MENU                 "
        print
        print
        print
        print "1:          Add new patient"
        print "2:          Next patient"
        print "3:          Save file"
        print "4:          Print patient in index(x)"
        print "5:          Print patient queue"
        print "6:          Move patient queue"
        print "7:          Delete patient"
        print "8:          Find patient (ID) "
        print "9:          Edit patient details"
        print
        print "10:         EXIT"
        print
        print
        print
 
        try:
            menuselect = input("\nPlease Select an Option: ")
            print
            if menuselect == 1:
                if queue_patient(input("ID: "), raw_input("Forename: "),\
                            raw_input("Initial: "), raw_input("Surname: ")) == 0:
                   print "Patient successfully added"
 
                else:
                   print "\nPatient already exists with that ID Number"
 
            elif menuselect == 2:
                treat_next_patient()
 
            elif menuselect == 3:
                write_queue(raw_input("Where would you like the file to be saved:... "))
                print "File is now saved..."
 
            elif menuselect == 4:
                list_patient(input("Enter the position of the patient:... "))
 
            elif menuselect == 5:
                list_queue()
 
            elif menuselect == 6:
                move_patient(input("Move from position: "), input("To position: "))
 
            elif menuselect == 7:
                delete_patient(input("Delete the patient in position:... "))
 
            elif menuselect == 8:
                if(find_patient_id(input("Patient ID:... "))) == -1:
                    print "No such patient exists with that ID number..."
 
            elif menuselect == 9:
                edit(input("ID of patient: "), \
                                    raw_input("CHange details in this format(id, forename, initial,"
                                              " surname): "))
                
            elif menuselect == 10:
                   abc = 1
 
            else:
                print "InvAlid Selection. Try again."
 
        except NameError:
            print "\nInvalid Selection. Try again."
 
 
 
 
 
#Saves to file before exitting, as requested#
            
    write_queue(raw_input("Save On exit? "))
 
Loading Advertisement...
 
[+][-]09.02.2008 at 09:54PM PDT, ID: 22374223

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.02.2008 at 11:51PM PDT, ID: 22374650

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Python Scripting Language
Tags: Getting error messages..
Sign Up Now!
Solution Provided By: pepr
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628