Reusing Code with the Import Statement
One problem with learning something new is that, if it is abstract, like calculus, for
example, it is hard to justify caring about it. When was the last time you used the math you learned in high school at the grocery store? Above we saw how to create functions as an alternative to executing shell commands one after another in a script. We also know that a module is really just a script, or some lines of code in a file. It isn’t anything tricky, but it does need to be arranged in a particular way so that it can be reused in another future program. Let’s import the previous system information scripts in both Bash and Python and execute.
Keep in mind that you never need to specify the .py portion of the file you are importing. In fact if you do this, the import will not work. Here is what it looks like when we do that on Noah’s Macbook Pro laptop:
In [1]: import pysysinfo
But, actually, there are a few problems with this. If you plan to run Python code, it should always be executed from the command line as a part of a script or program you write. Using import is to help with this “reusing code” idea we keep throwing around. Here is the punch line: what if you only wanted to print the output of the diskspace portion of the script? The answer is you can’t. That is why you use functions.
They allow you to control when and how parts of your program run so that they don’t
all run at once, as they do in this example. Don’t just take our word for it, though. If
you import the example of a script that puts these commands into functions, you’ll see what I mean.
On one hand we want to be able to run our script on the command line to get the output, but on the other hand when we import it we don’t want all of the output all at once. Fortunately, the need to use a module as both a script that gets executed from the command line and as a reusable module is very common in Python.
You might have realized by now that functions are always “called” or run by attaching the “( )” after the name. In this case, we ran just that one function inside of a file that contained three functions: the function we just called disk_func , the uname_func , and finally the main function. Aha! We finally have our code reuse. We were able to import something we wrote earlier and interactively run just the part of it we needed. Of course, we can also run the other two functions we wrote separately.
Often, the point of writing a reusable module is so that you can take some of the code
and use it over and over again in a new script. So practice that by writing another script that uses one of the functions.
What’s fun about reusing code is that it is possible to make a completely different program just by importing the function from our previous program.
IPython
One of Python’s strengths is its interactive interpreter, or shell. The shell provides a way to quickly test ideas, test features and interfaces of modules you are working with, and perform some one-off tasks for which you would otherwise have written a three line script. The way that we tend to code is by simultaneously running a text editor and a Python prompt (actually, an IPython prompt, but we’ll get to that in a moment), frequently interacting with them both, switching back and forth between shell and editor, and often pasting code from one to the other. This approach allows us to see immediate results from Python about the way it handles code and to quickly get the code in the text editor working the way we want it to.
At its heart, IPython is a bundle of interactive Python goodness. It is an amazing Python shell, far superior to the standard Python shell. It also provides the ability to create highly customized console-based command environments; it allows for easy inclusion of an interactive Python shell into any Python application; and it can even be used as a system shell, with some level of success. This chapter will focus on using IPython to improve your productivity on *nix-shell and Python-related tasks. IPython also has an active, incredibly helpful community behind it. You can sign up for the mailing list at http://lists.ipython.scipy.org/mailman/listinfo/ipython-user. There is an excellent wiki at http://ipython.scipy.org/moin. And, as part of the wiki, there is a cookbook at http://ipython.scipy.org/moin/Cookbook. So, you can read or contribute to any of these resources as you wish. Another area that you can contribute to is the development of IPython. IPython development recently switched to a distributed source control system, so you can just branch their code and start hacking. And if you do something that could benefit others, you can submit your changes to them.
