Re: [Tutor] for vs while
On 9/28/07, James <[EMAIL PROTECTED]> wrote: > > > # doesn't work > for i in len( stuff ): > os.system( stuff[ i ] ) > j = i + 1 > print stuff[ j ] > > > Traceback (most recent call last): >File "", line 1, in > TypeError: 'int' object is not iterable > > What precisely causes this error? I come from a C background, and > while and for loops can be molded to do precisely the same thing; it > doesn't seem like this is the case in this scenario. You don't want to iterate through the length of the object (stuff), you want to iterate through the object itself. for i, j in stuff: os.system(i) print j and stuff would look like: stuff = [("cat /etc/passwd", "viewed /etc/passwd")] insert the pairs in tuples, rather than a list. cheers Josh ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] for vs while
On 9/28/07, Kent Johnson <[EMAIL PROTECTED]> wrote: > > > It's worth your time learning about Python data structures and for > loops. They are very powerful and useful and unlike anything built-in to > C. With a background in C you should find the official tutorial pretty > easy to read: > http://docs.python.org/tut/tut.html I found Dive Into Python (http://www.diveintopython.org) helpful as well coming from a primarily C background. cheers Josh ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Check if root
On 9/28/07, Robert Jackson <[EMAIL PROTECTED]> wrote: > > I'm trying to write a function that checks to see if the user that > is running the python script is 'root' (I'm obviously running this > Python program on Linux). Why not just use os.geteuid() ? import os if os.geteuid() != 0: print "You must be root to run this script." sys.exit(1) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Bound To Be A Typo
On Dec 17, 2007 12:16 PM, earlylight publishing < [EMAIL PROTECTED]> wrote: > > class Critter(object): > """A virtual pet""" > def ___init___(self, name): > print "A new critter has been born!" > > You're using 3 underscores before and after 'init'. The constructor for Python classes is '__init__' not '___init___' -- - http://stderr.ws/ "Insert pseudo-insightful quote here." - Some Guy ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Handling MySQLdb exceptions
On Dec 19, 2007 10:14 AM, Paul Schewietzek <[EMAIL PROTECTED]> wrote: > > Is there any way to handle this exception? As you can see, I already > tried it with _mysql_exceptions.OperationalError (the lines that are > commented out), but _mysql_exceptions is not defined to Python > > "OperationalError" is contained in the MySQLdb module and thus namespace, so you'll have to reference it like so: except MySQLdb.OperationalError: -- - http://stderr.ws/ "Insert pseudo-insightful quote here." - Some Guy ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting the type of a variable
On 10/27/06, Etrade Griffiths <[EMAIL PROTECTED]> wrote: Hi I want to check the type of a variable so that I know which format to use for printing egAlternatively, you could just cast it as a string. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor