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

24
THE PYTHON STD LIB BY EXAMPLE – APPLICATION BUILDING BLOCKS John Thursday, June 16, 2022

Upload: john-zhang

Post on 23-Jun-2015

117 views

Category:

Technology


1 download

TRANSCRIPT

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

THE PYTHON STD LIB BY EXAMPLE – APPLICATION BUILDING BLOCKS

JohnThursday, April 13, 2023

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

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

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

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=‘]

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

Quick example 1# file test.pyimport getoptimport sysopts,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')

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

quick example 2

# file test.pyimport getoptimport sysopts,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')

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

Module optparse

• it is a modern alternative for module getopt

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

Quick example

Example 2:

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

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

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

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.

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

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)

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

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).exampleparser.add_argument(‘-c’,nargs=3)we need write command line “perl -c 1 2 3”

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

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?’)

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

MODULE CMD: BUILD COMMAND LINE PROCESSORS

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

brief introduction

• the Cmd class is used as base class for interactive shells and other command interpreters.

• It can provide interactive prompt, command-line editing, and command completion.

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

quick example

• method do_greet() connect with command “greet”• method help_greet() connect with command “help greet”

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

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() )

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

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.

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

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

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

THE LOGGING MODULE

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

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.

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

Logging to file

• Use basicConfig set the log file name

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

The level of the events logging tracks

Level When it’s used Numeric value

DEBUG

Detailed information, typically of interest only when diagnosing problems. 10

INFO Confirmation that things are working as expected. 20

WARNING

An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.

30

ERROR

Due to a more serious problem, the software has not been able to perform some function. 40

CRITICAL

A serious error, indicating that the program itself may be unable to continue running. 50

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

Best practice: logging in single file

import logginglogging.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.DEBUG)

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

Best practice: Logging from multiple modules• Define logging config

in main file

• Write log in other files