Python basis - Standard Library

Python standard library (Python Standrad Library) contains a number of useful module, but also a part of each of the standard Python installation package. Familiar with the Python standard library is very important, because as long as you are familiar with these libraries can do anything, a lot of the problems can be easily solved.

We will explore some of the common library module. You can attach in your Python installation package document "Overview of library (Library Reference)" section to find full details of all modules.

Let's look at some useful modules.

Note : If you think this chapter too advanced, you can skip this chapter. However, I strongly recommend that you adapt to program in Python and then take a look at this chapter.

1. sysModule

sysModules include some of the features for a particular system. We have learned through the sys.argvlist includes command-line parameters.

Imagine some of the Python software we need to check the version being used, systhe module will give us relevant information.

>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=5, micro=1, releaselevel='final', serial=0)
>>> sys.version_info.major == 3
True

sysModule contains a version_infotuple, it provides us the version information. The first entry is the major version information. We can recall that information and use it.

2. Log module

If you want some debugging (Debugging) information or some important information is stored in one place, so you can check whether your program as you would expect to run like that, how should do? How should this information "stored somewhere"? This can loggingbe achieved module.

Save as stdlib_logging.py:

import os
import platform
import logging

if platform.platform().startswith('Windows'):
    logging_file = os.path.join(os.getenv('HOMEDRIVE'),
                                os.getenv('HOMEPATH'),
                                'test.log')
else:
    logging_file = os.path.join(os.getenv('HOME'),
                                'test.log')

print("Logging to", logging_file)

logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s : %(levelname)s : %(message)s',
    filename=logging_file,
    filemode='w',
)

logging.debug("Start of the program")
logging.info("Doing something")
logging.warning("Dying now")

Output:

$ python stdlib_logging.py
Logging to /Users/swa/test.log

$ cat /Users/swa/test.log
2014-03-29 09:27:36,660 : DEBUG : Start of the program
2014-03-29 09:27:36,660 : INFO : Doing something
2014-03-29 09:27:36,660 : WARNING : Dying now

If you can not run the catcommand, you can open a text editor test.logfile.

We used three standard library modules ---- osmodules to interact with the operating system, platforminformation module for acquiring platform ---- ---- Operating System, the loggingmodule is used to record * (Log) * Information .

First of all, we have by checking platform.platform()to confirm that the operating system we are using the returned string (For more information, see import platform; help(platform)). If it is Windows, we will find out the primary drive (Home Drive), home folder (Home Folder), and we want the file name to store information. It brings together these three parts, we get all the information about the location of the file. For other platforms, we need to know is the user's home folder location, so that we can get all the information on the location of the file.

We use the os.path.join()function to these three position-information aggregated together. Using this special function, not just these reasons paragraphs string pieced together is that this function will ensure that the full path to the location in line with the current operating system expected format.

Then we configure the loggingmodule, in a specific format it will write all the information we specified file.

Finally, whether the information is used for debugging, reminders, warnings and even other critical information, we can be polymerized and recorded. Once the program is running, we can examine this document, so we can know what the program is running what happened, even if it did not show what information the user at runtime.

Weekly Module Series

There are many standard library modules worth exploring, for example, some of the modules for commissioning (Debugging) of , processing module command-line options , regular expression (Regular Expressions) module and so on and so forth.

The best way to further explore the standard library is to read good writing by Doug Hellmann's Python Module of the Week series (you can also read it in a physical book or read the official Python documentation ).

to sum up

We have explored a number of functional modules of the Python standard library provides many. I strongly recommend you visit the Python standard library documentation for all modules can be used.

Next, we will introduce various other aspects of Python, Python let our trip more complete.

Resources

[1] Standard Library · A Byte of Python

Published 66 original articles · won praise 101 · views 30000 +

Guess you like

Origin blog.csdn.net/u010705932/article/details/104419180