How to analyze arguments in Python

How to analyze arguments in Python

In this tutorial, learn how to parse one or more arguments from the command-line or terminal using the getopt, sys, and argparse modules.

If you plan a data science or a machine learning project, then it is not uncommon to get started developing it in a Jupyter Notebook. It provides interactivity to your computing tools, lets you run your modules quickly and so on. There are many pointers to choose Jupyter Notebooks as your primary weapon of choice, especially for doing data science or machine learning projects. However, this tutorial does not aim to shed light on that part.


Consider you need to run a Python script as a batch job for the purpose of data ingestion in your data science project. Or you need to run a Python script to deploy a machine learning model to a remote server. In cases like these, just executing python your_script.py might not be sufficient. You may need to pass more options or arguments along with python your_script.py. You may already be familiar with the usage of additional arguments while running commands (like psls, etc.) on a Linux terminal.

In this tutorial, you are going to learn about how to parse additional arguments, options from the command line that are passed along with a Python script. Here is the summary of the contents for today's tutorial:

  • A basic introduction to argument parsing

. Argument parsing in Python

Using sys.argv

Using getopt

Using argparse

Note that this tutorial assumes basic familiarity with Python.

What is argument parsing?

If you are a Linux user, you might already be knowing this, I bet. In order to get a summary of the files and folders present in a particular directory, the command ls is often used. A typical output of running the ls command looks like -


However, you can supply many options to the ls command, -l, for example. Let's take a look at what that output looks like -

By passing the -l option, you got more information. Now, there is a slight difference between an option and an argument in this context. To remove a file or a folder from your current working directory, rm command is often used. Suppose, you have a text file named demo.txt and you ran rm demo.txt in order to remove the text file. In this case, demo.txt is the argument which you applied to the rm command. If you do not pass any argument to the rm command, you will get an error like so -

So, what is the difference between an option and an argument? It is now pretty obvious. Options are optional to pass whereas arguments are (often) necessary to pass. It is not essential to pass anything to the ls command for it to produce any output.

Now, when you hit ls -l or rm demo.txt, the operating system parses it in a certain way under the hood. This mechanism is generally specified by the developers of the operating system, and it is known as parsing.

If you are interested in knowing more Linux commands and the different options and arguments they support, reading the book Unix Concepts and Applications is highly recommended.

So, that was a basic introduction to argument parsing. Almost all the programming languages come with support for argument parsing. Python is no exception to this. There are three very prevalent ways in which you can do argument parsing in Python -

  • sys.argv
  • getopt
  • argparse

You will now take each of the above options one by one and see how to use them for parsing arguments (and options). Let's go chronologically.

Argument Parsing using sys.argv

Let's start simple. In this first example, you will create a Python file named demo1.py. Your program will accept an arbitrary number of arguments passed from the command-line (or terminal) while getting executed. The program will print out the arguments that were passed and the total number of arguments.


For example, if you execute python demo1.py abc 123, then the program would yield -

Number of arguments: 3 
Argument(s) passed: ['demo1.py', 'abc', '123']

Notice that the first argument is always the name of the Python file. But the code can be modified to not include it.

Now coming to the code -

import sys

print('Number of arguments: {}'.format(len(sys.argv)))
print('Argument(s) passed: {}'.format(str(sys.argv)))

You first imported the Python module sys, which comes with a standard installation of Python. You then employed the argv submodule which returns the list of the arguments passed to a Python script where argv[0] contains the name of the Python script. If you run sys.argv in a code cell like the following, you get the list of the configuration files responsible for making the IPython kernel function properly. It is better to not mess with them.

sys.argv


['/home/nbuser/anaconda3_501/lib/python3.6/site-packages/ipykernel/main.py',
'-f',
'/home/nbuser/.local/share/jupyter/runtime/kernel-dabba4f7-e3e0-4c39-99d2-261ba835c53f.json']

Let's now see how to use the getopt module for parsing arguments (and options).

Argument parsing using getopt

In comparison to sys.argv, the getopt module offers much more flexibility. Let's design a sample scenario first and write the code accordingly.


Say, you have a released a Python script which you have not documented yet. But you want to be able to provide the users with meaningful feedback when they execute that script in a way it is not meant to be. Your Python script does the simple task of adding two numbers and provides an output. The only constraint is that the user needs to pass the inputs in the form of command-line arguments along with the Python script.

Talking a bit more practically, the script ideally should be executed like -

python add_numbers.py -a 3 -b 8

The output should be 11. Here, -a and -b are the options, and 3, 8 are the arguments that you provide to the script. The options not only enhance the readability part but also helps to decide the evaluation flow (consider if you are dividing instead of doing addition). Let's start looking at the code for this first.

import getopt
import sys

Get the arguments from the command-line except the filename

argv = sys.argv[1:]
sum = 0

try:
# Define the getopt parameters
opts, args = getopt.getopt(argv, 'a:b:', ['foperand', 'soperand'])
# Check if the options' length is 2 (can be enhanced)
if len(opts) == 0 and len(opts) > 2:
print ('usage: add.py -a <first_operand> -b <second_operand>')
else:
# Iterate the options and get the corresponding values
for opt, arg in opts:
sum += int(arg)
print('Sum is {}'.format(sum))

except getopt.GetoptError:
# Print something useful
print ('usage: add.py -a <first_operand> -b <second_operand>')
sys.exit(2)

The idea is to first get all the arguments using sys.argv and then process it accordingly. Let's now come to the most important line of code - opts, args = getopt.getopt(argv, 'a:b:', ['foperand', 'soperand'])

The signature of the getopt() method looks like:

getopt.getopt(args, shortopts, longopts=[])

  • args is the list of arguments taken from the command-line.
  • shortopts is where you specify the option letters. If you supply a:, then it means that your script should be supplied with the option a followed by a value as its argument. Technically, you can use any number of options here. When you pass these options from the command-line, they must be prepended with '-'.
  • longopts is where you can specify the extended versions of the shortopts. They must be prepended with '--'.

You defined the shortopts to be a:b: which means your Python script would take two options as letters - 'a' and 'b'. By specifying ':' you are explicitly telling that these options will be followed by arguments.

You defined the longopts to be ['foperand', 'soperand'] which is a way of telling the code to accept f_operand and s_operand in place of a and b as the options. But these should always follow --.

The getopt module provides you with a handy Exception class GetoptError also for defining useful messages, so a to guide the user on how to use your Python script. This is why you wrapped the functional part of your script in a try block and defined the except block accordingly.

Here is how you can run the Python script:

Note that the above script was not defined to handle the longopts. This is something you can try as an exercise.

Let's now see how to use argparse for argument parsing.

Argument parsing using argparse

From the above two options, it is quite viable that they are not very readable. Once you see argparse in action, you will also agree that the above two options lack on the flexibility part as well. To understand the usage of argparse, let's start with a code snippet which implements the scenario you saw in the above section.

import argparse

Construct the argument parser

ap = argparse.ArgumentParser()

Add the arguments to the parser

ap.add_argument("-a", "--foperand", required=True,
help="first operand")
ap.add_argument("-b", "--soperand", required=True,
help="second operand")
args = vars(ap.parse_args())

Calculate the sum

print("Sum is {}".format(int(args['foperand']) + int(args['soperand'])))

First things first, argparse like the other two modules discussed above, comes with the standard installation of Python. You start by instantiating the argparse object. And the rest of the things become so simpler to write. Let's take the following line of code for example -

ap.add_argument("-a", "--foperand", required=True, help="first operand")

Here you added the argument that you expect to be supplied with the Python script when it is run. You provided the letter version of the argument along with its extended one. By specifying required=True you are explicitly asking the user to supply that particular argument. Finally, you appended a meaningful description of the argument which will be shown to the user in case he does not execute the script in the right manner.

The argument definition part is the same for the second argument, as well. You access the given arguments by specifying their respective indices.

The above code-snippet clearly shows how easy and flexible it is to define command-line argument parsing tasks with argparse. Here is how you can play with the above Python script:

Notice how much easier the process became with argparse.

What's next?

Thank you for reading today's tutorial until the end. Now, you are equipped with the native Python modules that are able to parse arguments supplied from the command-line along with your Python script. As an exercise, you can customize your machine learning and data science projects accordingly to facilitate argument parsing and turn them into useful script utilities. Not just machine learning or data science, but anywhere you feel it is relevant enough.

=======================================================================

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter


Learn More

☞ Complete Python Bootcamp: Go from zero to hero in Python 3

☞ Python for Time Series Data Analysis

☞ Python Programming For Beginners From Scratch

☞ Python Network Programming | Network Apps & Hacking Tools

☞ Intro To SQLite Databases for Python Programming

☞ Ethical Hacking With Python, JavaScript and Kali Linux

☞ Beginner’s guide on Python: Learn python from scratch! (New)

☞ Python for Beginners: Complete Python Programming


Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Description
Learn Hands-On Python Programming By Creating Projects, GUIs and Graphics

Python is a dynamic modern object -oriented programming language
It is easy to learn and can be used to do a lot of things both big and small
Python is what is referred to as a high level language
Python is used in the industry for things like embedded software, web development, desktop applications, and even mobile apps!
SQL-Lite allows your applications to become even more powerful by storing, retrieving, and filtering through large data sets easily
If you want to learn to code, Python GUIs are the best way to start!

I designed this programming course to be easily understood by absolute beginners and young people. We start with basic Python programming concepts. Reinforce the same by developing Project and GUIs.

Why Python?

The Python coding language integrates well with other platforms – and runs on virtually all modern devices. If you’re new to coding, you can easily learn the basics in this fast and powerful coding environment. If you have experience with other computer languages, you’ll find Python simple and straightforward. This OSI-approved open-source language allows free use and distribution – even commercial distribution.

When and how do I start a career as a Python programmer?

In an independent third party survey, it has been revealed that the Python programming language is currently the most popular language for data scientists worldwide. This claim is substantiated by the Institute of Electrical and Electronic Engineers, which tracks programming languages by popularity. According to them, Python is the second most popular programming language this year for development on the web after Java.

Python Job Profiles
Software Engineer
Research Analyst
Data Analyst
Data Scientist
Software Developer
Python Salary

The median total pay for Python jobs in California, United States is $74,410, for a professional with one year of experience
Below are graphs depicting average Python salary by city
The first chart depicts average salary for a Python professional with one year of experience and the second chart depicts the average salaries by years of experience
Who Uses Python?

This course gives you a solid set of skills in one of today’s top programming languages. Today’s biggest companies (and smartest startups) use Python, including Google, Facebook, Instagram, Amazon, IBM, and NASA. Python is increasingly being used for scientific computations and data analysis
Take this course today and learn the skills you need to rub shoulders with today’s tech industry giants. Have fun, create and control intriguing and interactive Python GUIs, and enjoy a bright future! Best of Luck
Who is the target audience?

Anyone who wants to learn to code
For Complete Programming Beginners
For People New to Python
This course was designed for students with little to no programming experience
People interested in building Projects
Anyone looking to start with Python GUI development
Basic knowledge
Access to a computer
Download Python (FREE)
Should have an interest in programming
Interest in learning Python programming
Install Python 3.6 on your computer
What will you learn
Build Python Graphical User Interfaces(GUI) with Tkinter
Be able to use the in-built Python modules for their own projects
Use programming fundamentals to build a calculator
Use advanced Python concepts to code
Build Your GUI in Python programming
Use programming fundamentals to build a Project
Signup Login & Registration Programs
Quizzes
Assignments
Job Interview Preparation Questions
& Much More

Guide to Python Programming Language

Guide to Python Programming Language

Guide to Python Programming Language

Description
The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.

The course is begin continuously developing by adding lectures regularly.

Please see the Promo and free sample video to get to know more.

Hope you will enjoy it.

Basic knowledge
An Enthusiast Mind
A Computer
Basic Knowledge To Use Computer
Internet Connection
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Description
Hello and welcome to brand new series of wiredwiki. In this series i will teach you guys all you need to know about python. This series is designed for beginners but that doesn't means that i will not talk about the advanced stuff as well.

As you may all know by now that my approach of teaching is very simple and straightforward.In this series i will be talking about the all the things you need to know to jump start you python programming skills. This series is designed for noobs who are totally new to programming, so if you don't know any thing about

programming than this is the way to go guys Here is the links to all the videos that i will upload in this whole series.

In this video i will talk about all the basic introduction you need to know about python, which python version to choose, how to install python, how to get around with the interface, how to code your first program. Than we will talk about operators, expressions, numbers, strings, boo leans, lists, dictionaries, tuples and than inputs in python. With

Lots of exercises and more fun stuff, let's get started.

Download free Exercise files.

Dropbox: https://bit.ly/2AW7FYF

Who is the target audience?

First time Python programmers
Students and Teachers
IT pros who want to learn to code
Aspiring data scientists who want to add Python to their tool arsenal
Basic knowledge
Students should be comfortable working in the PC or Mac operating system
What will you learn
know basic programming concept and skill
build 6 text-based application using python
be able to learn other programming languages
be able to build sophisticated system using python in the future

To know more: