Re: [Tutor] entering text on web site

2012-06-14 Thread Alan Gauld

On 14/06/12 03:44, Benjamin Fishbein wrote:


etc. I guess Python didn't come standard with a module that could do
what I wanted...
Which is the best module to get for this?


Python does come with low level modules that you can use but its a lot 
of work. The suggested add ins are generally easier to use or more 
powerful 'out of the box'. But you could use things like urlib2 and 
httplib and cgi etc to do what you want if you really need to stick with 
the standard library.


As to which is "best" that depend on your favoured programming style and 
the exact nature of your problem. We can't answer that for you.


--
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] entering text on web site

2012-06-14 Thread Timo

Op 14-06-12 04:44, Benjamin Fishbein schreef:
Thanks. I'm trying to automatically post text to a web page from 
Python and retrieve the resultant page. It's not just name and 
password, though; I need to do it over and over again with chunks of 
info from a list.

The page seems to be using POST.
form method='POST'
is written all throughout the code.
The links you recommended use modules like Client Cookie, mechanize, 
etc. I guess Python didn't come standard with a module that could do 
what I wanted...

Which is the best module to get for this?

I had successful results with the mechanize package in the past.

Timo



Ben


On Jun 13, 2012, at 8:27 AM, Walter Prins wrote:


Hi,

On 13 June 2012 13:35, Benjamin Fishbein > wrote:
Hello. I have been teaching myself Python and have been stuck on 
this problem for a week--I scoured the web for an answer and even 
asked a couple computer programmer friends.
I managed to write a simple program with urllib that downloads the 
content from web pages; but I want to put text in a text box on the 
webpage, have it entered into the website, and then get the results. 
Is there a way to do this with Python?


Please clarify, are you:
a) Trying to automatically post some text to a web page from Python
and then retrieve the resultant page? (for example, username and
password and retrieve resultant logon page.)
b) Trying to use Python to present a webpage that a user then puts
text in a box which the Python program then captures when the user
submits the page?

I'm assuming a.) If so, then I should ask, does this page use POST or
GET style submission?

In any case, here's a stackeroverflow question regarding using POST
and cookies etc:
http://stackoverflow.com/questions/2954381/python-form-post-using-urllib2-also-question-on-saving-using-cookies
.. with the default Python documentation doing a decent job of 
explaining GET:

http://docs.python.org/howto/urllib2.html

This also seems useful/relevant:
http://stackoverflow.com/questions/10916745/scraping-data-from-simple-website-change-post-to-get

Walter
___
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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Trouble running Python on Windows Vista

2012-06-14 Thread Tom Avos
Hi there,
I'm learning Python. I have to work with a Notebook running Widows Vista Home 
Premium.(have no other choice)
It works fine when working in Interactive Mode or by  Opening a .py file from 
the IDLE and then running it by pressing F5, but I can not run .py files by 
invoking them from the prompt (neither from IDLE nor Dos Command Line); 
 
Every time I try to do this I get the following error:(In this case I'm tryig 
to invoke triangulo.py saved in C:\python27); 
 
>>> triangulo.py
Traceback (most recent call last):
  File "", line 1, in 
    triangulo.py
NameError: name 'triangulo' is not defined
>>> 
 
The same happens for every file I want to run by calling them from the prompt, 
although if I double click on any .py file it runs ok.
 
I've installed Python as follows:
 
I've downloaded the Python 2.7.3 Windows Installer from 
www.python.org/download/. 
Once I've got it I run the installer and Python was installed on C:\Python27, 
and I've added Python to the my directory by doing set path=%path%;C:\python27.
 
Any ideas will be very welcome :-)
Thanks in advance for your attention.___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trouble running Python on Windows Vista

2012-06-14 Thread Kwpolska
On Thu, Jun 14, 2012 at 5:30 PM, Tom Avos  wrote:
> Hi there,
> I'm learning Python. I have to work with a Notebook running Widows Vista
> Home Premium.(have no other choice)
> It works fine when working in Interactive Mode or by  Opening a .py file
> from the IDLE and then running it by pressing F5, but I can not run .py
> files by invoking them from the prompt (neither from IDLE nor Dos Command
> Line);
>
> Every time I try to do this I get the following error:(In this case I'm
> tryig to invoke triangulo.py saved in C:\python27);
>
 triangulo.py
> Traceback (most recent call last):
>   File "", line 1, in 
>     triangulo.py
> NameError: name 'triangulo' is not defined

>
> The same happens for every file I want to run by calling them from the
> prompt, although if I double click on any .py file it runs ok.
>
> I've installed Python as follows:
>
> I've downloaded the Python 2.7.3 Windows Installer from
> www.python.org/download/.
> Once I've got it I run the installer and Python was installed on
> C:\Python27, and I've added Python to the my directory by doing set
> path=%path%;C:\python27.
>
> Any ideas will be very welcome :-)
> Thanks in advance for your attention.
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

That's not how you run files.

python path-to-your-file

If it isn't in your %PATH% (it might not be, as you need to set it in
Computer->Properties->Advanced system settings->Environment
Variables), prepend it with c:\python27\.

And please don't put them in C:\Python27\, it isn't supposed to be
used for that.

-- 
Kwpolska 
stop html mail      | always bottom-post
www.asciiribbon.org | www.netmeister.org/news/learn2quote.html
GPG KEY: 5EAAEA16   | Arch Linux x86_64, zsh, mutt, vim.
# vim:set textwidth=70:
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trouble running Python on Windows Vista

2012-06-14 Thread Alan Gauld

On 14/06/12 16:30, Tom Avos wrote:


It works fine when working in Interactive Mode or by  Opening a .py file
from the IDLE and then running it by pressing F5, but I can not run .py
files by invoking them from the prompt (neither from IDLE nor Dos
Command Line);


It looks like you are trying to run them from the Python prompt but you 
need to run them from the DOS prompt (or Windows Explorer as you 
discovered).


At the DOS prompt (looking something like C:\WINDOWS>) you type

python C:\Full\Path\To\File\triangulo.py

ie you need to tell Python where to find the file, python does not use 
the DOS PATH value. Alternatively CD into the folder with the .py file

and Python will find it in the local folder.

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] Trouble running Python on Windows Vista

2012-06-14 Thread Tom Avos


 De: Tom Avos 
Para: "Flynn, Stephen (L & P - IT)"  
Enviado: jueves, 14 de junio de 2012 13:19
Asunto: Re: [Tutor] Trouble running Python on Windows Vista



Great! You're right.. It works from the Command Line :-)
 
So, how should I call it inside the IDLE?? ... If I write ">>> triangulo.py" I 
get the "NameError"
 
Thanks again.
 


 De: "Flynn, Stephen (L & P - IT)" 
Para: Tom Avos  
Enviado: jueves, 14 de junio de 2012 13:00
Asunto: RE: [Tutor] Trouble running Python on Windows Vista
  

 
Looks like (from the “>>>”
prompt) that you’re still inside IDLE when you try to fire up the interpreter. 
This
is how one would kick off the python code from the command line. 
  
S. 
  


  
From:Tom Avos [mailto:toma...@yahoo.com.ar] 
Sent: Thursday, June 14, 2012 4:56
PM
To: Flynn, Stephen (L & P - IT)
Subject: Re: [Tutor] Trouble
running Python on Windows Vista  
  
Hi Stephen,  
I followed
your advice, but still no luck... I get the following error
("triangulo" highlighted):  
>>> python.exe
triangulo.py
SyntaxError: invalid syntax
>>>   
Best,  
   
   
De:" Flynn, Stephen (L & P - IT)"

Para: Tom Avos  
Enviado: jueves, 14 de junio de
2012 12:36
Asunto: RE: [Tutor] Trouble
running Python on Windows Vista  

 
Hi Tom,  
   
   
“python.exe triangulo.py”  
   
   
   
   
S.  
   
   
From:tutor-bounces+steve.flynn=capita.co...@python.org
[mailto:tutor-bounces+steve.flynn=capita.co...@python.org] On Behalf Of Tom Avos
Sent: Thursday, June 14, 2012 4:30
PM
To: tutor@python.org
Subject: [Tutor] Trouble running
Python on Windows Vista   
   
Hi there,   
I'm learning
Python. I have to work with a Notebook running Widows Vista Home Premium.(have
no other choice)   
It works fine
when working in Interactive Mode or by  Opening a .py file from the IDLE
and then running it by pressing F5, but I can not run .py files by invoking
them from the prompt (neither from IDLE nor Dos Command Line);
    
Every time I
try to do this I get the following error:(In this case I'm tryig to invoke 
triangulo.py
saved in C:\python27);
    
>>>
triangulo.py   
Traceback
(most recent call last):
  File "", line 1, in 
    triangulo.py
NameError: name 'triangulo' is not defined
>>>
    
The same happens
for every file I want to run by calling them from the prompt, although if I
double click on any .py file it runs ok.   
    
I've
installed Python as follows:   
    
I've
downloaded the Python 2.7.3
Windows Installer from www.python.org/download/.
Once I've got
it I run the installer and Python was installed on C:\Python27, and I've added
Python to the my directory by doing set path=%path%;C:\python27.   
    
Any ideas
will be very welcome :-)   
Thanks in
advance for your attention.   
 
   
Click here to report this email as spam.   
  
This email and any attachment to it are confidential.  Unless you
are the intended recipient, you may not use, copy or disclose either the
message or any information contained in the message. If you are not the
intended recipient, you should delete this email and notify the sender
immediately. 
  
 Any views or opinions expressed in this email are those of the
sender only, unless otherwise stated.  All copyright in any Capita
material in this email is reserved. 
  
 All emails, incoming and outgoing, may be recorded by Capita and
monitored for legitimate business purposes. 
  
 Capita exclude all liability for any loss or damage arising or
resulting from the receipt, use or transmission of this email to the fullest
extent permitted by law. 
  
 This message has been scanned for malware by Websense. www.websense.com   ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trouble running Python on Windows Vista

2012-06-14 Thread Tom Avos
Yes, If I open the file from the IDLE and then I press "f5" it works, but I 
want to know why it does not work if I write the filename after the ">>>" 
prompt . That way is much more handy, I would like to make it work.
 
 


 De: Kwpolska 
Para: Tom Avos  
Enviado: jueves, 14 de junio de 2012 16:01
Asunto: Re: [Tutor] Trouble running Python on Windows Vista
  
On Thu, Jun 14, 2012 at 8:54 PM, Tom Avos  wrote:
> De: Tom Avos 
> Para: "Flynn, Stephen (L & P - IT)" 
> Enviado: jueves, 14 de junio de 2012 13:19
> Asunto: Re: [Tutor] Trouble running Python on Windows Vista
>
> Great! You're right.. It works from the Command Line :-)
>
> So, how should I call it inside the IDLE?? ... If I write ">>> triangulo.py"
> I get the "NameError"
>
> Thanks again.
>
> De: "Flynn, Stephen (L & P - IT)" 
> Para: Tom Avos 
> Enviado: jueves, 14 de junio de 2012 13:00
> Asunto: RE: [Tutor] Trouble running Python on Windows Vista
>
> Looks like (from the “>>>” prompt) that you’re still inside IDLE when you
> try to fire up the interpreter. This is how one would kick off the python
> code from the command line.
>
> S.
>
> From: Tom Avos [mailto:toma...@yahoo.com.ar]
> Sent: Thursday, June 14, 2012 4:56 PM
> To: Flynn, Stephen (L & P - IT)
> Subject: Re: [Tutor] Trouble running Python on Windows Vista
>
> Hi Stephen,
> I followed your advice, but still no luck... I get the following error
> ("triangulo" highlighted):
 python.exe triangulo.py
> SyntaxError: invalid syntax

> Best,
>
>
> De: " Flynn, Stephen (L & P - IT)" 
> Para: Tom Avos 
> Enviado: jueves, 14 de junio de 2012 12:36
> Asunto: RE: [Tutor] Trouble running Python on Windows Vista
>
>
> Hi Tom,
>
>
> “python.exe triangulo.py”
>
>
>
>
> S.
>
>
> From: tutor-bounces+steve.flynn=capita.co...@python.org
> [mailto:tutor-bounces+steve.flynn=capita.co...@python.org] On Behalf Of Tom
> Avos
> Sent: Thursday, June 14, 2012 4:30 PM
> To: tutor@python.org
> Subject: [Tutor] Trouble running Python on Windows Vista
>
> Hi there,
> I'm learning Python. I have to work with a Notebook running Widows Vista
> Home Premium.(have no other choice)
> It works fine when working in Interactive Mode or by  Opening a .py file
> from the IDLE and then running it by pressing F5, but I can not run .py
> files by invoking them from the prompt (neither from IDLE nor Dos Command
> Line);
>
> Every time I try to do this I get the following error:(In this case I'm
> tryig to invoke triangulo.py saved in C:\python27);
>
 triangulo.py
> Traceback (most recent call last):
>   File "", line 1, in 
>     triangulo.py
> NameError: name 'triangulo' is not defined

>
> The same happens for every file I want to run by calling them from the
> prompt, although if I double click on any .py file it runs ok.
>
> I've installed Python as follows:
>
> I've downloaded the Python 2.7.3 Windows Installer from
> www.python.org/download/.
> Once I've got it I run the installer and Python was installed on
> C:\Python27, and I've added Python to the my directory by doing set
> path=%path%;C:\python27.
>
> Any ideas will be very welcome :-)
> Thanks in advance for your attention.
>
>
> Click here to report this email as spam.
>
> This email and any attachment to it are confidential.  Unless you are the
> intended recipient, you may not use, copy or disclose either the message or
> any information contained in the message. If you are not the intended
> recipient, you should delete this email and notify the sender immediately.
>
>  Any views or opinions expressed in this email are those of the sender only,
> unless otherwise stated.  All copyright in any Capita material in this email
> is reserved.
>
>  All emails, incoming and outgoing, may be recorded by Capita and monitored
> for legitimate business purposes.
>
>  Capita exclude all liability for any loss or damage arising or resulting
> from the receipt, use or transmission of this email to the fullest extent
> permitted by law.
>
>  This message has been scanned for malware by Websense. www.websense.com
>
>
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

Open the file in the IDLE editor and press F5.

-- 
Kwpolska 
stop html mail      | always bottom-post
www.asciiribbon.org | www.netmeister.org/news/learn2quote.html
GPG KEY: 5EAAEA16   | Arch Linux x86_64, zsh, mutt, vim.
# vim:set textwidth=70:___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trouble running Python on Windows Vista

2012-06-14 Thread Emile van Sebille

On 6/14/2012 12:19 PM Tom Avos said...

Yes, If I open the file from the IDLE and then I press "f5" it works,
but I want to know why it does not work if I write the filename after
the ">>>" prompt . That way is much more handy, I would like to make it
work.


Once you've got the >>> prompt you're executing python words, not file 
system file names.



IDLE is a development environment and is not a run environment. 
Further, IDLE is or has been known to suffer from various 'leaks' that 
contribute to oddities when made to do as you're asking, so even when 
you do get it going, it's not a stable platform.


I'd-like-apples-to-be-oranges-but-they're-not-ly y'rs,

Emile

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


Re: [Tutor] Trouble running Python on Windows Vista

2012-06-14 Thread Tom Avos
Thanks!!
 


 De: Emile van Sebille 
Para: tutor@python.org 
Enviado: jueves, 14 de junio de 2012 16:35
Asunto: Re: [Tutor] Trouble running Python on Windows Vista
  
On 6/14/2012 12:19 PM Tom Avos said...
> Yes, If I open the file from the IDLE and then I press "f5" it works,
> but I want to know why it does not work if I write the filename after
> the ">>>" prompt . That way is much more handy, I would like to make it
> work.

Once you've got the >>> prompt you're executing python words, not file system 
file names.


IDLE is a development environment and is not a run environment. Further, IDLE 
is or has been known to suffer from various 'leaks' that contribute to oddities 
when made to do as you're asking, so even when you do get it going, it's not a 
stable platform.

I'd-like-apples-to-be-oranges-but-they're-not-ly y'rs,

Emile

___
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


Re: [Tutor] Trouble running Python on Windows Vista

2012-06-14 Thread Alan Gauld

On 14/06/12 20:19, Tom Avos wrote:

Yes, If I open the file from the IDLE and then I press "f5" it works,
but I want to know why it does not work if I write the filename after
the ">>>" prompt . That way is much more handy, I would like to make it
work.


Its  not really the same but you can just import it:

>>> import triangulo   # note no .py

That will execute any runnable code in the file.

But its not really what the IDLE >>> prompt is intended for.

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] Notepad++ question

2012-06-14 Thread Alexander Quest
Got it Dave- sorry about not sending it to the newsgroup as well.

My question was regarding a piece of boilerplate code:

if __name__ == '__main__':
  main()

This calls the main function, but I don't understand what the 'if'
statement is doing here. In the simple programs that I've seen this so far,
there is no variable called "_name_", and even if there was, why is it
comparing it to "_main_"? Why can't the main function just be called by
typing main()- why do we need this if statement to precede it? Thanks.

-Alex

On Thu, Jun 7, 2012 at 6:16 PM, Dave Angel  wrote:

> On 06/07/2012 02:36 PM, Alexander Quest wrote:
> > Ok, thanks guys. I also had one more quick question regarding a piece of
> > boilerplate code:
> >
>
> To get a response, you will needs to leave your question at the python
> tutor newsgroup.  We are part of a group, not offering private advice.
>
> Normally, you just do a Reply-all to one of the existing messages on the
> thread, to include the group.  But you can also typetutor@python.org
>  as a CC:
>
> I've been volunteering my time on various forums for over 25 years now,
> so I think I speak for lots of other volunteers.
>
>
> --
>
> DaveA
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Notepad++ question

2012-06-14 Thread Alexander Quest
[Resending because I messed up on last email]

My question was regarding a piece of boilerplate code:

if __name__ == '__main__':
  main()

This calls the main function, but I don't understand what the 'if'
statement is doing here. In the simple programs that I've seen this so far,
there is no variable called "_name_", and even if there was, why is it
comparing it to "_main_"? Why can't the main function just be called by
typing main()- why do we need this if statement to precede it? Thanks.

-Alex

On Thu, Jun 14, 2012 at 3:17 PM, Alexander Quest wrote:

> Got it Dave- sorry about not sending it to the newsgroup as well.
>
> My question was regarding a piece of boilerplate code:
>
>
> if __name__ == '__main__':
>   main()
>
> This calls the main function, but I don't understand what the 'if'
> statement is doing here. In the simple programs that I've seen this so far,
> there is no variable called "_name_", and even if there was, why is it
> comparing it to "_main_"? Why can't the main function just be called by
> typing main()- why do we need this if statement to precede it? Thanks.
>
> -Alex
>
> On Thu, Jun 7, 2012 at 6:16 PM, Dave Angel  wrote:
>
>> On 06/07/2012 02:36 PM, Alexander Quest wrote:
>> > Ok, thanks guys. I also had one more quick question regarding a piece of
>> > boilerplate code:
>> >
>>
>> To get a response, you will needs to leave your question at the python
>> tutor newsgroup.  We are part of a group, not offering private advice.
>>
>> Normally, you just do a Reply-all to one of the existing messages on the
>> thread, to include the group.  But you can also typetutor@python.org
>>  as a CC:
>>
>> I've been volunteering my time on various forums for over 25 years now,
>> so I think I speak for lots of other volunteers.
>>
>>
>> --
>>
>> DaveA
>>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Notepad++ question

2012-06-14 Thread Dave Angel
On 06/14/2012 06:18 PM, Alexander Quest wrote:
> [Resending because I messed up on last email]
> 
> My question was regarding a piece of boilerplate code:
> 
> if __name__ == '__main__':
>   main()
> 
> This calls the main function, but I don't understand what the 'if'
> statement is doing here. In the simple programs that I've seen this so far,
> there is no variable called "_name_", and even if there was, why is it
> comparing it to "_main_"? Why can't the main function just be called by
> typing main()- why do we need this if statement to precede it? Thanks.
> 
> -Alex
> 

It may not matter for simple scripts.  But many times the same file is
used in two ways.  When it's used as a script, __name__ is set to the
literal "__main__"

But when it's used as a module (ie. somebody imports it) then the name
is set to the module name (filename without the .py)

So, you normally do not want to execute your main() function unless it's
being used as a script.

This becomes very important when you want to reuse code, or when you
want to write test cases for modules.


-- 

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


Re: [Tutor] Notepad++ question

2012-06-14 Thread Walter Prins
Hi Alex,

On 14 June 2012 23:18, Alexander Quest  wrote:
> if __name__ == '__main__':
>   main()
>
> This calls the main function, but I don't understand what the 'if' statement
> is doing here. In the simple programs that I've seen this so far, there is
> no variable called "_name_", and even if there was, why is it comparing it
> to "_main_"? Why can't the main function just be called by typing main()-
> why do we need this if statement to precede it? Thanks.

In short, consider the implications of the fact that your file, apart
from being a program that can run standalone, might also be a Python
module that might be used in another program.  Oftentimes you want to
write your Python code in such a way that when the module is run
directly you want it to do something useful (such as run a main()
function, e.g. maybe run some unit/self-tests or whatever), while when
you import it for use in another program/module then you probably
rather do *not* want it to run as if it is itself the "main program".
So, in order to differentiate the 2 cases, there exists the above
Python idiom.  So, when a module is directly run as the "main
program", then the name of the module being run, which is reflected by
the variable __name__, made available by the Python interpreter, will
be equal to "__main__", while when it's imported it will be equal to
the module name.  This allows your module to know when it's running
whether it's running as the main program or just running because it's
been imported by another module.

Does that answer your question?

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


Re: [Tutor] Notepad++ question

2012-06-14 Thread Emile van Sebille

On 6/14/2012 3:18 PM Alexander Quest said...

[Resending because I messed up on last email]

My question was regarding a piece of boilerplate code:

if __name__ == '__main__':
   main()




__name__ within a python module is either '__main__' when invoked 
directly from a command line, or contains the module name.  This allows 
the module to sense if it was imported or run directly and perform any 
relevant code that may depend on that.  One common use is to run test 
code within libraries intended to be imported.


This illustrates the difference:

$ cat > test.py
if __name__ == '__main__':
print "__name__ == '__main__'"
else:
print "__name__ == '",__name__,"'"

Emile@MIS2 ~
$ python test.py
__name__ == '__main__'

Emile@MIS2 ~
$ python
Python 2.4.3 (#1, May 18 2006, 07:40:45)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
__name__ == ' test '
>>>




This calls the main function, but I don't understand what the 'if'
statement is doing here. In the simple programs that I've seen this so
far, there is no variable called "_name_", and even if there was, why is
it comparing it to "_main_"? Why can't the main function just be called
by typing main()- why do we need this if statement to precede it? Thanks.

-Alex



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


Re: [Tutor] Notepad++ question

2012-06-14 Thread Alan Gauld

On 14/06/12 23:18, Alexander Quest wrote:


so far, there is no variable called "_name_", and even if there was,
why is it comparing it to "_main_"? Why can't the main function just


Note that in both cases there are two '_' characters before and after 
the name. __name__ and '__main__'. Double underscores usually indicates 
a special variable used by Python internally.


Thus __name__ is something that Python sets when the file is either 
imported or run, as others have already explained.


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] Notepad++ question

2012-06-14 Thread Alexander Quest
Thanks Walter; I believe I understand the reasoning behind it, though not
all of the mechanics, but for now, your answer is more than sufficient.

-Alex

On Thu, Jun 14, 2012 at 4:10 PM, Walter Prins  wrote:

> Hi Alex,
>
> On 14 June 2012 23:18, Alexander Quest  wrote:
> > if __name__ == '__main__':
> >   main()
> >
> > This calls the main function, but I don't understand what the 'if'
> statement
> > is doing here. In the simple programs that I've seen this so far, there
> is
> > no variable called "_name_", and even if there was, why is it comparing
> it
> > to "_main_"? Why can't the main function just be called by typing main()-
> > why do we need this if statement to precede it? Thanks.
>
> In short, consider the implications of the fact that your file, apart
> from being a program that can run standalone, might also be a Python
> module that might be used in another program.  Oftentimes you want to
> write your Python code in such a way that when the module is run
> directly you want it to do something useful (such as run a main()
> function, e.g. maybe run some unit/self-tests or whatever), while when
> you import it for use in another program/module then you probably
> rather do *not* want it to run as if it is itself the "main program".
> So, in order to differentiate the 2 cases, there exists the above
> Python idiom.  So, when a module is directly run as the "main
> program", then the name of the module being run, which is reflected by
> the variable __name__, made available by the Python interpreter, will
> be equal to "__main__", while when it's imported it will be equal to
> the module name.  This allows your module to know when it's running
> whether it's running as the main program or just running because it's
> been imported by another module.
>
> Does that answer your question?
>
> Walter
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor