Re: [Tutor] Python & MySQL... Define port.
Hello, You can specify the port of the instance you want to connect to simply by passing "port=NUM" in MySQLdb.connect(). Here is a formulation I have found generally useful: import MySQLdb def run_mysql(query, db, user, passwd, port=3306, socket="/var/run/mysqld/mysqld.sock", host="localhost"): """Runs query passed with connection info passed, returns results.""" try: db = MySQLdb.connect(user=user, passwd=passwd, db=db, port=port, unix_socket=socket, host=host) except MySQLdb.Error, e: raise e cursor = db.cursor() cursor.execute(query) data = cursor.fetchall() db.close() return data This sets common defaults, but you can of course pass in whatever you want when you call it. You might also want to check out the MySQLdb docs: http://mysql-python.sourceforge.net/MySQLdb.html. It contains a number of examples. ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins From: Network Administrator To: tutor@python.org Sent: Monday, February 9, 2009 1:29:21 PM Subject: [Tutor] Python & MySQL... Define port. Hello everybody! I want to use Python to query a given MySQL database. However, this database is running on port 3308, instead the traditional 3306. I read that (example): MySQLdb.connect(host="dbserv', db='inv', user='smith') can be used to query a given database; however, it didn't work in my particular case. This is my code: connection = MySQLdb.connect (db = "netlogin", user="netadmin", pass = "r4nas", port="3308") I appreciate if you give me assistance in the proper sintax of the last mentioned instruction. Regards, GatoLinux.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Simple CGI script and Apache configuration
Hello all, I'll try to give as much detail as I can, but this is a somewhat vague problem. I have a very simple script that I would like to implement as a CGI script, just so I can hit a URL and get some output. However, after following a number of tutorials, I am still seeing some very odd results. I am almost sure it's in my Apache configuration, but I figured a few people on this list would likely know what the minimal related Apache config should be. (The Apache docs are pretty daunting...) Local version wise, I am on Ubuntu 8.10, with Apache 2.2.9 and Python 2.5.2. I have libapache2-mod-python installed. Apache config is out of the box, along with: ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" In /var/www/cgi-bin, I have hello.py: #!/usr/bin/python import cgitb cgitb.enable() print "Content-type: text/html" print print "" print "Hello!" print "" Reload, hit http://localhost/cgi-bin/hello.py in a browser, I get centered text just fine. Now I want to do this same process on my remote webserver. On there, I am on Ubuntu 7.10, with Apache 2.2.4 and Python 2.5.1. I add: ScriptAlias /python/ "/var/www/samuelhuckins.com/python" Reload, hit http://samuelhuckins.com/python/hello.py, and I get a 404? The perms and ownership on the file is the same as in other directories. Do I need to add some sort of handler, with mod_python.publisher? I think I am just missing one of the basics of this whole process. Thanks for any assistance, Sam ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple CGI script and Apache configuration
Well that did seem to help, at least I get an error now:-) I changed my config to: ScriptAlias /python/ /var/www/samuelhuckins.com/python/ SetHandler mod_python PythonHandler mod_python.publisher PythonDebug On So I have the / as mentioned on ScriptAlias, and I added what seems to be the lines suggested in all the tutorials I came across. Now when you hit http://trac.samuelhuckins.com/python/hello.py, you get: Environment not foundWhen I hit the script on the command line, I get back what I think should display fine: ./hello.py Content-type: text/html Hello! I don't see anything in the Apache error logs. Any ideas? -Sam ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins From: Moos Heintzen To: wormwood_3 Sent: Sunday, February 22, 2009 5:36:32 PM Subject: Re: [Tutor] Simple CGI script and Apache configuration Just noticed that ScriptAlias /python/ "/var/www/samuelhuckins.com/python" doesn't have a slash at the end. Could that be it? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple CGI script and Apache configuration
Thanks for all the suggestions! I tried to go through them, and will relate what results I encountered. I changed my Apache config to: AllowOverride None Options ExecCGI Order allow,deny Allow from all I in fact did not have the cgi module enabled, so I did that. Then I ran "sudo /etc/init.d/apache2 reload", and hit http://samuelhuckins.com/cgi-bin/hello.py, which contains simply: #!/usr/bin/python print "Content-type: text/html" print print "" print "Hello!" print "" I get prompted to download the file, but it does not execute or appear in plain text. The logs just show the request being made. What is the missing element to get this script to execute? Thanks, Sam From: Martin Walsh To: Python Tutorlist Sent: Tuesday, February 24, 2009 12:33:35 AM Subject: Re: [Tutor] Simple CGI script and Apache configuration wormwood_3 wrote: > Hello all, Hi Sam, > I'll try to give as much detail as I can, but this is a somewhat vague > problem. I have a very simple script that I would like to implement as a > CGI script, just so I can hit a URL and get some output. However, after > following a number of tutorials, I am still seeing some very odd > results. I am almost sure it's in my Apache configuration, but I figured > a few people on this list would likely know what the minimal related > Apache config should be. (The Apache docs are pretty daunting...) > > Local version wise, I am on Ubuntu 8.10, with Apache 2.2.9 and Python > 2.5.2. I have libapache2-mod-python installed. Apache config is out of > the box, along with: > > ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" You need more than this to make apache cgi work, I think. Firstly, mod_cgi should be loaded -- look for a symlink named cgi.load or some such, in /etc/apache2/mods-enabled. Run 'a2enmod cgi' if you don't have one. Secondly, ExecCGI is usually enabled using the Options directive within a Directory definition -- but, less commonly, you might see something like 'AddHandler cgi-program .py' in an apache or site config. Of course, the script needs to be executable by the apache user (which would be 'www-data' on ubuntu, IIRC), and contain an appropriate shebang (#!) on the first line -- but it sounds like you have that covered. Both 'Options ExecCGI' and 'Addhandler cgi-program .py' are allowed in .htaccess also, given an appropriate AllowOverride directive for the path in question. Something to look for on the working system, if all else fails. You do *not* need mod python to run python cgi scripts. > In /var/www/cgi-bin, I have hello.py <http://hello.py>: > > #!/usr/bin/python > import cgitb > cgitb.enable() > > print "Content-type: text/html" > print > print "" > print "Hello!" > print "" > > Reload, hit http://localhost/cgi-bin/hello.py in a browser, I get > centered text just fine. Now I want to do this same process on my remote > webserver. On there, I am on Ubuntu 7.10, with Apache 2.2.4 and Python > 2.5.1. I add: > > ScriptAlias /python/ "/var/www/samuelhuckins.com/python" You can try appending something like this (untested): AllowOverride None Options ExecCGI # or, Options +ExecCGI to merge # with options from parent dir(s) Order allow,deny Allow from all > > Reload, hit http://samuelhuckins.com/python/hello.py, and I get a 404? > The perms and ownership on the file is the same as in other directories. > Do I need to add some sort of handler, with mod_python.publisher? I > think I am just missing one of the basics of this whole process. Hmmm, interesting. It's unlikely that any of my advice will help you with a 404. With an incomplete apache cgi config, the response I'd expect would be either a 403 (Forbidden), or the script itself in plain text. Do the logs provide any additional information? Re-check your spelling. A 404 with vanilla apache config might just indicate a typo. When you say 'Reload', I assume you mean the apache daemon (ie. /etc/init.d/apache2 reload or apache2ctl reload)? Again, you do *not* need mod python to run python cgi scripts. HTH, Marty ___ 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] Simple CGI script and Apache configuration
I wasn't sure if that was needed, so I took it out, sorry about that. I put ScriptAlias /cgi-bin/ "/var/www/samuelhuckins.com/cgi-bin/" in place, reloaded, and it works! I think the problem throughout was that I was mixing up what was necessary between CGI and mod_python. If you'd like a random programming epigram served up by this new config, check out: http://samuelhuckins.com/cgi-bin/qotd.py -Sam ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins From: Martin Walsh To: wormwood_3 Cc: Python Tutorlist Sent: Tuesday, February 24, 2009 2:32:48 AM Subject: Re: [Tutor] Simple CGI script and Apache configuration wormwood_3 wrote: > Thanks for all the suggestions! I tried to go through them, and will > relate what results I encountered. I changed my Apache config to: > > > AllowOverride None > Options ExecCGI > Order allow,deny > Allow from all > > > I in fact did not have the cgi module enabled, so I did that. Then I ran > "sudo /etc/init.d/apache2 reload", and hit > http://samuelhuckins.com/cgi-bin/hello.py, which contains simply: > > #!/usr/bin/python > print "Content-type: text/html" > print > print "" > print "Hello!" > print "" > > I get prompted to download the file, but it does not execute or appear > in plain text. The logs just show the request being made. What is the > missing element to get this script to execute? > When you look at the downloaded file, is it your python script? Looks like you changed the path where you're keeping your cgi script, did you update the ScriptAlias directive to suit? You may find this more helpful ... http://httpd.apache.org/docs/2.0/howto/cgi.html HTH, Marty___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Checking for custom error codes
Hi all, I am new to Python programming and this list, looks like a great place so far! Recently I was trying to do a "try: X except Y: Z" statement, checking for a custom error code that the rwhois.py module throws. Some details on the exercise and the full code can be found on this post ( http://assistedsilicon.blogspot.com/2007/08/fun-with-python-domainspotter.html ). So, here is what I tried: for potdomain in self.potdomains: try: who.whois(potdomain) self.availdomains.append(potdomain) except 'NoSuchDomain': pass self.totalchecked+=1 If you need more context, the whole program is here ( http://assistedsilicon.blogspot.com/2007/08/fun-with-python-domainspotter.html#code ). I first tried simply "except NoSuchDomain", since that was what I saw rwhois throw when I sent it a domain that was not registered. That did not work, so I quoted it. I got a warning that throwing a string exception is deprecated, but the check did not work anyway. Am I trying to check for this custom error wrongly? How should it be done? Thanks for any assistance! -Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Checking for custom error codes
Hi all, I am new to Python programming and this list, looks like a great place so far! Recently I was trying to do a "try: X except Y: Z" statement, checking for a custom error code that the rwhois.py module throws. Some details on the exercise and the full code can be found on this post ( http://assistedsilicon.blogspot.com/2007/08/fun-with-python-domainspotter.html ). So, here is what I tried: for potdomain in self.potdomains: try: who.whois(potdomain) self.availdomains.append(potdomain) except 'NoSuchDomain': pass self.totalchecked+=1 If you need more context, the whole program is here ( http://assistedsilicon.blogspot.com/2007/08/fun-with-python-domainspotter.html#code ). I first tried simply "except NoSuchDomain", since that was what I saw rwhois throw when I sent it a domain that was not registered. That did not work, so I quoted it. I got a warning that throwing a string exception is deprecated, but the check did not work anyway. Am I trying to check for this custom error wrongly? How should it be done? Thanks for any assistance! -Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Checking for custom error codes
>>Probably you need to import NoSuchDomain from rwhois: >>from rwhois import WhoisRecord, NoSuchDomain >> >>then use >> except NoSuchDomain: That sounds right, I will give it a shot soon. >>In general, when you ask a question here, "I tried X and it did not >>work" is not very informative and makes it difficult to give a good >>answer. It is very helpful to show the actual code you tried and the >>actual error message, including the full traceback. The error message >>and traceback include a lot of very helpful information; including them >>will greatly improve your chance of a correct answer. Sorry about that, I knew the code would be helpful, that's why I linked to my blog post that included the full code. I will post the traceback tonight when I can run it. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Checking for custom error codes
>>Examining rwhois.py reveals >>raise 'NoSuchDomain' >>>which is a string exception. Which should work even tho deprecated. >>>When you say it did not work what is the evidence? -- Bob Gailer 510-978-4454 Oakland, CA 919-636-4239 Chapel Hill, NC ___ I did speak too soon on this. When I run the script using the code at the end, I get: rwhois.py:327: DeprecationWarning: raising a string exception is deprecated raise 'NoSuchDomain', self.domain === def lookup(self): """ Looks up each word in our list with whois """ """ Using rwhois, which is either broken or lacks most data: """ from rwhois import WhoisRecord who = WhoisRecord() self.totalchecked = 0 self.availdomains = [] for potdomain in self.potdomains: try: who.whois(potdomain) self.availdomains.append(potdomain) except 'NoSuchDomain': pass self.totalchecked+=1 def printresults(self): """ Prints the ones that do not seem to be taken, with some stats. """ print "Results are in! " print "%s total words checked, as .com and .net domains." % (self.totalchecked / 2) print "--" print "Domains that seem to be available: \n" for availdomain in self.availdomains: print availdomain print "\n" print "--" Then, I get back items in the list such as: Domains that seem to be available: AA.com AARP.com AARP.net I confirmed in spotchecking that some of these are taken, such as with whois -H AARP.com: Domain Name: AARP.COM Registrar: NETWORK SOLUTIONS, LLC. Whois Server: whois.networksolutions.com This may, however, be something else wrong with my code, or the rwhois module, not the try, except check. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Checking for custom error codes
>>Probably you need to import NoSuchDomain from rwhois: >>from rwhois import WhoisRecord, NoSuchDomain >> >>then use >> except NoSuchDomain: I tried adding: from rwhois import WhoisRecord, NoSuchDomain who = WhoisRecord() self.totalchecked = 0 self.availdomains = [] for potdomain in self.potdomains: try: who.whois(potdomain) self.availdomains.append(potdomain) except NoSuchDomain: pass self.totalchecked+=1 But I get back: Traceback (most recent call last): File "domainspotter.py", line 150, in runMainParser() File "domainspotter.py", line 147, in runMainParser td.run() File "domainspotter.py", line 71, in run checkdomains.lookup() File "domainspotter.py", line 108, in lookup from rwhois import WhoisRecord, NoSuchDomain ImportError: cannot import name NoSuchDomain Maybe I need to import something else to be able to throw it. I think if someone can explain a more general form of this I would be on better footing: To use a custom error code (from a module) in a loop or anywhere else, do I need to import the code itself? I had assumed that once I imported the module that defined the error code, I could catch it just like a core Python error code. >>In general, when you ask a question here, "I tried X and it did not >>work" is not very informative and makes it difficult to give a good >>answer. It is very helpful to show the actual code you tried and the >>actual error message, including the full traceback. The error message >>and traceback include a lot of very helpful information; including them >>will greatly improve your chance of a correct answer. Commented on this with the warning text in last response. Will endeavour to do better:-) ___ 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] Checking for custom error codes
> Traceback (most recent call last): > File "domainspotter.py", line 150, in >runMainParser() > File "domainspotter.py", line 147, in runMainParser >td.run() > File "domainspotter.py", line 71, in run >checkdomains.lookup() > File "domainspotter.py", line 108, in lookup >from rwhois import WhoisRecord, NoSuchDomain > ImportError: cannot import name NoSuchDomain > > Maybe I need to import something else to be able to throw it. > > I think if someone can explain a more general form of this I would be on > better footing: To use a custom error code (from a module) in a loop or > anywhere else, do I need to import the code itself? I had assumed that > once I imported the module that defined the error code, I could catch it > just like a core Python error code. >>The problem is evident. The name NoSuchDomain is not defined. It is not a >>variable. It is just a string. When you call an error like this raise >>'NoSuchDomain', it's called something - don't know what (string >>exception?) - but as you found out, that method of raising errors is now >>frowned upon (deprecated). The way you are supposed to raise errors is to >>create a new class, subclassing Exception. Then you would be able to catch >>it by variable name as you are trying to do with the import. This is exactly what I needed, awesome! Looks like this is what you were saying to do?: http://docs.python.org/tut/node10.html#SECTION001050 >>But you certainly can't import a constant string! It's like trying to import >>the >>contents of a string variable, instead of the variable itself. Again, the >>problem is deprecation, the rwhois will eventually have to be re-written so >>that it uses exception classes, instead of just raising string errors. That would seem best. I will see if they have this in the works. Thanks again! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to sort a dictionary by values
>>hello there all, >>i am wondering how to sort a dictionary that i have by values. >>And i also need to sort them from greatest to least >>like if i have a dictionary >> >>d = {'a':21.3, 'b':32.8, 'c': 12.92} >> >>how could i sort these from least to greatest >>so that the order would turn out >>b,a,c Hi Shawn, I was not sure how to do this either, but some digging revealed a very handing cookbook recipe on just this! (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/304440) So, here is an example of it working in the Python interpreter: == Python 2.5.1 (r251:54863, May 2 2007, 16:56:35) [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> dict = {'a':21.3, 'b':32.8, 'c':12.92} >>> from operator import itemgetter >>> # This will print the dict, sorted by value: ... >>> print sorted(dict.items(), key=itemgetter(1)) [('c', 12.92), ('a', 21.301), ('b', 32.797)] == So just add that import, and use sorted() as I showed above, and you should be good to go. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to sort a dictionary by values
>>You can use d.__getitem__ as the key function for a sort of the keys. >>__getitem__() is the special method that is called for indexing a >>dictionary (or a list). Just curious: Is there a reason to use __getitem__() over itemgetter (used in the example in my reply)? >>In [24]: d = {'a':21.3, 'b':32.8, 'c': 12.92} >>In [26]: sorted(d.keys(), key=d.__getitem__, reverse=True) I think Shawn would want to leave off "reverse=True". The default is least to greatest, which is what he wanted, I think. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Bookpool sale on Addison Wesley
Thanks for the heads up! I have been looking to get Core Python Programming for a while now. ___ - Original Message From: Kent Johnson <[EMAIL PROTECTED]> To: Tutor@python.org Sent: Wednesday, August 8, 2007 6:31:46 PM Subject: [Tutor] Bookpool sale on Addison Wesley Bookpool is having a sale on all books from Addison-Wesley and Prentice Hall. In my opinion these are two of the best publishers for top-notch computer titles. A few Python books on sale: Core Python Programming $27.25 http://www.bookpool.com/sm/0132269937 Rapid Web Applications with TurboGears $24.50 http://www.bookpool.com/sm/0132433885 Some recommended non-Python books: Design Patterns: Elements of Reusable Object-Oriented Software $32.95 http://www.bookpool.com/sm/0201633612 Refactoring: Improving the Design of Existing Code http://www.bookpool.com/sm/0201485672 Agile Software Development Principles, Patterns, and Practices http://www.bookpool.com/sm/0135974445 Extreme Programming Explained: Embrace Change, 2nd Edition http://www.bookpool.com/sm/0321278658 and all the other books in this series These are just a few personal favorites, there are many more excellent books on sale. 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] Python Book Recommendations [Was:[Re: Security]]
Yeah, I loved this! I did not even know you could put variables and keywords in bookmarks like this. Awesomely cool. For anyone interested in more details, Lifehacker has a great article on this, associated plugins, tips and tricks: http://lifehacker.com/software/bookmarks/hack-attack-firefox-and-the-art-of-keyword-bookmarking-196779.php _ - Original Message From: Dick Moores <[EMAIL PROTECTED]> To: Python Tutor List Sent: Thursday, August 16, 2007 1:54:15 AM Subject: Re: [Tutor] Python Book Recommendations [Was:[Re: Security]] At 05:48 PM 8/14/2007, Kent Johnson wrote: >I also have a shortcut set up so if I type >py modulename >in the Firefox address bar it takes me directly to the docs for that >module. To do this, create a bookmark with this URL: >file://localhost/Users/kent/Library/Documentation/Python-Docs-2.5/lib/module-%s.html > >and give it the keyword 'py'. Great tips, Kent! I especially appreciate this one. Dick ___ 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] Loop optimization
Hello tutors, I am trying to understand the best cases in which to use for loops, list comprehensions, generators, and iterators. I have a rather simple process that I made initially as a for loop: self.potdomains = [] for word in self.dictcontents: self.potdomains.append(word + suffix1) self.potdomains.append(word + suffix2) So I setup an empty list, and then for each item in a list already made, I add something to the end of that item, and then append the new form to another list. What I am wondering is, would it be better in any way to do it using a for loop like this, or instead to use a list comprehension (which would just add readability, not change the logic, I believe), or a generator, or an iterator. My current thought it that a generator would be more useful if I needed to have more control over the process while running, perhaps to add error checking or some other potential interruption. But in purely in terms of running speed, which option would be best? Thanks all! -Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Loop optimization
>I think what you have is pretty clear. I can't think of a way to do this >with a single list comprehension because you add two items to the list >each time through the loop. You could use a list comp and a generator >expression, but the order of entries in the result will be different: >self.potdomains = [ word + suffix1 for word in self.dictcontents ] >self.potdomains.extend(word + suffix2 for word in self.dictcontents) I see what you mean now. I think in this case adding a list comprehension into the mix makes it less readable! >> or a generator, >def suffixGen(words): > for word in words: > yield word + suffix1 > yield word + suffix2 > >self.potdomains = list(suffixGen(self.dictcontents)) To me, in terms of immediate readability, the generator version seems best, at least as you have written it. >The only way to know for sure is to try it with your data. Use the >timeit module to test. It did not know of this module. It will be quite helpful, thanks! (I had been setting a variable to time.time() before a process I needed to time, and then another after, and subtracting them...:-) ) >This takes all attribute lookups out of the loop. Put this in a function >(method) and make sure suffix1 and suffix2 are local variables (probably >function parameters). Then test :-) I will do so tonight and report back. Thanks, Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Loop optimization
I ran a few tests, with the following results: 1. Timing using the time module: * Using for loop, src code: import time start = time.time() for word in self.dictcontents: self.potdomains.append(word + suffix1) self.potdomains.append(word + suffix2) end = time.time() runtime = end - start print "Using time(), for loop took %s s" % runtime ** I obtained the following results (using the full agid-4 dictionary, ~112K entries): python domainspotter.py --file resources/agid-4/infl.txt Using time(), for loop took 0.132480859756 s python domainspotter.py --file resources/agid-4/infl.txt Using time(), for loop took 0.143032073975 s python domainspotter.py --file resources/agid-4/infl.txt Using time(), for loop took 0.135424137115 s * Using generator, src code: def suffixGen(self, words): suffix1 = ".com" suffix2 = ".net" for word in words: yield word + suffix1 yield word + suffix2 def domainify(self): self.potdomains = [] words = self.dictcontents import time start = time.time() self.potdomains = list(CheckDomains.suffixGen(self, words)) end = time.time() runtime = end - start print "Using time(), generator took %s s" % runtime ** I obtained the following results (using the full agid-4 dictionary, ~112K entries): python domainspotter.py --file resources/agid-4/infl.txt Using time(), generator took 0.0830721855164 s python domainspotter.py --file resources/agid-4/infl.txt Using time(), generator took 0.0818212032318 s python domainspotter.py --file resources/agid-4/infl.txt Using time(), generator took 0.0830278396606 s This revealed that the generator seemed to be much faster, around 60% faster. I then wanted to try both possibilities with the timeit module, but was unable to get it working. I will start a new thread on that next, however, in case anyone has any further thoughts on the for loop versus generator issue. -Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Floating Confusion
Dear Tutors, Reading through Wesley's delightful Core Python Programming, I came across something I have not been able to grasp yet. Some introductory code: >>> 1 1 >>> 1.1 1.1001 >>> print 1 1 >>> print 1.1 1.1 The second case is, of course, what is throwing me. By having a decimal point, "1.1" is a float type, and apparently it cannot be represented by binary floating point numbers accurately. I must admit that I do not understand why this is the case. Would anyone be able to enlighten me? -Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Spawning terminals from Python - A failed question
Hello all, I had this question all written up, then found I had the answer:-) So I decided to share anyway in case anyone found it useful -- I am trying to write a script that will open terminal windows for me, based on certain criteria. In this case, gnome-terminals, in Ubuntu, but my question is more general: How do I launch a command from a script and having it continue running? The pertinent part of my script right now: # Spawn the 4 terminals, with needed positions and sizes, then exit quietly: "gnome-terminal --geometry=%dx%d+%d+%d" % (t1width, t1height, t1posx, t1posy) "gnome-terminal --geometry=%dx%d+%d+%d" % (t2width, t2height, t2posx, t2posy) "gnome-terminal --geometry=%dx%d+%d+%d" % (t3width, t3height, t3posx, t3posy) "gnome-terminal --geometry=%dx%d+%d+%d" % (t4width, t4height, t4posx, t4posy) I need something to pass these lines to of course, to actually launch them. I thought first of the commands module (http://docs.python.org/lib/module-commands.html), but I don't:q want to get status or output, I want to basically spawn the process so it keeps running regardless of what the script does next (like running something with " &" after it in linux). --- All I ended up having to do was: >>> import commands >>> commands.getoutput("gnome-terminal") ... >>> And the terminal launched. When I closed the interpreter, the terminal stayed open fine. So getoutput() was the ticket. -Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Spawning terminals from Python - A failed question
>>Umm, so just put the ampersand at the end of the command string and >>call os.system() Not sure what the point of this variation would be if os is being deprecated along with commands... >>However both os.system and the commands module are deprecated in >>favour >>of the new(ish) subprocess module where the equivalent incantation >>would be: >>p = subprocess.Popen("gnome-terminal", shell=True)HTH,-- Alan Good to know! I will start using this instead. -Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Equivalent of && in Python?
I have a script that reads some local system information, performs some calculations, and then launches some terminal windows: # Spawn the 4 terminals, with needed positions and sizes, then exit commands.getoutput("%s --geometry=%dx%d+%d+%d --working-directory=%s" % \ (terminal, t1width, t1height, t1posx, t1posy, workingdir)) commands.getoutput("%s --geometry=%dx%d+%d+%d --working-directory=%s" % \ (terminal, t2width, t2height, t2posx, t2posy, workingdir)) commands.getoutput("%s --geometry=%dx%d+%d+%d --working-directory=%s" % \ (terminal, t3width, t3height, t3posx, t3posy, workingdir)) commands.getoutput("%s --geometry=%dx%d+%d+%d --working-directory=%s" % \ (terminal, t4width, t4height, t4posx, t4posy, workingdir)) The oddity: When I call this script, sometimes all four terminals launch, one right after another, which is the desired behaviour. At other times, one will launch, and ONLY after I close it will the second launch, and so on until the fourth. I do not understand how this is happening. I thought each line in a script which does anything has to be done before the next one is executed, but I may be way off on this. If this were in a bash script, I could add " &&" after each line, but what to do in a Python script? -Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Integer ID Caching
I came across the topic of internal Python IDs recently, and learned that the internal numeric ids of small integers are cached to increase speed. I had read that as of 2.5, this applied to integers between -1 and 100. However, doing some quick tests, this seems not to be accurate: Python 2.5.1 (r251:54863, May 2 2007, 16:56:35) [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a = 10 >>> b = 10 >>> print id(a), id(b) 135716556 135716556 >>> c = 250 >>> d = 250 >>> print id(c), id(d) 135719604 135719604 >>> e = 300 >>> f = 300 >>> print id(e), id(f) 135718812 135718824 So the upper end of the interning range appears to be between 250 and 300, not 100. Does anyone know the exact number, when it changed, and if a decision has been made for future changes? I was unable to find anything specific to this in the documentation. Thanks as always, Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Socket Timeout Handling
I am trying to figure out the optimal way to make socket connections (INET) and check for timeouts. The socket module has settimeout(timeout) and setdefaulttimeout(timeout). However, so far as I can tell, these apply to socket objects. The type of socket connection I want to make is getfqdn(address). So I can set the default timeout for socket, but not a socket object (makes sense so far). I cannot use the getfqdn(address) method on a socket object, I have to use it on socket. This means (as I understand it thus far), that while I can set a timeout value for socket objects, this will not apply to when I use the getfqdn() method, which is where I need a timeout check! Some example code for the steps so far: >>> import socket >>> conn = socket.socket() >>> conn.setdefaulttimeout(2.0) Traceback (most recent call last): File "", line 1, in AttributeError: '_socketobject' object has no attribute 'setdefaulttimeout' >>> socket.setdefaulttimeout(2.0) >>> conn.getfqdn("64.33.212.2") Traceback (most recent call last): File "", line 1, in AttributeError: '_socketobject' object has no attribute 'getfqdn' >>> socket.getfqdn("64.33.212.2") '64-33-212-2.customers.pingtone.net' >>> # Disconnected network connection here ... >>> socket.getfqdn("64.33.212.2") '64.33.212.2' >>> # Reconnected network connection here >>> socket.getfqdn("64.33.212.2") '64-33-212-2.customers.pingtone.net' After I disconnected my network connection and called getfqdn(), it returned the IP address I called it with after about 25 seconds. So the default timeout was ignored? Is there some other way to call this function so that I can check for timeouts? Should I instead just put my network calls in a thread and see how long they take, stopping them after a certain period? Thanks for any help. -Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Socket Timeout Handling
Since no one bit on this yet, let me simplify to the core issue I am having: What is the best practice for checking for network connectivity errors when making network calls? Is it better to wrap the functions that make said calls in threads and time them? Or to use timeout variables for modules like socket? Something else? I found some good general info here: http://www.onlamp.com/pub/a/python/2003/11/06/python_nio.html But I have had a hard time finding info on network error handling specifically. Thoughts? __ - Original Message From: wormwood_3 <[EMAIL PROTECTED]> To: Python Tutorlist Sent: Thursday, September 6, 2007 9:40:21 AM Subject: [Tutor] Socket Timeout Handling I am trying to figure out the optimal way to make socket connections (INET) and check for timeouts. The socket module has settimeout(timeout) and setdefaulttimeout(timeout). However, so far as I can tell, these apply to socket objects. The type of socket connection I want to make is getfqdn(address). So I can set the default timeout for socket, but not a socket object (makes sense so far). I cannot use the getfqdn(address) method on a socket object, I have to use it on socket. This means (as I understand it thus far), that while I can set a timeout value for socket objects, this will not apply to when I use the getfqdn() method, which is where I need a timeout check! Some example code for the steps so far: >>> import socket >>> conn = socket.socket() >>> conn.setdefaulttimeout(2.0) Traceback (most recent call last): File "", line 1, in AttributeError: '_socketobject' object has no attribute 'setdefaulttimeout' >>> socket.setdefaulttimeout(2.0) >>> conn.getfqdn("64.33.212.2") Traceback (most recent call last): File "", line 1, in AttributeError: '_socketobject' object has no attribute 'getfqdn' >>> socket.getfqdn("64.33.212.2") '64-33-212-2.customers.pingtone.net' >>> # Disconnected network connection here ... >>> socket.getfqdn("64.33.212.2") '64.33.212.2' >>> # Reconnected network connection here >>> socket.getfqdn("64.33.212.2") '64-33-212-2.customers.pingtone.net' After I disconnected my network connection and called getfqdn(), it returned the IP address I called it with after about 25 seconds. So the default timeout was ignored? Is there some other way to call this function so that I can check for timeouts? Should I instead just put my network calls in a thread and see how long they take, stopping them after a certain period? Thanks for any help. -Sam ___ 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] Socket Timeout Handling
Have not gotten any responses on this, nor very much by way of searching, which is strange and a little disappointing for such a seemingly basic thing. (May just be too obvious of a thing, so no one wanted to post the solution:-). ) But, I did find a decent recipe on ASPN that serves the purpose, so I will share in case others needed to do the same check as I did. Before I needed to make the network call in my program, I have the following: if checkURL('http://www.google.com/'): networkup = True else: networkup = False if networkup: print "Internet connection seems to be up." else: print "Internet connection seems to be down. Please check it", print "and retry." sys.exit() Then I continue with my network calls. The function I use is: def checkURL(url): """For checking internet connection. Taken from recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/101276"""; try: p = urlparse(url) h = HTTP(p[1]) h.putrequest('HEAD', p[2]) h.endheaders() if h.getreply()[0] == 200: return 1 else: return 0 except: return 0 The nice thing about this check is that is just looks at the head of the site, and so is rather fast. One other consideration: While it is a rare day indeed that Google.com is ever down, to be even safer, would could check against several reliable sites, such as Amazon, Yahoo, w3c.org, etc. The status of each check could be put in a list, and if any list item was networkup, then the internet connection may be considered up. Hope someone finds this useful. -Sam ___ - Original Message From: wormwood_3 <[EMAIL PROTECTED]> To: Python Tutorlist Sent: Thursday, September 6, 2007 4:46:08 PM Subject: Re: [Tutor] Socket Timeout Handling Since no one bit on this yet, let me simplify to the core issue I am having: What is the best practice for checking for network connectivity errors when making network calls? Is it better to wrap the functions that make said calls in threads and time them? Or to use timeout variables for modules like socket? Something else? I found some good general info here: http://www.onlamp.com/pub/a/python/2003/11/06/python_nio.html But I have had a hard time finding info on network error handling specifically. Thoughts? __ - Original Message From: wormwood_3 <[EMAIL PROTECTED]> To: Python Tutorlist Sent: Thursday, September 6, 2007 9:40:21 AM Subject: [Tutor] Socket Timeout Handling I am trying to figure out the optimal way to make socket connections (INET) and check for timeouts. The socket module has settimeout(timeout) and setdefaulttimeout(timeout). However, so far as I can tell, these apply to socket objects. The type of socket connection I want to make is getfqdn(address). So I can set the default timeout for socket, but not a socket object (makes sense so far). I cannot use the getfqdn(address) method on a socket object, I have to use it on socket. This means (as I understand it thus far), that while I can set a timeout value for socket objects, this will not apply to when I use the getfqdn() method, which is where I need a timeout check! Some example code for the steps so far: >>> import socket >>> conn = socket.socket() >>> conn.setdefaulttimeout(2.0) Traceback (most recent call last): File "", line 1, in AttributeError: '_socketobject' object has no attribute 'setdefaulttimeout' >>> socket.setdefaulttimeout(2.0) >>> conn.getfqdn("64.33.212.2") Traceback (most recent call last): File "", line 1, in AttributeError: '_socketobject' object has no attribute 'getfqdn' >>> socket.getfqdn("64.33.212.2") '64-33-212-2.customers.pingtone.net' >>> # Disconnected network connection here ... >>> socket.getfqdn("64.33.212.2") '64.33.212.2' >>> # Reconnected network connection here >>> socket.getfqdn("64.33.212.2") '64-33-212-2.customers.pingtone.net' After I disconnected my network connection and called getfqdn(), it returned the IP address I called it with after about 25 seconds. So the default timeout was ignored? Is there some other way to call this function so that I can check for timeouts? Should I instead just put my network calls in a thread and see how long they take, stopping them after a certain period? Thanks for any help. -Sam ___ 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] making math problems mmmm fun
Don't have any ideas to Pythonize this problem for you, but I must say that I hope this problem was listed in a chapter entitled "Cruel and Unusual"! -Sam - Original Message From: max baseman <[EMAIL PROTECTED]> To: tutor@python.org Sent: Monday, September 10, 2007 6:28:23 PM Subject: [Tutor] making math problems fun hello all this is a homework in math i dont need to program it but i would like to :) so like any other time pleas dont just give a answer tutorials or a explanation. i dont like to use script something i dont understand :) thanks basically the problem is to find a bunch of ways to put 1,2,3,4,5 into different math problems to that equal 1-25, i haven't spent to much time thinking about how to do this but i cant think of a way to do it it without writing making the program rather long here is the page from the book for the rules i will be working on this for the next week or so thanks for any help :) . you may use any of the four basic arithmetic operations- addition, subtraction, multiplication, and division (according to the order of operations rules). for example, 2+1x3-4 is a 1-2-3-4 expression for the number 1. . you may use exponents. for example, 2² - 4 - 1 is a 1234 expression for the number 3 . you may use radicals for EX: √4x2+1 is equal to 3 so 3+√4x2+1 is a 1234 expression for 6 . you may use factorials for EX: 4! means 4x3x2x1 so 3+4!+1-2 is a 1234 expression for the number 26 . you may juxtapose two or more digits (that is put them next to each other) to form a number such as 12. for example 43-12 is a 1234 expression for 31 . you may use parentheses and brackets to change the meaning of a expression for example according to the rules of order of operations 1 +4x3² is a 1234 expression for 37. you can add parentheses and brackets to get [(1+4)x3]² which is a 1234 expression for 225 . must use 1,2,3,4 exactly once thanks for the help ill post if i find anything ___ 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] Image Analysis
I have thought up a project for myself that is rather beyond my current knowledge, but I think it will be fun and very informative. I'll leave out the details right now, but the task that will be the hardest is that I need some way to analyze an image for color patterns. I would like to be able to load an image file of some format, and be able to determine what color the pixels are, in short. From this, I need to generate statistics, such as what color is most common, find patterns in the image, etc. If anyone knows ANYTHING about this sort of analysis in Python, or even any helpful tutorials on image analysis that are more general, I would greatly appreciate it. My only lead right now is PIL, and I am not sure if it will meet my needs. Thanks, Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Socket Timeout Handling
>I did send you a response and it is listed on the gmane archive >so if you didn't see it something has gone adrift somewhere. Just searched all my mail, for some reason I did not get this. I will check the archive. Thanks! >The solution you posted seems to bear no resemblence >to the problem you posted? How does this relate to setting >socket timeouts or using getfqdn()? Initially I was asking about how to set socket timeouts. But my general query was just suggestions on how best to detect internet connection upness. Since I "did not" get any responses to the question in the form of socket timeouts in particular, I resent the question in a more general form. It was that form that my last reply was to address. You are right on what it does. I thought at first it would be best to use something native to the classes/functions I was using in the standard library if possible. The solution I ended up with was more general, but serves the purpose. -Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Socket Timeout Handling
Ok, just found your message in the archives. Thanks very much for that! By way of response-- >>That may be because your question ventures into fairly deep areas of >> networking >> that most folk who are just learning Python(ie readers of this list) >> have probably >> not encountered. True enough:-) I am learning this as I go. >>If you do want to get into depth on Python networking you may find the >> A-Press book Python Network Programming useful - I do regularly :-). I came across it a few times, was not sure if it would be as useful as more general books, but I think I will get it soon. >> However the only information I could see about timeouts there was >>related to socket.settimeout() which ISTR you didn't want to/couldn't >>use... The docs show there is settimeout() and setdefaulttimeout(). I will try to explain below why I did not think I could use these. I might have been wrong... > What is the best practice for checking for network connectivity > errors when making network calls? Is it better to wrap the functions > that make said calls in threads and time them? > Or to use timeout variables for modules like socket? >> Personally if i was doingt that I'd almost certainy put it in a thread >> and apply a timeout within the thread. but not having tried that I >> don't >> know how easy it would be! A friend shared an implementation of this with me that works well. I have attached it. Maybe you will find it useful! > I am trying to figure out the optimal way to make socket connections > (INET) and check for timeouts. The socket module has > settimeout(timeout) > and setdefaulttimeout(timeout). However, so far as I can tell, these > apply > to socket objects. The type of socket connection I want to make is > getfqdn(address). >> I don't understand, getfqdn() returns a domain name not an a socket? Yes. Example: >>> import socket >>> socket.getfqdn("64.233.169.99") 'yo-in-f99.google.com' And therein lies the rub! The main function of the script I have this in is to go through a list of IP addresses, and find their FQDN, and other information. It is at this step, when I get the FQDN, that I wanted to do some find of timeout setting. But since it is *not* a socket object, I cannot use settimeout()... > So I can set the default timeout for socket, but not a socket object > (makes sense so far). I cannot use the getfqdn(address) method on > a socket object, I have to use it on socket. >> Sorry it's not making sense to me, getfqdn takes a host name not a >> socket. Exactly my problem:-) >> setdefaulttimout is a function in the socket module not a method of >> socket. >> Thus you woulfd call that before reating a new socket: Thanks for the clarification, I will try this. >> What are you trying to do? Establish a socket connection to something >> or just do a name check that times out more quickly?(or slowly) Just trying to get the FQDNs from a list of IPs, and want to have some sane error handling in place in case my connection dies during the queries. Thanks! class ThreadTimeoutError(Exception): pass from threading import Thread class _ThreadedMethod(Thread): def __init__(self, target, args, kwargs): Thread.__init__(self) self.setDaemon(True) self.target, self.args, self.kwargs = target, args, kwargs self.start() def run(self): try: self.result = self.target(*self.args, **self.kwargs) except Exception, e: self.exception = e except: self.exception = Exception() else: self.exception = None def TimeoutThread(timeout=None): def timeoutthread_proxy(method): if hasattr(method, "__name__"): method_name = method.__name__ else: method_name = 'unknown' def timeoutthread_invocation_proxy(*args, **kwargs): worker = _ThreadedMethod(method, args, kwargs) if timeout is None: return worker worker.join(timeout) if worker.isAlive(): raise ThreadTimeoutError( "A call to %s() has timed out" % method_name) elif worker.exception is not None: raise worker.exception else: return worker.result timeoutthread_invocation_proxy.__name__= method_name return timeoutthread_invocation_proxy return timeoutthread_proxy ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Image Analysis
Thanks very much! This may be exactly what I need, and in any case will be a great starting point! -Sam ___ - Original Message From: "Carnell, James E" <[EMAIL PROTECTED]> To: tutor@python.org Sent: Tuesday, September 11, 2007 9:45:30 AM Subject: Re: [Tutor] Image Analysis "wormwood_3" <[EMAIL PROTECTED]> wrote > I need some way to analyze an image for color patterns. > > My only lead right now is PIL, and I am not sure if it will meet my > needs. I am using PIL and numpy # something hat at least looks sorta like import numpy import PIL myImage = Image.open("C:\\file.bmp") myImage.assarray(stuff) I can't remember the code and am at work, but this will give you a big array to work with. Each pixel for RGB looks kind of like [100,150,250] then each line is in a matrix etc. It's a bit of a pain traversing the matrices, but you can do anything you would do to an array. Pyro Robotics also has a small tutorial about this. http://pyrorobotics.org/?page=PyroModuleVisionSystem ___ 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] Image Analysis
Very strange. This is the second time that I know of in the last month that I have not received some emails from the list. I did not get the message Eike Welk sent out. Checking archives... - Original Message From: Dave Kuhlman <[EMAIL PROTECTED]> To: tutor@python.org Sent: Tuesday, September 11, 2007 11:09:36 PM Subject: Re: [Tutor] Image Analysis On Wed, Sep 12, 2007 at 01:57:35AM +0200, Eike Welk wrote: > I have attached the program, the input image, and the output image. > Please do not stuff 1 MB emails in my mailbox. Either (1) post the images on the Web and provide a link or (2) ask before emailing large attachments. Thank you. Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman ___ 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] Image Analysis
Could not find it. The searchable interface at ActiveState's site does not have it, and I don't know of any way to search the zipped archives... Eike, if you would be able to send me your post, even just to my address and not the tutor list, I would greatly appreciate it! Seems it had some neat things in it. -Sam - Original Message ---- From: wormwood_3 <[EMAIL PROTECTED]> To: Python Tutorlist Sent: Wednesday, September 12, 2007 9:21:28 AM Subject: Re: [Tutor] Image Analysis Very strange. This is the second time that I know of in the last month that I have not received some emails from the list. I did not get the message Eike Welk sent out. Checking archives... - Original Message From: Dave Kuhlman <[EMAIL PROTECTED]> To: tutor@python.org Sent: Tuesday, September 11, 2007 11:09:36 PM Subject: Re: [Tutor] Image Analysis On Wed, Sep 12, 2007 at 01:57:35AM +0200, Eike Welk wrote: > I have attached the program, the input image, and the output image. > Please do not stuff 1 MB emails in my mailbox. Either (1) post the images on the Web and provide a link or (2) ask before emailing large attachments. Thank you. Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman ___ 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] interpreted or compiled?
Michael, The most common method I know of to do this is py2exe: http://www.py2exe.org/ This lets you turn scripts into executable Windows programs. A different, and perhaps better, method is PyInstaller: http://pyinstaller.python-hosting.com/ This creates executables for Windows and Linux. If you just meant run a script without calling Python, you can add the she-bang line (#!/usr/bin/python as the first line. Change to wherever Python lives on your system), and make the file executable (this is all assuming you are on Linux, sorry). Then you can just do "./mycoolscript.py" and run it. Hope these help! -Sam - Original Message From: Michael <[EMAIL PROTECTED]> To: tutor@python.org Sent: Wednesday, September 12, 2007 9:58:46 AM Subject: [Tutor] interpreted or compiled? Hi As a new Python user I was curious if you can run Python without the environment, ie make it an executable? Thanks Michael ___ 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] deleting one line in multiple files
I think the problem is that the original script you borrowed looks at the file passed to input, and iterates over the lines in that file, removing them if they match your pattern. What you actually want to be doing is iterating over the lines of your list file, and for each line (which represents a file), you want to open *that* file, do the check for your pattern, and delete appropriately. Hope I am not completely off:-) If I am right so far, you want to do something like: import fileinput for file in fileinput.input("filelist.list", inplace=1): curfile = file.open() for line in curfile: line = line.strip() if not '
Re: [Tutor] deleting one line in multiple files
Thought I would do some more testing and get you a more finalized form this time. So I took the mygrep.py script, and put it in a folder with 3 test files with content like this: I am some lines of text yep I love text 435345 345345345 Then I ran: [EMAIL PROTECTED]:~/test$ python mygrep.py "
Re: [Tutor] vi and python
Hello, I go back and forth between SPE and VIM for Python myself. As for Python support in VIM, you can use most editions of VIM as a plain text editor, and it is fine for Python. But, if you install vim-python (http://ddtp.debian.net/ddt.cgi?desc_id=20183), you get some nice added features such as syntax highlighting, code completion, etc. Sort of like a basic IDE all in VIM! Not sure how you have vim installed. On Ubuntu for example, I just do "sudo apt-get install vim-full", and I get all the variants, including vim-python. Other handy things: * pydiction: http://www.vim.org/scripts/script.php?script_id=850 * python.vim: http://vim.sourceforge.net/scripts/script.php?script_id=30 Best of luck! -Sam __ - Original Message From: Danyelle Gragsone <[EMAIL PROTECTED]> To: python lista <[EMAIL PROTECTED]>; tutor@python.org Sent: Friday, September 14, 2007 8:35:21 PM Subject: [Tutor] vi and python Good Evening, I am running gentoo. I want to use vi to program in python. I wondered are there any other gentooovians out there who know if python support is already installed. Thanks, LadyNikon ___ 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] Loop optimization
Kent, You replied with the following some time ago regarding a question I asked about optimizing a loop: >> You should try an optimized for loop: >> append_ = self.potdomains.append_ >> s1_ = suffix1 >> s2_ = suffix2 >> for word in self.dictcontents: >> append_(word + s1_) >> append_(word + s2_) >> >> This will take out some of the difference at least. I realized when reviewing this that I do not know what the "NAME_" notation is about. I know that "_METHOD/ATTR" makes a method or attribute private to an instance, but what does the trailing underscore do? Thanks, Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Adding a GUI
Hi all, I am just starting to learn GUI programming, with wxPython. I have a script that that I have developed to a useful point, and I want to add a GUI to it. I am a little unsure as the the best approach to this. The script heretofore was just run at the command line. Would it make sense to add an option in the same script to have it run in "graphical mode", which would trigger the wxPython code I would add in, and otherwise use the same logic? More generally: Anyone have any tips on adding a GUI to a pre-existing script in a way that minimizes repetition of logic? I would rather not make a separate script that only used wxPython, and I do not think I have to, but perhaps there are good reasons to, etc. Thanks, Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding a GUI
I don't think either, I was planning on sticking with wxPython for now, since it is cross-platform, looks way better than Tkinter, and seems to have all the flexibility I would want. But regardless of what toolkit I use, the question of how best to combine it with pre-existent logic still remains the same:-) -Sam - Original Message From: Dotan Cohen <[EMAIL PROTECTED]> To: wormwood_3 <[EMAIL PROTECTED]> Sent: Sunday, September 16, 2007 2:21:53 AM Subject: Re: [Tutor] Adding a GUI On 16/09/2007, wormwood_3 <[EMAIL PROTECTED]> wrote: > Hi all, > > I am just starting to learn GUI programming, with wxPython. I have a script > that that I have developed to a useful point, and I want to add a GUI to it. > I am a little unsure as the the best approach to this. The script heretofore > was just run at the command line. Would it make sense to add an option in the > same script to have it run in "graphical mode", which would trigger the > wxPython code I would add in, and otherwise use the same logic? > > More generally: Anyone have any tips on adding a GUI to a pre-existing script > in a way that minimizes repetition of logic? I would rather not make a > separate script that only used wxPython, and I do not think I have to, but > perhaps there are good reasons to, etc. > > Thanks, > Sam > Are you interested in Qt or Tinker bindings? Qt are much nicer in Linux, but Tinker is cross-platform. Dotan Cohen http://what-is-what.com http://gibberish.co.il א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Adding a GUI
>> First of all Sam, thanks for your help with the fileinput() problem I >> was having! =) Sure thing:-) Sorry I could not actually solve it! I am still have a hard time getting my mind around the line.strip(), then printing based on a condition loop. Not sure why... The excerpt from Lutz' book was very helpful, thanks. One issue I still have: I do not really want to lose the ability to run the script from the CLI. This seems relatively common too, having the same script with a command line and a graphical version. Lutz seems to present the case of pure conversion, where a script was first CLI only, and will end up being GUI only, which I was hoping to avoid... I didn't mean to speak badly of Tkinter, I know it is the most ubiquitous, and can work quite well. I have used it before, and it is relatively easy to use. The main issue I have with it is just that it, well, looks bad! The windows and frames do not resemble native windows per OS, which is an advantage of wxPython. I will check out his book more, though, 300 pages on GUI programming would be helpful! Thanks, Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding a GUI
Hi, >> Wormwood, an easier way to create a crossplatform GUI than raw wxPython is >> to use pythoncard, which is a library that is on top of wxPython that is >> there to make it easier to use and to make it easier to >> layout GUI screens/dialogs. I have heard some good things about pythoncard. I will be sure to give it a try! >> I've found its a much faster "whiteboard to running software time" then >> wxPython by itself. (I've used both, raw wxPython a lot *more* than >> pythoncard, as i'd not yet heard of it). Awesome. >> Alan is right about separating the core logic of the program from the >> input/output methods, and using that in both a command line and a gui >> program. I hope I am not missing messages from the tutor list again. I did not see anything from Alan on my question, which you seemed to be referring to... But that aside, I definitely agree this is an important principle, which I always try to implement. >> I doubt you'll need a book to use pythoncard. Its about as easy as VB to >> build a form with the WYSIWYG, and very pythonic to use the forms you've >> built. >> To get up an going, install a compatible version of wxPython use this link: http://pythoncard.sourceforge.net/walkthrough1.html Thanks! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] linux terminal coloring in python
Hello, I actually wanted/looked for colored output in the CLI for quite a while myself, and finally found the solution! The magic?: "echo -e". The syntax is a little odd to use it, and I don't think I can explain it very succinctly. It may be best to see an example of it in action. I wrote a bash script to summarize files, and I used the "echo -e" syntax to color the output. Actual code: http://launchpod.homelinux.com:81/trac/code/browser/userextensions/summarizer.sh Details here, with screenshots: http://assistedsilicon.blogspot.com/2007/04/summary-execution.html Good luck! -Sam _ - Original Message From: Tiago Saboga <[EMAIL PROTECTED]> To: tutor@python.org Sent: Saturday, September 29, 2007 6:11:35 PM Subject: Re: [Tutor] linux terminal coloring in python On Sat, Sep 29, 2007 at 08:32:37AM -0700, Robert Jackson wrote: > I'm trying to get some pretty colored output for a Linux console / terminal window. Google searches only reveal the curses module to colorize output. > Is there a simpler way? Curses seems to be FAR too powerful for what it is I want to do (simply to colorize a few 'print' outputs to the console). I have looked for that last week and the best I could find was ColorANSI module, distributed with ipython (in a debian system, it's /usr/share/python-support/ipython/IPython/ColorANSI.py). I just bookmarked it, and don't know how to make it work. But it seems quite simple. Tiago. ___ 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] CGI File Woes
Hello all, I am working on a very simple CGI script. The site I want to use it on is a shared linux host, but I confirmed that .py files in the right dir with the right permissions and shebang execute just fine, Hello World sort of tests were successful. So now something a little more involved: #!/usr/bin/python2.4 import cgitb; cgitb.enable() thefile = open("template.html", "r") templatestuff = thefile.read() thefile.close() print "Content-Type: text/html" if templatestuff: print "Found it" title1 = "I am a title!" body1 = "I am some hot content" print templatestuff % (title1, body1) "template.html" is in the same dir, and is simply: http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";> %s %s If I run this script without the 3 lines after the import line, it works fine (namely I get an error that templatestuff is not defined, as would be expected). With those lines however, I am getting a 500 Internal Server Error. Since I am not shown an error page with cgitb, this would likely mean a syntax error. However, if I run the script locally, it works just fine, printing out the HTML with variables filled in. Now for the odd part: If I change that open line to "thefile = open("asdas", "r")", I get "IOError: [Errno 2] No such file or directory: 'asdas' ". So it seems the script is finding the template file when I have it as above, but is throwing something when it tries to open it. I have confirmed the file has the right permissions, I have even tried it with all permissions set on that file. I am just totally baffled why I cannot open any files from the script. Any ideas? -Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] CGI File Woes
>>Did you mean for there to be two tags or is it a typo? Just a typo. Should not have a big effect, but it has been fixed. >>I've never used cgitb (and until now didn't know it existed!) >>so can't comment. I had not heard of it until this week when I started working on CGI stuff, but I have found it super handy! All you have to do it "import cgitb; cgitb.enable()" and all tracebacks will get printed to nicely formatted HTML output on the page you were trying to load. >>But I usually see this when the web user doesn't have >>permission to open the html file. But... This definitely would make the most sense in that the error is not a Python error, at least I am assuming so from the lack of traceback. However, the odd thing is that if I hit template.html directly, the page renders just fine. This would mean the web user can access it, right? And I was assuming the Python script was being run by the web user as well, but perhaps it is not. >>For all users? >>Remember that the web server runs as a separate user >>and that's the user than needs to open the file. Sorry, I stated that poorly. What I meant was that in the process of troubleshooting, I ended up enabling ALL permissions, so user, group, and others could read, write, and execute. So since this is now the case, and the script runs fine locally, and I can hit template.html on its own on the web server... I am still quite baffled! -Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] CGI File Woes
Those examples were a lot of help Martin. Turned out the only issue was that I did not have this line right: print "Content-type: text/html\n\n" With that form, it loaded just fine. It had been running fine from the terminal, but without this line being right, Apache did not know what to do with it. I had spoken with my web-hosting provider, but since I had a shared account I was unable to view the server logs. And the person helping me knew nothing about Python (he kept slipping and calling it PHP actually, to my dismay and chagrin:-) ). Thanks for all the help, Alan and Martin. -Sam _ - Original Message From: Martin Walsh <[EMAIL PROTECTED]> To: tutor@python.org Sent: Sunday, September 30, 2007 1:07:02 PM Subject: Re: [Tutor] CGI File Woes No doubt cgitb is a great tool for debugging cgi, but IIUC there are at least two instances when you will not get the pretty printed tracebacks in the browser when using cgitb. One is after, what I would call, a 'compile time' exception such as SyntaxError, in your python code. The other is when the python code runs without exception, but you have not separated the header and the document content with a newline. At least, I have found these to be true when using apache-cgi. Consider the following examples: #!/usr/bin/env python # raises a SyntaxError import cgi import cgitb; cgitb.enable() print "Content-type: text/html\n\n" # NOTE: purposeful misspelling of the print statement prin "Hello, world!" # the code above will produce a server 500, with apache # complaining about "premature end of script headers" ... #!/usr/bin/env python # good python, bad data import cgi import cgitb; cgitb.enable() print "Content-type: text/html" print "Hello, world!" # this is syntactically correct python, and will # run from the command line, but the html header # and html content have no separation, so apache # will consider all of it to be header info, resulting # in another server 500, "malformed header" As others have advised, under these circumstances you can review the apache error log (if your webhost allows it). Or roll-your-own logging equivalent, and run your cgi from a terminal to catch SyntaxErrors and the like. HTH, Marty ___ 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] Really basic web templating
Hello all, I am trying to think of a way to make this happen, but it may not be at all possible:-) I would like to use Python to generate pages on demand for my website. By this I mean, when someone hits www.mysite.com/pagex.html, I want to generate the page pagex.html via a Python script. I have a script setup that can generate a page from a template file and a file with the body and title of the page in particular I want to generate. Now from this, I need to somehow be able to respond to requests, and then generate the page based on the page requested. I want to do this because my site is on a shared hosting account, so I cannot install a web framework like Django, and the site's pages will be rather simple. At the same time, I would like to use templating, so I do not have repeated identical code between the pages. Any ideas on this? Thanks, Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Really basic web templating
Well yes and no:-) This sort of application would fall under the sprawling category of CGI, yes, and I can use Python scripts on my web server, so it is supported. But nearly every tutorial I have seen regarding Python and CGI only have to do with form submissions, doing calculations and other things with data sent from webpages to Python scripts. But that is not really what I want to do. I am wondering what a script would need to do to take requests for pages on my site, and generate them from templates. I am not sure if I can do this without having access to Apache rewrite rules, etc. Perhaps this is just another area of CGI that I missed and have not seen tutorials on. If it is and you have seen some, please share! -Sam - Original Message From: Ian Witham <[EMAIL PROTECTED]> To: wormwood_3 <[EMAIL PROTECTED]> Cc: Python Tutorlist Sent: Sunday, September 30, 2007 11:52:38 PM Subject: Re: [Tutor] Really basic web templating On 10/1/07, wormwood_3 <[EMAIL PROTECTED]> wrote: Hello all, I am trying to think of a way to make this happen, but it may not be at all possible:-) I would like to use Python to generate pages on demand for my website. By this I mean, when someone hits www.mysite.com/pagex.html, I want to generate the page pagex.html via a Python script. I have a script setup that can generate a page from a template file and a file with the body and title of the page in particular I want to generate. Now from this, I need to somehow be able to respond to requests, and then generate the page based on the page requested. I want to do this because my site is on a shared hosting account, so I cannot install a web framework like Django, and the site's pages will be rather simple. At the same time, I would like to use templating, so I do not have repeated identical code between the pages. Any ideas on this? It sounds like you want to use CGI. I think virtually all web servers support it and there are a ton of CGI tutorials on the web Ian. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Really basic web templating
My host actually does support Python. But I don't have access to Apache rules nor the level of access to install apps like Django, so I am limited to just scripts I write. But for that price, i will definitely check out WebFaction! -Sam - Original Message From: Kent Johnson <[EMAIL PROTECTED]> To: wormwood_3 <[EMAIL PROTECTED]> Cc: Python Tutorlist Sent: Monday, October 1, 2007 6:44:45 AM Subject: Re: [Tutor] Really basic web templating wormwood_3 wrote: > I want > to do this because my site is on a shared hosting account, so I cannot > install a web framework like Django Another option is to switch to a hosting account that supports Python. I have been working with WebFaction and I'm very happy with them; an account with Django support is available for less than $10/month. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Really basic web templating
There was another host that I wanted to mention along these lines (for Python sites) that I think is even better: VPSLink (http://www.vpslink.com). They allow root SSH access, and can install your choice of OS (lots of linux flavors, ubuntu, SUSE, CentOS, etc) from a control panel. Aside from that they are similar to WebFaction, but having that much flexibility, with the base plan only ~$8/month, is pretty awesome. -Sam - Original Message From: Kent Johnson <[EMAIL PROTECTED]> To: wormwood_3 <[EMAIL PROTECTED]> Cc: Python Tutorlist Sent: Monday, October 1, 2007 8:36:03 AM Subject: Re: [Tutor] Really basic web templating wormwood_3 wrote: > My host actually does support Python. But I don't have access to > Apache rules nor the level of access to install apps like Django, so > I am limited to just scripts I write. Webfaction gives you both - you can install anything you like as long as it doesn't require superuser privileges and you have your own Apache instance that you can tweak any way you like. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Logging with proper format
Hello tutors, I am adding logging to a program I am writing. I have some messages I want to log that are rather long. The problem I am running into is that when the line is more than the 80 character line recommendation and I split it across 2 lines with "\", the output is affected. Example code: logger.info("Checked %s records in %s seconds, yielding an average of \ %s seconds per record." % (len(self.data), duration, avgquery) ) The current output: 2007-10-07 14:49:42,902 - ipinfo - INFO - Checked 4 records in 0.0698790550232 seconds, yielding an average of 0.0174697637558 seconds per record. Desired output would be: 2007-10-07 14:49:42,902 - ipinfo - INFO - Checked 4 records in 0.0698790550232 seconds, yielding an average of 0.0174697637558 seconds per record. So what is the best way to break long code lines and still preserve desired output? Thanks, Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Logging with proper format
Sent this almost an hour ago, did not get it from the list yet. No idea why, but sending again... -- Hello tutors, I am adding logging to a program I am writing. I have some messages I want to log that are rather long. The problem I am running into is that when the line is more than the 80 character line recommendation and I split it across 2 lines with "\", the output is affected. Example code: logger.info("Checked %s records in %s seconds, yielding an average of \ %s seconds per record." % (len(self.data), duration, avgquery) ) The current output: 2007-10-07 14:49:42,902 - ipinfo - INFO - Checked 4 records in 0.0698790550232 seconds, yielding an average of 0.0174697637558 seconds per record. Desired output would be: 2007-10-07 14:49:42,902 - ipinfo - INFO - Checked 4 records in 0.0698790550232 seconds, yielding an average of 0.0174697637558 seconds per record. So what is the best way to break long code lines and still preserve desired output? Thanks, Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Updating MySQL Database
Hello all, I have a script which takes data from a file or MySQL DB, looks up some stuff, then can print results to console or file. I would also like it to be able to update a MySQL database with the results. Does anyone have any ideas on how to do this? I can update records just fine, but what is the best way to do LOTS of updates aside from running an update statement per record? Using that method, for example, assuming I have a list of results, each line of the form "ip,fqdn": for line in inputlist: updatequery = "update resultstable set fqdn = line.split(",")[1] where ip = line.split(",")[0];" connection = MySQLdb.connect(db=self.todatabase, host=self.host, user=self.user, passwd=self.passwd, port=int(self.port)) cursor = connection.cursor() cursor.execute(updatequery) queryresults = cursor.fetchall() cursor.close() connection.close() But this means making a connection and query for every line of results, which is a lot. Any ideas on optimization? Thanks, Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Updating MySQL Database
Thanks to everyone who responded for the information and tips. * That line I had: > for line in inputlist: > updatequery = "update resultstable set fqdn = line.split(",")[1] > where ip = line.split(",")[0];" was totally bogus. I was typing and thinking, and not at the same rate:-) The real thing would be something more like: for line in inputlist: fqdn = line.split(",")[1] ip = line.split(",")[0] updatequery = "update resultstable set fqdn = '%s' where ip = '%s';" % (fqdn, ip) * I will try a first version with a single connection and close, looping through executes in the middle. Should have thought of that first... Thanks again, Sam _____ - Original Message From: Eric Walstad <[EMAIL PROTECTED]> To: wormwood_3 <[EMAIL PROTECTED]> Cc: Python Tutorlist Sent: Sunday, October 7, 2007 11:07:12 PM Subject: Re: [Tutor] Updating MySQL Database Hey Sam, wormwood_3 wrote: > Hello all, > > I have a script which takes data from a file or MySQL DB, looks up some > stuff, then can print results to console or file. I would also like it to be > able to update a MySQL database with the results. Does anyone have any ideas > on how to do this? I can update records just fine, but what is the best way > to do LOTS of updates aside from running an update statement per record? I recommend not worrying about speed on your first iteration of your script. I'd first define 'too slow', then write the script the way that feels intuitive to you. If your script passes your threshold of 'too slow', then look at optimizing it. If optimization is really necessary, I'd look into .executemany(). If that is still too slow for you then I'd consider writing the update SQL to a file and then calling mysql, passing it the sql file your script created. I found this last approach the fastest for a data import script I once wrote for importing millions of records to a PostgreSQL database. -E ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Missing LOTS of list messages
Hello tutors, I just discovered again that I am missing lots of messages from the list. Compared to this: http://www.nabble.com/Updating-MySQL-Database-t4585380.html where 8 messages are listed, my email client shows 5. They are not in spam, I just never got them. Since this is the third time I assume it is happening all the time. Has anyone else experienced this? I am using Yahoo! Mail, not sure if anyone experiences the same with other services... If anyone has ideas, I would welcome them. I hate the idea of missing great messages from the list (much less sending responses that have already been covered...) -Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding a project
When I started out (not long ago), I started working on projects I had the need for. I have done a lot of small sysadmin projects, and they have all been things I found I needed or would help me doing sysadmin work. Try to think of tasks you have to often do that are at all subject to automation. Even ones that seem like they might be hard to automate, might be a good target for a starting project. The good thing about working on sysadmin projects is that it is a well-travelled path. Someone has likely written something similar to what you need, so finding inspiration and help is not as hard as with other sorts of projects. If you really need inspiration, check out the Python Cookbook: http://aspn.activestate.com/ASPN/Python/Cookbook/ It has lots of great examples of relatively small things average people have completed in Python. Maybe you can start by finding several that do things you need, and write something to weave them together. Happy coding! -Sam ___ - Original Message From: Eric Lake <[EMAIL PROTECTED]> To: Python Tutor mailing list Sent: Tuesday, October 9, 2007 9:54:56 PM Subject: [Tutor] Finding a project What is a good way to find a project to work on? I really want to work on something but I can not think of anything useful to write. I am interested in sysadmin stuff (backups, monitoring, etc). That being said I don't really have experience writing apps like that. But I really want to learn. -- Thanks Eric Lake ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Updating MySQL Database
I loved that strip so much. I printed it today, and it is on the door of our IT office:-) - Original Message From: Kent Johnson <[EMAIL PROTECTED]> To: Python Tutorlist Sent: Wednesday, October 10, 2007 8:52:12 AM Subject: Re: [Tutor] Updating MySQL Database Kent Johnson wrote: > It > also looks like you are embedding the data in the SQL command, this is > very bad practice, it opens you to SQL injection attacks For a humorous explanation of why you don't want to directly embed data into SQL commands, see today's xkcd: http://xkcd.com/327/ 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] Logging in Linux
Hello tutors, I have become familiar with the basic use of the logging module. I have a program that prints out warning messages as well as info messages as it runs, merely dumping them into stdout. When I make such a script into a cron job, I simply redirect this output to /dev/null. Now, what I would like to do is instead send this output to one or more of the standard linux log files. First though, I am wondering, what is the standard procedure in a case like this? Is it more appropriate to make a totally separate log file for all user-added scripts (or one a piece), and put them in /var/log or elsewhere? Where have you all found to be helpful places to log messages from automated scripts on Linux systems? The end goal is simply to have a place that I can check to see that the script is running as expected, or troubleshoot issues if need arise. Thanks for any help or tips, Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Printing the code of a function
Hello all, This might be trivially easy, but I was having a hard time searching on it since all the component terms are overloaded:-) I am wondering if there is a way to print out the code of a defined function. So if I have: def foo(): print "Show me the money." then I would like to do something like: >>> foo.show_code def foo(): print "Show me the money." I checked out everything in dir(foo), but everything that looked promising (namely foo.func_code), didn't end up being anything close. Thanks for any help! Cordially, Sam ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Printing the code of a function
I actually didn't have a definite use case in mind, it was pure curiosity that arose while writing a few simple test functions. After having considered it more, I can't come up with a case where it would really be necessary, so I apologize for that. It makes sense why it wouldn't be possible without a disassembler now. Thanks for the info! ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins From: bob gailer To: wormwood_3 Cc: tutor@python.org Sent: Sunday, December 28, 2008 9:07:12 PM Subject: Re: [Tutor] Printing the code of a function wormwood_3 wrote: Hello all, This might be trivially easy, but I was having a hard time searching on it since all the component terms are overloaded:-) I am wondering if there is a way to print out the code of a defined function. Python does not store the source when compiling things. So the short answer is NO. There are some "disassemblers" for Python but I have no experience with them. They will not be able to reconstruct the exact source. But ... why do you want to do this? Perhaps if we had more information about the "use case" we could steer you to a solution. So if I have: def foo(): print "Show me the money." then I would like to do something like: >>> foo.show_code def foo(): print "Show me the money." I checked out everything in dir(foo), but everything that looked promising (namely foo.func_code), didn't end up being anything close. Thanks for any help! Cordially, Sam ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer Chapel Hill NC 919-636-4239___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python - Data Mining?
I have done some data analysis work with Python, mostly with MySQL databases. Just as easy as the examples Eric mentioned with SQLite. All depends on what database you have to work with. Did you have any in mind or just wondering about data mining and Python in general? Regarding graphing, I have had great luck with using pylab ( http://www.scipy.org/PyLab ). Here is a simple example to create graphs of a thing being counted per unit: # Grab the needed module: from pylab import * def GraphData(time_and_count): """ Creates graph image of counts per time. """ # Set axis labels and their properties: x = xlabel('Time') setp(x, fontweight='bold') y = ylabel('Count') setp(y, fontweight='bold') # Plot: plotted = plot(time_and_count.keys(), time_and_count.values(), '--') setp(plotted, marker='s') title('Count over Time') grid(True) savefig('results.png', dpi=100) # Make a test dictionary of counts per time: time_and_count = dict(enumerate('4 5 3 4 6 7 8 9 3'.split())) # Make a graph: graphData(time_and_count) If all goes well, you should end up with a file "results.png" in the dir you ran this script. There is a LOT more you can do with pylab, but this sort of function should get you going for simple graphs. ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins From: Eric Dorsey To: Nick Scholtes Cc: tutor@python.org Sent: Sunday, January 4, 2009 2:32:12 PM Subject: Re: [Tutor] Python - Data Mining? Hi Nick, I don't know about the graphing portion of your question, but yes Python does interact very well with databases. I have been working on a workout tracking program the last two months or so, and I'm new to programming. I'd highly recommend SQLite as a built-in database solution. I know it's included in Python version 2.5 which is what i'm currently running. You can call it at the top of your program with "import sqlite3", then you can run queries and create tables, etc. Here is some example code of SQLite usage in my program: #create the database, or connect if it already exists conn = sqlite3.connect('workoutstats.db') #create a variable called cursor to use, since its easier than typing out conn.cursor() all the time.. cursor = conn.cursor() #create a table cursor.execute(''' CREATE TABLE WR (id INTEGER PRIMARY KEY, reps SMALLINT(1000), weight SMALLINT(1000), exer VARCHAR(30), date DATE) ''') #query the WR table, feeding it the 'srch' variable which fills in where the SQL has a ? cursor.execute( "SELECT SUM(REPS) FROM WR WHERE EXER=?", (srch,) ) -Eric On Sun, Jan 4, 2009 at 11:25 AM, Nick Scholtes wrote: Hi, I'm still very, very new to Python and programming. I was wondering if anyone can point me in the right direction. As I gradually learn Python, one of the things I want to be able to do is take a database, run queries and extract information and then graph that information visually to see patterns in the data. Where should I start? Does Python do this? If not, what language is used for this? Thank you very much, Nick -- Art: http://www.coroflot.com/bellsoffreedom ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- (e) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Documentation and top matter
While PEP 8 and PEP 257 provide ample helpful information on the recommended ways to document classes, functions, and comments within code, I am having a hard time finding recommendations on how to document scripts by way of top matter. For example, I used this format for a while: #!/usr/bin/env python #- # Name:my_cool_name # Purpose: My awesome purpose. # # Author: My Name # # Started: 01/01/01 #- # IMPORT STUFF REST OF CODE This made it *really* easy to see what was going on as soon as you opened the file. Then I started shifting to something more like this: #!/usr/bin/env python #- """ My awesome purpose. """ author = "My Name" date_started = "2001-01-01" version = 0.1 #- IMPORT STUFF REST OF CODE This format is still readable and distinct, but by putting the information into attributes, they are accessible in an interpreter, by external tools, etc. Also putting the purpose in the first docstring allowed for use of .__doc__. But are there more generally accepted means of defining this information that are highly readable? I have also seen attributes in the form of "__author__ = 'My Name'", for which I found some discussion on comp.lang.python ( http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0128.html ). Recommendations? ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Getting multi-line input from user
Hello everyone, I'd like to prompt the user for input and get back multiple lines, for example if someone wanted to enter a few paragraphs. Then I'd like to be able to print their input out and preserve formatting. Here's what I have so far: control = True user_input = [] while control: if not user_input: entry = raw_input("Enter text, 'done' on its own line to quit: \n") user_input.append(entry) else: entry = raw_input("") user_input.append(entry) if entry == "done": del user_input[-1] control = False user_input = ' '.join(user_input) print user_input So you end up with: Enter text, 'done' on its own line to quit: I am some text. And I am more. I am a new paragraph. done I am some text. And I am more. I am a new paragraph. 1) Is there a more elegant/common way to get multi-line user input than this sort of thing? 2) How can I combine and print the output so that paragraphs and the like are preserved? Thanks, Sam ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting multi-line input from user
>>> For starters you can simplify things a lot: >>> >>> user_input = [] >>> entry = raw_input("Enter text, 'done' on its own line to quit: \n") >>> while entry != "done": >>>user_input.append(entry) >>>entry = raw_input("") >>> user_input = ' '.join(user_input) >>> print user_input >>> >>> 2) How can I combine and print the output so that paragraphs and the like >>> are preserved? >>> >>> I don't understand. Please give an example. That is much cleaner, thanks! On point 2, say I enter: Enter text, 'done' on its own line to quit: I am a sentence. I am another sentence. I am a new paragraph. done What I get out is: I am a sentence. I am another sentence. I am a new paragraph. But that just shows the double new lines of my new paragraph as an extra space. I'd like to print it out so it appears just as it was typed in, with appropriate newlines. -Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting multi-line input from user
Joining with newline works perfectly. Thanks everyone! ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins From: Alan Gauld To: tutor@python.org Sent: Wednesday, January 7, 2009 3:58:18 AM Subject: Re: [Tutor] Getting multi-line input from user "Andre Engels" wrote > The newline character is written \n in Python, so if you replace > > ' '.join(user_input) > > by > > '\n'.join(user_input) Yep, that's better than my suggestion of adding the \n at append time. :-) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Optional parameter passing
Hello all, I have used *args and **kwargs to have a function accept optional parameters, but is there a lazy way to optionally pass parameters? For example, say my script can accept a number of parameters for a database connection, such as user, password, and database name. The function that makes the MySQL call has a default for user, say "root". So if the user didn't pass in a value for the user option, I don't want to pass it to the function doing the MySQL call. I don't want to have to do: if options.user: do_mysql_query(user) else: do_mysql_query() and so on for each possible option. Is there a better way? Thanks, Sam ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Optional parameter passing
A small correction to my code, although the point was hopefully still clear: if options.user: do_mysql_query(options.user) else: do_mysql_query() From: wormwood_3 To: Python Tutorlist Sent: Monday, January 12, 2009 4:39:30 PM Subject: [Tutor] Optional parameter passing Hello all, I have used *args and **kwargs to have a function accept optional parameters, but is there a lazy way to optionally pass parameters? For example, say my script can accept a number of parameters for a database connection, such as user, password, and database name. The function that makes the MySQL call has a default for user, say "root". So if the user didn't pass in a value for the user option, I don't want to pass it to the function doing the MySQL call. I don't want to have to do: if options.user: do_mysql_query(user) else: do_mysql_query() and so on for each possible option. Is there a better way? Thanks, Sam ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Optional parameter passing
You know, that's a great idea :-) Just using options I could keep my initial checks the same (e.g. making sure needed options were included by looking at options.foo) and pass it along without adding much to the query function. Thanks! ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins From: Kent Johnson To: wormwood_3 Cc: Python Tutorlist Sent: Monday, January 12, 2009 5:09:31 PM Subject: Re: [Tutor] Optional parameter passing On Mon, Jan 12, 2009 at 4:39 PM, wormwood_3 wrote: > Hello all, > > I have used *args and **kwargs to have a function accept optional > parameters, but is there a lazy way to optionally pass parameters? For > example, say my script can accept a number of parameters for a database > connection, such as user, password, and database name. The function that > makes the MySQL call has a default for user, say "root". So if the user > didn't pass in a value for the user option, I don't want to pass it to the > function doing the MySQL call. I don't want to have to do: > > if options.user: > do_mysql_query(user) > else: > do_mysql_query() > > and so on for each possible option. Is there a better way? You would have to put the options into a collection, a list or dict or class. You can use *args and **kwargs at the point of call as well as in a function definition. What about having the defaults in the options object and just passing it to the function? do_mysql_query(options) or pass the options and have the function use code like user = options.user or 'root' Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Ip address
Hello, This is definitely possible. It's more a matter of system and OS configuration than Python though, so you might want to check out some Linux forums ( http://www.linuxforums.org/ ) for additional help. In short, I think the simplest would be: Have 3 separate network interfaces in your physical box, have your router provide one of the three addresses to each. Then you could configure the client to only connect through its associated interface/IP. If this sort of setup wouldn't work, please tell us more about your configuration between your client and your network. -Sam ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins From: W W To: irimia.sule...@unknownsoftware.ro Cc: tutor@python.org Sent: Monday, January 19, 2009 10:27:07 AM Subject: Re: [Tutor] Ip address On Mon, Jan 19, 2009 at 8:11 AM, Irimia, Suleapa wrote: Hello list, I am new to python and i have a question. I managed to build a client which connect to an website and grab things from there. I run this client on a linux box, which have multiple ip address. What do i want is to run 3 clients and each one to use different ip address to access that website. Anyone can point me to some documentation about how this can be done? I'm not 100% sure if I understand what you're saying, so let me clarify: You have one linux box, with 3 instances of your program running, and you want the server to see these as three unique IPs? I'm not aware of any way to do this without proxy servers or IP spoofing. HTH, Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn't. - Primo Levi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] dict() versus {}
When creating a list of dictionaries through a loop, I ran into a strange issue. I'll let the code talk: >>> l = 'i am a special new list'.split() >>> t = [] >>> for thing in l: ... t.append({thing: 1}) ... >>> t [{'i': 1}, {'am': 1}, {'a': 1}, {'special': 1}, {'new': 1}, {'list': 1}] This is what I expected. {} says to make a dictionary. Thing, not being quoted, is clearing a variable, which needs to be evaluated and used as the key. >>> t = [] >>> for thing in l: ... t.append(dict(thing=1)) ... >>> t [{'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}] This was what threw me. Why would the dict() function not evaluate thing? How can it take it as a literal string without quotes? Thanks for any insight, Sam ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict() versus {}
Hmm, looked through the latest docs, in the sections on dictionary types, don't see examples that point to this case well. Can you link to what you had in mind? ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins From: bob gailer To: wormwood_3 Cc: Python Tutorlist Sent: Wednesday, January 21, 2009 11:02:35 PM Subject: Re: [Tutor] dict() versus {} wormwood_3 wrote: When creating a list of dictionaries through a loop, I ran into a strange issue. I'll let the code talk: >>> l = 'i am a special new list'.split() >>> t = [] >>> for thing in l: ... t.append({thing: 1}) ... >>> t [{'i': 1}, {'am': 1}, {'a': 1}, {'special': 1}, {'new': 1}, {'list': 1}] This is what I expected. {} says to make a dictionary. Thing, not being quoted, is clearing a variable, which needs to be evaluated and used as the key. >>> t = [] >>> for thing in l: ... t.append(dict(thing=1)) ... >>> t [{'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}] This was what threw me. Why would the dict() function not evaluate thing? How can it take it as a literal string without quotes? I suggest you look dict up in the Python documentation. There it shows the result you got as an example. When in doubt read the manual. -- Bob Gailer Chapel Hill NC 919-636-4239___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict() versus {}
dict( [arg]) Return a new dictionary initialized from an optional positional argument or from a set of keyword arguments. If no arguments are given, return a new empty dictionary. If the positional argument arg is a mapping object, return a dictionary mapping the same keys to the same values as does the mapping object. But why doesn't the optional positional argument arg in this case, not being a mapping type, get evaluated?: dict(thing=1) And even if it makes sense for it not to be evaluated, wouldn't it be better for dict() to complain that it didn't get a string or an int as it expects for a keyword argument? Maybe I am missing the use case, so far it just seems strange to force the keyword to a string. -Sam From: bob gailer To: wormwood_3 Cc: Python Tutorlist Sent: Wednesday, January 21, 2009 11:25:12 PM Subject: Re: [Tutor] dict() versus {} wormwood_3 wrote: Hmm, looked through the latest docs, in the sections on dictionary types, don't see examples that point to this case well. Can you link to what you had in mind? 2.1 Built-in Functions ... dict( [mapping-or-sequence]) ... these all return a dictionary equal to {"one": 2, "two": 3}: ... dict(one=2, two=3) ________ From: bob gailer To: wormwood_3 Cc: Python Tutorlist Sent: Wednesday, January 21, 2009 11:02:35 PM Subject: Re: [Tutor] dict() versus {} wormwood_3 wrote: When creating a list of dictionaries through a loop, I ran into a strange issue. I'll let the code talk: >>> l = 'i am a special new list'.split() >>> t = [] >>> for thing in l: ... t.append({thing: 1}) ... >>> t [{'i': 1}, {'am': 1}, {'a': 1}, {'special': 1}, {'new': 1}, {'list': 1}] This is what I expected. {} says to make a dictionary. Thing, not being quoted, is clearing a variable, which needs to be evaluated and used as the key. >>> t = [] >>> for thing in l: ... t.append(dict(thing=1)) ... >>> t [{'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}] This was what threw me. Why would the dict() function not evaluate thing? How can it take it as a literal string without quotes? I suggest you look dict up in the Python documentation. There it shows the result you got as an example. When in doubt read the manual. -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer Chapel Hill NC 919-636-4239___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor