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

PYTHON PROGRAMMING
PYTHON PROGRAMMINGPYTHON PROGRAMMING
PYTHON PROGRAMMING
indupps
 
Python recursion
Python recursionPython recursion
Python recursion
Prof. Dr. K. Adisesha
 
Review functions
Review functionsReview functions
Review functions
Kgr Sushmitha
 
Function overloading
Function overloadingFunction overloading
Function overloading
Sudeshna Biswas
 
Header files of c++ unit 3 -topic 3
Header files of c++ unit 3 -topic 3Header 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++Lecture#6 functions in c++
Lecture#6 functions in c++
NUST Stuff
 
Function overloading
Function overloadingFunction overloading
Function overloading
Selvin Josy Bai Somu
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
Ritika Sharma
 
C# Method overloading
C# Method overloadingC# Method overloading
C# Method overloading
Prem Kumar Badri
 
C++ tokens and expressions
C++ tokens and expressionsC++ tokens and expressions
C++ tokens and expressions
NabeelaNousheen
 
Python functions
Python functionsPython functions
Python functions
Prof. Dr. K. Adisesha
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
03 function overloading
03 function overloading03 function overloading
03 function overloading
Jasleen Kaur (Chandigarh University)
 
Chapter 10 Library Function
Chapter 10 Library FunctionChapter 10 Library Function
Chapter 10 Library Function
Deepak Singh
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Nikhil Pandit
 
Function
FunctionFunction
Function
yash patel
 
Python advance
Python advancePython advance
Python advance
Deepak Chandella
 
C++ Function
C++ FunctionC++ Function
C++ Function
PingLun Liao
 

What's hot (20)

PYTHON PROGRAMMING
PYTHON PROGRAMMINGPYTHON PROGRAMMING
PYTHON PROGRAMMING
 
Python recursion
Python recursionPython recursion
Python recursion
 
Review functions
Review functionsReview functions
Review functions
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Header files of c++ unit 3 -topic 3
Header files of c++ unit 3 -topic 3Header files of c++ unit 3 -topic 3
Header files of c++ unit 3 -topic 3
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 
C# Method overloading
C# Method overloadingC# Method overloading
C# Method overloading
 
C++ tokens and expressions
C++ tokens and expressionsC++ tokens and expressions
C++ tokens and expressions
 
Python functions
Python functionsPython functions
Python functions
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
03 function overloading
03 function overloading03 function overloading
03 function overloading
 
Chapter 10 Library Function
Chapter 10 Library FunctionChapter 10 Library Function
Chapter 10 Library Function
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Function
FunctionFunction
Function
 
Python advance
Python advancePython advance
Python advance
 
C++ Function
C++ FunctionC++ Function
C++ Function
 

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

Python for Security Professionals
Python for Security ProfessionalsPython for Security Professionals
Python for Security Professionals
Aditya Shankar
 
Python and You Series
Python and You SeriesPython and You Series
Python and You Series
Karthik Prakash
 
TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVA
MuskanSony
 
Control structures functions and modules in python programming
Control structures functions and modules in python programmingControl structures functions and modules in python programming
Control structures functions and modules in python programming
Srinivas Narasegouda
 
Howto argparse
Howto argparseHowto argparse
Howto argparse
Manuel Cueto
 
Porting to Python 3
Porting to Python 3Porting to Python 3
Porting to Python 3
Lennart Regebro
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
baran19901990
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
Sangharsh agarwal
 
Debugging Python with gdb
Debugging Python with gdbDebugging Python with gdb
Debugging Python with gdb
Roman Podoliaka
 
08 subprograms
08 subprograms08 subprograms
08 subprograms
baran19901990
 
Unit iii
Unit iiiUnit iii
Unit iii
snehaarao19
 
UNIT III.ppt
UNIT III.pptUNIT III.ppt
UNIT III.ppt
Ajit Mali
 
UNIT III (2).ppt
UNIT III (2).pptUNIT III (2).ppt
UNIT III (2).ppt
VGaneshKarthikeyan
 
Nitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptxNitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptx
shivam460694
 
c++ referesher 1.pdf
c++ referesher 1.pdfc++ referesher 1.pdf
c++ referesher 1.pdf
AnkurSingh656748
 
Hadoop cluster performance profiler
Hadoop cluster performance profilerHadoop cluster performance profiler
Hadoop cluster performance profiler
Ihor Bobak
 
Pemrograman Python untuk Pemula
Pemrograman Python untuk PemulaPemrograman Python untuk Pemula
Pemrograman Python untuk Pemula
Oon Arfiandwi
 
Python
PythonPython
Porting to Python 3
Porting to Python 3Porting to Python 3
Porting to Python 3
Lennart Regebro
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/Invoke
Dmitri Nesteruk
 

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

Python for Security Professionals
Python for Security ProfessionalsPython for Security Professionals
Python for Security Professionals
 
Python and You Series
Python and You SeriesPython and You Series
Python and You Series
 
TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVA
 
Control structures functions and modules in python programming
Control structures functions and modules in python programmingControl structures functions and modules in python programming
Control structures functions and modules in python programming
 
Howto argparse
Howto argparseHowto argparse
Howto argparse
 
Porting to Python 3
Porting to Python 3Porting to Python 3
Porting to Python 3
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
Debugging Python with gdb
Debugging Python with gdbDebugging Python with gdb
Debugging Python with gdb
 
08 subprograms
08 subprograms08 subprograms
08 subprograms
 
Unit iii
Unit iiiUnit iii
Unit iii
 
UNIT III.ppt
UNIT III.pptUNIT III.ppt
UNIT III.ppt
 
UNIT III (2).ppt
UNIT III (2).pptUNIT III (2).ppt
UNIT III (2).ppt
 
Nitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptxNitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptx
 
c++ referesher 1.pdf
c++ referesher 1.pdfc++ referesher 1.pdf
c++ referesher 1.pdf
 
Hadoop cluster performance profiler
Hadoop cluster performance profilerHadoop cluster performance profiler
Hadoop cluster performance profiler
 
Pemrograman Python untuk Pemula
Pemrograman Python untuk PemulaPemrograman Python untuk Pemula
Pemrograman Python untuk Pemula
 
Python
PythonPython
Python
 
Porting to Python 3
Porting to Python 3Porting to Python 3
Porting to Python 3
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/Invoke
 

More from John(Qiang) Zhang

Git and github introduction
Git and github introductionGit and github introduction
Git and github introduction
John(Qiang) Zhang
 
Python testing
Python  testingPython  testing
Python testing
John(Qiang) Zhang
 
Introduction to jython
Introduction to jythonIntroduction to jython
Introduction to jython
John(Qiang) Zhang
 
Introduction to cython
Introduction to cythonIntroduction to cython
Introduction to cython
John(Qiang) Zhang
 
A useful tools in windows py2exe(optional)
A useful tools in windows py2exe(optional)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 modulesPython 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 pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in python
John(Qiang) Zhang
 

More from John(Qiang) Zhang (7)

Git and github introduction
Git and github introductionGit and github introduction
Git and github introduction
 
Python testing
Python  testingPython  testing
Python testing
 
Introduction to jython
Introduction to jythonIntroduction to jython
Introduction to jython
 
Introduction to cython
Introduction to cythonIntroduction to cython
Introduction to cython
 
A useful tools in windows py2exe(optional)
A useful tools in windows py2exe(optional)A useful tools in windows py2exe(optional)
A useful tools in windows py2exe(optional)
 
Python advanced 3.the python std lib by example – system related modules
Python advanced 3.the python std lib by example – system related modulesPython advanced 3.the python std lib by example – system related modules
Python advanced 3.the python std lib by example – system related modules
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in python
 

Recently uploaded

GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
fredae14
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
jpupo2018
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
David Brossard
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 

Recently uploaded (20)

GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 

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