Re: [Tutor] Serve a file using http
Sebastian Lara wrote: > Hello all, > > I'm using a SimpleXMLRPCServer to update a file remotely from a simple > xml-rpc python client. After that, I need to serve this file but I > don't know if I can do this with SimpleXMLRPCServer or if I need > another server. If you want to serve the file over HTTP so it is visible in a browser you will need another server. If your needs are simple then SimpleHTTPServer, in the standard library, may work for you. If you need more functionality then you should look at the other HTTP servers available for Python such as CherryPy and Karrigell. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading random line from a file
Tiger12506 wrote: > If you truly wish to kill yourself trying to make it as efficient memory as > possible, then you can follow this example. (This is more like what I would > write in C). > The getrandomline function picks a random byte between the beginning and the > end of the file, then backs up until the beginning of the line and uses > readline to return the whole line. It probably doesn't matter, but this will pick longer lines more often than short ones. > I tested it :-) Hmm. What happens if you run it on a file with only one line? (see below) > > > # > from os import stat > from random import randint > > def getrandomline(f, length): > pos = randint(0,length) > f.seek(pos) > while f.read(1)!='\n': > try: > f.seek(-2,1) > except IOError: # This is to catch seeking before the > beginning of the file > f.seek(0) I think you need a break here to avoid an infinite loop. Kent > return f.readline() > > f = file("quotes.txt","rb") > sizeoffile = stat("quotes.txt")[6] > > while (1): > print getrandomline(f, sizeoffile), > > f.close() > ### ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Basic DB question
z machinez wrote: > Thank you. I am mainly interested in finding ways to connect to aKDB+ > (KX Systems). That is the relational database that we are using. And I > would like to open a connection to it. I understand from the vendor that > they do not have a good ODBC driver. Googling 'python kdb kx' finds http://kx.com/a/k/connect/python/pyk-0.9/ There is a C interface to kdb so you might be able to use the ctypes module to access it from Python though that is probably not a beginner project and not something I can help with. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How do you install EGG files
Terry Carroll wrote: > On Wed, 11 Jul 2007, shawn bright wrote: > >> Hey there all, >> i got the news that storm was released as open source. Storm is a db orm for >> python. >> i have a downloaded package and i would like to play with it, but it does >> not come with any install instructions. >> i found the package here https://storm.canonical.com/FrontPage >> there is a makefile in the top level folder, so should i use gcc and try to >> 'make' 'make install' or is that not necessarily the way to go here? The makefile is just a front-end to the tests. I think you can just copy the storm directory (the one containing __init__.py, not the top-level storm-0.9 dir) to your site-packages directory. > I see it comes in an EGG format, which is just a ZIP file. What will (I > think) work is to open the EGG file with your favorite unzipper and unzip > into your site-packages directory (making sure to use the directory names > from the EGG file). egg files are zip files packaged for use with easy_install: http://peak.telecommunity.com/DevCenter/EasyInstall In many cases easy_install gives one-step installation of Python packages, including downloading from the CheeseShop. In the case of storm it didn't work for me (using the --dry-run option though). YMMV. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading random line from a file
> ---Original Message--- > From: Kent Johnson <[EMAIL PROTECTED]> > Subject: Re: [Tutor] reading random line from a file > Sent: 2007-07-18 10:19 > [SNIP] > > It probably doesn't matter, but this will pick longer lines more often > than short ones. > This method only keeps one line in memory, only reads through the file once, and does not favor lines based on any characteristic of the line. It's probably fast enough to not even bother keeping an index around: #!/bin/env python import os import random text = 'shaks12.txt' if not os.path.exists(text): os.system('wget http://www.gutenberg.org/dirs/etext94/shaks12.txt') f = file(text, 'rb') def randline(f): for i,j in enumerate(f): if random.randint(0,i) == i: line = j return line print randline(f) --- Yorick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How do you install EGG files
Hey thanks for this, yes, i used the easy_install method and it did work on the python 2.4, the python 2.5 failed. shawn On 7/18/07, Kent Johnson <[EMAIL PROTECTED]> wrote: Terry Carroll wrote: > On Wed, 11 Jul 2007, shawn bright wrote: > >> Hey there all, >> i got the news that storm was released as open source. Storm is a db orm for >> python. >> i have a downloaded package and i would like to play with it, but it does >> not come with any install instructions. >> i found the package here https://storm.canonical.com/FrontPage >> there is a makefile in the top level folder, so should i use gcc and try to >> 'make' 'make install' or is that not necessarily the way to go here? The makefile is just a front-end to the tests. I think you can just copy the storm directory (the one containing __init__.py, not the top-level storm-0.9 dir) to your site-packages directory. > I see it comes in an EGG format, which is just a ZIP file. What will (I > think) work is to open the EGG file with your favorite unzipper and unzip > into your site-packages directory (making sure to use the directory names > from the EGG file). egg files are zip files packaged for use with easy_install: http://peak.telecommunity.com/DevCenter/EasyInstall In many cases easy_install gives one-step installation of Python packages, including downloading from the CheeseShop. In the case of storm it didn't work for me (using the --dry-run option though). YMMV. 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
[Tutor] Question from a newbie
Hi I am totally new to Python. I downloaded it and I really like it as its very simple but I came across a small problem. My question is: How can I save something in Python. I tried to save it but it don't save it. Even I tried to add *.py as extension in the end of file name but still it doesn't works. I coded a simple program but don't know how to save it now in the PythonWin Editor. Let me tell you that I have installed the windows xp version of Python. Muhammad Junaid Khan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question from a newbie
Junaid.Khan/Finance/Lahore wrote: > > Hi > > I am totally new to Python. I downloaded it and I really like it as > its very simple but I came across a small problem. My question is: > > How can I save something in Python. I tried to save it but it don’t > save it. Even I tried to add *.py as extension in the end of file name > but still it doesn’t works. I coded a simple program but don’t know > how to save it now in the PythonWin Editor. Let me tell you that I > have installed the windows xp version of Python. > How do you know it's not saving? The way you save in PythonWin should be similar to saving in another program, but I haven't used it in a few years so I may have forgotten something. Thanks for giving us your IDE and OS. -Luke > > **Muhammad Junaid Khan** > > > > ___ > 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 from a newbie
I have exactly the same problem using PythonWin. I know it's not saving because in the location where I am saving my program to, nothing appears, I can even search my whole computer for the file but nothing... I just use Komodo Edit for writing and saving the final versions and PythonWin for testing lines of code up until then. - Original Message - From: "Luke Paireepinart" <[EMAIL PROTECTED]> To: "Junaid.Khan/Finance/Lahore" <[EMAIL PROTECTED]> Cc: Sent: Wednesday, July 18, 2007 10:40 AM Subject: Re: [Tutor] Question from a newbie Junaid.Khan/Finance/Lahore wrote: > > Hi > > I am totally new to Python. I downloaded it and I really like it as > its very simple but I came across a small problem. My question is: > > How can I save something in Python. I tried to save it but it don’t > save it. Even I tried to add *.py as extension in the end of file name > but still it doesn’t works. I coded a simple program but don’t know > how to save it now in the PythonWin Editor. Let me tell you that I > have installed the windows xp version of Python. > How do you know it's not saving? The way you save in PythonWin should be similar to saving in another program, but I haven't used it in a few years so I may have forgotten something. Thanks for giving us your IDE and OS. -Luke > > **Muhammad Junaid Khan** > > > > ___ > 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] while Loop
Hi all, I'm writing a calculator for an online game that I used to play but don't seem to be able to break out of the while loop, the program will just go over and over the loop, crashing the program and I don't know if Python is just really slow at this type of thing or i'm doing it completely wrong in Python (the equivalent code in JavaScript works fine). Python code - def main(): usedPocketsOne = 192000 junkiesOne = 500 labSpaceOne = 0 resultOne = 0 while usedPocketsOne > (junkiesOne - labSpaceOne) * 17: resultOne = resultOne + 1 usedPocketsOne = (UsedPocketsOne - junkiesOne + labSpaceOne) * 17 junkiesOne = junkiesOne + 1 main() And the JavaScript equivalent (incase there are any stalwarts beside Alan here) - function main() { var usedPocketsOne = 192000 var junkiesOne = 500 var labSpaceOne = 0 var resultOne = 0 while (usedPocketsOne > junkiesOne - labSpaceOne * 17) { resultOne = resultOne + 1 usedPocketsOne = usedPocketsOne - junkiesOne + labSpaceOne * 17 junkiesOne = junkiesOne + 1 } } Thanks in advance for any help :)___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] while Loop
Darren Williams wrote: > Hi all, > > I'm writing a calculator for an online game that I used to play but > don't seem to be able to break out of the while loop, the program will > just go over and over the loop, crashing the program and I don't know if > Python is just really slow at this type of thing or i'm doing it > completely wrong in Python (the equivalent code in JavaScript works fine). > > Python code - > > def main(): > usedPocketsOne = 192000 > junkiesOne = 500 > labSpaceOne = 0 > > resultOne = 0 > while usedPocketsOne > (junkiesOne - labSpaceOne) * 17: > resultOne = resultOne + 1 > usedPocketsOne = (UsedPocketsOne - junkiesOne + labSpaceOne) * 17 Maybe this should be usedPocketsOne = UsedPocketsOne - junkiesOne + labSpaceOne * 17 which is what you have in the JS. Kent > junkiesOne = junkiesOne + 1 > > main() > > And the JavaScript equivalent (incase there are any stalwarts beside > Alan here) - > > function main() { > var usedPocketsOne = 192000 > var junkiesOne = 500 > var labSpaceOne = 0 > > var resultOne = 0 > while (usedPocketsOne > junkiesOne - labSpaceOne * 17) { > resultOne = resultOne + 1 > usedPocketsOne = usedPocketsOne - junkiesOne + labSpaceOne * 17 > junkiesOne = junkiesOne + 1 > } > } > > Thanks in advance for any help :) > > > > > ___ > 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] while Loop
Darren Williams wrote: > Hi all, > > I'm writing a calculator for an online game that I used to play but > don't seem to be able to break out of the while loop, the program will > just go over and over the loop, crashing the program and I don't know > if Python is just really slow at this type of thing or i'm doing it > completely wrong in Python (the equivalent code in JavaScript works fine). I don't think you're doing it completely wrong, just partially :) And I doubt that, if it's quick in Javascript, it will be any slower in Python, since Javascript is interpreted as well, but by the browser while it's simultaneously downloading the content of the webpage and rendering it to the screen. Also, I'm pretty sure the code here is not equivalent to the Javascript code you included (specific remarks below.) > > Python code - > > def main(): > usedPocketsOne = 192000 > junkiesOne = 500 > labSpaceOne = 0 > > resultOne = 0 > while usedPocketsOne > (junkiesOne - labSpaceOne) * 17: I don't know much about Javascript, but doesn't it have order of operations? For example, usedPocketsOne > junkiesOne - labSpaceOne * 17 is generally assumed to be x = labSpaceOne * 17 y = junkiesOne - x usedPocketsOne > y Whereas your Python equivalent, with the parenthesis, usedPocketsOne > (junkiesOne - labSpaceOne) * 17 changes the order of operations (forces the subtraction before the multiplication) and hence changes the resulting value. That is, unless Javascript evaluates left-to-right with no operator heirarchy (which I would be saddened to learn, if it's true.) > resultOne = resultOne + 1 > usedPocketsOne = (UsedPocketsOne - junkiesOne + labSpaceOne) * 17 This shouldn't even run - you should get a syntax error because UsedPocketsOne is not defined anywhere else in the program (that you've shown us.) Perhaps you're running it in IDLE with no subprocess and some value for UsedPocketsOne is hanging around (from previous edits of the program, or interpreter session, or something else) and mucking up the works? -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] while Loop
Luke and Kent, you're right, I didn't think JavaScript calculated multiplaction and division before addition and subtraction but seems it does :) I dunno what you mean about usedPocketsOne not being defined, didn't I define it with usedPocketsOne = 192000? - Original Message - From: "Luke Paireepinart" <[EMAIL PROTECTED]> To: "Darren Williams" <[EMAIL PROTECTED]> Cc: Sent: Wednesday, July 18, 2007 11:57 AM Subject: Re: [Tutor] while Loop > Darren Williams wrote: >> Hi all, >> I'm writing a calculator for an online game that I used to play but >> don't seem to be able to break out of the while loop, the program will >> just go over and over the loop, crashing the program and I don't know if >> Python is just really slow at this type of thing or i'm doing it >> completely wrong in Python (the equivalent code in JavaScript works >> fine). > I don't think you're doing it completely wrong, just partially :) > And I doubt that, if it's quick in Javascript, it will be any slower in > Python, since Javascript is interpreted as well, but by the browser while > it's simultaneously downloading the content of the webpage and rendering > it to the screen. > Also, I'm pretty sure the code here is not equivalent to the Javascript > code you included (specific remarks below.) >> Python code - >> def main(): >> usedPocketsOne = 192000 >> junkiesOne = 500 >> labSpaceOne = 0 >> resultOne = 0 >> while usedPocketsOne > (junkiesOne - labSpaceOne) * 17: > I don't know much about Javascript, but doesn't it have order of > operations? > For example, > usedPocketsOne > junkiesOne - labSpaceOne * 17 > is generally assumed to be > x = labSpaceOne * 17 > y = junkiesOne - x > usedPocketsOne > y > > > Whereas your Python equivalent, with the parenthesis, > usedPocketsOne > (junkiesOne - labSpaceOne) * 17 > > changes the order of operations (forces the subtraction before the > multiplication) > and hence changes the resulting value. That is, unless Javascript > evaluates left-to-right with no operator heirarchy (which I would be > saddened to learn, if it's true.) >> resultOne = resultOne + 1 >> usedPocketsOne = (UsedPocketsOne - junkiesOne + labSpaceOne) * 17 > This shouldn't even run - you should get a syntax error because > UsedPocketsOne is not defined anywhere else in the program (that you've > shown us.) > Perhaps you're running it in IDLE with no subprocess and some value for > UsedPocketsOne is hanging around (from previous edits of the program, or > interpreter session, or something else) and mucking up the works? > -Luke > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] python to serve xml or json or....
Hello, I' ve a bunch of things to ask about using Python as a server side language in a web application: I have a mysql db and I've alredy developed a python app that query the db and shot-out an xml file (made by the nice elementtree module like some people here on the list suggested). This file is then chewed-up by a javascript file who's responsible to generate a graph (see http://www.JSViz.org). Now I need to put it on line so that a user from web browser can query the db and see the graph. Next step could be to use ajax but for now something like a "PHP style" in python would be enough. I saw some PSP (python server pages) solution around but since I'm more or less a newbie I can't understand if I have to go to webware or spyce or a full fledged django. I like to start simple and make things more perfect but If there is a full featured solution to my case I will apply It. Can You Help? Any advice would be appreciated! -- http://picio.gotdns.com ...Il mio blog su NSLU2 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] python and Java developers, same team
Hello, a friend of mine want me to join a team on a project. They use Java (the IDE is java studio creator), and the deployment server is Tomcat. Anyway I have all my apps in python and I'd like to continue my development using it instead of learning java. Is it possible to translate my apps from python to Java in a way they can integrate them in Tomcat? Is it jython? I have just some simple apps and scripts. (Or Is it better that I start to learn Java leaving a part my beloved python? I hope you answer NO ;) ) -- http://picio.gotdns.com ...Il mio blog su NSLU2 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python and Java developers, same team
Picio wrote: > Hello, a friend of mine want me to join a team on a project. They use > Java (the IDE is java studio creator), and the deployment server is > Tomcat. Anyway I have all my apps in python and I'd like to continue > my development using it instead of learning java. > Is it possible to translate my apps from python to Java in a way they > can integrate them in Tomcat? Is it jython? I have just some simple > apps and scripts. Jython is a good bet, depending on what the scripts do. You can write servlets in Jython and embed them in Tomcat. > (Or Is it better that I start to learn Java leaving a part my beloved > python? I hope you answer NO ;) ) You will probably have to learn some Java. Or run the other way :-) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python to serve xml or json or....
Picio wrote: > Hello, I' ve a bunch of things to ask about using Python as a server > side language in a web application: > I have a mysql db and I've alredy developed a python app that query > the db and shot-out an xml file (made by the nice elementtree module > like some people here on the list suggested). This file is then > chewed-up by a javascript file who's responsible to generate a graph > (see http://www.JSViz.org). > Now I need to put it on line so that a user from web browser can query > the db and see the graph. Next step could be to use ajax but for now > something like a "PHP style" in python would be enough. I saw some PSP > (python server pages) solution around but since I'm more or less a > newbie I can't understand if I have to go to webware or spyce or a > full fledged django. > I like to start simple and make things more perfect but If there is a > full featured solution to my case I will apply It. This is hard to answer because there are many possibilities spanning a broad spectrum, and which is best depends on both requirements and personal taste. Django, TurboGears and Pylons are popular and include or support template languages and AJAX. Many more options listed here: http://wiki.python.org/moin/WebFrameworks Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] while Loop
Darren Williams wrote: > Luke and Kent, you're right, I didn't think JavaScript calculated > multiplaction and division before addition and subtraction but seems > it does :) > > I dunno what you mean about usedPocketsOne not being defined, didn't I > define it with usedPocketsOne = 192000? no, I said UsedPocketsOne was not defined. Note the different starting letter. Python is case senstitive, meaning usedPocketsOne is not the same as UsedPocketsOne. So yes, you defined usedPocketsOne with the assignment to 192000, but you did not define UsedPocketsOne. Note what happens when I test your code: >>> def main(): usedPocketsOne = 192000 junkiesOne = 500 labSpaceOne = 0 resultOne = 0 while usedPocketsOne > (junkiesOne - labSpaceOne) * 17: resultOne = resultOne + 1 usedPocketsOne = (UsedPocketsOne - junkiesOne + labSpaceOne) * 17 >>> main() Traceback (most recent call last): File "", line 1, in -toplevel- main() File "", line 10, in main usedPocketsOne = (UsedPocketsOne - junkiesOne + labSpaceOne) * 17 NameError: global name 'UsedPocketsOne' is not defined You should get a similar error, unless you've somehow defined UsedPocketsOne somewhere else. HTH, -Luke Correction of previous e-mail: This raises a NameError, not a SyntaxError, as I said before. sorry about that. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] while Loop
Oops, didn't notice the uppercase U, thanks Luke. - Original Message - From: "Luke Paireepinart" <[EMAIL PROTECTED]> To: "Darren Williams" <[EMAIL PROTECTED]> Cc: Sent: Wednesday, July 18, 2007 3:08 PM Subject: Re: [Tutor] while Loop > Darren Williams wrote: >> Luke and Kent, you're right, I didn't think JavaScript calculated >> multiplaction and division before addition and subtraction but seems >> it does :) >> >> I dunno what you mean about usedPocketsOne not being defined, didn't I >> define it with usedPocketsOne = 192000? > no, I said UsedPocketsOne was not defined. Note the different starting > letter. > Python is case senstitive, meaning usedPocketsOne is not the same as > UsedPocketsOne. > So yes, you defined usedPocketsOne with the assignment to 192000, but > you did not define UsedPocketsOne. > > Note what happens when I test your code: > >>> def main(): > > usedPocketsOne = 192000 > junkiesOne = 500 > labSpaceOne = 0 > > resultOne = 0 > while usedPocketsOne > (junkiesOne - labSpaceOne) * 17: > resultOne = resultOne + 1 > usedPocketsOne = (UsedPocketsOne - junkiesOne + labSpaceOne) * 17 > > > >>> main() > > Traceback (most recent call last): > File "", line 1, in -toplevel- >main() > File "", line 10, in main >usedPocketsOne = (UsedPocketsOne - junkiesOne + labSpaceOne) * 17 > NameError: global name 'UsedPocketsOne' is not defined > > You should get a similar error, unless you've somehow defined > UsedPocketsOne somewhere else. > HTH, > -Luke > > Correction of previous e-mail: This raises a NameError, not a > SyntaxError, as I said before. sorry about that. > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading random line from a file
Yuck. Talk about a one shot function! Of course it only reads through the file once! You only call the function once. Put a second print randline(f) at the bottom of your script and see what happens :-) JS > This method only keeps one line in memory, only reads through the file > once, and does not favor lines based on any characteristic of the line. > It's probably fast enough to not even bother keeping an index around: > > #!/bin/env python > > import os > import random > > text = 'shaks12.txt' > if not os.path.exists(text): > os.system('wget http://www.gutenberg.org/dirs/etext94/shaks12.txt') > > f = file(text, 'rb') > > def randline(f): > for i,j in enumerate(f): > if random.randint(0,i) == i: > line = j > return line > > print randline(f) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] pybluez + link quality
Hi everybody: I would like to know how can i get the link wuality of a device (cellphone), with the pybluez module. (if there is another way to do it without the pybluez, i'm interested too) Thanks -- Flavio Percoco Premoli, A.K.A. [Flaper87] http://www.flaper87.com Usuario Linux registrado #436538 Geek by nature, Linux by choice, Debian of course. Key Fingerprint: CFC0 C67D FF73 463B 7E55 CF43 25D1 E75B E2DB 15C7 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pybluez + link quality
Flaper87 wrote: > Hi everybody: > > I would like to know how can i get the link wuality of a device > (cellphone), with the pybluez module. (if there is another way to do > it without the pybluez, i'm interested too) PyBluez is not cross-platform for all aspects of the bluetooth protocol, so you might want to check to see if what you're trying to do is cross-platform. -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pybluez + link quality
No, what i'm doing is just for linux 2007/7/18, Luke Paireepinart <[EMAIL PROTECTED]>: Flaper87 wrote: > Hi everybody: > > I would like to know how can i get the link wuality of a device > (cellphone), with the pybluez module. (if there is another way to do > it without the pybluez, i'm interested too) PyBluez is not cross-platform for all aspects of the bluetooth protocol, so you might want to check to see if what you're trying to do is cross-platform. -Luke -- Flavio Percoco Premoli, A.K.A. [Flaper87] http://www.flaper87.com Usuario Linux registrado #436538 Geek by nature, Linux by choice, Debian of course. Key Fingerprint: CFC0 C67D FF73 463B 7E55 CF43 25D1 E75B E2DB 15C7 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Working with bash (subversion)
I'm trying to write a program that will list all subversion repository directories, then issue a command using each directory as an argument, then parse those results. So far, I'm able to get a list of the directories...and that's it! Here's what I've got so far: = #!/usr/bin/env python import commands as c lsout = c.getoutput('ls -d /home/svn/repository/*/').split('\n') results = file("results.txt", "w") for row in lsout: results.write(c.getoutput('svnadmin lslocks ' + eval(row))) = lsout is a list of repository directories, like I wanted. (['/home/svn/repository/projecta/', '/home/svn/repository/projectb/', '/home/svn/repository/projectc/'] The next 3 lines are probably totally wrong. I want to perform the following bash command for each directory... == svnadmin lslocks /home/svn/repository/projecta == ...and then parse the results. I just don't know where/how to store the results from each svnadmin command. When I run the program in its current form, I get the following error: === Traceback (most recent call last): File "checklocks.py", line 8, in ? results.write(c.getoutput('svnadmin lslocks ' + eval(row))) File "", line 1 /home/svn/repository/projecta/ ^ SyntaxError: invalid syntax === Any advice would be much appreciated. Thanks! -Justin Cardinal ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with bash (subversion)
Justin Cardinal wrote: > I'm trying to write a program that will list all subversion repository > directories, then issue a command using each directory as an argument, > then parse those results. So far, I'm able to get a list of the > directories...and that's it! > Here's what I've got so far: > = > #!/usr/bin/env python > > import commands as c > > lsout = c.getoutput('ls -d /home/svn/repository/*/').split('\n') > results = file("results.txt", "w") > for row in lsout: > results.write(c.getoutput('svnadmin lslocks ' + eval(row))) > = > lsout is a list of repository directories, like I wanted. > (['/home/svn/repository/projecta/', '/home/svn/repository/projectb/', > '/home/svn/repository/projectc/'] > The next 3 lines are probably totally wrong. I want to perform the > following bash command for each directory... > == > svnadmin lslocks /home/svn/repository/projecta > == > ...and then parse the results. I just don't know where/how to store > the results from each svnadmin command. When I run the program in its > current form, I get the following error: > === > Traceback (most recent call last): > File "checklocks.py", line 8, in ? > results.write(c.getoutput('svnadmin lslocks ' + eval(row))) > File "", line 1 > /home/svn/repository/projecta/ > ^ > SyntaxError: invalid syntax > === > > Any advice would be much appreciated. Thanks! eval evaluates the string as a python command. Because there are no Python commands that start with a forward slash, Python's pointing to this as a syntax error. Because row is a string already (and note that 'column' would be a more apt term for this, as a 1-dimensional list is more similar to a single row than a single column) you can just do simple string concatenation (or you can use string substitution but in this case it's not necessary and would just make your code less readable.) Here's a basic example: >>> 'hello ' + 'world!' 'hello world!' Does that tell you everything you need to know? (recall that whether 'world!' is referenced using a variable name or used directly, the effect will be the same. I.E. a = 'ba' a + 'nana' has the same end result as 'ba' + 'nana'with the exception being that the variable 'a' is not defined or is not bound to a new value after this statement.) HTH, -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with bash (subversion)
Justin Cardinal wrote: > I'm trying to write a program that will list all subversion repository > directories, then issue a command using each directory as an argument, > then parse those results. So far, I'm able to get a list of the > directories...and that's it! > Here's what I've got so far: > = > #!/usr/bin/env python > > import commands as c As a side note, why is this 'commands' module part of the standard library? it just appears to wrap os.popen in a very bare way and it is not platform-independent. It seems pretty useless, as far as I can tell. -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with bash (subversion)
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 mkarg is quite useful, from time to time. Andreas Luke Paireepinart wrote: > Justin Cardinal wrote: >> I'm trying to write a program that will list all subversion repository >> directories, then issue a command using each directory as an argument, >> then parse those results. So far, I'm able to get a list of the >> directories...and that's it! >> Here's what I've got so far: >> = >> #!/usr/bin/env python >> >> import commands as c > As a side note, why is this 'commands' module part of the standard library? > it just appears to wrap os.popen in a very bare way and it is not > platform-independent. > It seems pretty useless, as far as I can tell. > -Luke > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.2 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFGnorcHJdudm4KnO0RAilrAKCDCM5eOvtqgVJ0ViouGpuEqOjDOACgxg88 4sCL39aNoFiZqRR4e1zUEJk= =Q4ZW -END PGP SIGNATURE- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading random line from a file
> import os > import random > > text = 'shaks12.txt' > if not os.path.exists(text): > os.system('wget http://www.gutenberg.org/dirs/etext94/shaks12.txt') > > def randline(f): >for i,j in enumerate(file(f, 'rb')): Alright. But put randline in a loop and you open a lot of file handles. Thank goodness python has GB. Seperate variable, open file at start, close file at end. So the file is read every time you call randline. At least as far as the line chosen. Whereas my version only reads at absolute most twice the same line. And it will run faster. Searching through the file lines to find the one who's index matches i is time-consuming. Yes, my version will favor longer lines, but I don't think that seriously strict randomization is necessary? IMHO, memory and speed are more important here. (You must forgive me a little, I've been studying C and assembly) I'm just proud of my function ;-) JS ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with bash (subversion)
> results.write(c.getoutput('svnadmin lslocks ' + eval(row))) Mmm... I want to add that the eval function tries to execute whatever is in the argument passed as python expressions. >>> eval('1+2') 3 >>> row = 4 >>> 1+row 5 >>> eval('1+row') 5 >>> JS ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python Image library
import time import ImageGrab # Part of PIL from ctypes import * # Load up the Win32 APIs we need to use. class RECT(Structure): _fields_ = [ ('left', c_ulong), ('top', c_ulong), ('right', c_ulong), ('bottom', c_ulong) ] # time.sleep(2) GetForegroundWindow = windll.user32.GetForegroundWindow GetWindowRect = windll.user32.GetWindowRect # Sleep for 2 seconds - click the window you want to grab. #time.sleep(2) # Grab the foreground window's screen rectangle. rect = RECT() foreground_window = GetForegroundWindow() GetWindowRect(foreground_window, byref(rect)) image = ImageGrab.grab((rect.left, rect.top, rect.right, rect.bottom)) # Save the screenshot as a BMP. time.sleep(2) image.save("c:\python_codes\screenshot.bmp") # Get the pixel 10 pixels along the top of the foreground window - this # will be a piece of the window border. # print time.time() start = time.time() pixels = image.getdata() for x in xrange(0, 500): for y in xrange(0, 500): rgb = pixels[500 * x + y] # print pixels[500 * 2 + 400] print ( time.time() - start ) # PIL returns colours as RGB values packed into a triple: #print "RGB(%d, %d, %d)" % (rgb[0], rgb[1], rgb[2]) # This prints RGB(0, 74, 216) on my XP machine getdata() returns a flattened list, [n] but i am not sure how to access it. when I want to get rgb from a window of 100,200, get data starts from 0(0~99, 0~199) the point of x,y = 2, 1 do I put in pixel[100] ? it's actually not the case @_@ what should I put in ? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with bash (subversion)
That fixed it, thanks! Now I just need to do some studying on working with lists (or whatever this output is...) so I can filter out the results I don't want. Here's an updated version of the program: #!/usr/bin/env python import commands as c lsout = c.getoutput('ls -d /home/svn/repository/*/').split('\n') results = [] for row in lsout: temp = c.getoutput('svnadmin lslocks ' + row).split('\n') if temp != ['']: results.append(temp) print results ...and the output: ['Path: /test/trunk/data.bin', 'UUID Token: opaquelocktoken:9ee85aae-c9dc-4388-8958-87b708e628a3', 'Owner: jcardinal', 'Created: 2007-07-17 14:36:18 -0500 (Tue, 17 Jul 2007)', 'Expires: ', 'Comment (1 line):', '', ''] Thanks very much to all who replied, it's amazing how quick help arrives! -Justin > Here's what I've got so far: > = > #!/usr/bin/env python > > import commands as c > > lsout = c.getoutput('ls -d /home/svn/repository/*/').split('\n') > results = file("results.txt", "w") > for row in lsout: > results.write(c.getoutput('svnadmin lslocks ' + eval(row))) > === > Traceback (most recent call last): > File "checklocks.py", line 8, in ? > results.write(c.getoutput('svnadmin lslocks ' + eval(row))) > File "", line 1 > /home/svn/repository/projecta/ > ^ > SyntaxError: invalid syntax > === > > Any advice would be much appreciated. Thanks! eval evaluates the string as a python command. Because there are no Python commands that start with a forward slash, Python's pointing to this as a syntax error. Because row is a string already (and note that 'column' would be a more apt term for this, as a 1-dimensional list is more similar to a single row than a single column) you can just do simple string concatenation (or you can use string substitution but in this case it's not necessary and would just make your code less readable.) Here's a basic example: >>> 'hello ' + 'world!' 'hello world!' Does that tell you everything you need to know? (recall that whether 'world!' is referenced using a variable name or used directly, the effect will be the same. I.E. a = 'ba' a + 'nana' has the same end result as 'ba' + 'nana'with the exception being that the variable 'a' is not defined or is not bound to a new value after this statement.) HTH, -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with bash (subversion)
Tiger12506 wrote: >> results.write(c.getoutput('svnadmin lslocks ' + eval(row))) >> > > Mmm... I want to add that the eval function tries to execute whatever is in > the argument passed as python expressions. > Did I not say that already? ;) I guess my term 'command' was the problem, eh? -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with bash (subversion)
Justin Cardinal wrote: > That fixed it, thanks! Now I just need to do some studying on working with > lists (or whatever this output is...) so I can filter out the results I > don't want. Here's an updated version of the program: > > #!/usr/bin/env python > > import commands as c > > lsout = c.getoutput('ls -d /home/svn/repository/*/').split('\n') > results = [] > for row in lsout: > temp = c.getoutput('svnadmin lslocks ' + row).split('\n') > if temp != ['']: > results.append(temp) > temp is a list, so results is going to end up being a list of lists. is that the desired behavior? > print results > > ...and the output: > > ['Path: /test/trunk/data.bin', 'UUID Token: > opaquelocktoken:9ee85aae-c9dc-4388-8958-87b708e628a3', 'Owner: jcardinal', > 'Created: 2007-07-17 14:36:18 -0500 (Tue, 17 Jul 2007)', 'Expires: ', > 'Comment (1 line):', '', ''] > > > Thanks very much to all who replied, it's amazing how quick help arrives! > Well, I for one would rather answer your questions than study for a Differential Equations test :) > -Justin -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Image library
You know the height and the width of the image, no? So you know that every 'width' number of pixels will start a new row. So if you wanted say the fifth row down, second pixel, how would you find it? The 1st line: 'width' number of pixels The 2nd line: 'width' number of pixels The 3rd line: 'width number of pixels The 4th line: 'width' number of pixels The 5th line: 2 pixels in from the left Add those up ~ width+width+width+width+2 Or 4*width+2 That number is the index to use to get the pixel at coords (2,5) so pixel = getdata() pixel[4*width+2] For this example. Work out a more general solution for yourself please. JS > getdata() returns a flattened list, [n] > > > but i am not sure how to access it. > > when I want to get rgb from a window of 100,200, > > get data starts from 0(0~99, 0~199) > > the point of x,y = 2, 1 > > do I put in > > pixel[100] ? > > > it's actually not the case @_@ > > what should I put in ? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Image library
ahh~ it goes horizontally first why didn't I think of that? thank you ~ On 7/19/07, Tiger12506 <[EMAIL PROTECTED]> wrote: You know the height and the width of the image, no? So you know that every 'width' number of pixels will start a new row. So if you wanted say the fifth row down, second pixel, how would you find it? The 1st line: 'width' number of pixels The 2nd line: 'width' number of pixels The 3rd line: 'width number of pixels The 4th line: 'width' number of pixels The 5th line: 2 pixels in from the left Add those up ~ width+width+width+width+2 Or 4*width+2 That number is the index to use to get the pixel at coords (2,5) so pixel = getdata() pixel[4*width+2] For this example. Work out a more general solution for yourself please. JS > getdata() returns a flattened list, [n] > > > but i am not sure how to access it. > > when I want to get rgb from a window of 100,200, > > get data starts from 0(0~99, 0~199) > > the point of x,y = 2, 1 > > do I put in > > pixel[100] ? > > > it's actually not the case @_@ > > what should I put in ? ___ 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] Python Image library
Tiger12506 wrote: > You know the height and the width of the image, no? > > So you know that every 'width' number of pixels will start a new row. > So if you wanted say the fifth row down, second pixel, how would you find > it? > > The 1st line: 'width' number of pixels > The 2nd line: 'width' number of pixels > The 3rd line: 'width number of pixels > The 4th line: 'width' number of pixels > The 5th line: 2 pixels in from the left > > Add those up ~ width+width+width+width+2 > Or 4*width+2 > if you start counting at the 0th row and 0th column, this will give you the 4th row and 2nd column. if you're counting from the 1st row and 1st column, this will give you the 5th row and 3rd column. > That number is the index to use to get the pixel at coords (2,5) > So this is actually (3,5) or, to count from 0, (2,4). But yeah, the general idea is there. If my math is wrong I'm sure you won't hesitate to correct me ;) -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Image library
that's illustrative. On 7/19/07, Luke Paireepinart <[EMAIL PROTECTED]> wrote: Tiger12506 wrote: > You know the height and the width of the image, no? > > So you know that every 'width' number of pixels will start a new row. > So if you wanted say the fifth row down, second pixel, how would you find > it? > > The 1st line: 'width' number of pixels > The 2nd line: 'width' number of pixels > The 3rd line: 'width number of pixels > The 4th line: 'width' number of pixels > The 5th line: 2 pixels in from the left > > Add those up ~ width+width+width+width+2 > Or 4*width+2 > if you start counting at the 0th row and 0th column, this will give you the 4th row and 2nd column. if you're counting from the 1st row and 1st column, this will give you the 5th row and 3rd column. > That number is the index to use to get the pixel at coords (2,5) > So this is actually (3,5) or, to count from 0, (2,4). But yeah, the general idea is there. If my math is wrong I'm sure you won't hesitate to correct me ;) -Luke ___ 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] Python Image library
> if you start counting at the 0th row and 0th column, this will give you > the 4th row and 2nd column. > if you're counting from the 1st row and 1st column, this will give you > the 5th row and 3rd column. >> That number is the index to use to get the pixel at coords (2,5) >> > So this is actually (3,5) or, to count from 0, (2,4). > But yeah, the general idea is there. > If my math is wrong I'm sure you won't hesitate to correct me ;) > -Luke No. You're right, of course. :-) I wasn't thinking. JS ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] odd
I ran this for x in range(5,10): print x and OP was 5 6 7 8 9 why is that? shouldn't it print t 6 7 8 9 10? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd
> for x in range(5,10): > print x > > and OP was > > 5 > 6 > 7 > 8 > 9 > > why is that? shouldn't it print > > t > 6 > 7 > 8 > 9 > 10? no. the (well, one) syntax for range() is (start, stop) where it counts starting from 'start' up to but not including 'stop'. if you're familiar with C/C++ (or PHP or Java), it's similar to the counting loop, "for (int i=5; i < 10; i++)", which counts 5, 6, 7, 8, 9. hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd
* elis aeris <[EMAIL PROTECTED]> [2007-07-19 08:51]: > I ran this > > > for x in range(5,10): >print x > > and OP was > > 5 > 6 > 7 > 8 > 9 > > why is that? shouldn't it print > > 5 > 6 > 7 > 8 > 9 > 10? That is the expected behaviour, per the documentation: http://docs.python.org/lib/built-in-funcs.html#l2h-58 -- David Rock [EMAIL PROTECTED] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd
from Guido's tutorial: The given end point is never part of the generated list; range(10) generates a list of 10 values, the legal indices for items of a sequence of length 10. It is possible to let the range start at another number, or to specify a different increment (even negative; sometimes this is called the `step') On 7/19/07, elis aeris <[EMAIL PROTECTED]> wrote: I ran this for x in range(5,10): print x and OP was 5 6 7 8 9 why is that? shouldn't it print t 6 7 8 9 10? ___ 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] if and things
try this: for a in range(10): r, g, b = pixel[1030*(y-a) + x] if g > r and g > b: box += 1 This is an example of "unpacking" a tuple into separate variables, r, g and b. On 7/19/07, elis aeris <[EMAIL PROTECTED]> wrote: # pixel[] is a list of tuples: (r,g,b) # pixel[1030*(y-a) + x][0] = r # pixel[1030*(y-a) + x][1] = g # pixel[1030*(y-a) + x][2] = b for a in range(0, 10): ifpixel[1030*(y-a) + x][1] > pixel[1030*(y-a) + x][0] and pixel[1030*(y-a) + x][1] > pixel[1030*(y-a) + x][2]: box = box + 1 print box i have never used double conditions before, is this correct? I want box++ when the g is both bigger than r and b. import time import ImageGrab # Part of PIL from ctypes import * # Load up the Win32 APIs we need to use. class RECT(Structure): _fields_ = [ ('left', c_ulong), ('top', c_ulong), ('right', c_ulong), ('bottom', c_ulong) ] # time.sleep(2) GetForegroundWindow = windll.user32.GetForegroundWindow GetWindowRect = windll.user32.GetWindowRect # Sleep for 2 seconds - click the window you want to grab. #time.sleep(2) # Grab the foreground window's screen rectangle. rect = RECT() foreground_window = GetForegroundWindow() GetWindowRect(foreground_window, byref(rect)) image = ImageGrab.grab((rect.left, rect.top, rect.right, rect.bottom)) # Save the screenshot as a BMP. time.sleep(2) image.save("c:\python_codes\screenshot.bmp") # Get the pixel 10 pixels along the top of the foreground window - this # will be a piece of the window border. # print time.time() start = time.time() pixels = image.getdata() for x in xrange(0, 500): for y in xrange(0, 500): rgb = pixels[500 * x + y] print pixels[1][0] print ( time.time() - start ) # PIL returns colours as RGB values packed into a triple: #print "RGB(%d, %d, %d)" % (rgb[0], rgb[1], rgb[2]) # This prints RGB(0, 74, 216) on my XP machine ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] if and things
full code below. # pixel[] is a list of tuples: (r,g,b) # pixel[1030*(y-a) + x][0] = r # pixel[1030*(y-a) + x][1] = g # pixel[1030*(y-a) + x][2] = b for a in range(0, 10): ifpixel[1030*(y-a) + x][1] > pixel[1030*(y-a) + x][0] and pixel[1030*(y-a) + x][1] > pixel[1030*(y-a) + x][2]: box = box + 1 print box i have never used double conditions before, is this correct? I want box++ when the g is both bigger than r and b. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] if and things
man that looks totally pythonic. On 7/19/07, Ian Witham <[EMAIL PROTECTED]> wrote: try this: for a in range(10): r, g, b = pixel[1030*(y-a) + x] if g > r and g > b: box += 1 This is an example of "unpacking" a tuple into separate variables, r, g and b. On 7/19/07, elis aeris <[EMAIL PROTECTED]> wrote: > > # pixel[] is a list of tuples: (r,g,b) > # pixel[1030*(y-a) + x][0] = r > # pixel[1030*(y-a) + x][1] = g > # pixel[1030*(y-a) + x][2] = b > > for a in range(0, 10): > ifpixel[1030*(y-a) + x][1] > pixel[1030*(y-a) + x][0] and > pixel[1030*(y-a) + x][1] > pixel[1030*(y-a) + x][2]: > box = box + 1 > > print box > > > i have never used double conditions before, is this correct? > > I want box++ when the g is both bigger than r and b. > > > > > > > > > > > > > > > > > > import time > > import ImageGrab # Part of PIL > from ctypes import * > > # Load up the Win32 APIs we need to use. > class RECT(Structure): > _fields_ = [ > ('left', c_ulong), > ('top', c_ulong), > ('right', c_ulong), > ('bottom', c_ulong) > ] > > # time.sleep(2) > > GetForegroundWindow = windll.user32.GetForegroundWindow > GetWindowRect = windll.user32.GetWindowRect > > # Sleep for 2 seconds - click the window you want to grab. > #time.sleep(2) > > > > # Grab the foreground window's screen rectangle. > rect = RECT() > foreground_window = GetForegroundWindow() > GetWindowRect(foreground_window, byref(rect)) > image = ImageGrab.grab((rect.left, rect.top, rect.right, rect.bottom)) > > # Save the screenshot as a BMP. > time.sleep(2) > > > image.save("c:\python_codes\screenshot.bmp") > > # Get the pixel 10 pixels along the top of the foreground window - this > # will be a piece of the window border. > > # print time.time() > > start = time.time() > > pixels = image.getdata() > for x in xrange(0, 500): > for y in xrange(0, 500): > rgb = pixels[500 * x + y] > > print pixels[1][0] > > print ( time.time() - start ) > > # PIL returns colours as RGB values packed into a triple: > #print "RGB(%d, %d, %d)" % (rgb[0], rgb[1], rgb[2]) # This prints > RGB(0, 74, 216) on my XP machine > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] if and things
> man that looks totally pythonic. What you had is correct though. Good job. JS ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] if and things
It's nice to see you haven't given up. A few suggestions to make you code a little more creative. > import time > > import ImageGrab # Part of PIL > from ctypes import * > > # Load up the Win32 APIs we need to use. > class RECT(Structure): > _fields_ = [ >('left', c_ulong), >('top', c_ulong), >('right', c_ulong), >('bottom', c_ulong) >] > > # time.sleep(2) > > GetForegroundWindow = windll.user32.GetForegroundWindow > GetWindowRect = windll.user32.GetWindowRect > > # Sleep for 2 seconds - click the window you want to grab. > #time.sleep(2) > > > > # Grab the foreground window's screen rectangle. > rect = RECT() > foreground_window = GetForegroundWindow() > GetWindowRect(foreground_window, byref(rect)) > image = ImageGrab.grab((rect.left, rect.top, rect.right, rect.bottom)) > > # Save the screenshot as a BMP. > time.sleep(2) > > > image.save("c:\python_codes\screenshot.bmp") > > # Get the pixel 10 pixels along the top of the foreground window - this > # will be a piece of the window border. > > # print time.time() > > start = time.time() > > pixels = image.getdata() > for x in xrange(0, 500): > for y in xrange(0, 500): >rgb = pixels[500 * x + y] You will be planning to do something else with this right? As it is, you are looping 250,000 times and resetting the variable rgb each time, losing the previous value. I imagine that you have other plans. > print pixels[1][0] > > print ( time.time() - start ) Oh. I bet this is all supposed to be indented. Nevermind about the above loop. > # PIL returns colours as RGB values packed into a triple: > #print "RGB(%d, %d, %d)" % (rgb[0], rgb[1], rgb[2]) # This prints RGB(0, > 74, 216) on my XP machine Here is what I really meant to look at - yes this commented line print "RGB(%d, %d, %d)" % (rgb[0], rgb[1], rgb[2]) Since the percent formatter is followed by a tuple (which is what you are making by putting rgb[0], rgb[1], rgb[2] into parentheses) and rgb is already a tuple, you can write this much more simply as: print "RGB(%d, %d, %d)" % rgb JS ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] if and things
given up? man i have a project to go live :) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] if and things
# pixel[] is a list of tuples: (r,g,b) # pixel[1030*(y-a) + x][0] = r # pixel[1030*(y-a) + x][1] = g # pixel[1030*(y-a) + x][2] = b for a in range(0, 10): ifpixel[1030*(y-a) + x][1] > pixel[1030*(y-a) + x][0] and pixel[1030*(y-a) + x][1] > pixel[1030*(y-a) + x][2]: box = box + 1 print box i have never used double conditions before, is this correct? I want box++ when the g is both bigger than r and b. import time import ImageGrab # Part of PIL from ctypes import * # Load up the Win32 APIs we need to use. class RECT(Structure): _fields_ = [ ('left', c_ulong), ('top', c_ulong), ('right', c_ulong), ('bottom', c_ulong) ] # time.sleep(2) GetForegroundWindow = windll.user32.GetForegroundWindow GetWindowRect = windll.user32.GetWindowRect # Sleep for 2 seconds - click the window you want to grab. #time.sleep(2) # Grab the foreground window's screen rectangle. rect = RECT() foreground_window = GetForegroundWindow() GetWindowRect(foreground_window, byref(rect)) image = ImageGrab.grab((rect.left, rect.top, rect.right, rect.bottom)) # Save the screenshot as a BMP. time.sleep(2) image.save("c:\python_codes\screenshot.bmp") # Get the pixel 10 pixels along the top of the foreground window - this # will be a piece of the window border. # print time.time() start = time.time() pixels = image.getdata() for x in xrange(0, 500): for y in xrange(0, 500): rgb = pixels[500 * x + y] print pixels[1][0] print ( time.time() - start ) # PIL returns colours as RGB values packed into a triple: #print "RGB(%d, %d, %d)" % (rgb[0], rgb[1], rgb[2]) # This prints RGB(0, 74, 216) on my XP machine ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading random line from a file
> ---Original Message--- > From: Tiger12506 <[EMAIL PROTECTED]> > Yuck. Talk about a one shot function! Of course it only reads through the > file once! You only call the function once. Put a second print randline(f) > at the bottom of your script and see what happens :-) > > JS > *sigh* #!/bin/env python import os import random text = 'shaks12.txt' if not os.path.exists(text): os.system('wget http://www.gutenberg.org/dirs/etext94/shaks12.txt') def randline(f): for i,j in enumerate(file(f, 'rb')): if random.randint(0,i) == i: line = j return line print randline(text) print randline(text) print randline(text) -- Yorick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Creating Packages
Hello- I have written a class to help folks like me manipulate data segments (the kind one deals with when reading/writing data files). The classes and tests are written -- at least enough to get things going -- what I need help with is creating a package out of this and then creating routines to install them. Can one of you help me out either with some tutoring or pointing me to some literature? I would like to use the cheese shop. Thanks for your help, --greg ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Creating Packages
Greg Lindstrom wrote: > Hello- > > I have written a class to help folks like me manipulate data segments > (the kind one deals with when reading/writing data files). The classes > and tests are written -- at least enough to get things going -- what I > need help with is creating a package out of this and then creating > routines to install them. Can one of you help me out either with some > tutoring or pointing me to some literature? I would like to use the > cheese shop. Here is a starting point for the CheeseShop: http://wiki.python.org/moin/CheeseShopTutorial For info on packaging see http://docs.python.org/dist/dist.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor