SlideShare a Scribd company logo
THE PYTHON STD LIB BY EXAMPLE
– APPLICATION BUILDING BLOCKS
John
Saturday, December 21, 2013
Brief introduction
• This lesson covers some of the more frequently
reused building blocks that solve problems
common to many applications
• Command-line argument parsing: module
getopt,optparse,argparse.
• Interactive programs: readline, cmd,
shlex,fileinput
• Manage application configuration: ConfigParser
• Manage log file: logging
• Others: atexit, sched etc
Module getopt – option parsing
• The getopt() take 3 arguments:
1. The sequeence of arguments to be parsed.
Usually it is sys.argv[1:]
2. Single-character option, following a colon(:)
mean it require an argument. E.g. ab:c:, it mean
a is -a simple flag, while –b and –c require an
argument.
3. It is optional. If used, it is a list of long-style
option names. Having suffix “=“ means ruquire
an argument. E.g [‘noarg’,’witharg=‘]
Quick example 1
# file test.py
import getopt
import sys
opts,args = getopt.getopt(sys.argv[1:],’ab:c:’)
for opt in opts:
print opt
•Then run command line “python test.py –a –b valb –c valc”, the output
should be
('-a', '')
('-b', 'val')
('-c', 'val')
quick example 2
# file test.py
import getopt
import sys
opts,args = getopt.getopt(sys.argv[1:],’’,[‘opta’,’optb=‘,’optc=‘])
for opt in opts:
print opt
•run command line “python test.py --opta --optb val --optc val”
('--opta', '')
('--optb', 'val')
('--optc', 'val')
Module optparse
• it is a modern alternative for module getopt
Quick example

Example 2:
More options in add_option
method
• option dest: the attribute name
• option default: the default value. also can be set in
set_defaults method.
• option type: convert the argument string to specific type,
e.g. int, float,string
• option choices: validation use a list of candidate strings. e.g.
choices=[‘a’,’b’,’c’] . the valid value should be ‘a’, or ‘b’ or ‘c’
• option help: help message
• option action: store, store_const,store_true,append,acount
Module argparse
• argparse added to python 2.7 as a
replacement of optparse.
• Change the OptionParser by ArgumentParser,
and add_option by add_argument in
previous example.
More Options of ArgumentParser
• add help automatically:

parser = argparse.ArgumentParser(add_help=True)

• version control: version=‘1.0’ (-v or --version
will show the version number)
More options of add_argument
method
• All options in optparse.add_option method.
• option nargs: number of expected
arguments. (? mean 0 or 1 arguments, *
means 0 or all, + means 1 or all).
example
parser.add_argument(‘-c’,nargs=3)
we need write command line “perl -c 1 2 3”
Module getpass: handle passowrd
prompts securely
• print a promt and then read input from the user.

• We can change the prompt:
getpass(prompt=‘what is your favorite color?’)
MODULE CMD:
BUILD COMMAND
LINE PROCESSORS
brief introduction
• the Cmd class is used as base class for
interactive shells and other command
interpreters.
• It can provide interactive prompt, commandline editing, and command completion.
quick example

• method do_greet() connect with command “greet”
• method help_greet() connect with command “help greet”
auto-completion
• auto-completion is already enabled in previous
example.
• if user want to do user-defined on auto-completion,
use complete_<comand> (e.g. complete_greet() )
Overrite Base Class method
• Method cmploop(intro) is the main
processing loop. Each iteration in cmdloop
call parseline and onecmd.
• Method emptyline: behavior if emptyline.
Default behavior is that rerun previous
command.
• Method default: default method if command
is not found.
Running shell command
• An exclamation point(!) map to the do_shell()
method and is intended “shelling out” to run other
commands.
• First import subprocess module.
• Second, define do_shell() method
THE LOGGING
MODULE
Brief introduction
• The logging module define standard API for
reporting error and status information.
• Both application developer and module
author benefit from this module, but has
different considerations.
Logging to file
• Use basicConfig set the log file name
The level of the events logging
tracks
Level

Numeric
value

When it’s used

DEBU Detailed information, typically of interest only when
G
diagnosing problems.
INFO Confirmation that things are working as expected.
An indication that something unexpected happened, or
WAR
indicative of some problem in the near future (e.g. ‘disk space
NING
low’). The software is still working as expected.
ERRO Due to a more serious problem, the software has not been
R
able to perform some function.
CRITIC A serious error, indicating that the program itself may be
AL
unable to continue running.

10
20
30
40
50
Best practice: logging in single file
import logging
logging.basicConfig(level=logging.WARNING) # Only the level
equal or alove this level can be displayed.
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
•Write the log to file:
logging.basicConfig(filename='example.log',level=logging.DEBU
G)
Best practice: Logging from
multiple modules
• Define logging config
in main file

• Write log in other files

More Related Content

What's hot (20)

PYTHON PROGRAMMING
PYTHON PROGRAMMING
indupps
 
Python recursion
Python recursion
Prof. Dr. K. Adisesha
 
Review functions
Review functions
Kgr Sushmitha
 
Function overloading
Function overloading
Sudeshna Biswas
 
Header files of c++ unit 3 -topic 3
Header files of c++ unit 3 -topic 3
MOHIT TOMAR
 
Lecture#6 functions in c++
Lecture#6 functions in c++
NUST Stuff
 
Function overloading
Function overloading
Selvin Josy Bai Somu
 
Functions in C++
Functions in C++
Mohammed Sikander
 
Function overloading(C++)
Function overloading(C++)
Ritika Sharma
 
C# Method overloading
C# Method overloading
Prem Kumar Badri
 
C++ tokens and expressions
C++ tokens and expressions
NabeelaNousheen
 
Python functions
Python functions
Prof. Dr. K. Adisesha
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
03 function overloading
03 function overloading
Jasleen Kaur (Chandigarh University)
 
Chapter 10 Library Function
Chapter 10 Library Function
Deepak Singh
 
16717 functions in C++
16717 functions in C++
LPU
 
Functions in C++
Functions in C++
Nikhil Pandit
 
Function
Function
yash patel
 
Python advance
Python advance
Deepak Chandella
 
C++ Function
C++ Function
PingLun Liao
 

Similar to Python advanced 3.the python std lib by example – application building blocks (20)

Howto argparse
Howto argparse
Manuel Cueto
 
How to write a well-behaved Python command line application
How to write a well-behaved Python command line application
gjcross
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and More
Matt Harrison
 
Introduction to Python for Security Professionals
Introduction to Python for Security Professionals
Andrew McNicol
 
Introduction to python
Introduction to python
Syed Zaid Irshad
 
Python for Linux System Administration
Python for Linux System Administration
vceder
 
1B-Introduction_to_python.ppt
1B-Introduction_to_python.ppt
AmritMarwaha1
 
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
sangeeta borde
 
Tutorial on-python-programming
Tutorial on-python-programming
Chetan Giridhar
 
Python Programming1.ppt
Python Programming1.ppt
Rehnawilson1
 
OpenGurukul : Language : Python
OpenGurukul : Language : Python
Open Gurukul
 
Introduction to python 3
Introduction to python 3
Youhei Sakurai
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way Presentation
Amira ElSharkawy
 
vvvvReadme
vvvvReadme
Mitazaki Yan
 
Lesson1 python an introduction
Lesson1 python an introduction
Arulalan T
 
PyCon 2011: IronPython Command Line
PyCon 2011: IronPython Command Line
Lecturer UC Davis & Northwestern
 
Python ppt
Python ppt
Mohita Pandey
 
Pyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdf
Mattupallipardhu
 
BACKGROUND A shell provides a command-line interface for users. I.docx
BACKGROUND A shell provides a command-line interface for users. I.docx
wilcockiris
 
Python1_Extracted_PDF_20250404_1054.pptx
Python1_Extracted_PDF_20250404_1054.pptx
ecomwithfaith
 
How to write a well-behaved Python command line application
How to write a well-behaved Python command line application
gjcross
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and More
Matt Harrison
 
Introduction to Python for Security Professionals
Introduction to Python for Security Professionals
Andrew McNicol
 
Python for Linux System Administration
Python for Linux System Administration
vceder
 
1B-Introduction_to_python.ppt
1B-Introduction_to_python.ppt
AmritMarwaha1
 
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
sangeeta borde
 
Tutorial on-python-programming
Tutorial on-python-programming
Chetan Giridhar
 
Python Programming1.ppt
Python Programming1.ppt
Rehnawilson1
 
OpenGurukul : Language : Python
OpenGurukul : Language : Python
Open Gurukul
 
Introduction to python 3
Introduction to python 3
Youhei Sakurai
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way Presentation
Amira ElSharkawy
 
Lesson1 python an introduction
Lesson1 python an introduction
Arulalan T
 
BACKGROUND A shell provides a command-line interface for users. I.docx
BACKGROUND A shell provides a command-line interface for users. I.docx
wilcockiris
 
Python1_Extracted_PDF_20250404_1054.pptx
Python1_Extracted_PDF_20250404_1054.pptx
ecomwithfaith
 
Ad

More from John(Qiang) Zhang (7)

Git and github introduction
Git and github introduction
John(Qiang) Zhang
 
Python testing
Python testing
John(Qiang) Zhang
 
Introduction to jython
Introduction to jython
John(Qiang) Zhang
 
Introduction to cython
Introduction to cython
John(Qiang) Zhang
 
A useful tools in windows py2exe(optional)
A useful tools in windows py2exe(optional)
John(Qiang) Zhang
 
Python advanced 3.the python std lib by example – system related modules
Python advanced 3.the python std lib by example – system related modules
John(Qiang) Zhang
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in python
John(Qiang) Zhang
 
A useful tools in windows py2exe(optional)
A useful tools in windows py2exe(optional)
John(Qiang) Zhang
 
Python advanced 3.the python std lib by example – system related modules
Python advanced 3.the python std lib by example – system related modules
John(Qiang) Zhang
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in python
John(Qiang) Zhang
 
Ad

Recently uploaded (20)

FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Safe Software
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
June Patch Tuesday
June Patch Tuesday
Ivanti
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
SOFTTECHHUB
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Safe Software
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
June Patch Tuesday
June Patch Tuesday
Ivanti
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
SOFTTECHHUB
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 

Python advanced 3.the python std lib by example – application building blocks

  • 1. THE PYTHON STD LIB BY EXAMPLE – APPLICATION BUILDING BLOCKS John Saturday, December 21, 2013
  • 2. Brief introduction • This lesson covers some of the more frequently reused building blocks that solve problems common to many applications • Command-line argument parsing: module getopt,optparse,argparse. • Interactive programs: readline, cmd, shlex,fileinput • Manage application configuration: ConfigParser • Manage log file: logging • Others: atexit, sched etc
  • 3. Module getopt – option parsing • The getopt() take 3 arguments: 1. The sequeence of arguments to be parsed. Usually it is sys.argv[1:] 2. Single-character option, following a colon(:) mean it require an argument. E.g. ab:c:, it mean a is -a simple flag, while –b and –c require an argument. 3. It is optional. If used, it is a list of long-style option names. Having suffix “=“ means ruquire an argument. E.g [‘noarg’,’witharg=‘]
  • 4. Quick example 1 # file test.py import getopt import sys opts,args = getopt.getopt(sys.argv[1:],’ab:c:’) for opt in opts: print opt •Then run command line “python test.py –a –b valb –c valc”, the output should be ('-a', '') ('-b', 'val') ('-c', 'val')
  • 5. quick example 2 # file test.py import getopt import sys opts,args = getopt.getopt(sys.argv[1:],’’,[‘opta’,’optb=‘,’optc=‘]) for opt in opts: print opt •run command line “python test.py --opta --optb val --optc val” ('--opta', '') ('--optb', 'val') ('--optc', 'val')
  • 6. Module optparse • it is a modern alternative for module getopt
  • 8. More options in add_option method • option dest: the attribute name • option default: the default value. also can be set in set_defaults method. • option type: convert the argument string to specific type, e.g. int, float,string • option choices: validation use a list of candidate strings. e.g. choices=[‘a’,’b’,’c’] . the valid value should be ‘a’, or ‘b’ or ‘c’ • option help: help message • option action: store, store_const,store_true,append,acount
  • 9. Module argparse • argparse added to python 2.7 as a replacement of optparse. • Change the OptionParser by ArgumentParser, and add_option by add_argument in previous example.
  • 10. More Options of ArgumentParser • add help automatically: parser = argparse.ArgumentParser(add_help=True) • version control: version=‘1.0’ (-v or --version will show the version number)
  • 11. More options of add_argument method • All options in optparse.add_option method. • option nargs: number of expected arguments. (? mean 0 or 1 arguments, * means 0 or all, + means 1 or all). example parser.add_argument(‘-c’,nargs=3) we need write command line “perl -c 1 2 3”
  • 12. Module getpass: handle passowrd prompts securely • print a promt and then read input from the user. • We can change the prompt: getpass(prompt=‘what is your favorite color?’)
  • 14. brief introduction • the Cmd class is used as base class for interactive shells and other command interpreters. • It can provide interactive prompt, commandline editing, and command completion.
  • 15. quick example • method do_greet() connect with command “greet” • method help_greet() connect with command “help greet”
  • 16. auto-completion • auto-completion is already enabled in previous example. • if user want to do user-defined on auto-completion, use complete_<comand> (e.g. complete_greet() )
  • 17. Overrite Base Class method • Method cmploop(intro) is the main processing loop. Each iteration in cmdloop call parseline and onecmd. • Method emptyline: behavior if emptyline. Default behavior is that rerun previous command. • Method default: default method if command is not found.
  • 18. Running shell command • An exclamation point(!) map to the do_shell() method and is intended “shelling out” to run other commands. • First import subprocess module. • Second, define do_shell() method
  • 20. Brief introduction • The logging module define standard API for reporting error and status information. • Both application developer and module author benefit from this module, but has different considerations.
  • 21. Logging to file • Use basicConfig set the log file name
  • 22. The level of the events logging tracks Level Numeric value When it’s used DEBU Detailed information, typically of interest only when G diagnosing problems. INFO Confirmation that things are working as expected. An indication that something unexpected happened, or WAR indicative of some problem in the near future (e.g. ‘disk space NING low’). The software is still working as expected. ERRO Due to a more serious problem, the software has not been R able to perform some function. CRITIC A serious error, indicating that the program itself may be AL unable to continue running. 10 20 30 40 50
  • 23. Best practice: logging in single file import logging logging.basicConfig(level=logging.WARNING) # Only the level equal or alove this level can be displayed. logging.debug('This message should go to the log file') logging.info('So should this') logging.warning('And this, too') •Write the log to file: logging.basicConfig(filename='example.log',level=logging.DEBU G)
  • 24. Best practice: Logging from multiple modules • Define logging config in main file • Write log in other files