Re: [Tutor] Simple reg-ex syntax?
How I would improve this: compile the regular expression. This is more efficient. self.digit_extractor = re.compile('(\d+)') then, use the findall method: self.allNumbers = self.digit_extractor.findall(self.aString) which will even work with multiline strings, but doesn't convert to integers. To convert, use a list comprehension: self.allNumbers = [int(i) for i in self.digit_extractor.findall(self.aString)] Cheers On Wednesday 12 March 2008 21:59, Allen Fowler wrote: Hello, I have code that looks something like: self.aString = abc123xyz self.theNumber = int(re.search('(\d+)',self.aString).group()) Is there a more Pythonic way of doing this? (Both the reg-ex and the Int coercion.) How about when I will need to extract more than one substring? Thank you, :) ___ _ Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs ___ 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] Begining Python
Meftah Tayeb wrote: hi my friend, i have active python installed and the python-Win is Ready but i have a Very Small problem: i am blind, and i am using a Screen reader this screen reader use the Microsoft Active Accessibiliti (MSAA) to find informations about actual object in the screen cursor You can develop Python programs using any text editor so you should be able to find a way to write programs that works with MSAA. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Correct way to call an outside program?
Allen Fowler wrote: Hello, I need to call an external command line .exe utility from my Python script. What is the best way to capture the output (if any) and (optionally) direct it to my normal standard output? subprocess.Popen().communicate() will do it: In [1]: import subprocess In [7]: x=subprocess.Popen('ls', stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() In [10]: print x[0] ... If you just want stdout and stderr of the subprocess to go to stdout and stderr of the calling process you can omit those arguments to Popen(). Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Correct way to call an outside program?
simplest way to run external commands ! import os cmd=/usr/bin/ssh 10.0.0.20 uptime os.popen(cmd) my cmd is just an example, use any cmd you want its output will be displayed to you. hope this helps On Thu, Mar 13, 2008 at 12:05 PM, Kent Johnson [EMAIL PROTECTED] wrote: Allen Fowler wrote: Hello, I need to call an external command line .exe utility from my Python script. What is the best way to capture the output (if any) and (optionally) direct it to my normal standard output? subprocess.Popen().communicate() will do it: In [1]: import subprocess In [7]: x=subprocess.Popen('ls', stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() In [10]: print x[0] ... If you just want stdout and stderr of the subprocess to go to stdout and stderr of the calling process you can omit those arguments to Popen(). Kent ___ 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] Begining Python
Am Mittwoch, den 12.03.2008, 09:39 +0100 schrieb Meftah Tayeb: hi my friend, i have active python installed and the python-Win is Ready but i have a Very Small problem: i am blind, and i am using a Screen reader this screen reader use the Microsoft Active Accessibiliti (MSAA) to find informations about actual object in the screen cursor for Java Based applications, this screen Reader use the Java Access Bridj (JAB) if it is no installed, java application is no accessible but in python: no accessibiliti layer is found for python ! Well, the problem here is, Python is just a language, so by default it has only a minimal UI. OTOH, the good thing from your point of view might be that it is text based. I don't know about the technologies you mentioned, but you might consider using IronPython, which is implemented inside the .NET world, which might have better support (or not) for your tools Andreas I'd suggest you: do not develope a new Accessibiliti layer please, develope only a MSAA Server for represanting all python UI object to MSAA based application and for JAVA / Python integration, please develope a JAVA Access Bridj (JAB) server to represanting python UI object to existing JAB Based application Thank you, Meftah Tayeb - Original Message - From: Terry Carroll [EMAIL PROTECTED] To: python tutor tutor@python.org Sent: Wednesday, March 12, 2008 12:52 AM Subject: Re: [Tutor] Begining Python On Tue, 11 Mar 2008, Meftah Tayeb wrote: 2. do you know a Free IDE for Python (for windows) ? Meftah, I'd suggest you install Activestate's distribution of Python, which includes the PythonWin environment (not quite an IDE, in that there is no GUI designer, for example) and many Windows-specific python modules. http://www.activestate.com/Products/activepython/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ 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] Hard time understanding classes
Thank you for all the suggestions regarding my struggle to understand classes, I will follow up on all the links provided and continue to search. On Wed, Mar 12, 2008 at 5:12 PM, Ole Henning Jensen [EMAIL PROTECTED] wrote: Norm All wrote: I am learning python and so far have done pretty well, until I got to the subject of classes which I have been struggling to understand for a while. I have not found any easy to understand tutorial on the web or looking in some books for a clear step by step example. Would appreciate any help or links to read. While this is a very long discussion thread, I found it extremly helpful in my understanding of classes and how they should be built/designed. http://www.nabble.com/designing-POOP-tc15290468.html ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Hello
Hi, I'm in a Python class and we were given the assignment to create our own graphic and my idea was to have a program that asks the user to click six points and then create a circle using the six points they clicked. Once they've done that I want eyes and a mouth to appear in the circle. I was wondering if that was possible or if I need to have them also click the points where the two eyes and nose should appear as well. I have the face written, it looks like this... def main(): winWidth = 200 # give a name to the window width winHeight = 150 #and height win = GraphWin('Face', winWidth, winHeight) # give title and dimensions win.setCoords(0, 0, winWidth, winHeight) # make right side up coordinates! head = Circle(Point(40,100), 25) # set center and radius head.setFill(yellow) head.draw(win) eye1 = Circle(Point(30, 105), 5) eye1.setFill('blue') eye1.draw(win) eye2 = Line(Point(45, 105), Point(55, 105)) # set endpoints eye2.setWidth(3) eye2.draw(win) mouth = Oval(Point(30, 90), Point(50, 85)) # set corners of bounding box mouth.setFill(red) mouth.draw(win) message = Text(Point(winWidth/2, 20), 'Click anywhere to quit.') message.draw(win) win.getMouse() win.close() main() I'm struggling with the Circle, however. This is what I have so far, but it won't work. def main(): winWidth = 300 winHeight = 300 win = GraphWin('Draw a Circle', winWidth, winHeight) win.setCoords(0, 0, winWidth, winHeight) win.setBackground('yellow') message = Text(Point(winWidth/2, 20), 'Click on six points') message.draw(win) # Get and draw six points of circle p1 = win.getMouse() p1.draw(win) p2 = win.getMouse() p2.draw(win) p3 = win.getMouse() p3.draw(win) p4 = win.getMouse() p4.draw(win) p5 = win.getMouse() p5.draw(win) p6 = win.getMouse() p6.draw(win) points = [p1, p2, p3, p4, p5, p6] # Use Oval object to draw the circle Circle = Oval(points) Circle.setFill('green') Circle.setOutline('pink') Circle.setWidth(4) # width of boundary line Circle.draw(win) # Wait for another click to exit message.setText('Click anywhere to quit.') message.setTextColor('red') message.setStyle('italic') message.setSize(20) win.getMouse() win.close() Any advice you have would help out a lot. Thank you for your time. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello
It appears to me that the following line would not work: Circle = Oval(points) The variable points is a list of six points, and I don't know how one would define a circle or oval with 6 points. At the top part of your program, an oval is defined using two points, which makes sense. Maybe you should use two of the points for the outline of the face, one point for each eye, and two for the mouth, which amounts to 6 points total. Greg -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Christopher Marlett Sent: Thursday, March 13, 2008 9:55 AM To: tutor@python.org Subject: [Tutor] Hello Hi, I'm in a Python class and we were given the assignment to create our own graphic and my idea was to have a program that asks the user to click six points and then create a circle using the six points they clicked. Once they've done that I want eyes and a mouth to appear in the circle. I was wondering if that was possible or if I need to have them also click the points where the two eyes and nose should appear as well. I have the face written, it looks like this... def main(): winWidth = 200 # give a name to the window width winHeight = 150 #and height win = GraphWin('Face', winWidth, winHeight) # give title and dimensions win.setCoords(0, 0, winWidth, winHeight) # make right side up coordinates! head = Circle(Point(40,100), 25) # set center and radius head.setFill(yellow) head.draw(win) eye1 = Circle(Point(30, 105), 5) eye1.setFill('blue') eye1.draw(win) eye2 = Line(Point(45, 105), Point(55, 105)) # set endpoints eye2.setWidth(3) eye2.draw(win) mouth = Oval(Point(30, 90), Point(50, 85)) # set corners of bounding box mouth.setFill(red) mouth.draw(win) message = Text(Point(winWidth/2, 20), 'Click anywhere to quit.') message.draw(win) win.getMouse() win.close() main() I'm struggling with the Circle, however. This is what I have so far, but it won't work. def main(): winWidth = 300 winHeight = 300 win = GraphWin('Draw a Circle', winWidth, winHeight) win.setCoords(0, 0, winWidth, winHeight) win.setBackground('yellow') message = Text(Point(winWidth/2, 20), 'Click on six points') message.draw(win) # Get and draw six points of circle p1 = win.getMouse() p1.draw(win) p2 = win.getMouse() p2.draw(win) p3 = win.getMouse() p3.draw(win) p4 = win.getMouse() p4.draw(win) p5 = win.getMouse() p5.draw(win) p6 = win.getMouse() p6.draw(win) points = [p1, p2, p3, p4, p5, p6] # Use Oval object to draw the circle Circle = Oval(points) Circle.setFill('green') Circle.setOutline('pink') Circle.setWidth(4) # width of boundary line Circle.draw(win) # Wait for another click to exit message.setText('Click anywhere to quit.') message.setTextColor('red') message.setStyle('italic') message.setSize(20) win.getMouse() win.close() Any advice you have would help out a lot. Thank you for your time. ___ 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] Hard time understanding classes
Hi Norm. Jerol created a 3-part video series on Python Objects, he uses IPython to talk about how and why they work: http://showmedo.com/videos/series?name=IntroductionToPythonObjectsUsingIPython_JerolH If you find his videos useful do please leave him a comment - authors love to know that their work is appreciated. Ian. Norm All wrote: Hello, I am learning python and so far have done pretty well, until I got to the subject of classes which I have been struggling to understand for a while. I have not found any easy to understand tutorial on the web or looking in some books for a clear step by step example. Would appreciate any help or links to read. Regards. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- http://Services.ShowMeDo.com http://ShowMeDo.com [EMAIL PROTECTED] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Correct way to call an outside program?
Thank you for the help. :) - Original Message simplest way to run external commands ! import os cmd=/usr/bin/ssh 10.0.0.20 uptime os.popen(cmd) my cmd is just an example, use any cmd you want its output will be displayed to you. hope this helps [SNIP] subprocess.Popen().communicate() will do it: In [1]: import subprocess In [7]: x=subprocess.Popen('ls', stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() In [10]: print x[0] If you just want stdout and stderr of the subprocess to go to stdout and stderr of the calling process you can omit those arguments to Popen(). Looking for last minute shopping deals? Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Correct way to call an outside program?
Allen Fowler wrote: Thank you for the help. :) - Original Message simplest way to run external commands ! import os cmd=/usr/bin/ssh 10.0.0.20 http://10.0.0.20 uptime os.popen(cmd) This is deprecated in python 2.5+. Use subrpocess instead of os.popen to make sure your code continues to work in the future. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] hypotenuse
I am in an early lesson in A Byte of Python. Instead of writing a program to find the area of a rectangle I thought it would be useful to write a program to determine the length of the diagonal of a golden rectangle, which would of course equal the sq root of the sum of the squares of the width and height. Here is my program: height = input (Height:) Height:1 width = input (Width:) Width:1.618 int = ((height**2) + (width**2)) print int 3.617924 hypotenuse * hypotenuse = int SyntaxError: can't assign to operator I looked ahead in the lesson and could find no mention of square roots. How do I find the square root of an integer? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] hypotenuse
Robert Childers wrote: I am in an early lesson in A Byte of Python. Instead of writing a program to find the area of a rectangle I thought it would be useful to write a program to determine the length of the diagonal of a golden rectangle, which would of course equal the sq root of the sum of the squares of the width and height. Here is my program: height = input (Height:) Height:1 width = input (Width:) Width:1.618 int = ((height**2) + (width**2)) print int 3.617924 hypotenuse * hypotenuse = int SyntaxError: can't assign to operator I looked ahead in the lesson and could find no mention of square roots. How do I find the square root of an integer? There is a library called 'math' with a 'sqrt' function. To import modules, you use the 'import' command, as: import math To import a specific function from a module, you use a different syntax: from math import sqrt If you used the first style of import, you'd call the function like this: x = math.sqrt(2) With the second style, you'd call the function like this: x = sqrt(2) One other note: You named your variable 'int'. You should avoid naming your variables after keywords, because you will run into problems later. So you shouldn't use the names int, char, for, while, lambda, def, is, and, and names like that. For example: int('12345') 12345 int = 3 * 3 int('12345') Traceback (most recent call last): File pyshell#2, line 1, in module int('12345') TypeError: 'int' object is not callable As you can see, naming the variable 'int' kept us from being able to call the 'int' function to convert the string into an integer. HTH, -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] hypotenuse
On 14/03/2008, Robert Childers [EMAIL PROTECTED] wrote: I am in an early lesson in A Byte of Python. Instead of writing a program to find the area of a rectangle I thought it would be useful to write a program to determine the length of the diagonal of a golden rectangle, which would of course equal the sq root of the sum of the squares of the width and height. Here is my program: height = input (Height:) Height:1 width = input (Width:) Width:1.618 int = ((height**2) + (width**2)) print int 3.617924 hypotenuse * hypotenuse = int SyntaxError: can't assign to operator I looked ahead in the lesson and could find no mention of square roots. How do I find the square root of an integer? Hi Robert, This kind of thing: hypotenuse * hypotenuse = int will never work. The thing on the left side of an equals sign must always be a single name. (there is an exception to this -- unpacking -- but I won't explain it now. You should come to it in time) Python provides a square root function, but it's not available by default. You need to import the math module first -- your tutorial should cover importing. Basically, the code will look something like this: import math hyp_squared = height**2 + width**2 hypotenuse = math.sqrt(hyp_squared) Finally, int is a built-in type, so it's bad programming style to use it as a variable name. (the same goes for list, str, and a few others) That's why, in my example, I used hyp_squared as the name for the hypotenuse squared. -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] hypotenuse
assert 4**0.5 == 2 More generally: nth root of x: x ** (1.0/n) Or even more generally, take the 3rd root of the square of x: x ** (2.0/3.0) And when we are already extending the scope of the mailing list to math basics: 1.0/(x**2) == x**-2 (negating the power gives the inverse.) Andreas Am Donnerstag, den 13.03.2008, 18:02 -0700 schrieb Robert Childers: I am in an early lesson in A Byte of Python. Instead of writing a program to find the area of a rectangle I thought it would be useful to write a program to determine the length of the diagonal of a golden rectangle, which would of course equal the sq root of the sum of the squares of the width and height. Here is my program: height = input (Height:) Height:1 width = input (Width:) Width:1.618 int = ((height**2) + (width**2)) print int 3.617924 hypotenuse * hypotenuse = int SyntaxError: can't assign to operator I looked ahead in the lesson and could find no mention of square roots. How do I find the square root of an integer? ___ 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] hypotenuse
On 14/03/2008, Scott Kerr [EMAIL PROTECTED] wrote: Hello, I am also new to python and programming. Since two have already posted that you need to import math modules to do square roots, I am curious. Why can you not use something like: hypotenuse = hyp_squared**1/2 or hypotenuse = hyp_squared**.5 Hmm, good question :-) I suppose there is no strong reason. The best I can offer is that for many people, the concept of a square root is more accessible through the name sqrt, whereas fractional exponentiation requires a brief brain pause while they remember what it means. Hence Luke and I, who are familiar with the math module, more easily think math.sqrt than x**0.5. And the same benefit might apply to a hypothetical reader of the code. Interestingly, using x**0.5 seems faster too: Morpork:~ repton$ python -m timeit -s x = 2 y = x**0.5 100 loops, best of 3: 0.298 usec per loop Morpork:~ repton$ python -m timeit -s import math -s x = 2 y = math.sqrt(x) 100 loops, best of 3: 0.487 usec per loop Morpork:~ repton$ python -m timeit -s import math -s m = math.sqrt -s x = 2 y = m(x) 100 loops, best of 3: 0.371 usec per loop though the advantage vanishes if you need a function: Morpork:~ repton$ python -m timeit -s m = lambda x: x**0.5 -s x = 2 y = m(x) 100 loops, best of 3: 0.526 usec per loop (if you've never seen the timeit module before, it's a good way to benchmark short snippets of code. The -s option is used for non-benchmarked startup code. So, for example, ' python -m timeit -s import math -s x = 2 y = math.sqrt(x)' means import math, set x to 2, then run y=math.sqrt(x) many many times to estimate its performance. From the interpreter, type 'import timeit; help(timeit)' for more information) -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor