Re: [Tutor] NameError: is defined
On Wed, Jul 20, 2011 at 4:37 AM, brandon w wrote: > ** > Hi > I am running Linux with Python 2.6.6. I have done lists, tuples, > dictionaries, etc. Now I want to move on to creating a "class". I keep > getting an error for everything I try. Here is the error: * > > NameError: name 'MyClass' is not defined* > > I had originally tried to create my own class by watching some video > tutorials. Nothing worked. Then from the python2.6-doc documentation I just > decided to copy and paste from the documentation. > > *class MyClass: > """A simple example class""" > i = 12345 > def f(self): > return 'hello world'* > > > *>>> MyClass > Traceback (most recent call last): > File "", line 1, in > NameError: name 'MyClass' is not defined* > > Still the same error. What am I doing wrong? > I tried this in gnome-terminal in a Python shell and using IDLE. > > First I do: *import myscript.py > *(no errors)* > * > Then I run: *MyClass* > (error) > > I try: n = *MyClass()* > (error) > > I try: > *MyClass.n > n.MyClass > i.MyClass > MyClass.i > i.MyClass() > f.MyClass > f.MyClass()* > > (nothing but errors) > You have to specify where MyClass lives. In this case it's in myscript.py. So you have to do: import myscript#Note: without .py n = myscript.MyClass() or: from myscript import MyClass n = MyClass() -- André Engels, andreeng...@gmail.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Basic question on spaces
On 19-Jul-11 20:39, Alexander Quest wrote: Hello; I'm a new student of Python using "Python Programming for Absolute Beginners" 3rd edition by Michael Dawson as my guide. This is a basic question regarding spaces. I'm not sure how to make it so spaces do not show up between variables and basic strings, particularly before commas and after dollar signs, as in the simple "tipper" program I have below. You don't want to use print with a comma-separated list of values, then. Your best bet would be the format string method, like this: print """ Okay, based on that bill, a 15% tip would be ${0}, and a 20% tip would be ${1}. """.format(percent15, percent20) -- Steve Willoughby / st...@alchemy.com "A ship in harbor is safe, but that is not what ships are built for." PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Basic question on spaces
Hello; I'm a new student of Python using "Python Programming for Absolute Beginners" 3rd edition by Michael Dawson as my guide. This is a basic question regarding spaces. I'm not sure how to make it so spaces do not show up between variables and basic strings, particularly before commas and after dollar signs, as in the simple "tipper" program I have below. #Tip program: calculates 15% and 20% tip for a given bill. bill = int(input("Hello! Welcome to the tipper program. \nWhat is the amount of " "your bill, in dollars please: ")) percent15 = bill * .15 percent20 = bill * .20 print("\nOkay, based on that bill, a 15% tip would be $", percent15, ", and \n" "a 20% tip would be $", percent20, ".") input("\n\nPress the enter key to exit.") As you can see, this is quite rudimentary; I have not discovered any special function that eliminates spaces yet, if such a function exits. The problem is, as stated above, unwanted spaces after both dollar signs, before the comma after '15.0' and before the period after '20.0." Apologies for asking such a basic question, but any help will be appreciated. -Alex ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] NameError: is defined
Hi I am running Linux with Python 2.6.6. I have done lists, tuples, dictionaries, etc. Now I want to move on to creating a "class". I keep getting an error for everything I try. Here is the error: * NameError: name 'MyClass' is not defined* I had originally tried to create my own class by watching some video tutorials. Nothing worked. Then from the python2.6-doc documentation I just decided to copy and paste from the documentation. *class MyClass: """A simple example class""" i = 12345 def f(self): return 'hello world'* *>>> MyClass Traceback (most recent call last): File "", line 1, in NameError: name 'MyClass' is not defined* Still the same error. What am I doing wrong? I tried this in gnome-terminal in a Python shell and using IDLE. First I do: *import myscript.py *(no errors)* * Then I run: *MyClass* (error) I try: n = *MyClass()* (error) I try: *MyClass.n n.MyClass i.MyClass MyClass.i i.MyClass() f.MyClass f.MyClass()* (nothing but errors) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Resend: Using pexpect to SCP files
On 19 July 2011 13:20, Johan Geldenhuys wrote: It works fine for me in an interactive idle session.. Have you tried this to see if works like this? Does the command work when run directly from the command line? > I am using pexpect in a script to SCP files to a inux server. > Here is a snippet from the code: > > def doScp(self, user, password, host, path, files): > > fNames = " ".join(files) > self.logger.log('Running command for %s' % fNames) > try: > > self.child = pexpect.spawn("scp %s %s@%s:%s"%(fNames, user, host, > path)) > # The script times out here: Add a print here to see what is actually send to scp (or log it to your logger). > i = self.child.expect(['assword:', r"yes/no"], timeout=30) > except: > self.logger.logException() > > if i==0: > self.logger.log('Sending password') > self.child.sendline(password) > elif i==1: > self.logger.log('Sending yes and password') > self.child.sendline("yes") > self.child.expect("assword:", timeout=30) > self.child.sendline(password) > try: > data = self.child.read() > self.logger.log(`data`) > except: > self.logger.logException() The above 5 lines are only run when i == 1, not sure if this was intended. > self.child.expect(PROMPT) > self.logger.log('Done with SCP') You never close the child so you *might*t have zombie processes around. Which might cause the server not to respond to you. Regardless It is always good to close so add self.child.close(). > This executes at the line " i = self.child.expect(['assword:', r"yes/no"], > timeout=30)". From what I can see using tcpdump on the linux side, the scp > traffic is going into the linux server, but it is not sending anything back. > Is there anything obvious wrong here and is there a way I can see the exact > command sent to out? > > The reason I chose to use pexpect is that is a pure Python method for doing > interactive sessions for scp. > Is there a different way of doing scp in a pure pythin self contained > module? Piramiko is not an option because I cannot install it on the device > I run my script on. It works fine for me with the below function. Br Sander def doScp(user,password, host, path, files): fNames = ' '.join(files) print fNames child = pexpect.spawn('scp %s %s@%s:%s' % (fNames, user, host,path)) print 'scp %s %s@%s:%s' % (fNames, user, host,path) i = child.expect(['assword:', r"yes/no"], timeout=30) if i == 0: child.sendline(password) elif i == 1: child.sendline("yes") child.expect("assword:", timeout=30) child.sendline(password) data = child.read() print data child.close() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Resend: Using pexpect to SCP files
Resend in text format Hi there all, I am using pexpect in a script to SCP files to a inux server. Here is a snippet from the code: def doScp(self, user, password, host, path, files): fNames = " ".join(files) self.logger.log('Running command for %s' % fNames) try: self.child = pexpect.spawn("scp %s %s@%s:%s"%(fNames, user, host, path)) # The script times out here: i = self.child.expect(['assword:', r"yes/no"], timeout=30) except: self.logger.logException() if i==0: self.logger.log('Sending password') self.child.sendline(password) elif i==1: self.logger.log('Sending yes and password') self.child.sendline("yes") self.child.expect("assword:", timeout=30) self.child.sendline(password) try: data = self.child.read() self.logger.log(`data`) except: self.logger.logException() self.child.expect(PROMPT) self.logger.log('Done with SCP') This executes at the line " i = self.child.expect(['assword:', r"yes/no"], timeout=30)". From what I can see using tcpdump on the linux side, the scp traffic is going into the linux server, but it is not sending anything back. Is there anything obvious wrong here and is there a way I can see the exact command sent to out? The reason I chose to use pexpect is that is a pure Python method for doing interactive sessions for scp. Is there a different way of doing scp in a pure pythin self contained module? Piramiko is not an option because I cannot install it on the device I run my script on. Thank for helping. Johan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Using pexpect to SCP files
Hi there all, I am using pexpect in a script to SCP files to a inux server. Here is a snippet from the code: def doScp(self, user, password, host, path, files): fNames = " ".join(files) self.logger.log('Running command for %s' % fNames) try: self.child = pexpect.spawn("scp %s %s@%s:%s"%(fNames, user, host, path)) i = self.child.expect(['assword:', r"yes/no"], timeout=30) except: self.logger.logException() if i==0: self.logger.log('Sending password') self.child.sendline(password) elif i==1: self.logger.log('Sending yes and password') self.child.sendline("yes") self.child.expect("assword:", timeout=30) self.child.sendline(password) try: data = self.child.read() self.logger.log(`data`) except: self.logger.logException() self.child.expect(PROMPT) self.logger.log('Done with SCP') This executes to the line in red and then times out. From what I can see using tcpdump on the linux side, the scp traffic is going into the linux server, but it is not sending anything back. Is there anything obvious wrong here and is there a way I can see the exact command sent to out? The reason I chose to use pexpect is that is a pure Python method for doing interactive sessions for scp. Is there a different way of doing scp in a pure pythin self contained module? Piramiko is not an option because I cannot install it on the device I run my script on. Thank for helping. Johan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing module and running
Hi David, On 19 July 2011 09:45, David Merrick wrote: > I want to install the first module > > http://code.google.com/p/python-nose/downloads/list > > What operating system? What version of Python? Even so, ignoring the OS and Python version issues for now (and noting that that may affect the comments below), I'll say that normally the easiest way to install Python packages is using the "easy_install" command which is part of the "setuptools" package, available here: http://pypi.python.org/pypi/setuptools#using-setuptools-and-easyinstall , by using the command: easy_install nose (This assumes that easy_install is on the search path, or that your current directory is the Python\Scripts folder already.) Alternatively you can download the tarball (tar.gz file) you want, extract it, then install it by changing into the folder you've extracted to and running the "setup.py" script with an "install" parameter: python setup.py install (Again, this assumes that python is on your environment/shell search PATH, and that your current folder is the root folder of the nose package, e.g. nose-1.0.0.) If you don't know how to extract/open tart.gz files, then install IZArc, available here: http://www.izarc.org/ Having said all that, you really should just "easy_install nose" or perhaps preferably "pip install nose", it's the easiest, not just for this package, but for any other packages you might care to install into your Python distribution... Regards, Walter -- Don't be a vampire (http://slash7.com/pages/vampires) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Installing module and running
I want to install the first module http://code.google.com/p/python-nose/downloads/list -- Dave Merrick merrick...@gmail.com Ph 03 3423 121 Cell 027 3089 169 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor