name must be ni.py.
You could put the two functions, dbl() and addem() into a module called arith, which is short
for arithmetic. This module name follows all the module name rules.
Creating the Custom Module in a Test Directory
The file name for your new module should be arith.py in order to properly follow naming
conventions. In Listing 13.8, you can see that the module resides in /home/pi/py3prog as its test
directory.
LISTING 13.8 The arith.py Module File
Click here to view code image
1: pi@raspberrypi ~ $ cat /home/pi/py3prog/arith.py
2: def dbl(value):
3: result=value * 2
4: return result
5:
6: def addem(a, b):
7: result=a + b
8: return result
9: pi@raspberrypi ~ $
The arith module is a very simple one. It contains only the two functions from Hour 12. In Listing
13.8, you can see that the dbl() function starts on line 2 and ends on line 4. The addem() function
starts on line 6 and ends on line 8.
By the Way: Simple or Complex Modules
A module can be as simple or as complex as you choose. The custom modules shown
here are very simple. If desired, you can add help utilities to your modules, include
Python version directives, import other modules, and so on.
To create a custom module, you can use your favorite text editor or the Python IDLE editor. Be sure to
save the module to a location you have designated as a test directory. Often, this is simply a
subdirectory of your home login directory, as shown in Listing 13.8.
Testing the Custom Module
To test your module, you need to ensure that your present working directory is in the same location
where the module file resides. In Listing 13.9, the os module is used to show the present working
directory, /home/pi, on line 2. This is not the test directory which was designated earlier, and thus
the import of the arith module on line 4 does not work.
LISTING 13.9 A Test of the arith Custom Module
Click here to view code image
1: >>> import os