Writing the python code

The “summarize” package will not flex your knowledge of python, it is just a means to learn how to package code. The source code itself will consist of the single __init__.py file, which will contain the following 3 functions:

main()
  • The default function that is run when the package is invoked from the command line
  • Uses argparse to accept the -N parameter, which specifies the number of values that are generated, and defaults to 5.
gen_numbers(n_numbers)
  • A function that uses numpy.random.randint to generate n_numbers random numbers ranging from 0 to 99
summarize(numbers)
  • Returns the mean of the numpy.ndarray, numbers.

Create the package directory

The current directory is the repository, create another summarize directory to serve as the package.

$ mkdir summarize
$ cd summarize

When the package is installed on a system, you can imagine this directory and its contents being copied into the site-packages path.

Creating the __init__.py file

While the __init__.py file was designed to allow for a directory to be imported as a module with sub-modules, it is a good convention to always follow when creating packages.

In your preferred text editor, create a file called __init__.py that contains the following sections:

The header section

1
2
3
4
#!/usr/bin/env python

import numpy as np
import argparse

Line 1 tells your shell how to run this file when executed, and lines 3 and 4 import the packages necessary to run this python program. While argparse is built in to all python distributions, Numpy is an external dependency that must be installed to be imported and run.

To test whether you have numpy already installed on your system, you can use pip list to list out all installed packages.

$ pip list | grep numpy

If you don’t see a line for numpy, please install it using your preferred method.

$ pip install --user numpy

The main() function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def main():
    # CLI arguments
    parser = argparse.ArgumentParser(description='A simple tool for computing the mean of a random list')
    parser.add_argument('-N', metavar='INT', type=int, help='Number of random integers [%(default)s]', default=5)
    args = parser.parse_args()
    # Generate the random numbers
    numbers = gen_numbers(args.N)
    # Calculate the mean
    mean = summarize(numbers)
    # Print the mean
    print(mean)

Transforming your python script into a tool usable on the CLI through argparse can be done in as few as 3 lines. The first instruction (line 3) constructs the ArgumentParser object and also describes the the tool itself. Line 4 adds the first and only argument, which restricts values to integers and includes a description which states the default of 5 numbers. Line 5 parses the input and generates the args object. The value passed in through the -N parameter can then be accessed through args.N.

After setting up the CLI arguments, the random numbers are generated in line 7 and the mean is calculated in line 9. Finally, the calculated mean is printed before exiting.

The gen_numbers() function

After the main() function, add the gen_numbers() function to generate a variable length array of random integers.

1
2
3
4
5
def gen_numbers(n_numbers):
    '''
    Generates n_numbers integers ranging from 0 to 99
    '''
    return np.random.randint(100, size=n_numbers)

The summarize() function

Even though it may seem redundant, create a summarize() function to compute the mean of the input array. Creating a specialized function for this will help with testing later.

1
2
3
4
5
def summarize(numbers):
    '''
    Computes the mean of the numbers ndarray
    '''
    return np.mean(numbers)

Epilogue

1
2
if __name__ == "__main__":
    main()

These two lines tell python what to run when the script is invoked. In our case, the main() function is run. This section should always exist at the end of a file so all functions and global-scoped variables have already been initialized before running anything.

Current structure

At this point, you should have a directory called summarize containing the file __init__.py.

$ cd ..
$ tree summarize
summarize/
└── __init__.py

0 directories, 1 file

Assuming you have numpy already installed, running __init__.py with the -h argument should present you with its help text.

$ python summarize/__init__.py -h
usage: __init__.py [-h] [-N INT]

A simple tool for computing the mean of a random list

optional arguments:
  -h, --help  show this help message and exit
  -N INT      Number of random integers [5]

Running it without an argument should also return a number.

$ python summarize/__init__.py
52.2