Re: [Tutor] Shorten Code
On 22/11/11 19:41, Mic wrote: A Tkinter program is never going to be an online GUI, it only works on the desktop. For that you need to use HTML5/Flash/Javascript/Java etc. And its all a lot more messy. No, I didn't mean that it have to work online. It is just fine if it works on the desktop. OK, So I'll guess you want a desktop program that eventually sends commands to an online service? If so thats a lot easier to do with Tkinter... :-) def chair1_clicked(self): """This method runs if chair one is clicked""" First thing to do is take the variable out of the function name and pass it as a parameter. def chair_clicked(self, chairID): Now you can call that and pass in the actual chair that was clicked as a parameter. (I'm going to assume you went for a list of chairs so the actual value of aChair is the index in the list) def change_chair1_value(): global chair1_value button2_value=not chair1_value You really want to stop using global values. Make them attributes of your class. Life will be much easier! And you won;t need all these little functions cluttering up your code. if chair_value: Becomes if self.chairs[chairID][0] This assumes you store the value and button as a tuple and value is the first element. So when you create the chairs your code looks like: for ch in ch_data: chairs.append((ch_data[0], Button(self, text=ch_data[1],) ) self.chair1.configure(bg="yellow") You are still hard coding the color rather than using your variables text_file=open("Hamburg_Dortmund20_00","w") text_file.write("Hamburg-Dortmund") text_file.close() Hmmm, that looks suspicious. You just created a text file with one line. def chair_color_red(): global chair1_color chair1_color=("yellow") change_button2_color_red() The function says red but the code says 'yellow' Again take the value out of the function name and make it a parameter. def set_chair_color(chairID, col): chairs[chairID][1] = col And now I'm assuming the second value of chairs is the color. Its getting complicated. Maybe time we introduced dictionaries(*) Then it looks like: def set_chair_color(chairID, col): chairs[chairID]['color'] = col and your initialisation code looks like: for id,ch in enumerate(ch_data): chairs[id] = {'value':0, 'color':'white', 'button' = Button(self, text=ch_data[0],) ) self.chair1.configure(bg="green") os.remove ("Hamburg_Dortmund20_00") And now you delete that file you created without having done anything with it? I suspect this is all a bit much in one go, if so, we can break it down into more manageable chunks. (*)Instead of a dictionary we could have used a class but I think that's a leap too far for you just yet. But this is a project that would be easier using classes and objects. An exercise for the future. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Shorten Code
Button1 is supposed to represent chair one in the train. So call it chair_1 or put it in a list called chairs so that you can access them as chairs[0] or whatever. But get a name that reflects what its actually used for. Yes, perhaps that is a good way to go. I didn't think about that. at the top of my email, I am writing a larger program. This program is supposed to be a GUI online booking program for train tickets. OK, That's a pity. A Tkinter program is never going to be an online GUI, it only works on the desktop. For that you need to use HTML5/Flash/Javascript/Java etc. And its all a lot more messy. - No, I didn't mean that it have to work online. It is just fine if it works on the desktop. I can only imagine how much more complicated things would be then. But if I understood it correctly, with the help and information I got from you in this mail and in your previous mail, I can manage to solve my problems? It is a bit hard right now to understand, because I am unfamiliar with using tuples/lists and data tables. I will do my best to get it to work. However, I understand if you don't have time to answer these probably stupid and simple questions, I am still grateful for your previous answers! They are neither simple nor stupid, they are very sensible questions and you are tackling a significant program. Sadly I don't think your approach will work for an 0nline solution (ie a web based one). I just got my program to work today, but I want to shorten the code using your suggestions in this and your previous email. I still find this hard to do because that way of thinking is completely new to me. I have one of these functions below for each chair. I just want to use one instead of so many functions. That is why I am trying to understand and implement your suggestions. def chair1_clicked(self): """This method runs if chair one is clicked""" def change_chair1_value(): global chair1_value button2_value=not chair1_value chair1_clicked () if chair_value: self.chair1.configure(bg="yellow") text_file=open("Hamburg_Dortmund20_00","w") text_file.write("Hamburg-Dortmund") text_file.close() def chair_color_red(): global chair1_color chair1_color=("yellow") change_button2_color_red() else: self.chair1.configure(bg="green") os.remove ("Hamburg_Dortmund20_00") def chair1_color_green(): global chair1_color chair_color=("green") chair1_color_green() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Shorten Code.
> >> > > for var in [value,value_1]: >> > > var = "green" >> >> > Um, that won't work. You typed that example too quickly. > > >> Excuse: It was early morning and I hadn't had any coffee... > >> In your defense Alan, after you typed that code in your response you >mentioned > the necessity of defining the variables: Yes, but that's still not good enough. The loop above only sets var to green >it doesn't change the values of the variables in the list. That's why you need >to use enumerate and access them via an index. Basically its just not a good >plan, best to avoid it. Alan G.___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Shorten Code.
On Nov 18, 2011, at 13:15, ALAN GAULD wrote: > > > > for var in [value,value_1]: > > >var = "green" > > > > Um, that won't work. You typed that example too quickly. > > Oops! Yes. You'd need to enumerate and access the variables > via an index. yuk. Don't do it folks! :-) > > Excuse: It was early morning and I hadn't had any coffee... > > Alan G. > In your defense Alan, after you typed that code in your response you mentioned the necessity of defining the variables: >But you need to have already created the variables >somewhere and unless there >is a big list its not usually >worth while. Alexander Etter > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Shorten Code.
> > for var in [value,value_1]: > >var = "green" > > Um, that won't work. You typed that example too quickly. Oops! Yes. You'd need to enumerate and access the variables via an index. yuk. Don't do it folks! :-) Excuse: It was early morning and I hadn't had any coffee... Alan G.___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Shorten Code.
On 11/18/2011 04:01 AM, Alan Gauld wrote: On 18/11/11 08:16, Mic wrote: What if I don’t write the same line of code more than once, but I write similiar lines more than once. Is that okay? Ler For example: value="green” value_1=”green” If you had a lot of these you could shorten it with a loop (BTW the English term in programming terminology is "loop" not "sling" ;-) for var in [value,value_1]: var = "green" Um, that won't work. You typed that example too quickly. Mic, the problem is not "shortening code," but making it more readable, and easier to maintain. If you have a series of variables that hold similar or identical values, or which are treated in consistent ways, then you should probably make a list out of them. And that will naturally shorten code like this. student0 = 4 student1 = 3 student2 = 12 student3 = 11 Replace with students = list((4,3,12,11)) Then if you want to deal with a particular student, you might do student[2] = 5 But if you want to deal with the ith student, you could do student[i] = and if you want to do something with all of them: for index, student in enumerate(students): students[indes] += 65 There are plenty of more advanced methods that would make even that simpler, but I'm trying to keep it simple. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Shorten Code.
On 18/11/11 08:16, Mic wrote: What if I don’t write the same line of code more than once, but I write similiar lines more than once. Is that okay? Ler For example: value="green” value_1=”green” If you had a lot of these you could shorten it with a loop (BTW the English term in programming terminology is "loop" not "sling" ;-) for var in [value,value_1]: var = "green" But you need to have already created the variables somewhere and unless there is a big list its not usually worth while. One other trick you can use for this specific type of assignment is value = value_1 = "green" But it gets a bit unreadable for long lists of names. click=-1 click1=-1 click2=-1 Same here, but imagine it had been: click=-1 click1= 2 click2=-1 And here you are changing both name and value. The best abbreviation here is probably tuple expansion: click, click1, click2 = -1,2,-1 > Do you have any general tips on how to make your code shorter? Shorter is not necessarily better. Clarity is far more important and you should always consider whether tricks like those above are helping or hindering clarity. Only use them if they make your code easier to read. (Also they all make debugging slightly harder so you should think about that too) HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Shorten Code.
From: Wayne Werner Sent: Thursday, November 17, 2011 8:30 PM To: Mic Cc: tutor@python.org Subject: Re: [Tutor] Clock in tkinter? Say that I have a class and I want to make 100 objects. Then it could look like this: class Chairs(object): #Create the objects chair1=Chairs("10","20") chair2=Chairs("10","20") chair3=Chairs("10","20") How do I shorten this? I have thought of using a for sling. I have looked in my programming book and on the internet, but I don’t know how to make this shorter. The arguements (“10”, “20”) should be the same for every object, which should make it easier than if they were different each time? If you ever write a line of code more than once, it's a good sign that you have what's called a code smell. This example is very smelly code ;) What you should do instead is have a collection of chairs: chairs = [] for _ in range(100): # the underscore `_` indicates that you don't care about the value chairs.append(Chairs("10","20)) -- Yes, I got that right now. But now that you talked about shortening code, I have a general question. What if I don’t write the same line of code more than once, but I write similiar lines more than once. Is that okay? For example: value="green” value_1=”green” click=-1 click1=-1 click2=-1 I know that I can make this shorter, with a for sling for example, but the problem is that I need to use these variables later in my program, and I don’t know how do to then, to be able to use them later on, in a function for example. Do you have any general tips on how to make your code shorter? I also hope I have learnt to post better now. Thanks! Mic <>___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor