By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

Starting Python... some questions

P: n/a
I started using Python a couple of days ago - here are a few
questions:

* Doesn't the __main__() method automatically execute when I run my
python program?
* Only when I do an import of my test.py file within python and then
run test.__main__() I can see where my bugs are. Is this correct?
(right now this is my only way of running my python program and see
where I have problems)
* Once I've done an import and then I wish to make a change to the
file I've imported I have to quit Python, restart and import that
module again in order for the module to be refreshed. Is there no "re-
import" ?
* Finally, could someone tell me why I'm having problems with the
small module below?
- Python pretends I provide chassis_id() with three parameters, even
though I clearly only provide it with two - why?

Thanks!

#!/usr/bin/python
import scapy
import struct

class lldp_class:
def __init__(self):
self.chassis_id_tlv = None

def chassis_id(subtype, chassis_info):
if subtype == 4:
chassis_data = struct.pack("!B",chassis_info)
subtype_data = struct.pack("!B",subtype)
self.chassis_id_tlv = subtype_data + chassis_data

def __main__():
p = lldp_class()
p.chassis_id(4, "01:80:C2:00:00:0E")
payload = p.chassis_id_tlv
ether = scapy.Ether(dst="01:02:03:04:05:06")
fullpayload = ether + payload
sendp(fullpayload)

Mar 13 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
je*********@yahoo.com wrote:
I started using Python a couple of days ago - here are a few
questions:

* Doesn't the __main__() method automatically execute when I run my
python program?
* Only when I do an import of my test.py file within python and then
run test.__main__() I can see where my bugs are. Is this correct?
(right now this is my only way of running my python program and see
where I have problems)
No, there is no special __main__ function, to the best of my knowledge.
I think the idiom you're thinking of is:

if __name__="__main__":
#Your main stuff here

But that won't execute if you do an import. Only when you call the
script directly, like:

python myscript.py

for example.
* Once I've done an import and then I wish to make a change to the
file I've imported I have to quit Python, restart and import that
module again in order for the module to be refreshed. Is there no "re-
import" ?
There is. The reload command. Usage is similar to import:

import MyModule
#Do your edit, and reload thusly:
reload MyModule
* Finally, could someone tell me why I'm having problems with the
small module below?
- Python pretends I provide chassis_id() with three parameters, even
though I clearly only provide it with two - why?
Python expects self as the first parameter. You might want to take
another look at the class bits of your tutorial of choice, or:
http://docs.python.org/tut/node11.html

The function definition should look more like:

def chassis_id(self, subtype, chassis_info):

HTH.
-Jordan Greenberg
Mar 13 '07 #2

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

je*********@yahoo.com wrote:
I started using Python a couple of days ago - here are a few
questions:

* Doesn't the __main__() method automatically execute when I run my
python program?
* Only when I do an import of my test.py file within python and then
run test.__main__() I can see where my bugs are. Is this correct?
(right now this is my only way of running my python program and see
where I have problems)
* Once I've done an import and then I wish to make a change to the
file I've imported I have to quit Python, restart and import that
module again in order for the module to be refreshed. Is there no "re-
import" ?
* Finally, could someone tell me why I'm having problems with the
small module below?
- Python pretends I provide chassis_id() with three parameters, even
though I clearly only provide it with two - why?
No, Python is executing a module. You should explicit call __main__()
at top scope of
your module. For ex.

if __name__ == '__main__':
__main__()

This snippet calls __main__() when the module is executed directly,
but not imported as a module.

Once a module had been imported, you should invoke reload() function
to reload it from filesystem.
- --
Thinker Li - th*****@branda.to th********@gmail.com
http://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF9i/j1LDUVnWfY8gRAgidAKCxHUWf/XTuT8fRQmeL3hnsO4fohQCgt27R
jCNoz4hgp2CxD7H5HPwkfgM=
=4g/m
-----END PGP SIGNATURE-----

Mar 13 '07 #3

P: n/a
On Mon, 12 Mar 2007 21:39:11 -0700, jezonthenet wrote:
I started using Python a couple of days ago - here are a few
questions:

* Doesn't the __main__() method automatically execute when I run my
python program?

No.

* Only when I do an import of my test.py file within python and then
run test.__main__() I can see where my bugs are. Is this correct?
(right now this is my only way of running my python program and see
where I have problems)
That's a good way of choosing to run __main__ manually, but there are
other ways.

If you add a line to the end of your program:

__main__()
the function will be executed whenever the program runs. That is both when
you import it, and when you run it from the command line.

To ensure it doesn't run when you import, do this:
if __name__ == "__main__":
__main__()
* Once I've done an import and then I wish to make a change to the
file I've imported I have to quit Python, restart and import that
module again in order for the module to be refreshed. Is there no "re-
import" ?
Yes there is. Do this:

import my_module
# do things
# edit the file
# don't forget to save changes (that always catches me out)
reload(my_module)

* Finally, could someone tell me why I'm having problems with the
small module below?
- Python pretends I provide chassis_id() with three parameters, even
though I clearly only provide it with two - why?

Thanks!

#!/usr/bin/python
import scapy
import struct

class lldp_class:
def __init__(self):
self.chassis_id_tlv = None

def chassis_id(subtype, chassis_info):
if subtype == 4:
chassis_data = struct.pack("!B",chassis_info)
subtype_data = struct.pack("!B",subtype)
self.chassis_id_tlv = subtype_data + chassis_data
Class methods always take a first parameter that points to the
class instance. By convention, it should be called "self". So you should
write this as:

def chassis_id(self, subtype, chassis_info):
if subtype == 4:
chassis_data = struct.pack("!B",chassis_info)
subtype_data = struct.pack("!B",subtype)
self.chassis_id_tlv = subtype_data + chassis_data
When you call the method:

my_instance = lldp_class()
my_instance.chassis_id(4, "01:80:C2:00:00:0E")

Python automatically fills in a reference to the instance and calls this:

lldp_class.chassis_id(my_instance, 4, "01:80:C2:00:00:0E")
Just remember to always put "self" as the first argument to a class
method, and you will (almost never) go wrong!

--
Steven D'Aprano

Mar 13 '07 #4

P: n/a
Jo****@bag.python.org, "Greenberg <"@bag.python.org, "greenbergj\""@NOSPAM.xs4all.nl, wi*****@bag.python.org, ">"@bag.python.org writes:
je*********@yahoo.com wrote:
* Doesn't the __main__() method automatically execute when I run
my python program?

No, there is no special __main__ function, to the best of my
knowledge.
Guido van Rossum has mused on the idiom of the 'main' function in modules:

<URL:http://www.artima.com/weblogs/viewpost.jsp?thread=4829>

My idiom for this is:

=====
def __main__(argv=None):
""" Mainline code for this program """

from sys import argv as sys_argv
if argv is None:
argv = sys_argv

foo = Foo(argv) # Foo is the main thing I need
exitcode = None
try:
foo.do_the_main_thing()
except SystemExit, e:
exitcode = e.code

return exitcode

if __name__ == "__main__":
import sys
exitcode = __main__(argv=sys.argv)
sys.exit(exitcode)
=====

The reason for the local 'sys' imports is that I can boilerplate this
code and not have to worry about whether the rest of the program
actually uses the 'sys' module.

The reason for parameterising 'argv', but defaulting it to the system
argv, is that I can unit-test the '__main__' function
programmatically, while still letting it pick up the real argv when
the program is run.

Same reason for catching SystemExit, and then going on to call
'sys.exit()' myself: it allows the __main__ function to be unit-tested
easily, while still letting me use 'sys.exit()' in my application code
whenever it makes sense.

Now, when I write unit tests for my program (i.e. a Python module
designed to be run as a command), it can still be imported safely into
my unit tests, and all the code gets covered by test cases except the
three-line stanza at the end.
Htag.pl 0.0.23 - Simon Huggins <hu****@earth.li Released under GPL
Copyright (C) 1999-2002 Project Purple. http://www.earth.li/projectpurple/

Got sigmatch == ^/home/bignose/\.sigs/news.*$

--
\ "The right to search for truth implies also a duty; one must |
`\ not conceal any part of what one has recognized to be true." |
_o__) -- Albert Einstein |
Ben Finney

Mar 13 '07 #5

P: n/a
Ben Finney <bi****************@benfinney.id.auwrites:
Now, when I write unit tests for my program (i.e. a Python module
designed to be run as a command), it can still be imported safely
into my unit tests, and all the code gets covered by test cases
except the three-line stanza at the end.
All I need now is for Python to automatically execute a '__main__'
function if the module name is '__main__' (which it seems the OP
expected should happen), and unit test coverage could be 100% :-)
Htag.pl 0.0.23 - Simon Huggins <hu****@earth.li Released under GPL
Copyright (C) 1999-2002 Project Purple. http://www.earth.li/projectpurple/

Got sigmatch == ^/home/bignose/\.sigs/news.*$
Er, okay. That was a bit strange. Sorry for the flubbed sigmonster.

--
\ "I bet one legend that keeps recurring throughout history, in |
`\ every culture, is the story of Popeye." -- Jack Handey |
_o__) |
Ben Finney

Mar 13 '07 #6

P: n/a
You only need three things here:

je*********@yahoo.com wrote:
>
#!/usr/bin/python
import scapy
import struct

class lldp_class:
def __init__(self):
self.chassis_id_tlv = None

def chassis_id(subtype, chassis_info):
Make that
def chassis_id(self, subtype, chassis_info):

When you call "p.chassis_id", "p" is passed as the first parameter to
chassis_id. By convention, we call that "self", in the same way the object
of a C++ method is called "this" (although that is enforced, rather than
convention).
> if subtype == 4:
chassis_data = struct.pack("!B",chassis_info)
subtype_data = struct.pack("!B",subtype)
self.chassis_id_tlv = subtype_data + chassis_data

def __main__():
Make that
def main():

The underscores have a special meaning that you do not need here. Note
that the name "main" is not required; some people call it something like
"test".
> p = lldp_class()
p.chassis_id(4, "01:80:C2:00:00:0E")
payload = p.chassis_id_tlv
ether = scapy.Ether(dst="01:02:03:04:05:06")
fullpayload = ether + payload
sendp(fullpayload)
Now, at the end, add:

if __name__=="__main__":
main()

That should do it.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Mar 13 '07 #7

P: n/a
je*********@yahoo.com a écrit :
I started using Python a couple of days ago - here are a few
questions:

* Doesn't the __main__() method automatically execute when I run my
python program?
Which "__main__" method ???

Anyway, the answer is no. Every code at the top-level (which includes
import, class and def statements...) is executed when the module is
first loaded - whether as a proper module (ie: imported from somewhere
else) or as a main program (ie-python mymodule.py). In the first case,
the special variable __name__ is set to the module's name, in the second
to '__main__'. So you can rely on this to have code executed when the
module is used as a program. The usual idiom is

# mymodule.py
(imports here)
(classes and defs here)

def main(argv):
(code of main function here)
return 0

if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))

* Only when I do an import of my test.py file within python and then
run test.__main__()
BTW, you should *not* use names with 2 leadings and 2 trailing
underscores. These names are reserved for Python implementation stuff.
I can see where my bugs are. Is this correct?
Nope. You can also spot bugs by reading the code or running it thru the
debugger !-)

More seriously : using the above idiom (or any variant of - the
important part being the conditional on __name__ == '__main__'), you can
just
$ python mymodule.py

to run your program.
(right now this is my only way of running my python program and see
where I have problems)
* Once I've done an import and then I wish to make a change to the
file I've imported I have to quit Python, restart and import that
module again in order for the module to be refreshed. Is there no "re-
import" ?
reload(module_object)

But it's of very limited use. The best thing to do is usually to have a
simple test file that setup the desired state (imports etc) that you
execute after each change, passing the -i option to the python
interpreter (this will leave the interpreter in interactive mode after
execution of the test file, so you can inspect your objects, test things
etc).
* Finally, could someone tell me why I'm having problems with the
small module below?
- Python pretends I provide chassis_id() with three parameters, even
though I clearly only provide it with two - why?
Without even reading the code, I can tell you it's an instance or
classmethod with either a wrong declaration or wrongly called.
>
#!/usr/bin/python
import scapy
import struct

class lldp_class:
Do yourself a favor: use new-style classes. Also, it would be better to
stick to usual naming conventions (Python relies heavily on conventions):
http://www.python.org/dev/peps/pep-0008/

class Lldp(object):
def __init__(self):
self.chassis_id_tlv = None

def chassis_id(subtype, chassis_info):
Bingo. You need to have self as the first argument. The instance is
passed as the first argument of a method.

def chassis_id(self, subtype, chassis_info):
if subtype == 4:
chassis_data = struct.pack("!B",chassis_info)
subtype_data = struct.pack("!B",subtype)
self.chassis_id_tlv = subtype_data + chassis_data

def __main__():

def main():
p = lldp_class()
p.chassis_id(4, "01:80:C2:00:00:0E")
For the record: this is interpreted as:
lldp_class.chassis_id(p, 4, "01:80:C2:00:00:0E")

payload = p.chassis_id_tlv
ether = scapy.Ether(dst="01:02:03:04:05:06")
fullpayload = ether + payload
sendp(fullpayload)
if __name__ == '__main__':
main()

As a side note, it looks like there are a couple point where your design
may be improved. Like passing subtype and chassis_info to the __init__
of your class.

HTH
Mar 13 '07 #8

P: n/a
Steven D'Aprano a écrit :
On Mon, 12 Mar 2007 21:39:11 -0700, jezonthenet wrote:
>I started using Python a couple of days ago - here are a few
questions:

* Doesn't the __main__() method automatically execute when I run my
python program?


No.

>* Only when I do an import of my test.py file within python and then
run test.__main__() I can see where my bugs are. Is this correct?
(right now this is my only way of running my python program and see
where I have problems)

That's a good way of choosing to run __main__ manually, but there are
other ways.

If you add a line to the end of your program:

__main__()
the function will be executed whenever the program runs. That is both when
you import it, and when you run it from the command line.

To ensure it doesn't run when you import, do this:
if __name__ == "__main__":
__main__()
>* Once I've done an import and then I wish to make a change to the
file I've imported I have to quit Python, restart and import that
module again in order for the module to be refreshed. Is there no "re-
import" ?

Yes there is. Do this:

import my_module
# do things
# edit the file
# don't forget to save changes (that always catches me out)
reload(my_module)

>* Finally, could someone tell me why I'm having problems with the
small module below?
- Python pretends I provide chassis_id() with three parameters, even
though I clearly only provide it with two - why?

Thanks!

#!/usr/bin/python
import scapy
import struct

class lldp_class:
def __init__(self):
self.chassis_id_tlv = None

def chassis_id(subtype, chassis_info):
if subtype == 4:
chassis_data = struct.pack("!B",chassis_info)
subtype_data = struct.pack("!B",subtype)
self.chassis_id_tlv = subtype_data + chassis_data

Class methods
s/Class/Instance/

classmethods are methods that takes the class as first parameter.
always take a first parameter that points to the
class instance.
(snip)
Just remember to always put "self" as the first argument to a class
method,
idem.
Mar 13 '07 #9

P: n/a
Ben Finney wrote:
Ben Finney <bi****************@benfinney.id.auwrites:
>Now, when I write unit tests for my program (i.e. a Python module
designed to be run as a command), it can still be imported safely
into my unit tests, and all the code gets covered by test cases
except the three-line stanza at the end.

All I need now is for Python to automatically execute a '__main__'
function if the module name is '__main__' (which it seems the OP
expected should happen), and unit test coverage could be 100% :-)
Short hint to go further:
exec file('/usr/lib/python2.4/timeit.py') in {'__name__': '__main__'}

nd
Mar 13 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.