Re: [Tutor] 3 questions for my port scanner project
2. I got a while loop which does the port scan itself. How can I end it while its working ? using the break statement anywhere inside the loop will exit the loop. 3. For some reason the scan is too slow (2-3 seconds for a port). Is there a way to make it faster (other port scanner work allot faster... The ports which do not respond are the ones which take most of the time.You can use the timer object to fix the time for each port.For eg. if a port does not respond within .1 sec it can reasonably be expected to be closed.The exact implementation will depend upon your code.You can also use threads to ping more than one port simultaneously. And I loved your microsoft quote :). Cheers -- 1. The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners. 2. Unix is user friendly - it's just picky about it's friends. 3. Documentation is like sex: when it is good, it is very, very good. And when it is bad, it is better than nothing. - Dick Brandon ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor __ Do you Yahoo!? Read only the mail you want - Yahoo! Mail SpamGuard. http://promotions.yahoo.com/new_mail ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Recursive Tkinter buttons
On Sat, 26 Feb 2005 19:48:25 + Adam Cripps [EMAIL PROTECTED] wrote: On Fri, 25 Feb 2005 12:21:18 +0100, Michael Lange snip You see, in my example above I called the list buttonlist instead of button; maybe this naming helps avoid confusion . Best regards Michael Many thanks for the help here. I got all my buttons displayed and stored in the list with: for i in range (1, 11): submittext = self.s = Button(text=submittext, command = self.showButton) self.s.grid(column=4, row=i+4) submitlist.append(self.s) Hi Adam, note that there's no use in making the button an attribute of its parent class with self.s = Button(text=submittext, command = self.showButton) because self.s gets overridden on every iteration in the for-loop; it's not a bug, but might be a source of confusion, when you try to access self.s later in your code; you don't need the reference anyway, because you keep the references to all buttons in the list. - however, when I click the button, I want self.showButton to know which one of them was pressed. I've seen in other gui programming the idea of an id or identifier - I can't see that here. Ideally, I would like to know the value of i in self.showButtons - but when I use self.showButtons(i) showButtons gets called straight at run time. Any ideas? You have two options here: 1. use a lambda expression as button command, lambda allows you to pass an argument to the callback: s = Button(text=submittext, command = lambda index=i: self.showButton(index)) 2. if you don't like lambdas, you can use the button's bind() method instead of the command option: s = Button(text=submittext) s.bind('ButtonRelease-1', self.showButton) s.bind('KeyRelease-space', self.showButton) bind() passes an event to the callback which allows you to find out which widget sent the event via the event's widget attribute: def showButton(self, event): button = event.widget print button['text'] I hope this helps Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Recursive Tkinter buttons
- however, when I click the button, I want self.showButton to know which one of them was pressed. I've seen in other gui programming the idea of an id or identifier - I can't see that here. Ideally, I would like to know the value of i in self.showButtons - but when I use self.showButtons(i) showButtons gets called straight at run time. The easy way to do this is to use a defrault parameter in a lambda: submittext = for i in range (1, 11): b = Button(text=submittext, command = lambda n=i: self.showButton(n)) b.rid(column=4, row=i+4) submitlist.append(b) Notice the button creation line now uses a lambda with default parameter set to i. That lambda calls your method but now passes in the value of i for that button. (You need to modify your method to accept that value of course!) HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 3 questions for my port scanner project
On Sun, 27 Feb 2005 03:24:07 -0800 (PST), Shitiz Bansal [EMAIL PROTECTED] wrote: The ports which do not respond are the ones which take most of the time.You can use the timer object to fix the time for each port.For eg. if a port does not respond within .1 sec it can reasonably be expected to be closed.The exact implementation will depend upon your code.You can also use threads to ping more than one port simultaneously. Thank you very much for yuor help !! Now I only need to figure out how to make a progress bar and my trubles are over :) -- 1. The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners. 2. Unix is user friendly - it's just picky about it's friends. 3. Documentation is like sex: when it is good, it is very, very good. And when it is bad, it is better than nothing. - Dick Brandon ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sys.argv[1: ] help
Danny Yoo wrote: I am reading ' Learning Python second edition' by Mark Lutz and David Ascher, and I trying the code examples as I go along. However I am having a problem with the following, which I don't seem to be able to resolve :- # test.py import sys print sys[ 1: ] This I believe is supposed to print the 1st argument passed to the program. However if I try test.py fred All I get at the command line is [] Hi Jay, Are you sure that is what your program contained? I'm surprised that this didn't error out! The program: ## import sys print sys[1:] ## should raise a TypeError because 'sys' is a module, and not a list of elements, and modules don't support slicing. Just out of curiosity, can you confirm that you aren't getting an error message? (I know I'm being a bit silly about asking about what looks like a simple email typo, but computer programming bugs are all-too-often about typos. *grin* When you write about a program, try using cut-and-paste to ensure that the program that you're running is the same as the program you're showing us.) Best of wishes to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Hi, Sorry for the late response, I tried all of the the suggestions, including correcting my typo of print sys[1:] and tried print sys,argv[1:], this does now work as long as I run 'python test.py fred joe' it returns all the arguments. If I try just test.py all I get is '[]' . Is there something wrong with my environmental variables in Windows XP, I would like to be able to just use the file name rather than having to type python each time. Any help would be gratefully received. Richard G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sys.argv[1: ] help
Richard, if you try to print sys.argv[1:] when sys.argv only contain sys.argv[0] then you are bound to get an empty list returned, [] . Im not sure I understand the problem you think you've got but here's what happens with sys.argv for me, and it's correct. [argl.py] $ cat argl.py #!/usr/bin/python import sys print sys.argv[1:] ./argl.py [] ./argl.py a b c ['a', 'b', 'c'] Is that what your getting ? Sorry for the late response, I tried all of the the suggestions, including correcting my typo of print sys[1:] and tried print sys,argv[1:], this does now work as long as I run 'python test.py fred joe' it returns all the arguments. If I try just test.py all I get is '[]' . Is there something wrong with my environmental variables in Windows XP, I would like to be able to just use the file name rather than having to type python each time. Any help would be gratefully received. Richard G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Re: How do you share a method (function) among several objects?
Javier Ruere wrote: Xif wrote: Hello There are several different objects. However, they all share the same function. Since they are not the same or similar, it's not logical to use a common superclass. So I'm asking, what's a good way to allow those objects to share that function? The best solution I've found so far is to put that function in a module, and have all objects import and use it. But I doubt that's a good use-case for modules; writing and importing a module that contains just a single function seems like an abuse. Thanks, Xif Could you give an example? Javier ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor +++ This Mail Was Scanned By Mail-seCure System at the Tel-Aviv University CC. Sure, I can describe my particular case. It's a program that retrieves / updates Microsoft Excel spreadsheet data. There are two major classes: 1) an Excel class, that represents of the whole Excel program 2) a Cells class, that abstracts retrieval and editing of cells. Both classes use a function called getCells() as part of their __getitem__() methods. getCells() parses the __getitem__() call arguments, and returns an iterator over the appropriate cells. The difference between the 2 classes is that a Cells instance just converts the generator into a list and returns it: #code return list(getCells(self.sheet, cells)) #/code while an Excel instance returns the values of the cells: #code return [cell.Value for cell in getCells(self.sheet, cells)] #/code As you can see, both use the getCells() function. So my question is, where is the best way to put it so instances of both classes can use it? Xif ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sys.argv[1: ] help
Are you using XP still? I've never seen this before - ./arg1.py a b c But anyhoo, I tried out just 'c:\python23\foo.py' as opposed to 'c:\python23\python foo.py' and while foo.py will run, it doesn't echo to the console, as on my machine running a .py file runs it through pythonw.exe - I'd check it out for your machine, it's probably the same. You'd need to change the association to python.exe, but that would mean that you always got a DOS box for every Python script you ran, which is annoying with GUIs. Erm, if you don't want to type in python each time, either change the association or create a batch file called x or a or something that runs Python and stick it in a directory that's in your PATH system variable. Only problem with that is passing command line variables ...might just be better to type python Good Luck, Liam Clarke On Sun, 27 Feb 2005 17:55:54 +, Richard gelling [EMAIL PROTECTED] wrote: Hi, No What I get if I was to type in ./arg1.py a b c All I get is [] If i type at the command prompt python arg1.py a b c I get ['a','b','c'] as expected All the other programs and examples I have typed in work fine just by typing in the file name, I don't have to preced the file name with python, only this example. I hope this makes it clearer Richard G. Nick Lunt wrote: Richard, if you try to print sys.argv[1:] when sys.argv only contain sys.argv[0] then you are bound to get an empty list returned, [] . Im not sure I understand the problem you think you've got but here's what happens with sys.argv for me, and it's correct. [argl.py] $ cat argl.py #!/usr/bin/python import sys print sys.argv[1:] ./argl.py [] ./argl.py a b c ['a', 'b', 'c'] Is that what your getting ? Sorry for the late response, I tried all of the the suggestions, including correcting my typo of print sys[1:] and tried print sys,argv[1:], this does now work as long as I run 'python test.py fred joe' it returns all the arguments. If I try just test.py all I get is '[]' . Is there something wrong with my environmental variables in Windows XP, I would like to be able to just use the file name rather than having to type python each time. Any help would be gratefully received. Richard G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Re: How do you share a method (function) among several objects?
You could a real generic superclass for the classes, it only needs to contain that one function. Personally, I see nothing wrong with chucking one function in a module on it's own, it's whatever works for you. You could create a class for it also, and give each of the other classes an instance of that function's class. Regards, Liam Clarke On Sun, 27 Feb 2005 20:20:19 +0200, Xif [EMAIL PROTECTED] wrote: Javier Ruere wrote: Xif wrote: Hello There are several different objects. However, they all share the same function. Since they are not the same or similar, it's not logical to use a common superclass. So I'm asking, what's a good way to allow those objects to share that function? The best solution I've found so far is to put that function in a module, and have all objects import and use it. But I doubt that's a good use-case for modules; writing and importing a module that contains just a single function seems like an abuse. Thanks, Xif Could you give an example? Javier ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor +++ This Mail Was Scanned By Mail-seCure System at the Tel-Aviv University CC. Sure, I can describe my particular case. It's a program that retrieves / updates Microsoft Excel spreadsheet data. There are two major classes: 1) an Excel class, that represents of the whole Excel program 2) a Cells class, that abstracts retrieval and editing of cells. Both classes use a function called getCells() as part of their __getitem__() methods. getCells() parses the __getitem__() call arguments, and returns an iterator over the appropriate cells. The difference between the 2 classes is that a Cells instance just converts the generator into a list and returns it: #code return list(getCells(self.sheet, cells)) #/code while an Excel instance returns the values of the cells: #code return [cell.Value for cell in getCells(self.sheet, cells)] #/code As you can see, both use the getCells() function. So my question is, where is the best way to put it so instances of both classes can use it? Xif ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sys.argv[1: ] help
Yeah, right click on a .py and check if it's associated with pythonw or python.exe GL, Liam Clarke On Sun, 27 Feb 2005 18:28:18 +, Richard gelling [EMAIL PROTECTED] wrote: Hi, Yes, I use both Wndows XP and Linux( at work ) . I left that in by mistake I am actually just typing in arg1,py a b c at the windows XP command prompt Sorry for the confusion. Liam Clarke wrote: Are you using XP still? I've never seen this before - ./arg1.py a b c But anyhoo, I tried out just 'c:\python23\foo.py' as opposed to 'c:\python23\python foo.py' and while foo.py will run, it doesn't echo to the console, as on my machine running a .py file runs it through pythonw.exe - I'd check it out for your machine, it's probably the same. You'd need to change the association to python.exe, but that would mean that you always got a DOS box for every Python script you ran, which is annoying with GUIs. Erm, if you don't want to type in python each time, either change the association or create a batch file called x or a or something that runs Python and stick it in a directory that's in your PATH system variable. Only problem with that is passing command line variables ...might just be better to type python Good Luck, Liam Clarke On Sun, 27 Feb 2005 17:55:54 +, Richard gelling [EMAIL PROTECTED] wrote: Hi, No What I get if I was to type in ./arg1.py a b c All I get is [] If i type at the command prompt python arg1.py a b c I get ['a','b','c'] as expected All the other programs and examples I have typed in work fine just by typing in the file name, I don't have to preced the file name with python, only this example. I hope this makes it clearer Richard G. Nick Lunt wrote: Richard, if you try to print sys.argv[1:] when sys.argv only contain sys.argv[0] then you are bound to get an empty list returned, [] . Im not sure I understand the problem you think you've got but here's what happens with sys.argv for me, and it's correct. [argl.py] $ cat argl.py #!/usr/bin/python import sys print sys.argv[1:] ./argl.py [] ./argl.py a b c ['a', 'b', 'c'] Is that what your getting ? Sorry for the late response, I tried all of the the suggestions, including correcting my typo of print sys[1:] and tried print sys,argv[1:], this does now work as long as I run 'python test.py fred joe' it returns all the arguments. If I try just test.py all I get is '[]' . Is there something wrong with my environmental variables in Windows XP, I would like to be able to just use the file name rather than having to type python each time. Any help would be gratefully received. Richard G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Re: How do you share a method (function) among several objects?
Well, for me, the more logical answer is : multi-inheritance ! If part of your class is the same, (same semantic, same implementation), then you want to have a base class for that. If you dislike this kindof inheritance, then your function should be an external one. Even more because it's 'just' an implementation function. The user don't need it as a method ... So why bother add it to your object ? Pierre Xif a écrit : Javier Ruere wrote: Xif wrote: Hello There are several different objects. However, they all share the same function. Since they are not the same or similar, it's not logical to use a common superclass. So I'm asking, what's a good way to allow those objects to share that function? The best solution I've found so far is to put that function in a module, and have all objects import and use it. But I doubt that's a good use-case for modules; writing and importing a module that contains just a single function seems like an abuse. Thanks, Xif Could you give an example? Javier ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor +++ This Mail Was Scanned By Mail-seCure System at the Tel-Aviv University CC. Sure, I can describe my particular case. It's a program that retrieves / updates Microsoft Excel spreadsheet data. There are two major classes: 1) an Excel class, that represents of the whole Excel program 2) a Cells class, that abstracts retrieval and editing of cells. Both classes use a function called getCells() as part of their __getitem__() methods. getCells() parses the __getitem__() call arguments, and returns an iterator over the appropriate cells. The difference between the 2 classes is that a Cells instance just converts the generator into a list and returns it: #code return list(getCells(self.sheet, cells)) #/code while an Excel instance returns the values of the cells: #code return [cell.Value for cell in getCells(self.sheet, cells)] #/code As you can see, both use the getCells() function. So my question is, where is the best way to put it so instances of both classes can use it? Xif ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Pierre Barbier de Reuille INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP Botanique et Bio-informatique de l'Architecture des Plantes TA40/PSII, Boulevard de la Lironde 34398 MONTPELLIER CEDEX 5, France tel : (33) 4 67 61 65 77fax : (33) 4 67 61 56 68 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sys.argv[1: ] help
Hi, It is actually associated with just 'python', changed it to associate with 'pythonw' and I got nothing on the same example not even the [], so I am assuming that 'python' is the correct one? Liam Clarke wrote: Yeah, right click on a .py and check if it's associated with pythonw or python.exe GL, Liam Clarke On Sun, 27 Feb 2005 18:28:18 +, Richard gelling [EMAIL PROTECTED] wrote: Hi, Yes, I use both Wndows XP and Linux( at work ) . I left that in by mistake I am actually just typing in arg1,py a b c at the windows XP command prompt Sorry for the confusion. Liam Clarke wrote: Are you using XP still? I've never seen this before - ./arg1.py a b c But anyhoo, I tried out just 'c:\python23\foo.py' as opposed to 'c:\python23\python foo.py' and while foo.py will run, it doesn't echo to the console, as on my machine running a .py file runs it through pythonw.exe - I'd check it out for your machine, it's probably the same. You'd need to change the association to python.exe, but that would mean that you always got a DOS box for every Python script you ran, which is annoying with GUIs. Erm, if you don't want to type in python each time, either change the association or create a batch file called x or a or something that runs Python and stick it in a directory that's in your PATH system variable. Only problem with that is passing command line variables ...might just be better to type python Good Luck, Liam Clarke On Sun, 27 Feb 2005 17:55:54 +, Richard gelling [EMAIL PROTECTED] wrote: Hi, No What I get if I was to type in ./arg1.py a b c All I get is [] If i type at the command prompt python arg1.py a b c I get ['a','b','c'] as expected All the other programs and examples I have typed in work fine just by typing in the file name, I don't have to preced the file name with python, only this example. I hope this makes it clearer Richard G. Nick Lunt wrote: Richard, if you try to print sys.argv[1:] when sys.argv only contain sys.argv[0] then you are bound to get an empty list returned, [] . Im not sure I understand the problem you think you've got but here's what happens with sys.argv for me, and it's correct. [argl.py] $ cat argl.py #!/usr/bin/python import sys print sys.argv[1:] ./argl.py [] ./argl.py a b c ['a', 'b', 'c'] Is that what your getting ? Sorry for the late response, I tried all of the the suggestions, including correcting my typo of print sys[1:] and tried print sys,argv[1:], this does now work as long as I run 'python test.py fred joe' it returns all the arguments. If I try just test.py all I get is '[]' . Is there something wrong with my environmental variables in Windows XP, I would like to be able to just use the file name rather than having to type python each time. Any help would be gratefully received. Richard G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sys.argv[1: ] help
Hi, It is actually associated with just 'python', changed it to associate with 'pythonw' and I got nothing on the same example not even the [], so I am assuming that 'python' is the correct one? Liam Clarke wrote: Yeah, right click on a .py and check if it's associated with pythonw or python.exe GL, Liam Clarke On Sun, 27 Feb 2005 18:28:18 +, Richard gelling [EMAIL PROTECTED] wrote: Hi, Yes, I use both Wndows XP and Linux( at work ) . I left that in by mistake I am actually just typing in arg1,py a b c at the windows XP command prompt Sorry for the confusion. Liam Clarke wrote: Are you using XP still? I've never seen this before - ./arg1.py a b c But anyhoo, I tried out just 'c:\python23\foo.py' as opposed to 'c:\python23\python foo.py' and while foo.py will run, it doesn't echo to the console, as on my machine running a .py file runs it through pythonw.exe - I'd check it out for your machine, it's probably the same. You'd need to change the association to python.exe, but that would mean that you always got a DOS box for every Python script you ran, which is annoying with GUIs. Erm, if you don't want to type in python each time, either change the association or create a batch file called x or a or something that runs Python and stick it in a directory that's in your PATH system variable. Only problem with that is passing command line variables ...might just be better to type python Good Luck, Liam Clarke On Sun, 27 Feb 2005 17:55:54 +, Richard gelling [EMAIL PROTECTED] wrote: Hi, No What I get if I was to type in ./arg1.py a b c All I get is [] If i type at the command prompt python arg1.py a b c I get ['a','b','c'] as expected All the other programs and examples I have typed in work fine just by typing in the file name, I don't have to preced the file name with python, only this example. I hope this makes it clearer Richard G. Nick Lunt wrote: Richard, if you try to print sys.argv[1:] when sys.argv only contain sys.argv[0] then you are bound to get an empty list returned, [] . Im not sure I understand the problem you think you've got but here's what happens with sys.argv for me, and it's correct. [argl.py] $ cat argl.py #!/usr/bin/python import sys print sys.argv[1:] ./argl.py [] ./argl.py a b c ['a', 'b', 'c'] Is that what your getting ? Sorry for the late response, I tried all of the the suggestions, including correcting my typo of print sys[1:] and tried print sys,argv[1:], this does now work as long as I run 'python test.py fred joe' it returns all the arguments. If I try just test.py all I get is '[]' . Is there something wrong with my environmental variables in Windows XP, I would like to be able to just use the file name rather than having to type python each time. Any help would be gratefully received. Richard G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Recursive Tkinter buttons
On Sun, 27 Feb 2005 16:06:32 -, Alan Gauld [EMAIL PROTECTED] wrote: - however, when I click the button, I want self.showButton to know which one of them was pressed. I've seen in other gui programming the idea of an id or identifier - I can't see that here. Ideally, I would like to know the value of i in self.showButtons - but when I use self.showButtons(i) showButtons gets called straight at run time. The easy way to do this is to use a defrault parameter in a lambda: submittext = for i in range (1, 11): b = Button(text=submittext, command = lambda n=i: self.showButton(n)) b.rid(column=4, row=i+4) submitlist.append(b) Notice the button creation line now uses a lambda with default parameter set to i. That lambda calls your method but now passes in the value of i for that button. (You need to modify your method to accept that value of course!) HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld Yay! Thanks for the tips Michael/Alan - works a treat, although I must admit, I'm not sure what Lambda does. Adam -- http://www.monkeez.org PGP key: 0x7111B833 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Re: How do you share a method (function) among several objects?
The position to put it is a design choice and there is no single best solution. What I'd do is to gather all the small homeless functions in a single separate module. And if they come to be too numerous, I'll sort them in some modules, ... But that's because I don't like having a single function in a module ^_^ Of course, if this is a complex function, that can make sense ... I hope I helped and didn't make things worst ;) Pierre Xif a crit : Ok, so keeping getCells() as an external function makes sense. But where exactly do you recommend I'd put it? In a seperate module, like I currently do, even though it's going to be the only piece of code contained inside that module? Xif Pierre Barbier de Reuille wrote: Well, for me, the more logical answer is : multi-inheritance ! If part of your class is the same, (same semantic, same implementation), then you want to have a base class for that. If you dislike this kindof inheritance, then your function should be an external one. Even more because it's 'just' an implementation function. The user don't need it as a method ... So why bother add it to your object ? Pierre Xif a crit : Javier Ruere wrote: Xif wrote: Hello There are several different objects. However, they all share the same function. Since they are not the same or similar, it's not logical to use a common superclass. So I'm asking, what's a good way to allow those objects to share that function? The best solution I've found so far is to put that function in a module, and have all objects import and use it. But I doubt that's a good use-case for modules; writing and importing a module that contains just a single function seems like an abuse. Thanks, Xif Could you give an example? Javier ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor +++ This Mail Was Scanned By Mail-seCure System at the Tel-Aviv University CC. Sure, I can describe my particular case. It's a program that retrieves / updates Microsoft Excel spreadsheet data. There are two major classes: 1) an Excel class, that represents of the whole Excel program 2) a Cells class, that abstracts retrieval and editing of cells. Both classes use a function called getCells() as part of their __getitem__() methods. getCells() parses the __getitem__() call arguments, and returns an iterator over the appropriate cells. The difference between the 2 classes is that a Cells instance just converts the generator into a list and returns it: #code return list(getCells(self.sheet, cells)) #/code while an Excel instance returns the values of the cells: #code return [cell.Value for cell in getCells(self.sheet, cells)] #/code As you can see, both use the getCells() function. So my question is, where is the best way to put it so instances of both classes can use it? Xif ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Pierre Barbier de Reuille INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP Botanique et Bio-informatique de l'Architecture des Plantes TA40/PSII, Boulevard de la Lironde 34398 MONTPELLIER CEDEX 5, France tel : (33) 4 67 61 65 77fax : (33) 4 67 61 56 68 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sys.argv[1: ] help
Yeah, python.exe is the right one... bizarre... I'll have a poke at it when I get home from work. Sorry I haven't been more helpful. Cheers, Liam Clarke On Sun, 27 Feb 2005 18:57:30 +, Richard gelling [EMAIL PROTECTED] wrote: Hi, It is actually associated with just 'python', changed it to associate with 'pythonw' and I got nothing on the same example not even the [], so I am assuming that 'python' is the correct one? Liam Clarke wrote: Yeah, right click on a .py and check if it's associated with pythonw or python.exe GL, Liam Clarke On Sun, 27 Feb 2005 18:28:18 +, Richard gelling [EMAIL PROTECTED] wrote: Hi, Yes, I use both Wndows XP and Linux( at work ) . I left that in by mistake I am actually just typing in arg1,py a b c at the windows XP command prompt Sorry for the confusion. Liam Clarke wrote: Are you using XP still? I've never seen this before - ./arg1.py a b c But anyhoo, I tried out just 'c:\python23\foo.py' as opposed to 'c:\python23\python foo.py' and while foo.py will run, it doesn't echo to the console, as on my machine running a .py file runs it through pythonw.exe - I'd check it out for your machine, it's probably the same. You'd need to change the association to python.exe, but that would mean that you always got a DOS box for every Python script you ran, which is annoying with GUIs. Erm, if you don't want to type in python each time, either change the association or create a batch file called x or a or something that runs Python and stick it in a directory that's in your PATH system variable. Only problem with that is passing command line variables ...might just be better to type python Good Luck, Liam Clarke On Sun, 27 Feb 2005 17:55:54 +, Richard gelling [EMAIL PROTECTED] wrote: Hi, No What I get if I was to type in ./arg1.py a b c All I get is [] If i type at the command prompt python arg1.py a b c I get ['a','b','c'] as expected All the other programs and examples I have typed in work fine just by typing in the file name, I don't have to preced the file name with python, only this example. I hope this makes it clearer Richard G. Nick Lunt wrote: Richard, if you try to print sys.argv[1:] when sys.argv only contain sys.argv[0] then you are bound to get an empty list returned, [] . Im not sure I understand the problem you think you've got but here's what happens with sys.argv for me, and it's correct. [argl.py] $ cat argl.py #!/usr/bin/python import sys print sys.argv[1:] ./argl.py [] ./argl.py a b c ['a', 'b', 'c'] Is that what your getting ? Sorry for the late response, I tried all of the the suggestions, including correcting my typo of print sys[1:] and tried print sys,argv[1:], this does now work as long as I run 'python test.py fred joe' it returns all the arguments. If I try just test.py all I get is '[]' . Is there something wrong with my environmental variables in Windows XP, I would like to be able to just use the file name rather than having to type python each time. Any help would be gratefully received. Richard G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SubClassing
On Fri, 25 Feb 2005 05:54:39 -0200, Ismael Garrido [EMAIL PROTECTED] wrote: def __init__(self, this, that, new): Parent.__init__(self, this, that) #note self self.new = new If the paren's init t has a lot of possible arguments, it may be easier to do things this way: class Child(Parent): def __init__(self, new, *args, **kwargs): Parent.__init__(self, *args, **kwargs) self.new = new This way, the Child class doesn't need to know or care about what parameters get passed on to Parent; it uses the ones it needs, and passes all the rest on. Jeff Shannon ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sys.argv[1: ] help
(I know I'm being a bit silly about asking about what looks like a simple email typo, but computer programming bugs are all-too-often about typos. *grin* Sorry for the late response, I tried all of the the suggestions, including correcting my typo of print sys[1:] and tried print sys,argv[1:], Hi Richard, Please, please copy and paste your code literally whenever you're talking about code. You have another email typo here, when you put a comma instead of a period in: sys,argv[1:] I know that's not what you meant, but I'm really trying to stress the idea that computers are not forgiving of typos. There are actually a large class of programming bugs that are simple typos. Make things easier for us: just copy and paste the code that you're talking about, and we'll be better able to replicate the situation that's on your side. I think there's been a lot of confusion on this thread, so let's backtrack again and make sure we're on the same page. I'll assume for the moment that your program looks like this: ## import sys print sys.argv[1:] ## this does now work as long as I run 'python test.py fred joe' it returns all the arguments. I expect to see: ### ['fred', 'joe'] ### because sys.argv should contain the list: ['test.py', 'fred', 'joe'] If I try just test.py all I get is '[]'. Ok, this is also expected. If we give no command line arguments to our program, then we should get back an empty list from sys.argv[1:]. So at the moment, I actually have no clue what problem you're running into. *grin* What exactly are you getting stuck on? Is there something wrong with my environmental variables in Windows XP, I would like to be able to just use the file name rather than having to type python each time. Wait. Ok, I think I understand the question now. I think you are trying to say that if you enter the command: ### C:\ test.py fred joe ### at your Windows command prompt, that Python responds with: ### [] ### Does this sound right? If so, then you are asking a question that's specific to Windows. The Windows command shell has a bug that has bitten folks before: Windows doesn't appear to correctly pass command line arguments to Python programs if we try to call the program directly. A workaround is to create a '.CMD' wrapper for your Python program. Add a file called 'test.cmd' in the same directory as your 'test.py' program with the following content: ### python test.cmd %* ### Once you have this file, try: ### C:\ test fred joe ### at your command line. You may find the discussion underneath: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/366355 useful, as the folks there further discuss this issue. Let's backtrack for a moment again: the reason we were not able to answer your question better was precisely because you were paraphrasing way too much: ... as I run 'python test.py fred joe' it returns all the arguments. If I try just test.py all I get is '[]'. If you had shown us exactly what you were entering into the command line, we would have caught the real cause of the problem much earlier. Ok, I'll stop ranting about this now. *grin* Best of wishes to you. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sys.argv[1: ] help
Add a file called 'test.cmd' in the same directory as your 'test.py' program with the following content: ### python test.cmd %* ### Scratch that! *grin* Sorry, meant to write that the test.cmd should contain: ### python test.py %* ### Darn it, but I don't have a Windows box handy to test this. Can someone double check this to make sure I haven't screwed up again? Sorry about that; I should never post anything without testing it first. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sys.argv[1: ] help
On Sun, 27 Feb 2005 17:55:54 +, Richard gelling [EMAIL PROTECTED] wrote: No What I get if I was to type in ./arg1.py a b c All I get is [] It sounds as though the command shell is not passing along the additional parameters. Try opening Windows Explorer, and go to the Folder Options (in the Tools menu, IIRC). Go to the File Types tab, find PY (Python File), and click the Advanced button. In the resulting dialog, select the open action and click edit, then look at the command line that it's using. You want something that looks like: C:\Python23\python.exe %1 %* The '%*' bit at the end is what I suspect may be missing in your settings. (There's some chance that cmd.exe uses a different set of settings than Windows Explorer does, in which case you'll have to research that independently. I know that you can use the assoc command to associate the .py extension with the Python.File filetype, but I'm not sure how to create filetypes or change actions that are taken upon filetypes if it's different from the Explorer settings) Jeff Shannon ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Recursive Tkinter buttons
Yay! Thanks for the tips Michael/Alan - works a treat, although I must admit, I'm not sure what Lambda does. Adam Lambda is basically a function without a name that can be used inline. Take for example, this code. def funct(x): return sin(x) is the same as funct = lambda x: sin(x) There seems to be a running debate over whether lambdas are worth the trouble. I believe it's a matter of preference. HTH, Jacob ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 3 questions for my port scanner project
Use a canvas and redraw a rectangle slightly larger every time through the scanning loop. Thats think this is the easy part... The hard part is to make the bar move with the program (so every port it finishes the bar will slightly move, which depends on the total number of ports to scan...). But since you know the range of ports you can calculate the total number. If you keep a count of how many scanned you can work out the percentage scanned. You then draw a rectangle the same percentage of the total width. After each port scanned recalculate the percentage and redraw the rectangle. Where's the problem? :-) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] open a socket from a named file on linux
Hello, I am trying to open a socket connection to a named file on my computer and can not seem to get it working. Any help or advice would be great. The details I attempting to control xfmedia, http://spuriousinterrupt.org/projects/xfmedia/ , via it's remote from python and I can not get a connection. connection information from readme file quote Xfmedia has a remote control system, which consists of a UNIX socket in /tmp, xfmedia_remote.$UID.$SESSION_ID, where $UID is the uid of the user running xfmedia, and $SESSION_ID is a number (starting from zero) corresponding to the instance of xfmedia. (For example, if you're running one copy of xfmedia, $SESSION_ID will be 0. If you start a second instance, its $SESSION_ID will be 1. And so on.) /quote when attempting to creat a connection with this command s = socket.fromfd('/tmp/xfmedia_remote.1001.0', socket.AF_UNIX, socket.SOCK_STREAM) i get this error /xfmedia_remote.1001.0', socket.AF_UNIX, socket.SOCK_STREAM) Traceback (most recent call last): File stdin, line 1, in ? which i understand i need to use the file descripter instead but i can not not get the file descripter because when i attempt to open /tmp/xfmedia_remote.1001.o with the open command i get an error f = open('/tmp/xfmedia_remote.1001.0') Traceback (most recent call last): File stdin, line 1, in ? IOError: [Errno 6] No such device or address: '/tmp/xfmedia_remote.1001.0' yet ls shows the file exists, xfmedia is working fine. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] puzzling traceback -- what to do with it?
Hi all, I just ran a program of mine and got the traceback: Traceback (most recent call last): File C:\PYTHON24\lib\idlelib\rpc.py, line 233, in asyncqueue self.putmessage((seq, request)) File C:\PYTHON24\lib\idlelib\rpc.py, line 333, in putmessage raise IOError IOError This stumps me, as I've almost no idea what rpc.py, putmessage, and asyncqueue are. (A quick glance at the code made me realize my code-reading skills and knowledge of IDLE aren't up to tracking this down on my own.) Furthermore, the code that produced this runs just fine via other methods (running from within SciTe, the python command line, etc.) And, sometimes, but not always, closing the offending code, running something else, and then trying again with the code that caused the traceback makes it work. I'm thinking IDLE bug, but also that it would be a good idea to solicit opinions/expertise here before running off screaming BUG to the IDLE folks :-) Any suggestions for what the problem might be, or how to narrow it down before reporting? Thanks and best, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor