How do I use Python / NumPy for a fairly large project?

So far, I am writing one-script Python programs, mainly for word processing and data analysis.

Now I want to bring one of my old Matlab modeling projects to Python / NumPy. In this project, I have one main program (in a file .m

) with several dozen functions (each in a separate file .m

). There were global variables that were used in all functions, so I didn't need to enter them as arguments for each function. But then I cannot run / test a separate function without running the main program, because the global variables will be undefined or will call another function in another file. Also, organizing files is a mess. It is painful to add new functions by changing existing ones, especially the main program.

This time I want to do things right. I want the program to have the right architecture, if that's the right word. I need to know first in order to organize all these functions. I don't think that every little function that has its own file is a good idea. I think maybe I can split these functions into multiple groups and each group can be one file? Will it be a file .py

or some other file? Second, I would like it to be easily extensible so that I can easily add new features.

I believe there must be some standard way of doing this, but I have no idea.

One more question: when I start the Matlab program, after it finishes, I still have all the variables in the workspace, so I can still check numbers, make graphs, etc. But when I run my python script through the IPython shell, after that it clears everything. Are there any similarities to the work area?

+3


source to share


3 answers


Every time you have a bunch of functions sharing the same global state:

foo = 1
def do_something1():
    print foo

def do_something2():
    global foo
    foo += 1

      

you'd better define a class:

class NoGlobal(object):
     """docstring -- Pick a better name for your class please :)"""

     def __init__(self):
          self.foo = 1

     def do_something1(self):
          print self.foo

     def do_something2(self):
          self.foo += 1

      



Now you don't use the global state, and you can run your "simulation" as much as you like without antialiasing with your globals. Just instantiate a new class and you're ready to start a new simulation.

If you leave it so that you can play with things after the script finishes, then what's for the parameter -i

:

python -i yourscript.py

      

+5


source


Regarding the Python interactive shell: IPython is very useful if you want to work interactively in Python.



Starts in pylab mode: ipython --pylab

provides an interactive python shell with Matlab-like functions (the names in the numpy and matplotlib packages are already imported into the scope.) You can also save a session or load saved sessions and arbitrary python programs.

+2


source


I think I found some of what I was looking for and asked in this question . Things can be organized in Modules (files .py

) and Packages (file folder __init__.py

). This way I can group a bunch of functions into a module and put all the modules for a project into a package.

I still need to understand the thing class

with global variables.

+1


source







All Articles