Python Common Modules

Source: Internet
Author: User
Tags locale month name shuffle string format string to file timedelta

Common modules:

1.time and datetime modules;
2.random module;
3.os, SYS and shutil modules;
4.json, pickle and shelve modules;
5.xml and ymal processing;
6.configparser and sbuprocess modules;
7.hashlib module;
8.collections module;

Modules and Packages
1. Concept

    • The module is used to logically organize Python code (variables, functions, classes, logic: Implementing a function), which is essentially a python file that ends with a. py. (Example: File: test.py, corresponding module name: test);
    • Package: Used to logically organize the module, which is essentially a directory (must have a __init__.py file);

2. How to Import

    • Import Module_name: The essence is to interpret the module_name once and assign the interpretation result to module_name;
    • From module_name import name: Essentially puts the name variable in the module_name in the current program, so it can print out the value of the name variable when it is called directly. Remember to call the module only when the import module name, do not need to add. py;
    • Import module_name1, Module_name2,... : Import multiple modules;
    • From module_name import name as NM: Aliases the import module;
    • From module_name Import * (this method is not recommended);

3. The essence of the import module is to interpret the Python file again
Import module_name---> module_name.py > module_name.py path--->sys.path;

4. The essence of the import package is to execute the __init__.py file under the package, so if you want to invoke the Python program under the package, you need to import the required programs under the __init__.py package;

1.time and datetime modules

A common time representation method in Python:
A. Time stamp;
B. Formatted time string;
C.struct_time (meta-group);
Time Module method (from source code):

ImportTime# Timestamp: Returns the time (in seconds) from January 1, 1970 00:00:00 until nowTime_time=Time.time ()Print("Time_time is%s" %Time_time)# struct Time: Returns the format of the struct in UTC, or the current time if no arguments are passedGm_time=Time.gmtime (Time_time)Print("Gm_time is {}".format(Gm_time))# struct Time: Returns the struct format for local times, or the current time if no arguments are passedLocal_time=Time.localtime ()Print("Local_time is {}".format(Local_time))# Return to processor Time, 3.3 start obsolete, change to time.process_time () measuring processor time, unstable, not on MacClock_time=Time.clock ()Print("Clock_time is {}".format(Clock_time))# return process run timeProcess_time=Time.process_time ()Print("Process_time is {}".format(Process_time))# Delay execution time in seconds; the function has no return valueSleep_time=Time.sleep (2)Print("Sleep time is {}".format(Sleep_time))# Returns the time difference from UTC, in secondsAlt_zone=Time.altzonePrint("Alt_zone is {}".format(Alt_zone))# The return time format is: Fri May 4 14:54:43 2018; If the argument is passed, the parameter is converted; otherwise the current time is converted; The parameter is a time-tupleAsc_time=Time.asctime ()Print("Asc_time is {}".format(Asc_time))# The return time format is: Fri May 4 14:57:19 2018; If the argument is passed, the parameter is converted; otherwise the current time is converted; The parameter is secondsC_time=Time.ctime ()Print("C_time is {}".format(C_time))# return Timestamp: Turn the struct time object into a timestampMk_time=Time.mktime (Time.localtime ())Print("Mk_time is {}".format(Mk_time))# Return string time: convert UTC struct_time format to the specified string format timeStrf_time=Time.strftime ("%d:%m:%Y%H:%m:%s ", Time.gmtime ())Print("Strf_time is {}".format(Strf_time))# return Struct_time time: Turn string time into Struct_time format timeStrp_time=Time.strptime ("2018-05-04","%y-%m-%d")Print("Strp_time is {}".format(Strp_time))

The relationship between the conversions is as follows:

Format parameter Description:

    %Y  Year with century as a decimal number.    %m  Month as a decimal number [01,12].    %d  Day of the month as a decimal number [01,31].    %H  Hour (24-hour clock) as a decimal number [00,23].    %M  Minute as a decimal number [00,59].    %S  Second as a decimal number [00,61].    %z  Time zone offset from UTC.    %a  Locale‘s abbreviated weekday name.    %A  Locale‘s full weekday name.    %b  Locale‘s abbreviated month name.    %B  Locale‘s full month name.    %c  Locale‘s appropriate date and time representation.    %I  Hour (12-hour clock) as a decimal number [01,12].    %p  Locale‘s equivalent of either AM or PM.    %% A literal "%" character.

The DateTime module describes:
The 1.datetime module contains the following classes:

class name function Description
Date Date objects, commonly used properties are Year,month,day
Time Time Object
Datetime DateTime objects, commonly used properties are Hour,minute,second,microsecond
Timedelta Time interval, which is the length between two points in time
TimeZone Time
Tzinfo Time zone Information Object

Constants included in the 2.datetime module

Constants function Description usage return value
Maxyear Returns the maximum number of years that can be represented Datetime. Maxyear 9999
Minyear Returns the minimum year that can be represented Datetime. Minyear 1

3. Time plus minus

ImportDatetimePrint(Datetime.datetime.now ())# return 2016-08-19 12:47:03.941925Print(Datetime.date.fromtimestamp (Time.time ()))# Timestamp is directly converted into a date format 2016-08-19Print(Datetime.datetime.now ())Print(Datetime.datetime.now ()+Datetime.timedelta (3))# Current time + 3 daysPrint(Datetime.datetime.now ()+Datetime.timedelta (-3))# Current Time-3 daysPrint(Datetime.datetime.now ()+Datetime.timedelta (Hours=3))# Current time + 3 hoursPrint(Datetime.datetime.now ()+Datetime.timedelta (minutes= -))# Current time +30 minutesC_time=Datetime.datetime.now ()Print(C_time.replace (minute=3, hour=2))# time Replacement

As shown in the following diagram ( Note: There is a small error on the way, the time tuple is 7 tuples ):

2.random Module

Basic methods of 1.random modules:

ImportRandomPrint(Random.random ())# randomly generate floating-point numbers between [0, 1]Print(Random.randint (1,Ten))# random generation of integers [a, b] range, including endpointsPrint(Random.randrange (0, -,2))# randomly generated [A, b] range specifies the number of steps (2---even)# Random.choice: Random selection of an element from a non-empty sequencePrint(Random.choice ("Hello"))# randomly generates elements in a specified stringPrint(Random.choice ([1,3,5,2,4,6]))# Randomly generates elements from the specified listPrint(Random.choice ("ABC","123","Liu",555)))# randomly generates elements in a specified tuplePrint(Random.sample ("Hello",3))# randomly generates a specified number of elements for a specified sequencePrint(Random.uniform (1,Ten))# randomly generates floating-point numbers for a specified interval# Shuffle functionItems=[1,2,3,4,5,6,7,8,9,0]Print("before shuffling:", items) random.shuffle (items)Print("Shuffle After:", items)

The result of the run output is:

0.4690573091913972364l4123[‘l‘, ‘h‘, ‘l‘]5.548564168880079洗牌前: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]洗牌后: [8, 2, 9, 3, 6, 0, 4, 1, 7, 5]

2.random Module Method Practical application: Generate random Verification code
(1) Generate random 4-bit digital verification code

# 生成随机4位数字验证码=""         # 最终生成的验证码forinrange(4):    = random.randint(09)    +=str(current)print(check_code)       # 运行输出结果为:7345

(2) Generate random 4-bit string verification code (number + character)

# Generate random 4-bit string verification code (number + character)Check_code= ""         # The final generated verification code forIinch Range(4): cur=Random.randrange (0,4)# random guessing range    # The range of random guesses is equal to the number of loops, taking the letters    ifCur==I:temp= CHR(Random.randint ( $, -))# Randomly take a single letter    Else: Temp=Random.randint (0,9) Check_code+= Str(temp)Print(Check_code)# The result of the run output is: K666
3.os, SYS and shutil modules

The 1.os module provides a very rich way to work with files and directories, and the commonly used methods are as follows:

Os.access (path, mode): Verify permission Mode Os.chdir (PATH): Changes the current working directory Os.chflags (PATH, flags): Sets the path of the tag to the number tag os.chmod (path, mode) : Change Permissions Os.chown (path, UID, GID): Change file owner Os.chroot (PATH): Change the root directory of the current process Os.close (FD): Close file descriptor Fdos.closerange (Fd_low, Fd_ High): Close all file descriptors, Fd_high (not included) from Fd_low, Error ignores os.dup (FD): Copy file descriptor Fdos.dup2 (FD, FD2): Copy one file descriptor fd to another Fd2os.fchdir (FD): Change the current working directory by file descriptor Os.fchmod (FD, mode): Change the access rights of a file, specified by the parameter fd, parameter mode is the file access permission under Unix Os.fchown (FD, UID, GID) : Modify the ownership of a file, this function modifies a file's user ID and user group ID, which is specified by the file descriptor fd Os.fdatasync (FD): Forces the file to be written to disk, specified by the file descriptor FD, But does not force the update of the file's status information Os.fdopen (fd[, mode[, BufSize]): Creates a file object with the file descriptor FD and returns the file Object Os.fpathconf (FD, name) : Returns the system configuration information for an open file Os.fstat (FD): Returns the state of the file descriptor FD, like stat () Os.fstatvfs (FD): Returns information about the file system that contains file descriptor FD files, such as STATVFS () Os.fsync (FD : Force file descriptor for FD to write to hard disk os.ftruncate (fd, Length): Clip file descriptor is fd corresponding file, so it can not exceed file size OS.GETCWD (): Returns the current working directory Os.getcwdu () : Returns the Unicode object Os.isatty (FD) for the current working directory: Returns TRUE if the file descriptor FD is open while connected to the TTY (-like) device, otherwise returns FALSEOS.LCHFLAGS (path, flags) : Set the path marked as a number tag, similar to Chflags (), but no soft connection os.lchmod (path, mode): Modify the Connection filePermissions Os.lchown (path, UID, GID): Change the file owner, similar to Chown, but do not track links os.link (src, DST): Create a hard link, called the parameter DST, point to Srcos.listdir (path) : Returns a list of the names of files or folders contained in the folder specified by Path Os.lseek (FD, POS, how): Set file descriptor fd Current position pos,how modified by: Seek_set or 0: Calculate from File Pos;seek_ Cur or 1: calculated from the current position; Seek_end or 2: Starts at the end of the file, Os.lstat (path): Like stat (), but no soft connection os.major: Extracts the device major number from the original device number ( Use St_dev or St_rdev field in stat) Os.makedev (major, minor): an original device number with major and minor device number Os.makedirs (path[, mode]) : Recursive folder creation functions, such as mkdir (), but all Intermediate-level folders created need to contain subfolders Os.minor (device): Extract the minor number of devices from the original device number (using St_dev in stat or st_ Rdev field) Os.mkdir (path[, mode]): Create a folder named path in the digital mode mode, the default mode is 0777 (octal) Os.mkfifo (path[, mode]): Create a named pipe, Mode is a number default of 0666 (octal) Os.mknod (filename[, mode=0600, device): Create a file system node named filename (file, device special file name or name pipe) Os.open (Files, falgs[, Mode]): Opens a file and sets the option that needs to be opened, the mode parameter is optional os.openpty (): Opens a new pseudo-terminal pair, returns the Pty and TTY file descriptor os.pathconf (PAHT, name) : Returns system configuration information for related files Os.pipe (): Creates a pipeline that returns a pair of file descriptors (R, W) for Read and write Os.popen (command[, mode[, BufSize]): Opens a pipe from a command Os.read ( FD, N): reads a maximum of n bytes from the file descriptor fd, returns a string containing the read bytes,The file descriptor fd corresponding file has reached the end, returning an empty string Os.readlink (path): Returns the file that the soft connection points to Os.remove (path): Delete the path to a file, if it is a folder, will be thrown oserror ; View the following rmdir () to remove a directoryos.removedirs (path): Recursively delete directory Os.rename (SRC, DST): rename a file or directory from src to Dstos.renames (old, new) : Rename the directory recursively, or rename the file Os.rmdir (path): Delete the empty directory specified by path, and if the directory is not empty, throw the Oserrot exception Os.stat (path): Gets the path information specified by path, which is equivalent to C The stat () system call in the API os.stat_float_times ([NewValue]): Determines whether Stat_result displays a timestamp with a float object Os.statvfs (path) : Gets file system statistics for the specified path Os.symlink (src, DST): Creates a soft connection os.tcgetpgrp (FD): Returns the process group associated with Terminal FD, an open file descriptor returned by Os.open () os.tcsetpgrp (FD, PG): Sets the process group associated with Terminal FD (an open file descriptor returned by Os.open ()) to Pgos.tempnam ([dir, [prefix]]): Returns a unique path name used to create a temporary file Os.tmpfile () : Returns an open mode (w+b) file object that has no folder entry, no file descriptor, and will automatically delete Os.tmpnam (): Returns a unique path for creating a temporary file Os.ttyname (FD): Returns a String, It represents an end device associated with the file descriptor FD, and if the FD is not associated with the end device, an exception Os.unlink (path) is thrown: Delete the file path Os.utime (path, times): Returns the time of access and modification of the specified path file Os.walk (top[, topdown=true[, onerror=none[, Followlinks=false]]): Output file names in folders by walking in the tree, up or down Os.write (FD, str) : writes string to file descriptor fd, returns the actual written string length

2.sys Module Common functions:

sys模块常见函数:sys.argv:实现从程序外部向程序传递参数,第一个元素是程序本身路径sys.exit([arg]):程序中的推出,arg=0为正常退出sys.getdefaultencoding():获取系统当前编码,一般默认为ASCIIsys.setdefaultencoding():设置系统默认编码,执行dir(sys)时不会看到这个方法,再解释器中执行不通过,可以先执行reload(sys),再执行setdefaultencoding("utf-8"),此时将系统默认编码设置为utf-8sys.getfilesystemencoding():获取文件系统使用编码方式sys.path:获取指定模块搜索路径的字符串集合,可以将写好的模块放在得到的某个路径下,就可以在程序中import时正确找到sys.platform:获取当前系统平台sys.version:获取python解释器版本信息sys.stdin,sys.stdout,sys.stderr:标准I/O流对应的对象

3.shutil module:
Shutil: Advanced file, folder, compression package processing module

  shutil.copyfileobj (fsrc, fdst[, length]): Copies the contents of the file to another file, the Copyfileobj method only copies the file contents Shutil.copyfile (src, DST) : Copy files, copyfile Copy only file contents shutil.copy (src, DST): Copy files and Permissions shutil.copy2 (SRC, DST): Copy files and status information Shutil.copymode (SRC, DST): copy-only permissions , the content, group, and user are unchanged, as long as the DST file exists, otherwise the exception Shutil.copystat (SRC, DST): Copy only state information, which is the file attributes, including: Mode bits, Atime, Mtime, Flagsshutil.ignore _patterns (*patterns): Ignoring a file, selective copy Shutil.copytree (SRC, DST, symlinks=flase, Ignore=none): Recursive de-copying file Shutil.rmtree ( path[, ignore_errors[, onerror]): Recursive deletion of files shutil.move (SRC, DST): Recursively moving files, like the MV Command, is actually renaming shutil.make_archive (base_ Name, format, ...) : Create a compressed package and return the file path, for example: Zip, Tarbase_name: The file name of the compressed package, or the path to the compressed package. Only the file name is saved to the current directory, otherwise saved to the specified path such as:www=> saved to the current path such as:/users/alex/www = Save to/users/alexformat: Package type, "Zip", "tar", "Bztar" , "Gztar" Root_dir: Folder path to compress (default current directory) Owner: User, default Current user group: groups, default current group logger: used for logging, usually logging. Logger Object  
import shutil# 将 C:\Downloads\test 下的文件打包放置当前程序目录= shutil.make_archive("wwwwwwwwww"‘gztar‘, root_dir=‘C:\\Downloads\\test‘)# 将 C:\Downloads\test 下的文件打包放置 \Users\mydir目录(当前目录)= shutil.make_archive("/Users/mydir/wwwwwwwwww"‘gztar‘, root_dir=‘C:\\Downloads\\test‘)

At the bottom of Python, Shutil handles the compression package by calling the ZipFile and tarfile two modules for processing, in a detailed:

import  zipfile # compression  z_file =  zipfile. ZipFile ( "Test.zip" ,  "W" ) z_file =  write ( Span class= "st" > "A.log" ) z_file =  write ( "data.date" ) z_ File.close () # unzip  z_unfile =  zipfile. ZipFile ( "Test.zip" ,  "R" ) Z_unfile.extractall () Z_unfile.close () 
import  tarfile # compression  tar_in =  tarfile. open  ( "My.tar" ,  "W" ) Tar_in.add ( \\  users  \\  bbs.log ", Arcname=  " Bbs.log ") Tar_in.add (  "C:  \\  users  \\  cmdb.log ", Arcname=  " Cmdb.log ") tar_ In.close () # unzip  tar_out =  tarfile. open  ( "My.tar" , ) Tar_out.extractall ()  #可设置解压地址 
       tar_out.close ()  
4.json, pickle and shelve modules

Python Common Modules

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.