Re: [Tutor] '__name__' == '__main__'

2012-02-20 Thread Dave Angel

On 02/20/2012 11:55 PM, Michael Lewis wrote:

I am back to being confused. I just tried running the module without first
importing it, and it worked just fine. How do I do this properly to where
the module only runs if I import it?

I'd still like a definition of "just fine."  But anyway, your first 
problem is you somehow added extra quotes to a line that you had right 
in an earlier version.  Comparing two unequal literal strings will never 
prove true.  So the dependent clause never runs.




if "__name__" == '__main__':
 GetUserInput()


that should be
if __name__ == "__main__":
 do something
else:
do something else

You'll have to decide which one you want to call GetUserInput() from.

You'll then have to decide if you really want to throw away the result.  
Just because IDLE dumps something out doesn't mean it'll get printed out 
when a person runs the program from a commandline.


At the very least, you want
print GetUserInput()

although most of my earlier arguments might apply someday, once you're 
past this stuff.  Try inserting temporary prints into the code, so you 
can tell what has executed and what has not.




--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] '__name__' == '__main__'

2012-02-20 Thread Christian Witts

On 2012/02/21 06:55 AM, Michael Lewis wrote:
I am back to being confused. I just tried running the module without 
first importing it, and it worked just fine. How do I do this properly 
to where the module only runs if I import it?


Code:

def MultiplyText(text, multiplier):
'''Recieve a S & int. For digits in S, multiply by multiplier and 
return updated S.'''
return ' '.join(str(int(num) * multiplier) if num.isdigit() else 
num for num in text)



def GetUserInput():
'''Get S & multiplier. Test multiplier.isdigit(). Call 
MultiplyText(text, multiplier)'''

text = raw_input('Enter some text: ')
while True:
multiplier = raw_input('Enter a multiplier: ')
try:
multiplier = int(multiplier)
break
except ValueError:
continue
return MultiplyText(text.split(), multiplier)


if "__name__" == '__main__':
GetUserInput()

What I did in IDLE:

>>>
>>> GetUserInput()
Enter some text: 4 times
Enter a multiplier: 2
'8 times'
>>>

--
Michael J. Lewis
mjole...@gmail.com 
415.815.7257



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


If you change '__main__' to the name of the file, without the extension, 
it will match __name__.

Alternatively, you can have an else after your `__name__ == '__main__'`

$ cat t1.py
if __name__ == '__main__':
print 'Executed from __main__'

if __name__ == 't1':
print 'Executed by import from t2'

$ cat t2.py
import t1

$ python t1.py
Executed from __main__

$ python t2.py
Executed by import from t2

$ cat t3.py
if __name__ == '__main__':
print 'Executed from __main__'
else:
print 'Executed by import'

$ cat t4.py
import t3

$ python t3.py
Executed from __main__

$ python t4.py
Executed by import

--

Christian Witts
Python Developer

COMPUSCAN | CONFIDENCE IN CREDIT

Telephone : +27 21 888 6000
National Call Centre : 0861 51 41 31
Fax : +27 21 413 2424
Email : cwi...@compuscan.co.za 
Website: www.compuscan.co.za 

Would you like to hear more from us? Register here and receive our 
newsletters and other business communications. 



*/NOTE:/* This e-mail (including attachments) is subject to the 
disclaimer published at our website 
.
If you cannot access the disclaimer, request it from 
email.disclai...@compuscan.co.za 
 or 0861 514131.

National Credit Regulator Credit Bureau Registration No. NCRCB6
/Please consider the environment before printing/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] '__name__' == '__main__'

2012-02-20 Thread Michael Lewis
I am back to being confused. I just tried running the module without first
importing it, and it worked just fine. How do I do this properly to where
the module only runs if I import it?

Code:

def MultiplyText(text, multiplier):
'''Recieve a S & int. For digits in S, multiply by multiplier and
return updated S.'''
return ' '.join(str(int(num) * multiplier) if num.isdigit() else num
for num in text)


def GetUserInput():
'''Get S & multiplier. Test multiplier.isdigit(). Call
MultiplyText(text, multiplier)'''
text = raw_input('Enter some text: ')
while True:
multiplier = raw_input('Enter a multiplier: ')
try:
multiplier = int(multiplier)
break
except ValueError:
continue
return MultiplyText(text.split(), multiplier)


if "__name__" == '__main__':
GetUserInput()

What I did in IDLE:

>>>
>>> GetUserInput()
Enter some text: 4 times
Enter a multiplier: 2
'8 times'
>>>

-- 
Michael J. Lewis

mjole...@gmail.com
415.815.7257
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __name__=='__main__'

2012-02-20 Thread Dave Angel

On 02/20/2012 10:07 PM, Michael Lewis wrote:

Now that I am better understanding '__name__'=='__main__', I need to get
advice on one last part. Since you put this in the file as an if statement,
some instruction must come after. What do you suggest putting after this
statement/is that piece of code ever put into action?
In my example below, I've done a few tests like putting a print statement
under '__name__'=='__main__' and it isn't printed. I am not sure what to
put in this code block.

def MultiplyText(text, multiplier):
 '''Recieve a S&  int. For digits in S, multiply by multiplier and
return updated S.'''
 return ' '.join(str(int(num) * multiplier) if num.isdigit() else num
for num in text)


def GetUserInput():
 '''Get S&  multiplier. Test multiplier.isdigit(). Call
MultiplyText(text, multiplier)'''
 text = raw_input('Enter some text: ')
 while True:
 multiplier = raw_input('Enter a multiplier: ')
 try:
 multiplier = int(multiplier)
 break
 except ValueError:
 continue
 return MultiplyText(text.split(), multiplier)


if '__name__' == '__main__':
 GetUserInput()



I don't see any print statement in the if-clause.  In fact I don't see 
any prints in the entire program.  Just how do you expect to know if it 
even ran?


What IS there is a call to GetUserInput().  But for some reason that 
function doesn't return user-input.  Why not?  A function should nearly 
always take some arguments, and return a result.  And its name should 
reflect what it's going to do


Besides that, you do return a value from GetUserInput()., but never use 
that value.  So what's the point?


Probably what belongs in the if clause is a call to main().  Then you 
need to write main() function, to call GetUserInput() and save the value 
returned (a tuple of string and int, my choice).  Then it'd call 
MultiplyText with those two, and with this mysterious num value that you 
conjured up in MultiplyText.


Once you've factored the two functions into separately called entities, 
you have a chance of debugging your multiple mistakes still remaining. 
  In between the two function calls in main(), you can actually print 
out your intermediate results, and see if they look reasonable to you.



In fact, while testing, you just might want to try calling 
MultiplyText() with whatever literal arguments make sense.  And print 
the result you get.  A few of those tests, and you might get comfortable 
with  the function.


--

DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __name__=='__main__'

2012-02-20 Thread Michael Lewis
Now that I am better understanding '__name__'=='__main__', I need to get
advice on one last part. Since you put this in the file as an if statement,
some instruction must come after. What do you suggest putting after this
statement/is that piece of code ever put into action?
In my example below, I've done a few tests like putting a print statement
under '__name__'=='__main__' and it isn't printed. I am not sure what to
put in this code block.

def MultiplyText(text, multiplier):
'''Recieve a S & int. For digits in S, multiply by multiplier and
return updated S.'''
return ' '.join(str(int(num) * multiplier) if num.isdigit() else num
for num in text)


def GetUserInput():
'''Get S & multiplier. Test multiplier.isdigit(). Call
MultiplyText(text, multiplier)'''
text = raw_input('Enter some text: ')
while True:
multiplier = raw_input('Enter a multiplier: ')
try:
multiplier = int(multiplier)
break
except ValueError:
continue
return MultiplyText(text.split(), multiplier)


if '__name__' == '__main__':
GetUserInput()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __name__=='__main__'

2012-02-20 Thread Dave Angel

On 02/20/2012 06:46 PM, Michael Lewis wrote:

Hi everyone,

I am having some trouble understanding how to use __name__== '__main__'.
Can you please give me some insight? Also, to use this, it needs to be
within a function? Do you typically just throw it in your very last
function or create a separate function just for this? I at first put it
outside and after all my functions but got the error below and then put it
inside my last function and the program ran. (side note, I have an error in
my return for MultiplyText that I am still trying to work out, so you can
ignore that part).

Code:


Nothing magic about that line  (if __name__ == "__main__").  You need to 
understand the components, and why you're using them.


As others have pointed out, your original error was simply due to a 
typo.  Every module has a __name__ attribute.  If a module is imported, 
that attribute is set to the module's name.  But if it is run directly 
(eg.  if you runpython   myfile.py)  then it has the name "__main__" 
rather than "myfile".


So if you fix the typo, and still have an error, then explain what you 
run, and how, rather than saying something like "code now runs WITHOUT 
me explicitly importing it."


For example:

my source file is called  X.py

which looks like:

When I run the Linux commandline:

python X

I get the following results.

(and yes,  I know the above won't work.  I'm trying to force you to tell 
us what you really did)




--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __name__=='__main__'

2012-02-20 Thread Robert Sjoblom
> I am having some trouble understanding how to use __name__== '__main__'. Can
> you please give me some insight?

if __name__ == '__main__': allows you to specify code that will only
be run if you run the actual script it's in; anything in the if block
won't be run if you import the module.

>Also, to use this, it needs to be within a
> function? Do you typically just throw it in your very last function or
> create a separate function just for this? I at first put it outside and
> after all my functions but got the error below and then put it inside my
> last function and the program ran. (side note, I have an error in my return
> for MultiplyText that I am still trying to work out, so you can ignore that
> part).

No, it doesn't have to be in a function, but if you wish you can have
it in a function main() or similar. The reason why you're getting an
error isn't because it's outside a function, it's because you're not
using the correct name:
> Traceback (most recent call last):
>   File "C:/Python27/Homework/Homework5_1.py", line 24, in 
> if __name == '__main__':
> NameError: name '__name' is not defined
As it says in the traceback, '__name' is not defined. It should be '__name__'.

> def GetUserInput():
>     '''Get S & multiplier. Test multiplier.isdigit(). Call
> MultiplyText(text, multiplier)'''
>     while True:
>         text = raw_input('Enter some text: ')
>         multiplier = raw_input('Enter a multiplier: ')
>         try:
>             multiplier.isdigit()
>             break
>         except ValueError:
>             continue
>     new_text = MultiplyText(text, multiplier)
>     return new_text
>
>     if __name == '__main__':
>         print GetUserInput()

You shouldn't have the "if '__name__' " block in any of your functions
(except possibly main()), it looks bad. I'm unsure if it'll affect
things when you import functions, but on the off-chance that it does,
you shouldn't do it.

A short example:
critter.py:
class Critter(object):
"""A virtual pet."""
def __init__(self):
print("A new critter has been born!")

def talk(self):
print("Hi, I'm an instance of class Critter.")

if __name__ == '__main__':
crit1 = Critter()
crit2 = Critter()

crit1.talk()
crit2.talk()

If I run this from critter.py, I will get the following output:
A new critter has been born!
A new critter has been born!
Hi, I'm an instance of class Critter.
Hi, I'm an instance of class Critter.

However, if I create a critter_import.py and inside it import my Critter class:

from critter import Critter

if __name__ == '__main__':
print("This is critter_import.py")

The output will only be:
This is critter_import.py

If I subsequently create critters in critter_import.py, I will indeed
get the output that we see in critter.py, but that's because of class
behaviour and not because the if '__name__' block in critter.py
executes.

I might have made a mess of explaining things. In short: if '__name__'
== '__main__': only happens if you run the actual file, whereas it
won't happen if you import things from the file.
-- 
best regards,
Robert S.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __name__=='__main__'

2012-02-20 Thread Michael Lewis
Thanks. I did end up catching those, but to be fair to all the others, I
did ask that they ignore that issue as I was still working through it on my
own.

On Mon, Feb 20, 2012 at 4:55 PM, bob gailer  wrote:

> No one else has caught another problem. I comment on it below:
>
>
> On 2/20/2012 6:46 PM, Michael Lewis wrote:
>
>> Hi everyone,
>>
>> I am having some trouble understanding how to use __name__== '__main__'.
>> Can you please give me some insight? Also, to use this, it needs to be
>> within a function? Do you typically just throw it in your very last
>> function or create a separate function just for this? I at first put it
>> outside and after all my functions but got the error below and then put it
>> inside my last function and the program ran. (side note, I have an error in
>> my return for MultiplyText that I am still trying to work out, so you can
>> ignore that part).
>>
>> Code:
>>
>> '''homework 5_1'''
>>
>> def MultiplyText(text, multiplier):
>>'''Recieve a S. For digits in S, multiply by multiplier and return
>> updated S.'''
>>for num in text:
>>return ''.join(str(int(num) * multiplier) if num.isdigit() else
>> num for num in text)
>>
> This will fail, as multiplier is a string.
>
>
>>
>> def GetUserInput():
>>'''Get S & multiplier. Test multiplier.isdigit(). Call
>> MultiplyText(text, multiplier)'''
>>while True:
>>text = raw_input('Enter some text: ')
>>multiplier = raw_input('Enter a multiplier: ')
>>try:
>>multiplier.isdigit()
>>
> multiplier.isdigit() returns True or False. It will not raise an exception!
>
> break
>>except ValueError:
>>continue
>>new_text = MultiplyText(text, multiplier)
>>return new_text
>>
>>if __name == '__main__':
>>print GetUserInput()
>>
>>  To fix both problems replace
> multiplier.isdigit()
> with
> multiplier = int(multiplier)
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
>
>


-- 
Michael J. Lewis

mjole...@gmail.com
415.815.7257
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Scribes with python debugger

2012-02-20 Thread Tahir Hafiz
Does anyone know of a plugin or some other way to run python code in the 
scribes text editor in a similar way to Idle which has a debugger?

Thanks,
Tahir


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __name__=='__main__'

2012-02-20 Thread bob gailer

No one else has caught another problem. I comment on it below:

On 2/20/2012 6:46 PM, Michael Lewis wrote:

Hi everyone,

I am having some trouble understanding how to use __name__== 
'__main__'. Can you please give me some insight? Also, to use this, it 
needs to be within a function? Do you typically just throw it in your 
very last function or create a separate function just for this? I at 
first put it outside and after all my functions but got the error 
below and then put it inside my last function and the program ran. 
(side note, I have an error in my return for MultiplyText that I am 
still trying to work out, so you can ignore that part).


Code:

'''homework 5_1'''

def MultiplyText(text, multiplier):
'''Recieve a S. For digits in S, multiply by multiplier and return 
updated S.'''

for num in text:
return ''.join(str(int(num) * multiplier) if num.isdigit() 
else num for num in text)

This will fail, as multiplier is a string.



def GetUserInput():
'''Get S & multiplier. Test multiplier.isdigit(). Call 
MultiplyText(text, multiplier)'''

while True:
text = raw_input('Enter some text: ')
multiplier = raw_input('Enter a multiplier: ')
try:
multiplier.isdigit()

multiplier.isdigit() returns True or False. It will not raise an exception!

break
except ValueError:
continue
new_text = MultiplyText(text, multiplier)
return new_text

if __name == '__main__':
print GetUserInput()


To fix both problems replace
multiplier.isdigit()
with
multiplier = int(multiplier)

--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 96, Issue 83

2012-02-20 Thread Michael Lewis
> On Mon, Feb 20, 2012 at 6:46 PM, Michael Lewis  wrote:
> >  I at first put it outside and after all my functions but got the error
> below
>
> That's the right place for it, you just spelled it wrong.
>
> > and then put it inside my last function and the program ran.
>
> That's not the right place for it.  Your program ran, but only because
> nothing ever called your GetUserInput() function any more.


Now I am confused. I fixed the spelling and put it outside all my
functions; however, the code now runs WITHOUT me explicitly importing it.

def MultiplyText(text, multiplier):
'''Recieve a S & int. For digits in S, multiply by multiplier and
return updated S.'''
return ' '.join(str(int(num) * multiplier) if num.isdigit() else num
for num in text)


def GetUserInput():
'''Get S & multiplier. Test multiplier.isdigit(). Call
MultiplyText(text, multiplier)'''
text = raw_input('Enter some text: ')
while True:
multiplier = raw_input('Enter a multiplier: ')
try:
multiplier = int(multiplier)
break
except ValueError:
continue
return MultiplyText(text.split(), multiplier)


if __name__ == '__main__':
GetUserInput()

>
> > Traceback (most recent call last):
> > ? File "C:/Python27/Homework/Homework5_1.py", line 24, in 
> > ? ? if __name == '__main__':
> > NameError: name '__name' is not defined
>
> It needs to be spelled "__name__" -- that's two underscores on each side.
>
> --
> Jerry
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ssh from Windows to a Solaris server

2012-02-20 Thread Steven D'Aprano

Alan Gauld wrote:

On 20/02/12 21:17, Do Nguyen (donguye) wrote:


command1 = "plink -ssh -pw myPassword myUserName@myServerIP"
p1 = subprocess.Popen(command1)
p2 = subprocess.Popen('ls')

I could verify that command1 was executed successfully, ie. the ssh to
myServer worked, but command2 was treated locally in the Windows


Yes, because you created a new subprocess.Popen object.
If you want to communicate with ssh in the first subprocess you need  to 
send commands to stdin on p1. There are lots of examples how to 
read/write to stdin/out in the subprocess documentation. (I'm assuming 
that plink stays open, I've never used it so don't know)


I don't know about plink, but the right way to run commands on a remote 
machine using ssh is to give those commands to ssh as an argument. E.g.


http://linuxers.org/howto/how-run-commands-remote-machine-using-ssh
http://systemsboy.com/2006/07/send-remote-commands-via-ssh.html




--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __name__=='__main__'

2012-02-20 Thread Alan Gauld

On 20/02/12 23:46, Michael Lewis wrote:


I am having some trouble understanding how to use __name__== '__main__'.
Can you please give me some insight?


Others have told you how to fix it. The insight is that when
you import a python file its __name__ attribute is set to the module 
name. When you run it as the top level script its __name__ is set to 
__main__.


So you want to create an 'if' test, and it can be anywhere in your code 
there is nothing special about it, but usually it's at the bottom of the 
file and looks like:


def moduleFunc()
   # this is the code that gets used as a module

def anotherFunc():
   #and so is this

def main():
   # put your main program code here
   # it doesn't get run if the file
   # is imported
   #   - unless the user chooses to explicitly of course!

if __name__ == "__main__":
   main()
# or call it test() if the file is only expected to be used as a module

HTH,

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __name__=='__main__'

2012-02-20 Thread Alan Gauld

On 20/02/12 23:46, Michael Lewis wrote:


it inside my last function and the program ran. (side note, I have an
error in my return for MultiplyText that I am still trying to work out,


You can remove the outer for loop, you are already looping over text
inside the generator expression.

HTH,

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ssh from Windows to a Solaris server

2012-02-20 Thread Alan Gauld

On 20/02/12 21:17, Do Nguyen (donguye) wrote:


command1 = "plink -ssh -pw myPassword myUserName@myServerIP"
p1 = subprocess.Popen(command1)
p2 = subprocess.Popen('ls')

I could verify that command1 was executed successfully, ie. the ssh to
myServer worked, but command2 was treated locally in the Windows


Yes, because you created a new subprocess.Popen object.
If you want to communicate with ssh in the first subprocess you need  to 
send commands to stdin on p1. There are lots of examples how to 
read/write to stdin/out in the subprocess documentation. (I'm assuming 
that plink stays open, I've never used it so don't know)


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __name__=='__main__'

2012-02-20 Thread Steven D'Aprano

Michael Lewis wrote:


Error I got when __name == ' __main__' was outside of any function:

Traceback (most recent call last):
  File "C:/Python27/Homework/Homework5_1.py", line 24, in 
if __name == '__main__':
NameError: name '__name' is not defined



You have misspelled __name__ as __name.


--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __name__=='__main__'

2012-02-20 Thread Jerry Hill
On Mon, Feb 20, 2012 at 6:46 PM, Michael Lewis  wrote:
>  I at first put it outside and after all my functions but got the error below

That's the right place for it, you just spelled it wrong.

> and then put it inside my last function and the program ran.

That's not the right place for it.  Your program ran, but only because
nothing ever called your GetUserInput() function any more.

> Traceback (most recent call last):
>   File "C:/Python27/Homework/Homework5_1.py", line 24, in 
>     if __name == '__main__':
> NameError: name '__name' is not defined

It needs to be spelled "__name__" -- that's two underscores on each side.

-- 
Jerry
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] __name__=='__main__'

2012-02-20 Thread Michael Lewis
Hi everyone,

I am having some trouble understanding how to use __name__== '__main__'.
Can you please give me some insight? Also, to use this, it needs to be
within a function? Do you typically just throw it in your very last
function or create a separate function just for this? I at first put it
outside and after all my functions but got the error below and then put it
inside my last function and the program ran. (side note, I have an error in
my return for MultiplyText that I am still trying to work out, so you can
ignore that part).

Code:

'''homework 5_1'''

def MultiplyText(text, multiplier):
'''Recieve a S. For digits in S, multiply by multiplier and return
updated S.'''
for num in text:
return ''.join(str(int(num) * multiplier) if num.isdigit() else num
for num in text)


def GetUserInput():
'''Get S & multiplier. Test multiplier.isdigit(). Call
MultiplyText(text, multiplier)'''
while True:
text = raw_input('Enter some text: ')
multiplier = raw_input('Enter a multiplier: ')
try:
multiplier.isdigit()
break
except ValueError:
continue
new_text = MultiplyText(text, multiplier)
return new_text

if __name == '__main__':
print GetUserInput()

Error I got when __name == ' __main__' was outside of any function:

Traceback (most recent call last):
  File "C:/Python27/Homework/Homework5_1.py", line 24, in 
if __name == '__main__':
NameError: name '__name' is not defined

-- 
Michael J. Lewis
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] arrays, while loops

2012-02-20 Thread Ricardo Araoz
El 20/02/12 13:42, Dave Angel escribió:
> I'm not sure who you are, but you forgot to include the list. 
> Therefore I'll forward this to the list, and add my comments about
> your suggestions.
>
> On 02/20/2012 11:31 AM, Ricardo Araoz wrote:
>> Untested :
>> while True:
>>  try:
>>  amounts[index] = int (input ("Enter an amount: "))
>>  amtinput = int (amounts)
>
> int() won't operate on a list.  Probably you meant it to operate on a
> string, but there are no strings in it, it's a list of ints.
>
>

sorry I meant :

amtinput = int (input ("Enter an amount: "))
amounts[index] = amtinput




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ssh from Windows to a Solaris server

2012-02-20 Thread Evert Rol
> I’m a newbie in python programming …
>  
> I wrote the followings to ssh from Windows to a Solaris server:
>  
> command1 = "plink -ssh -pw myPassword myUserName@myServerIP"
> p1 = subprocess.Popen(command1)
> p2 = subprocess.Popen('ls')
>  
> I could verify that command1 was executed successfully, ie. the ssh to 
> myServer worked, but command2 was treated locally in the Windows and was not 
> executed on the server.

As far as I know subprocess, you create a completely new shell environment 
underneath for every process.
So essentially, you are now opening a shell, typing your ssh command, closing 
the shell (and thus the ssh session), and then create a new shell to type 'ls'.
You can put the commands in a single shell script and run that through 
subprocess. 
With ssh, it could be somewhat easier:

>>> command1 = "plink -ssh -pw myPassword myUserName@myServerIP ls"
>>> p1 = subprocess.Popen(command1)

So, you can just put the command you want to run at the end of the ssh command.
But, I don't know if plink allows that: I simply don't know plink. If it 
doesn't work this way, there may be an option for it though.
(Can't say I like the idea it lets you specify the password on the command 
line, but hey.)

Cheers,

  Evert


>  The error was as follows:
>  
> Traceback (most recent call last):
>   File "C:\Python32\ProgramFiles\test-paramiko.py", line 42, in 
> p2 = subprocess.Popen('ls')
>   File "C:\Python32\lib\subprocess.py", line 741, in __init__
> restore_signals, start_new_session)
>   File "C:\Python32\lib\subprocess.py", line 960, in _execute_child
> startupinfo)
> WindowsError: [Error 2] The system cannot find the file specified
>  
> Any hint ?  I am using python 3.2 and did try to install paramiko but could 
> not make it works.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] ssh from Windows to a Solaris server

2012-02-20 Thread Do Nguyen (donguye)
I'm a newbie in python programming ...

 

I wrote the followings to ssh from Windows to a Solaris server:

 

command1 = "plink -ssh -pw myPassword myUserName@myServerIP"

p1 = subprocess.Popen(command1)

p2 = subprocess.Popen('ls')

 

I could verify that command1 was executed successfully, ie. the ssh to
myServer worked, but command2 was treated locally in the Windows and was
not executed on the server.  The error was as follows:

 

Traceback (most recent call last):

  File "C:\Python32\ProgramFiles\test-paramiko.py", line 42, in 

p2 = subprocess.Popen('ls')

  File "C:\Python32\lib\subprocess.py", line 741, in __init__

restore_signals, start_new_session)

  File "C:\Python32\lib\subprocess.py", line 960, in _execute_child

startupinfo)

WindowsError: [Error 2] The system cannot find the file specified

 

Any hint ?  I am using python 3.2 and did try to install paramiko but
could not make it works.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What made Python differ from other Languages?

2012-02-20 Thread Mark Lawrence

On 20/02/2012 16:43, Sunil Tech wrote:

*I am Beginner (very little i know), i want to know what are new things i
can find in Python.*




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


This sums up the Python philosophy.

C:\Users\Mark\cpython\PCbuild>py -3.2 -c "import this"
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] arrays, while loops

2012-02-20 Thread bob gailer
Thanks for your response. Please always reply-all so a copy goes to the 
tutor list. I'm cc'ing this to that list.


On 2/19/2012 10:13 AM, Deborah Knoll wrote:
This is  for a class - I wasn't trying to hide that fact. I didn't 
want someone to write this for me,


I understand and appreciate that. I was commenting more to the other 
list responders.
just give some help so I could learn, which I did get. As for this 
being too advanced, it's a beginner Program Logic course and I think 
the code is suppose to be written fairly simply
Something got derailed - the question you posed requires prior learning; 
your attempts don't show that you had that learning.

- it's an online course which makes it harder but I am learning.

Would you provide a link to the course so we could see it?
I didn't know of your policy but from now on I will keep it to more 
simple quesions instead of the whole program.
That's always a good idea. Your program wasn't that big. But there was 
so much "wrong" with it - that made me question the relevance of the 
assignment.
I do want to say thanks to everyone for the help - I'm excited to be 
learning more.


I like hearing that. Please also note that we like to see our answers 
interspersed with your comments. Please do the same when replying to us. 
Also delete any part of the email to which you reply that is not 
relevant to your reply. And please reply. We actually enjoy helping.


Some "generic" comments:

1) when working with a fixed-lengthobject (e.g.a list of length 7) start 
by defining a constant and then refer to that constant. e.g.:

SIZE = 7
amounts = [0]*SIZE

2) when obtaining user input that should be a numberbut might not it is 
essential that you (in one of several ways) anticipate and handle "bad" 
input. One way is to use isdigit() to ensure that all the characters 
entered are digits. This assumes that inputs with a decimal point are 
not allowed, but your problem does not rule them out - it's just harder 
to detect "bad" input if decimal points are allowed. So let's assume 
integers only for now. Let's create a function whose job is to get an 
integer and keep trying till the user enters one. We might as well throw 
in the range check as this is the best place to do that.


def getInteger():
  while True: # this keeps asking for in put until user get it right
x = raw_input ("Enter an integer amount between 1 and 1000: ") # 
spell out all the requirements

if x.isdigit():
  y = int(x)
  if y >= 1 and y <= 1000:
return y
  else:
print(x + " is not between 1 and 1000")
else:
  print(x + " is not an integer")
print getInteger()

I wrote this as a stand-alone program so I could test it by itself, and 
added the last line to actually run the test.


Once the getInteger function is working then you can add the other logic.

Another approach is using try and except as others have mentioned. 
However I doubt whether your course has introduced exception handling.



--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What made Python differ from other Languages?

2012-02-20 Thread Joel Goldstick
I think python is great because:

1. it is very easy to read other peoples code and understand it
2. without being cryptic,  you can do a lot of logic with very little
typing because the language was well conceived -- it didn't get added
on to in an ad hoc way that (IMO) php has been.
3. there is an active great community so you can learn about what you
don't understand here or stack overflow, or meetups, etc.
-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What made Python differ from other Languages?

2012-02-20 Thread Alan Gauld

On 20/02/12 16:43, Sunil Tech wrote:

/I am Beginner (very little i know), i want to know what are new things
i can find in Python./



There is a wealth of detail on this on the Python web site.

Try the FAQ for a starter:

http://docs.python.org/faq/general.html


And the success stories for more concrete evidence:

http://www.python.org/about/success/

And for simple advocacy try:

http://www.python.org/about/quotes/

HTH,
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What made Python differ from other Languages?

2012-02-20 Thread Dave Angel

On 02/20/2012 12:16 PM, Darin Lawson Hosking wrote:

Sunil

Here is great way to get started on Python, even though the first few
videos are setting up a Linux virtual machine to develop within, he quickly
moves onto Python.

http://www.youtube.com/user/RaspberryPiTutorials/videos

Darin



The above was sent as a private email to me.  And I wasn't even the one 
he apparently was addressing it to.


--

DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What made Python differ from other Languages?

2012-02-20 Thread Carlos Daniel Ruvalcaba Valenzuela
Just to express my opinion on the matter which is cannot stress
enough, Python does not only come with the kitchen sink, it comes with
a very useful kitchen sink, most of the standard library modules are
very useful and not just random additions, I take for example a
comparision against C#, I had to process some XML files from email
attachments, you cannot do this in C# out of the box (there is no pop3
or imap library, only XML handling), in python there is buildin pop3
module and xml processing, thus the time I invest on looking for a C#
pop3 library (or creating my own) I already got it done with python
and core modules, plus there is a standard way of searching, getting
and installing modules which makes developing with python a bliss.

To sum up, python is also different because there is a large amount of
libraries and code to harness, making it easy to work with, fast to
develop and flexible enough to cover different application types.

Regards,
Carlos Daniel Ruvalcaba Valenzuela
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What made Python differ from other Languages?

2012-02-20 Thread Dave Angel

On 02/20/2012 11:43 AM, Sunil Tech wrote:

*I am Beginner (very little i know), i want to know what are new things i
can find in Python.*




There are thousands of computer languages out there.  Nearly every 
feature has an analog out there somewhere.  So "what's new" really 
depends on what language you're comparing it with.


You said you're a beginner.  Is that a beginner at Python, a beginner at 
programming, a beginner with computers, what?


If I assume you're knowledgeable in C++, I'd point to this set of features:

1) No braces. You build program structure simply by proper indentation.

2) No declarations.  A "variable" can contain an integer at one moment, 
and a dictionary of name/value pairs a moment later.  The objects have 
the type, not the name.


3) Built in types for the most useful collections, tuple, list, 
dictionary, and set.  And a large set of operations that can readily be 
done on them.


4) Duck typing.  You can pass anything you like to a function, and the 
things you pass may not be subclasses of the expected types, but if the 
right operations (methods) are supported, it'll work.  So you can define 
a "file like" object to a function that's expecting a file, and it 
doesn't have to be derived from file, it just has to have the right 
methods, and the equivalent semantics.


5) The kitchen sink.  The standard library has an enormous collection of 
useful stuff, most of it written in python.  Most of the time, you don't 
need to look to third party libraries to accomplish your goal.


6) Easy debugging.  There are a number of IDE's available, but it's not 
hard to debug code without one.  And you can run code fragments from a 
standard interpreter, and examine data and define new functions on the 
fly.  The system also supports introspection, so even the simplest error 
message tells you a lot about what was happening.  And when you need to, 
you can examine any object in the system, stack frames, types of 
objects, and so on.


7) Easy testing.  You can define some sanity tests in your own source 
code, in a special string called a docstring.  That string can thus have 
two uses:  documentation for the reader of the code, and first line of 
tests to make sure the code meets those tests.


--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] What made Python differ from other Languages?

2012-02-20 Thread Sunil Tech
*I am Beginner (very little i know), i want to know what are new things i
can find in Python.*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] arrays, while loops

2012-02-20 Thread Dave Angel
I'm not sure who you are, but you forgot to include the list.  Therefore 
I'll forward this to the list, and add my comments about your suggestions.


On 02/20/2012 11:31 AM, Ricardo Araoz wrote:

El 20/02/12 00:00, Dave Angel escribió:

On 02/19/2012 07:01 PM, Deborah Knoll wrote:



A couple of  side notes...


Here is my new code:
amounts = [0 for index in range (7)]
myamt = len(amounts)


myamt = 7
amounts = [0 for index in range(myamt)][0 for index in range(myamt)]
 or even
amounts = [0] * myamt



def getnumbers():
  for index in range(myamt):
  amounts[index] = int (input ("Enter an amount: "))
  amtinput = int (amounts)



  while amtinput<   1 or amtinput>   1001:
  print ("Try again")



  amounts[index] = int (input ("Enter an amount: "))
  amtinput = int (amounts)
  amtinput.sort()
  amtinput.reverse()

Untested :
while True:
 try:
 amounts[index] = int (input ("Enter an amount: "))
 amtinput = int (amounts)


int() won't operate on a list.  Probably you meant it to operate on a 
string, but there are no strings in it, it's a list of ints.



 except ValueError:
 print ('Not an integer')
 if 1<= amtinput<= 1001:
 break
 else:
 print('Value must be between 1 and 1001')




--

DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor