Re: [Tutor] Print Screen
Wow.. I have visions of writing a little wanna-be VNC client/server now using the ImageGrab.grab() =DThis ImageGrab trick does exactly what I wanted. Thanks for the tip!Actually, I want to write a little package for the learning experience sometime over the holidays (plus I use VNC fairly often), but I can't find any direction, or any already made packages for python for the VNC protocol (no libs?). On 11/2/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote: >> Anyone know of a way to capture special keys like "Print Screen"?>> I have a small script to grab all they keycodes, but it doesn't seem to>> catch several keys on the keyboard. I've got a utility that I'd like to >> be able to automagically get a screenshot when something goes wrong so I>> dont have to hope the user can re-create the error. Universal support>> would be best, but WinXP is the main OS >> I'm not exactly sure what you want here :-) but if you want to capture> when the 'Print Screen' key (or any other key) has actually been> pressed, try pyHook. Note: pyHook only works on Windows! >Also note that if you want all of the keypresses, but you _don't_ careabout the application with focusreceiving the input, you can do a complete key grab using TKInter orPygame, and probably the other GUI packages too. But, like I said, if you were, for example, typing an e-mail and youstarted a script that did a complete grab like this, you'd no longer beable to typeinto the e-mail window. Using pyHook, your program could see all the keypresses, but they'd also still be sent to the e-mail program.Actually, I've never tried it, but I'm pretty sure that's how the GUIpackages' key capturing works.You may be asking 'well, it sounds like pyHook does a better job of this anyway!'Yeah, you're right.However, as Alan exclaimed, pyHook works only on Windows!So the solution I offered would be more portable.Hope that helps,-Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Print Screen
Chris Hengge wrote: > Wow.. I have visions of writing a little wanna-be VNC client/server > now using the ImageGrab.grab() =D > This ImageGrab trick does exactly what I wanted. Thanks for the tip! > > Actually, I want to write a little package for the learning experience > sometime over the holidays (plus I use VNC fairly often), but I can't > find any direction, or any already made packages for python for the > VNC protocol (no libs?). Heh, I was actually writing my own VNC, that's when I ran into the ImageGrab function. I also did something cool, which is : self.prevscr = self.currscr self.currscr = ImageGrab.grab().resize((800,600)) diff = ImageChops.difference(self.prevscr,self.currscr) Obviously you need a currscr before you start, which I declared during the initialization of the class. The cool part of this is that the ImageChops difference will return black pixels in any area of the screen that hasn't changed. so you can just send the changed parts over the network (if you have a good algorithm to discard the unchanged portions). Would be much faster than sending the whole screen every frame, obviously. Not CPU-faster, but network-faster. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Checking the format of IP address...
Hi Folks, I want to implement a simple program that verifies the IP provided by the user is in the right format or not. How to go about it..? Any suggestions.. TIA. Regards, Asrarahmed -- To HIM you shall return. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Syntax Error? Variable Scope?
Chuck Coker wrote: > Hi Folks, > > I am new to Python, but I have many years experience in software > development. I have a question about variable scope. I'm having a > problem that I suspect is merely a syntax error or something of that > nature. > > I'm not real clear on variable scoping rules, either. I can't figure > out how to declare a variable containing a file pointer outside a > class and then access the variable from inside a method inside the > class. I could be creating errors when trying to use the "global" > keyword due to not fully understanding the scope rules. The examples > I found so far in books and on the web are to simplistic for what I > want to do. Actually you seem to be using 'global' correctly. > > I am using a load-testing app named The Grinder (version 3, beta 30) > and Python 2.5. I think you mean Jython. > > I want my script to open an external CSV (comma-separated values) file > containing x number of records with people's names, addresses, etc. I > want the file opened outside the TestRunner class (the main class for > The Grinder) when the script is first started. > > Then I want each instance of the TestRunner class to read one line > from the external CSV file. After it gets the line from the file, I > want to split the line at the commas into components and do the > processing stuff. > > After the last instance of TestRunner finishes, I want the script to > close the file and exit nicely. > > My reasoning for opening and closing the CSV file outside the class is > that I want the file to stay open and have the file cursor maintain > its location after each read. For example, if I am running 2,000 > threads, the file gets opened, whatever thread gets there first reads > line 1, the next thread that gets there reads line 2, and so on until > all 2,000 threads have had a chance to get a different line from the > file. Then I want the file to close. This is a very strange design. Why do you want to split the processing of the file among 2,000 threads? Why not have a single thread that loops over the lines of the file and processes them? To open the file outside the thread, just open it at global scope as Dustin suggested. Additionally you could pass the open file to __init__() and not rely on it being global. More comments inline. > > Here are some code snippets: > > -- > from net.grinder.script import Test > from net.grinder.script.Grinder import grinder > from net.grinder.plugin.http import HTTPPluginControl, HTTPRequest > from HTTPClient import NVPair > connectionDefaults = HTTPPluginControl.getConnectionDefaults() > httpUtilities = HTTPPluginControl.getHTTPUtilities() > > [... snip ...] > > fileNewUserInfo = 'new-user-info.csv' > fileNewUserInfoIsOpen = 0 > > [... snip ...] > > class TestRunner: > """A TestRunner instance is created for each worker thread.""" > > # The instance initialization method. > def __init__(self): > print 'We are in TestRunner.__init__()' > global infile > global fileNewUserInfoIsOpen > if (fileNewUserInfoIsOpen == 0): > infile = open(fileNewUserInfo, "r") > fileNewUserInfoIsOpen += 1; > print 'We are inside the open if statement' If you have multiple TestRunners this is a race condition waiting to happen. You need a lock on fileNewUserInfoIsOpen to prevent two threads from passing the condition and both opening the file. I would use infile itself as the flag though, if you really have to do it this way. > #global infile > self._userTokenCtx = '' > self._userTokenRi = '' > self._userInfo = [] > > for line in infile.readlines(): > self._userInfo.append(string.split((line),',')) > print line This processes the entire file at once, it is not processing a single line as you described. > > print self._userInfo > > def nextMethod(self): > > [... snip ...] > -- > > The script blows up at this line inside the for loop: > > self._userInfo.append(string.split((line),',')) > > with this error message in my error log: > > 11/2/06 7:12:20 PM (thread 0): Aborting thread due to Jython exception > "NameError: string" whilst creating per-thread test runner object > > NameError: string > File "new-user-registration.py", line 356, in __init__ > > I've tried using "import string" and it doesn't like that. I'm sure > I'm missing something very basic here due to my newness with Python. > Can anyone offer any insights? import string should work, what error do you get? Though as Dustin mentioned, the split() method of line, or the csv module, is recommended. He got the spelling wrong, though - you would use line.split(','). Jython doesn't have a csv module but there are some add-on modules that work. Kent > > Thanks, > Chuck > ___ Tutor maillist - Tuto
Re: [Tutor] Checking the format of IP address...
Asrarahmed Kadri wrote: > > Hi Folks, > > I want to implement a simple program that verifies the IP provided by > the user is in the right format or not. > > How to go about it..? > Any suggestions.. How about googling 'python ip address'? You seem to come here first when you need to do something new. A little effort on your part would be appreciated. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Checking the format of IP address...
Sorry. I will try to first go to GOOGLE and if I cannot find there, then I'll post my query. Thanks for all the support till now. Regards, Asrarahmed On 11/3/06, Kent Johnson <[EMAIL PROTECTED]> wrote: Asrarahmed Kadri wrote:>> Hi Folks,>> I want to implement a simple program that verifies the IP provided by > the user is in the right format or not.>> How to go about it..?> Any suggestions..How about googling 'python ip address'?You seem to come here first when you need to do something new. A little effort on your part would be appreciated.Kent-- To HIM you shall return. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Checking the format of IP address...
Hi, I would probably take the string, separate the numbers and check they are acceptable: def correct_ip(ip): # Check if my IP address has 4 numbers separated by dots num=ip.split('.') if not len(num)==4: return False # Check each of the 4 numbers is between 0 and 255 for n in num: try: if int(n) < 0 or int(n) > 255: return False except: return False return True You could also go the regexp way, but I find it a bit more complicated. Hope it helps, G On 11/3/06, Asrarahmed Kadri <[EMAIL PROTECTED]> wrote: > > Hi Folks, > > I want to implement a simple program that verifies the IP provided by the > user is in the right format or not. > > How to go about it..? > Any suggestions.. > > TIA. > Regards, > Asrarahmed > > > -- > To HIM you shall return. > ___ > 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] question about classes and atributes
I think I don't understand the OOP in python, could anyone explain why this code works? class example: atribute = "hello world" print example.atribute Why you don't have to make an object of the class to access to the atribute? ( class example: atribute = "hello world" obj = example() print obj.atribute Thanks in advance. __ LLama Gratis a cualquier PC del Mundo. Llamadas a fijos y móviles desde 1 céntimo por minuto. http://es.voice.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about classes and atributes
Because your atribute is a class attribute: class C: ca = 123 print C.ca # 123 c1 = C() print c1.ca# 123 c1.ca = 140 print c1.ca# 140 print C.ca # 123 c2 = C() print c2.ca# 123 C.ca = 141 print C.ca # 141 print c1.ca# 140 print c2.ca# 141 Basically, when an instance does not have an attribute, it looks them up in the class, which might recurse into base classes. Furthermore, objects & classes 101 material: class C: def method(self): print self c = C() print c.method # bound method to c print C.method # unbound method, checks that first argument is a C print C.__dict__["method"] # function, does NOT check first argument. So basically, the whole self argument handling is magic that happpens during attribute lookup. Andreas Am Freitag, den 03.11.2006, 14:27 +0100 schrieb euoar: > I think I don't understand the OOP in python, could anyone explain why > this code works? > > class example: > atribute = "hello world" > > print example.atribute > > Why you don't have to make an object of the class to access to the > atribute? > > ( class example: >atribute = "hello world" > > obj = example() > print obj.atribute > > > Thanks in advance. > > > __ > LLama Gratis a cualquier PC del Mundo. > Llamadas a fijos y móviles desde 1 céntimo por minuto. > http://es.voice.yahoo.com > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor signature.asc Description: Dies ist ein digital signierter Nachrichtenteil ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about classes and atributes
Andreas Kostyrka escribió: > Because your atribute is a class attribute: > > class C: > ca = 123 > > print C.ca # 123 > c1 = C() > print c1.ca# 123 > c1.ca = 140 > print c1.ca# 140 > print C.ca # 123 > c2 = C() > print c2.ca# 123 > C.ca = 141 > print C.ca # 141 > print c1.ca# 140 > print c2.ca# 141 > > Basically, when an instance does not have an attribute, it looks them up > in the class, which might recurse into base classes. > > Furthermore, objects & classes 101 material: > > class C: > def method(self): > print self > > c = C() > print c.method # bound method to c > print C.method # unbound method, checks that first argument is a C > print C.__dict__["method"] > # function, does NOT check first argument. > > So basically, the whole self argument handling is magic that happpens > during attribute lookup. > > Andreas > > Am Freitag, den 03.11.2006, 14:27 +0100 schrieb euoar: > >> I think I don't understand the OOP in python, could anyone explain why >> this code works? >> >> class example: >> atribute = "hello world" >> >> print example.atribute >> >> Why you don't have to make an object of the class to access to the >> atribute? >> >> ( class example: >>atribute = "hello world" >> >> obj = example() >> print obj.atribute >> >> >> Thanks in advance. >> >> >> __ >> LLama Gratis a cualquier PC del Mundo. >> Llamadas a fijos y móviles desde 1 céntimo por minuto. >> http://es.voice.yahoo.com >> ___ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> Thank you for your answer and the examples. So without self it is an instance variable (like "static" in java/c#). But then, I don't understand how is it possible this in your example: c1.ca = 140 or down: print C.method Are you creating new atributes and methods at run time? Is that what has happened? In fact I have tried also this: class example: atribute = "green" obj = example() obj.a_new_atribute = "white" And even this seems to be correct: class example: atribute = "green" example._other_atribute = "yellow" So, in python, you can add methods at run time to an object, and even you can add them to a class at run time? __ LLama Gratis a cualquier PC del Mundo. Llamadas a fijos y móviles desde 1 céntimo por minuto. http://es.voice.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] One million and counting
Alan Gauld escribió: > Hi folks, > > In just thought I'd mention that my web tutor has now passed > the million visitors mark. Thanks to all those on the tutor > list who have paid a visit. > > Alan Gauld > Author of the Learn to Program web site > http://www.freenetpages.co.uk/hp/alan.gauld > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > Congratulations Alan, that's really a nice web, nice place to begin with programing __ LLama Gratis a cualquier PC del Mundo. Llamadas a fijos y móviles desde 1 céntimo por minuto. http://es.voice.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about classes and atributes
>>> I think I don't understand the OOP in python, could anyone explain why >>> this code works? >>> >>> class example: >>> atribute = "hello world" >>> >>> print example.atribute >>> >>> Why you don't have to make an object of the class to access to the >>> atribute? >>> because that attribute is part of the Class object that's created when you declare a class. As you can see by the following code: >>> class example: attribute = 'hello, world!' >>> example there is actually a type of object called 'class.' when you make an instance of a class, >>> a = example() >>> a <__main__.example instance at 0x00B40440> it is now a example object instance. > Thank you for your answer and the examples. So without self it is an > instance variable (like "static" in java/c#). But then, I don't > understand how is it possible this in your example: > > c1.ca = 140 > Because c1 is an instance of the class 'C', it has the attribute .ca already in it. This is a reference to the class attribute 'ca' which is equal to 123. However, when you change c1.ca, because the class attribute 'ca' is immutable (since it's an integer) a local copy of ca is created with the value 140 in it. C.ca is still 123. If you now do C.ca = 234, c1.ca is still 140, because it now has a local (instance) attribute called 'ca' that hides the class attribute. However, if you do something like this... class C: ca = 123 c1 = C() C.ca = 567 print c1.ca you will get an output of '567'. because c1 never got the local instance attribute to replace the reference to the class-wide attribute, so changing the C.ca will also affect c1.ca. > or down: > > print C.method > > Are you creating new atributes and methods at run time? Is that what has > happened? In fact I have tried also this: > > class example: > atribute = "green" > > obj = example() > obj.a_new_atribute = "white" > > And even this seems to be correct: > > class example: > atribute = "green" > > example._other_atribute = "yellow" > > > So, in python, you can add methods at run time to an object, and even you can > add them to a class at run time? > No, you're confusing Python with a compiled language. You're not adding methods to an object at run-time because there's not a distinction between runtime and compile-time in Python, because compile-time doesn't exist. You can add attributes to classes any time you want inside your program. Just like I can add an element to a list any time I want. a = [1,2,3] a.append(4) Classes are just objects, as lists are, and integers are, and everything else is as well. And when I execute the code, Python knows how to do all of these things. You see, an interpreted session is not the same as you think of 'at run time' being. For the most part, an interpreted session is exactly the same as if I were to type the code into a text document, save it, and execute it. So yeah, anywhere in your program you can add methods to classes, but really saying 'at run-time' is confusing terminology. It implies that if I were running someone else's program I could just add methods in on the fly whenever I wanted to. This is not true, unless they've enabled this functionality. HTH, -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] numpy help
Howdy, I'm a college student and for one of we are writing programs to numerically compute the parameters of antenna arrays. I decided to use Python to code up my programs. Up to now I haven't had a problem, however we have a problem set where we are creating a large matrix and finding it's inverse to solve the problem. To invert the matrix I've tried using numpy.numarray.linear_algebra.inverse and numpy.oldnumeric.linear_algebra.inverse which both give me the same error ( I was hoping they called different routines but I think they call the same one ). This is the error message I receive: Traceback (most recent call last): File "C:\Documents and Settings\Chris & Esther\Desktop\636_hw5_2\elen636_hw5_2.py", line 60, in matrix_inverse = numpy.numarray.linear_algebra.generalized_inverse(matrix) File "C:\Python25\lib\site-packages\numpy\oldnumeric\linear_algebra.py", line 59, in generalized_inverse return linalg.pinv(a, rcond) File "C:\Python25\lib\site-packages\numpy\linalg\linalg.py", line 557, in pinv u, s, vt = svd(a, 0) File "C:\Python25\lib\site-packages\numpy\linalg\linalg.py", line 485, in svd a = _fastCopyAndTranspose(t, a) File "C:\Python25\lib\site-packages\numpy\linalg\linalg.py", line 107, in _fastCopyAndTranspose cast_arrays = cast_arrays + (_fastCT(a.astype(type)),) TypeError: can't convert complex to float; use abs(z) I've tried inverting small complex matrices and it worked fine. Does anyone know why it won't work for this larger matrix? Any ideas how I can work around this problem and get the correct inverse matrix? Chris P.S. elen636_math.py is my personal library of functions I've create to solve the problem while elen636_hw5_2.py is the program that I'm actually running # Purpose: # This is a library of functions for ELEN 636 that # so far has the ability to calculate the Sine and # Cosine integrals as well as the mutual impedance # between two parallel antennas. # # Author: Christopher Smith # E-mail: [EMAIL PROTECTED] # Date: 10/30/2006 ### ### NOTE: The functions below for the sine and cosine integrals are similar ### to the functions I turned in for homework assignment 4 problem 6 ### except that I added the ability to check for convergence. ### I also added the factor into the mutual impedance formula so that the ### answer is given in terms of the terminal input impedance instead of ### the loop impedance as it was formerly giving. ### # depends on the math library from math import * import numpy.numarray, numpy def factorial(n): """ This function calculates the factorial of a number. """ sum = 1.0 for m in range(1, int(n)+1): sum = float(m)*sum return sum def Si(x): """ This function computes the sine integral. It uses a power series expansion that can be found in Abramowitz and Stegun's math functions reference book. """ start = 0.0 stop = 10.0 sine_int = 0.0 convergence = 1.0*10**(-6) # want to have the difference between # the last run and this run below # this value while 1: for n in range(int(start), int(stop)): n = float(n) sine_int += ((-1)**n)*x**(2*n +1)/((2*n+1)*factorial(2*n+1)) sine_int_new = sine_int + ((-1.)**stop)*x**(2.*stop +1.)/((2.*stop+1.)*factorial(2.*stop+1.)) converge_check = sine_int_new - sine_int if abs(converge_check) < convergence: break else: start = stop stop += 5.0 return sine_int_new def Ci(x): """ This function computes the cosine integral. It uses a power series expansion that can be found in Abramowitz and Stegun's math functions reference book. """ start = 1.0 stop = 10.0 convergence = 1.0*10.**(-6) # want to have the difference between # the last run and this run below # this value # The first number in the sum is Euler's constant to 10 digits cosine_int = 0.5772156649 + log(x) while 1: for n in range(int(start), int(stop)): m = float(n) cosine_int = cosine_int +((-1)**m)*x**(2*m)/((2*m)*factorial(2*m)) cosine_int_new = cosine_int + ((-1)**stop)*x**(2*stop)/((2*stop)*factorial(2*stop)) converge_check = cosine_int_new - cosine_int if abs(converge_check) < convergence: break else: start = stop stop += 5.0 #print stop return cosine_int_new def mutual_impedance(length1_tot, length2_tot, stagger, d): """ This function computes the mutual impedance between two antennas for the Parallel in Echelon Configuration. The formulas are taken from a paper by Howard King, "Mutual Impedance of Unequal Length Antennas in Eche
Re: [Tutor] question about classes and atributes
"euoar" <[EMAIL PROTECTED]> wrote in > Thank you for your answer and the examples. > So without self it is an instance variable (like "static" > in java/c#). Without self it is a class attribute like static etc in C++/Java. An instance variable is one that is unique to an instance! Although I think it may be more powerful since I seem to recall that static members are not accessible via inheritance whereas Python class variables are. Also i'm not sure if statics can be reached via an instance whereas Python class variables can. But my Java/C# is very rusty on statics... Note also that you can go even further by specifying class methods too but they need special syntax. If you are only familiar with Java style statics you might find the concept of class variables and methods a little different in Python, which follows the traditional OOP style of Lisp and SmallTalk rather than the hybrid OOP style of Java etc. That is, a class variable/method is usually treated as one that applies to the class itself, or one that is shared by all instances. Java tend to use static methods as a replacement for traditional functions, ie. things you can do without creating an instance. You can do both things in any of the languages but conceptually they tend to be treated differently, especially since Python supports stand-alone functions. > Are you creating new atributes and methods at run time? > Is that what has happened? In fact I have tried also this: Yes, Python classes are a special type of container (really a special type of dictionary) , so just as you can add new keys to a dictionary you an add new attributes to a class or object at run time. > So, in python, you can add methods at run time to an > object, and even you can add them to a class at run time? I'm not sure about adding methods at run time, I've never tried it but I think the magic around the self parameter might not work. But you can definitely add attributes. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Print Screen
Oh wow! That is great trick for lowering network requirements. Have you actually implimented that into a working app for test? You could shave a bit more from the bandwidth using that trick if you locked to a specific window for transfer *thinking blinking tray icons etc would get stripped*. How are you doing the intelligent transfer? Did you setup a grid on the screen and only update if gridX has changes? Other then that, are there any resources you can share for getting me started with my own VNC? Or do you by chance have any design docs you created/followed I could review to get an idea of the process for this? I'm thinking I'll probably have to find something in another language and just try to duplicate it with python to get me started. On 11/3/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote: Chris Hengge wrote:> Wow.. I have visions of writing a little wanna-be VNC client/server> now using the ImageGrab.grab() =D> This ImageGrab trick does exactly what I wanted. Thanks for the tip!> > Actually, I want to write a little package for the learning experience> sometime over the holidays (plus I use VNC fairly often), but I can't> find any direction, or any already made packages for python for the > VNC protocol (no libs?).Heh, I was actually writing my own VNC, that's when I ran into theImageGrab function.I also did something cool, which is :self.prevscr = self.currscrself.currscr = ImageGrab.grab().resize((800,600))diff = ImageChops.difference(self.prevscr,self.currscr)Obviously you need a currscr before you start, which I declared duringthe initialization of the class. The cool part of this is that the ImageChops difference will returnblack pixels in any area of the screen that hasn't changed.so you can just send the changed parts over the network (if you have agood algorithm to discard the unchanged portions). Would be much faster than sending the whole screen every frame, obviously.Not CPU-faster, but network-faster. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about classes and atributes
Alan Gauld wrote: > "euoar" <[EMAIL PROTECTED]> wrote in >> So, in python, you can add methods at run time to an >> object, and even you can add them to a class at run time? > > I'm not sure about adding methods at run time, I've never > tried it but I think the magic around the self parameter > might not work. But you can definitely add attributes. Sure it works: In [1]: class foo(object): pass ...: In [2]: f=foo() In [3]: f.show() --- Traceback (most recent call last) D:\Projects\e3po\ in () : 'foo' object has no attribute 'show' In [4]: def show(self): print "Hi, I'm a foo" ...: In [5]: foo.show=show In [6]: f.show() Hi, I'm a foo More advanced explanation: The magic around the self parameter is actually built-in to every function object (or its class, anyway). Functions have __get__() methods which means they can be used as descriptors. When Python evaluates f.show(), quite a few steps happen: - find the value of the show attribute in the class definition. This finds the show function object. - the function object has a __get__() method, so call show.__get__(obj) where obj is the original object being accessed. - the __get__() method wraps obj and the original function into a new callable object and returns that. - finally the temporary callable is actually called (by calling its __call__() method) and the wrapper adds the self parameter to the argument list and dispatches to the wrapped (original) function. The descriptor mechanism is only used for class attributes, not instance attributes, so if you want to add a method to an individual instance you have to do a little more work using new.instancemethod: In [12]: def show2(self): print "I'm still a foo" : The naive approach won't work: In [14]: f.show2 = show2 In [15]: f.show2() : show2() takes exactly 1 argument (0 given) In [17]: import new In [21]: f.show2 = new.instancemethod(show2, f) In [22]: f.show2() I'm still a foo I hope that makes sense to someone; I had to see it about 10 times myself before a light bulb went on. (This is number 10 :-) More info here and in the references: http://www.python.org/doc/2.2.3/whatsnew/sect-rellinks.html#SECTION00032 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Print Screen
Date: Thu, 02 Nov 2006 20:09:59 -0500From: Bill Burns <[EMAIL PROTECTED]>Subject: Re: [Tutor] Print Screen To: Chris Hengge <[EMAIL PROTECTED]>Cc: pythontutor < tutor@python.org>Message-ID: <[EMAIL PROTECTED]>Content-Type: text/plain; charset=ISO-8859-1; format=flowed >>pressed, try pyHook. Note: pyHook only works on Windows!To elaborate, pyHook only works on Win2k and later.I had no success with Windows 98 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 33, Issue 12
thank you .you can stop sending me these now thanks From: [EMAIL PROTECTED]Reply-To: tutor@python.orgTo: tutor@python.orgSubject: Tutor Digest, Vol 33, Issue 12Date: Fri, 03 Nov 2006 12:00:11 +0100>Send Tutor mailing list submissions to> tutor@python.org>>To subscribe or unsubscribe via the World Wide Web, visit> http://mail.python.org/mailman/listinfo/tutor>or, via email, send a message with subject or body 'help' to> [EMAIL PROTECTED]>>You can reach the person managing the list at> [EMAIL PROTECTED]>>When replying, please edit your Subject line so it is more specific>than "Re: Contents of Tutor digest...">>>Today's Topics:>>1. Re: Print Screen (Chris Hengge)>2. Re: Print Screen (Luke Paireepinart)>3. Checking the format of IP address... (Asrarahmed Kadri)>>>-->>Message: 1>Date: Fri, 3 Nov 2006 00:08:19 -0800>From: "Chris Hengge" <[EMAIL PROTECTED]>>Subject: Re: [Tutor] Print Screen>To: "Luke Paireepinart" <[EMAIL PROTECTED]>>Cc: pythontutor >Message-ID:> <[EMAIL PROTECTED]>>Content-Type: text/plain; charset="iso-8859-1">>Wow.. I have visions of writing a little wanna-be VNC client/server now>using the ImageGrab.grab() =D>This ImageGrab trick does exactly what I wanted. Thanks for the tip!>>Actually, I want to write a little package for the learning experience>sometime over the holidays (plus I use VNC fairly often), but I can't find>any direction, or any already made packages for python for the VNC protocol>(no libs?).>>On 11/2/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote:> >> >> > >> Anyone know of a way to capture special keys like "Print Screen"?> > >> I have a small script to grab all they keycodes, but it doesn't seem to> > >> catch several keys on the keyboard. I've got a utility that I'd like to> > >> be able to automagically get a screenshot when something goes wrong so> > I> > >> dont have to hope the user can re-create the error. Universal support> > >> would be best, but WinXP is the main OS> > >>> > >>> > >> > > I'm not exactly sure what you want here :-) but if you want to capture> > > when the 'Print Screen' key (or any other key) has actually been> > > pressed, try pyHook. Note: pyHook only works on Windows!> > >> > Also note that if you want all of the keypresses, but you _don't_ care> > about the application with focus> > receiving the input, you can do a complete key grab using TKInter or> > Pygame, and probably the other GUI packages too.> > But, like I said, if you were, for example, typing an e-mail and you> > started a script that did a complete grab like this, you'd no longer be> > able to type> > into the e-mail window. Using pyHook, your program could see all the> > keypresses, but they'd also still be sent to the e-mail program.> > Actually, I've never tried it, but I'm pretty sure that's how the GUI> > packages' key capturing works.> > You may be asking 'well, it sounds like pyHook does a better job of this> > anyway!'> > Yeah, you're right.> > However, as Alan exclaimed, pyHook works only on Windows!> > So the solution I offered would be more portable.> > Hope that helps,> > -Luke> >> >>-- next part -->An HTML attachment was scrubbed...>URL: http://mail.python.org/pipermail/tutor/attachments/20061103/7f2cbe57/attachment-0001.htm>>-->>Message: 2>Date: Fri, 03 Nov 2006 02:35:41 -0600>From: Luke Paireepinart <[EMAIL PROTECTED]>>Subject: Re: [Tutor] Print Screen>To: Chris Hengge <[EMAIL PROTECTED]>>Cc: pythontutor >Message-ID: <[EMAIL PROTECTED]>>Content-Type: text/plain; charset=ISO-8859-1; format=flowed>>Chris Hengge wrote:> > Wow.. I have visions of writing a little wanna-be VNC client/server> > now using the ImageGrab.grab() =D> > This ImageGrab trick does exactly what I wanted. Thanks for the tip!> >> > Actually, I want to write a little package for the learning experience> > sometime over the holidays (plus I use VNC fairly often), but I can't> > find any direction, or any already made packages for python for the> > VNC protocol (no libs?).>Heh, I was actually writing my own VNC, that's when I ran into the>ImageGrab function.>I also did something cool, which is :> self.prevscr = self.currscr> s
[Tutor] GUI with Designer
I am wanting to get into some GUI coding with Python and have heard about PyQT and wxPython. Now I am definately not looking for some type of holy war but can anyone give me a good reason to pick one over the other. Also I would like to have a designer with it or a seperate designer that could be used with either. I plan on coding in Windows XP. Thanks, -Todd ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about classes and atributes
Thank you folks, for your excellent answers. This is really a fantastic place to learn python :-) __ LLama Gratis a cualquier PC del Mundo. Llamadas a fijos y móviles desde 1 céntimo por minuto. http://es.voice.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] questions about having python GUI apps running in the background
First, I have to thank Alan Gauld for his previous helpful posts--I didn't get to thank him last until the topic had changed much so I felt I missed that window. Also, big congratz on 1 millionth visitor to his great site (I like to think it may have been me that day or at least close). And thanks to others who continue to be so helpful. This is a pretty general topic, but it's Friday afternoon so here goes... I want to create a Python GUI app that is meant to open at start up (either on Windows, Mac, or Linux) and stay running all the time--what I may be incorrectly calling "running in the background". This would be for the reasons of wanting the user to be able to interact with it quickly without having to open the program every time--it partly will have a "stickies" type use--and also to send timed pop up dialogues or alarms to the user at various pre-set times (which may be randomly assigned, user assigned, or both). My concerns are: 1) The app may eat up too much memory if it is not done right. What is reasonable and how can I know how much RAM it's chewing up? 2) What is a good way to time the popups/alarms? The time module? wxTimer? Other? 3) How can it have it's own icon in the tray or the dock? Any input would be appreciated. Thanks _ Get FREE company branded e-mail accounts and business Web site from Microsoft Office Live http://clk.atdmt.com/MRT/go/mcrssaub0050001411mrt/direct/01/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] GUI with Designer
wxPython is good for cross-platform stuff and has a few gui designers (Boa Constructor and others comes to mind), I don't know much about PyQT state in this, but PyGtk + Glade (Gui Designer) is a very good combo. Is about choise, I suggest you to do some simple tests with everything until you find something to be confortable with. * PyGtk + Glade * Boa Contructor * SPE + wxPython On 11/3/06, Todd Dahl <[EMAIL PROTECTED]> wrote: > I am wanting to get into some GUI coding with Python and have heard about > PyQT and wxPython. Now I am definately not looking for some type of holy war > but can anyone give me a good reason to pick one over the other. > > Also I would like to have a designer with it or a seperate designer that > could be used with either. I plan on coding in Windows XP. > > Thanks, > > -Todd > > ___ > 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] GUI with Designer
I vouch for the SPE with wxGlade and XRC! (packaged together with IDE)On 11/3/06, Carlos Daniel Ruvalcaba Valenzuela < [EMAIL PROTECTED]> wrote:wxPython is good for cross-platform stuff and has a few gui designers (Boa Constructor and others comes to mind), I don't know much aboutPyQT state in this, but PyGtk + Glade (Gui Designer) is a very goodcombo.Is about choise, I suggest you to do some simple tests with everything until you find something to be confortable with.* PyGtk + Glade* Boa Contructor* SPE + wxPythonOn 11/3/06, Todd Dahl <[EMAIL PROTECTED]> wrote: > I am wanting to get into some GUI coding with Python and have heard about> PyQT and wxPython. Now I am definately not looking for some type of holy war> but can anyone give me a good reason to pick one over the other. >> Also I would like to have a designer with it or a seperate designer that> could be used with either. I plan on coding in Windows XP.>> Thanks,>> -Todd>> ___ > Tutor maillist - Tutor@python.org> http://mail.python.org/mailman/listinfo/tutor>>> ___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 33, Issue 12
On Fri, 3 Nov 2006, dean dermody wrote: > > thank you .you can stop sending me these now thanks Done. In the future, please know that you can usually unsubscribe yourself from these kinds of technical mailing lists. Instructions on how to do so are usually at the footer of each message you receive. Good luck to you. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] GUI with Designer
At 02:10 PM 11/3/2006, Chris Hengge wrote: I vouch for the SPE with wxGlade and XRC! (packaged together with IDE) I'd be very interested in hearing why you suggest that combination. Dick Moores On 11/3/06, Carlos Daniel Ruvalcaba Valenzuela < [EMAIL PROTECTED]> wrote: wxPython is good for cross-platform stuff and has a few gui designers (Boa Constructor and others comes to mind), I don't know much about PyQT state in this, but PyGtk + Glade (Gui Designer) is a very good combo. Is about choise, I suggest you to do some simple tests with everything until you find something to be confortable with. * PyGtk + Glade * Boa Contructor * SPE + wxPython On 11/3/06, Todd Dahl <[EMAIL PROTECTED]> wrote: > I am wanting to get into some GUI coding with Python and have heard about > PyQT and wxPython. Now I am definately not looking for some type of holy war > but can anyone give me a good reason to pick one over the other. > > Also I would like to have a designer with it or a seperate designer that > could be used with either. I plan on coding in Windows XP. > > Thanks, > > -Todd > > ___ > 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] question
I don't get the output I would expect from the following. The variable clean1 gives me an empty string. But if i change the for loop to print i[26:40] I get all the info. what do I need to do to capture all the data to clean1? Thanks. >>> a = open('arp.txt') >>> file = a.read() >>> file = file.split('\n') >>> a.close() >>> b = open('arplist.txt','w') >>> clean1 = [] >>> >>> for i in file: ... clean1 = i[26:40] ... >>> clean1 '' ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about classes and atributes
"Kent Johnson" <[EMAIL PROTECTED]> wrote > Alan Gauld wrote: >> I'm not sure about adding methods at run time, I've never > Sure it works: > > In [1]: class foo(object): pass >...: > In [4]: def show(self): print "Hi, I'm a foo" > > In [5]: foo.show=show > > In [6]: f.show() > Hi, I'm a foo Cool! I'm constantly amazed at the power and simplicity of Python. > More advanced explanation: Yes, it makes sense when its explained. But I'd never have intuitively thought of that! Thanks for the info Kent. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] GUI with Designer
Well, I use SPE which comes with wxGlade and XRC. For the small amount of gui I've done with python I think SPE offers the best IDE coder experience (coming from a VS world). The tools make sense to me.wxGlade is a GUI designer written in Python with the popular GUI toolkit wxPython, that helps you create wxWidgets/wxPython user interfaces. At the moment it can generate Python, C++, Perl and XRC (wxWidgets' XML resources) code.XRC(wxWidgets' XML resources) is nice because it allows you to abstract your interface design (think of any program that uses XML to format skins). Overall, I think everyone using python should give SPE a try, even without gui programming its a great tool for writing code. It's free, and written in python using wxPython.. Stani (the Dev) is a great person for helping out with questions on using his package, he puts out regular updates and fixes. He's got some help from a few other people so its packaged in .exe, .rpm and standalone .zip formats. It's also on the standard repo's for Ubuntu. If you like it, be kind and toss the guy a few bucks for his efforts. If you do, you will get your name mentioned on the SPE news page and get a nice copy of his user manual (pdf).If you want to know more about SPE, check out: http://www.serpia.org/speor video demonstations at:http://showmedo.com/videos/series?name=PythonDevelopmentWithSPE On 11/3/06, Dick Moores <[EMAIL PROTECTED]> wrote: At 02:10 PM 11/3/2006, Chris Hengge wrote: I vouch for the SPE with wxGlade and XRC! (packaged together with IDE) I'd be very interested in hearing why you suggest that combination. Dick Moores On 11/3/06, Carlos Daniel Ruvalcaba Valenzuela < [EMAIL PROTECTED]> wrote: wxPython is good for cross-platform stuff and has a few gui designers (Boa Constructor and others comes to mind), I don't know much about PyQT state in this, but PyGtk + Glade (Gui Designer) is a very good combo. Is about choise, I suggest you to do some simple tests with everything until you find something to be confortable with. * PyGtk + Glade * Boa Contructor * SPE + wxPython On 11/3/06, Todd Dahl <[EMAIL PROTECTED]> wrote: > I am wanting to get into some GUI coding with Python and have heard about > PyQT and wxPython. Now I am definately not looking for some type of holy war > but can anyone give me a good reason to pick one over the other. > > Also I would like to have a designer with it or a seperate designer that > could be used with either. I plan on coding in Windows XP. > > Thanks, > > -Todd > > ___ > 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] question
"Doug Potter" <[EMAIL PROTECTED]> wrote >I don't get the output I would expect from the following. > >>> a = open('arp.txt') > >>> file = a.read() > >>> file = file.split('\n') Easier to do file = open('arp.txt').readlines() But file is a bad name since its an alias for open... > >>> b = open('arplist.txt','w') Not sure why you do this > >>> clean1 = [] > >>> clean is an empty list > >>> for i in file: > ... clean1 = i[26:40] clean is now overwritten by a string until you reach the end of the file upon which clean1 will be an empty string > >>> clean1 > '' Which it is... I think you may have meant to use the append method for i in file: clean1.append(i[26:40]) And since you can iterate over a file the whole thing shrinks to: clean1 = [] for i in open('arp.txt'): clean1.append(i[26:40]) print clean1 Or even more succinctly: clean1 = [i[26:40] for i in open('arp.txt')] print clean1 HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] question
Hi Doug, I'm not a Python guru, but shouldn't you be putting the output of file.split('\n') into a list, and not back into a string (for clarity's sake?). Also, if you have two trailing newlines on the file, your final string will be '', so you should be doing clean1.append(i[26:40]) in your for loop, right? Let me know if that helps... Jonathon Doug Potter wrote: > I don't get the output I would expect from the following. > The variable clean1 gives me an empty string. But if i change the for > loop to print i[26:40] I get all the info. > what do I need to do to capture all the data to clean1? > > Thanks. > > >>> a = open('arp.txt') > >>> file = a.read() > >>> file = file.split('\n') > >>> a.close() > >>> b = open('arplist.txt','w') > >>> clean1 = [] > >>> > >>> for i in file: > ... clean1 = i[26:40] > ... > >>> clean1 > '' > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor