Python as a Scripting Language

Developing a set of opinions

One of Python's strengths is as a language for quickly developing the sort of simple scripts system administration scripts that one might otherwise have written in Bash or Perl. Bash and Perl are both quite serviceable, but neither are as readable as Python, and using Python for our scripts accelerates our learning by enforcing Python everywhere.

So let's see what tools we might use for Python as a Scripting language.

One tool we'll need right away is the ability to call system commands and other programs external to Python. For this the module of choice for recent versions of Python is the os.subprocess module. Let's see if we can use this to convert the current notebook to HTML for us, but first, let's just see if we can print the first few lines of the file.

Running Programs Using the Subprocess module

In [3]:
import subprocess

# See if we can display output 1st!  Just running "jupyter nbconvert" will display a help screen.

# Set up the program and arguments as an array.  If we do it this way, we don't have to specify the 
# shell=True argument or worry about having full paths to the program (jupyter) that we're trying to run.
arguments = ['jupyter', 'nbconvert']

# Don't do this (although that will work if shell=True)
# arguments = 'jupyter nbconvert'

# Run it, and get the completed process stdout = subprocess.PIPE means don't throw away stdout.  We'll 
# be needing it.
result = subprocess.run(arguments, stdout = subprocess.PIPE)

# Convert bytes output to a string
output = result.stdout.decode("UTF-8")

# Print the first few lines...
lines = output.split("\n")

for i in range(6):
    print(lines[i])
This application is used to convert notebook files (*.ipynb) to various other
formats.

WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.

Options
In [4]:
# To convert our notebook to HTML, let's just add some arguments.

arguments.append('--to')
arguments.append('html')
arguments.append('--output')
arguments.append('tools_for_python_scripting.html')
arguments.append('tools_for_python_scripting.ipynb')

# Our total command line will look like 
# jupyter nbconvert --to html --output tools_for_python_scripting.html tools_for_python_scripting.ipynb
# In other words, do the conversion "to html" to the output html file specified, given the notebook file specified.
# Easy peasy!


result = subprocess.run(arguments, stdout = subprocess.PIPE)

# Does the file exist as we expect?

import os
print(os.path.exists('tools_for_python_scripting.html'))
True

Parsing arguments on the command line

There are a variety of options for parsing command line arguments in Python. Two that are supported as of Python 3.X are getopt and argparse. There are also parsers written that are outside of the core language itself, such as docopt, as discussed in Rob Wells' article you shoud be using docopt.

http://leancrew.com/all-this/2015/06/better-option-parsing-in-python-maybe/