程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Introduction to Python zero foundation-10 standard library

編輯:Python

10 Introduction to standard library

10.1 Operating system interface os

os  Modules provide many functions that interact with the operating system :

os.getcwd() Return to the current directory

os.chdir() Change the current working directory

os.system() stay shell Middle execution command

dir() Return functions and properties in the module

help() Return to module help

>>> import os
>>> os.getcwd() # Return to the current directory
'C:\\Python310'
>>> os.chdir('/server/accesslogs') # Change the current working directory
>>> os.system('mkdir today') # Create folder ‘today’
>>> import os
>>> dir(os)
<returns a list of all module functions>
>>> help(os)
<returns an extensive manual page created from the module's docstrings>

For daily file and directory management tasks , shutil  The module provides a higher level interface that is easier to use :

Copy copyfile、 Moving files move

>>> import shutil
>>> shutil.copyfile('data.db', 'archive.db') # src, dest
'archive.db'
>>> shutil.move('/build/executables', 'installdir')
'installdir'

10.2.  File wildcard

glob  Module provides a function to create a list of files by using wildcard search in the directory :

>>> import glob
>>> glob.glob('*.py')
['primes.py', 'random.py', 'quote.py']

10.3.  Command line arguments

General utility scripts usually need to handle command line arguments . These parameters are stored as a list in  sys  Modular  argv  Properties of the . for example , Output command line arguments :

establish demo.py, write in

import sys
print(sys.argv))

Then run the terminal  python demo.py one two three

Terminal output :

['demo.py', 'one', 'two', 'three']

You can see , The first 0 The first parameter is the name of the file .

argparse  The module provides a more complex mechanism for handling command-line parameters . The following script can extract one or more file names , And you can select the number of lines to display :

import argparse
parser = argparse.ArgumentParser(
prog='top',
description='Show top lines from each file')
parser.add_argument('filenames', nargs='+')
parser.add_argument('-l', '--lines', type=int, default=10)
args = parser.parse_args()
print(args)

When passing  python top.py --lines=5 alpha.txt beta.txt  When run from the command line , The script will  args.lines  Set to  5  And will  args.filenames  Set to  ['alpha.txt', 'beta.txt'].

10.4.  Error output redirection and program termination

sys  The module also has  stdin , stdout  and  stderr  Properties of . The latter is useful for warning and error messages , Even in  stdout  You can see them back when they're reset :

>>> sys.stderr.write('Warning, log file not found starting a new one\n')
Warning, log file not found starting a new one

The most direct way to terminate a script is to use  sys.exit() .

10.5.  String pattern matching

re  Module provides for advanced string processing Regular expressions Tools . For complex matching and operation , Regular expressions provide simplicity , Optimized solution . In reptiles , We often need to use re Extract the required URL .

>>> import re
>>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')
['foot', 'fell', 'fastest']
>>> re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat')
'cat in the hat'

When only simple functions are needed , String methods are preferred because they are easier to read and debug :

>>> 'tea for too'.replace('too', 'two')
'tea for two'

10.6.  mathematics

math  Module provides the bottom layer of floating-point mathematics C Access to library functions :

>>> import math
>>> math.cos(math.pi / 4)
0.70710678118654757
>>> math.log(1024, 2)
10.0

random  Module provides tools for random selection :

>>> import random
>>> random.choice(['apple', 'pear', 'banana'])
'apple'
>>> random.sample(range(100), 10) # sampling without replacement
[30, 83, 16, 4, 8, 81, 41, 50, 18, 33]
>>> random.random() # random float
0.17970987693706186
>>> random.randrange(6) # random integer chosen from range(6)
4

statistics  The module calculates the basic statistical properties of numerical data ( mean value , Median , Variance, etc ):

>>> import statistics
>>> data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]
>>> statistics.mean(data)
1.6071428571428572
>>> statistics.median(data)
1.25
>>> statistics.variance(data)
1.3720238095238095

SciPy project <https://scipy.org> There are many other modules for numerical calculation ( Fourier transform 、 Signal processing, etc ).

10.7.  Internet access

There are many modules that can be used to access the Internet and handle Internet protocols . Two of the simplest  urllib.request  For from URL Retrieving data , as well as  smtplib  For sending mail .( Third party Library requests Than urllib More convenient )

>>> from urllib.request import urlopen
>>> with urlopen('http://worldtimeapi.org/api/timezone/etc/UTC.txt') as response:
... for line in response:
... line = line.decode() # Convert bytes to a str
... if line.startswith('datetime'):
... print(line.rstrip()) # Remove trailing newline
...
datetime: 2022-01-01T01:36:47.689215+00:00
>>> import smtplib
>>> server = smtplib.SMTP('localhost')
>>> server.sendmail('[email protected]', '[email protected]',
... """To: [email protected]
... From: [email protected]
...
... Beware the Ides of March.
... """)
>>> server.quit()

( Please note that , The second example needs to be in localhost Mail server running on .)

10.8.  Date and time

datetime  Module provides classes that operate on dates and times in simple and complex ways . Although support date and time algorithm , But the focus of the implementation is effective member extraction for output formatting and operation . The module also supports time zone aware objects .

>>> # dates are easily constructed and formatted
>>> from datetime import date
>>> now = date.today()
>>> now
datetime.date(2022, 6, 12)
>>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
'06-12-22\. 12 Jun 2022 is a Sunday on the 12 day of June.'
>>> # dates support calendar arithmetic
>>> birthday = date(1964, 7, 31)
>>> age = now - birthday
>>> age.days
14368

10.9.  data compression

Common data archiving and compression formats are directly supported by modules , Include :zlibgzipbz2lzmazipfile  and  tarfile.:

>>> import zlib
>>> s = b'witch which has which witches wrist watch'
>>> len(s)
41
>>> t = zlib.compress(s)
>>> len(t)
37
>>> zlib.decompress(t)
b'witch which has which witches wrist watch'
>>> zlib.crc32(s)
226805979

10.10.  Performance measurement

some Python Users are interested in understanding the relative performance of different approaches to the same problem . Python Provides a measurement tool that can immediately answer these questions .

for example , Tuple packets and unpacking may be more attractive than traditional exchange parameters .timeit  Module can quickly demonstrate some advantages in operating efficiency :

>>> from timeit import Timer
>>> Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
0.57535828626024577
>>> Timer('a,b = b,a', 'a=1; b=2').timeit()
0.54962537085770791

And  timeit  The level of fine granularity is the opposite , profile  and  pstats  Module provides tools for identifying time critical parts in larger code blocks .

10.11 The quality control

One way to develop high-quality software is to write tests for each function during the development process , And run these tests often during the development process .

doctest  Module provides a tool , Used to scan the module and verify the program docstring Embedded in test . Test construction is as simple as cutting and pasting a typical call and its results into a document string . This improves the documentation by providing the user with examples , And it allows doctest The module ensures that the code remains true to the document :

def average(values):
"""Computes the arithmetic mean of a list of numbers.
>>> print(average([20, 30, 70]))
40.0
"""
return sum(values) / len(values)
import doctest
doctest.testmod() # automatically validate the embedded tests

unittest  Modules don't look like  doctest  Modules are so easy to use , But it allows a more comprehensive set of tests to be maintained in a single file :

import unittest
class TestStatisticalFunctions(unittest.TestCase):
def test_average(self):
self.assertEqual(average([20, 30, 70]), 40.0)
self.assertEqual(round(average([1, 5, 7]), 1), 4.3)
with self.assertRaises(ZeroDivisionError):
average([])
with self.assertRaises(TypeError):
average(20, 30, 70)
unittest.main() # Call all test methods from the command line .

10.12.  With batteries

Python Yes “ With batteries ” Idea . This is best seen through the complexity and power of its packages . for example :

  • The xmlrpc.client and xmlrpc.server modules make implementing remote procedure calls into an almost trivial task. Despite the modules’ names, no direct knowledge or handling of XML is needed.

  • email  Package is a library for managing email , Include MIME And other compliance  RFC 2822  Standardized mail documents . And  smtplib  and  poplib  Different ( What they actually do is send and receive messages ), The email package provides a complete tool set , Used to build or decode complex message structures ( Including accessories ) And the implementation of Internet coding and header protocol .

  • json  Package provides powerful support for parsing this popular data exchange format . csv  Module supports reading and writing files directly in comma separated value format , This format is usually supported by databases and spreadsheets . XML Deal with by  xml.etree.ElementTree , xml.dom  and  xml.sax  Package support . These modules and software packages together greatly simplify Python Data exchange between applications and other tools .

  • sqlite3  The module is SQLite The wrappers of the database , A slightly nonstandard SQL Syntax update and access persistent database .

  • Internationalization is supported by many modules , Include  gettext , locale , as well as  codecs  package .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved