| | |
How to prevent statement execution when importing modules?
Thread Solved |
Hi,
I am a Java programmer dabbling in Python for the first time. Mostly, the transition has been pretty smooth, but this week something stumped me. Please forgive me if the answer to this is completely obvious - I've read van Rossum's tutorial and can't figure it out.
I am working on a Python project composed of one main module and several smaller helper modules. Each module is currently in its own file, which (I believe) is the convention Python uses. The main module is called MainProgram.py and one of the helper modules is called Helper.py. MainProgram.py has to import a method called somefunc() from Helper.py.
Now, one of the conventions I use when designing OO systems in Java is this: all of my helper classes have diagnostic code in their main methods, so that when you invoke the class's main from the command line, you get to see whether the methods of that class work the way they are supposed to. Unfortunately, Python modules have no main method to speak of, so I did what I thought was the next best thing. In my Helper module:
The idea is that somefunc() executes with arbitrary arguments of 'A', 'B', and 'C' when Helper.py is invoked on its own. Then, in the MainProgram module:
Right? Unfortunately, the import statement also triggers that diagnostic invocation of somefunc() at the bottom of Helper.py - this is neither trivial nor good in the context of my actual project. It's also very confusing, since you'd think that importing a method from a module wouldn't call for any execution at all.
Can anyone tell me if there's a way to do what I'm describing in Python? Again, I'm sorry if this is a total n00b question - that's what I am at the moment.
I am a Java programmer dabbling in Python for the first time. Mostly, the transition has been pretty smooth, but this week something stumped me. Please forgive me if the answer to this is completely obvious - I've read van Rossum's tutorial and can't figure it out.
I am working on a Python project composed of one main module and several smaller helper modules. Each module is currently in its own file, which (I believe) is the convention Python uses. The main module is called MainProgram.py and one of the helper modules is called Helper.py. MainProgram.py has to import a method called somefunc() from Helper.py.
Now, one of the conventions I use when designing OO systems in Java is this: all of my helper classes have diagnostic code in their main methods, so that when you invoke the class's main from the command line, you get to see whether the methods of that class work the way they are supposed to. Unfortunately, Python modules have no main method to speak of, so I did what I thought was the next best thing. In my Helper module:
Python Syntax (Toggle Plain Text)
#!/usr/bin/python2.3 # Helper.py def somefunc(a, b, c): # code for this function # Code that tests somefunc somefunc('A', 'B', 'C')
Python Syntax (Toggle Plain Text)
#!/usr/bin/python2.3 # MainProgram.py from Helper import somefunc somefunc('Actual', 'data', 'here') # Other code which does other stuff
Can anyone tell me if there's a way to do what I'm describing in Python? Again, I'm sorry if this is a total n00b question - that's what I am at the moment.
In your Python folder there is a folder \lib\test\ that should give you an idea how the Pythonians do their testing of modules. It looks like you might need to write a simple test program. Also IDEs like IDLE have a check module option.
Hope someone else knows more about that subject. I started using Python more seriously about 5 months ago and have great fun with it, but have not touched every corner of its many capabilities.
Hope someone else knows more about that subject. I started using Python more seriously about 5 months ago and have great fun with it, but have not touched every corner of its many capabilities.
May 'the Google' be with you!
I knew I stumbled upon this subject before, there is a nice article on Test-Driven Development in Python on:
http://www.onlamp.com/pub/a/python/2...dd_pyunit.html
Hope that helps!
http://www.onlamp.com/pub/a/python/2...dd_pyunit.html
Hope that helps!
May 'the Google' be with you!
Aha!!
Thank you very much, vegaseat. That:
...was just what I needed. And the article was very informative - I knew about doctest and unittest, but not enough. Thanks!
Thank you very much, vegaseat. That:
Python Syntax (Toggle Plain Text)
if __name__ == '__main__': # Test code here
![]() |
Similar Threads
- missing return statement (Java)
- how to prevent user close window during script execution? (Shell Scripting)
- order of evaluation (C)
- Here is the doc file for the assignment (C++)
- Switch Statement, Fall-Through. (C)
- Dialog Box alongwith Form (Visual Basic 4 / 5 / 6)
Other Threads in the Python Forum
- Previous Thread: cgi help !
- Next Thread: Text Editors ?
| Thread Tools | Search this Thread |
alarm app assignment avogadro beginner bluetooth character cipher cmd code customdialog cx-freeze data decimals dictionary directory dynamic error examples exe file float format function generator getvalue gnu graphics gui halp homework http ideas images import input ip itunes java keycontrol leftmouse line linux list lists loop maintain maze millimeter module mouse number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh string strings sudokusolver terminal text threading time tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo vigenere web webservice wikipedia wxpython xlib






