Python-common modules

Module : In python, xxx.py is a module

Use of common system built-in modules
    |-- math
    |-- random
    |-- uuid

    | - os

    |-- sys
    |-- time
    |-- datetime
    |-- calendar
    |-- hashlib
    |-- hmac
    |-- base64
 

How to import modules
 
import xxxx import random
            import uuid, sys
            import os.path as 
from package path import module name
from package path. module name import method, class, variable
from functools import partial

 

1、math:

This module is mainly used for a module of mathematics and related calculations

Perform mathematical calculations
|-- math.ceil(num) # Round up
|-- math.floor(num) #
Round down |-- math.abfs(num) # Find absolute value
|-- math.modf( num) # Return the integer and fractional part in the form of a tuple
|-- math.pi # Pi
|-- math.pow(num, num2) # Power num**num2
|-- math.sqrt(num) # Square root     extraction
Attributes:
pi # 鈥榮率
    e # natural constant
Method:
    trigonometric function sin cos ...
    logarithm log log10 ...
    ceil() # round up
    floor()     # round down
    fabs # absolute value
sqrt # square root
    pow # power
 

2 、

os.path模块
    'abspath', 'altsep', 'basename', 'commonpath', 'commonprefix',
    'curdir', 'defpath', 'devnull', 'dirname', 'exists', 'expanduser', 
    'expandvars', 'extsep', 'genericpath', 'getatime', 'getctime', 'getmtime',
    'getsize', 'isabs', 'isdir', 'isfile', 'islink', 'ismount', 'join', 
    'lexists', 'normcase', 'normpath', 'os', 'pardir', 'pathsep', 'realpath',
    'relpath', 'samefile', 'sameopenfile', 'samestat', 'sep', 'split',
    'splitdrive', 'splitext', 'stat', 'supports_unicode_filenames', 'sys']

Mainly manage the operating system file management system
|-- chdir(path) # Modify the current workspace path
|-- curdir # Current workspace path
|-- os.getcwd() # Get the absolute path of the path
|-- os. path.abspath(path) # Get the absolute path of the path
|-- os.cpu_count() # Get the CPU architecture of the current computer
|-- os.device_encoding(fd) # Get the code of the device
|-- os.getpid() # Get the process number
|-- os.getppid() # Get the parent process number of the current process
|-- os.kill() # Kill the process by number
|-- os.listdir(path) # Traverse all files under path And folders, return names in the form of a list
|-- os.mkdir() # Create a single-level folder (directory)
|-- os.makedirs() # Create a multi-level directory
|-- os.name # Get operating system Name
|-- os.rename() # rename
|-- os.remove() # Remove files
|-- os.removes() # Remove multi-level directories
|-- os.rmdir() # Delete directories
|-- os.sep # File separation corresponding to the current system Character
|-- abspath # Get absolute path dirname + basename
|-- altsep # File separator
 |-- basename # File name
 |-- dirname # Owning directory
 |-- exists()
 |-- join()
 |-- split ()    

3、os.path

|-- os.path.abspath(path) # Get the absolute path of the path
|-- os.path.altsep # The path separator
|-- os.path.basename(path) # Get the file name corresponding to the path
|- -os.path.dirname(path) # Get the directory where the file is located
|-- os.path.exists(path) # Determine whether the file exists
|-- os.path.getatime(path) # Get the last access time of the file
|- -os.path.getctime(path) # Get file creation time
|-- os.path.getmtime(path) # Get file modification time 
|-- os.path.getsize(path) # Get file size, unit is B (bytes)
|-- os.path.isdir(path) # Determine whether the path is a directory
|-- os.path.isfile(path) # Determine whether the path is a file
|-- os.path.isabs(path ) # Determine whether the path is an absolute path
|-- os.path.islink(path) # Determine whether the path is a link (soft link, hard link)
|-- os.path.ismount(path) # Determine whether the path is A mount file (U disk, CD drive, etc.)
|-- os.path.join(path1, path2) # After completing path splicing, a separator will be automatically generated according to the system
|-- os.path.split(path) # Separate file paths
 

4、sys

    |-- argv # Script passing parameters
    |-- exit() # Exit the system
    |-- getdefaultencoding() # Get the default encoding of the system
    |-- getfilesystemencoding() # Get the file encoding
    |-- setrecursionlimit(num) # Set recursive Limit value
    |-- getrecursionlimit() # Get the limit value of recursion
    |-- getrefcount() # Get the reference count of the object

5、random

    random.random() # Random [0~1) interval
    random.randint() # [a, b] Random integers in the interval
    random.uniform # [a, b] Random numbers in the interval
    random.choice(container) # Random selection One element
    random.shuffle(container) # shuffle, randomly shuffle data

6、time

 'sleep', 'strftime', 'strptime', 'struct_time', 'thread_time',
   'thread_time_ns', 'time', 'time_ns', 'timezone', 'tzname']


    |-- asctime() # Return a date string 
    |-- ctime() # Return a date string 
    |-- gmtime() # Get a time object
    |-- localtime() # Get a time object
    |-- time () # Get the current timestamp
    |-- sleep(sec) # Sleep
    |-- strftime() # Format time
    |-- strptime() # Convert a string format time into a date object

7、datetime

    |-- time([hour[,minutes][,seconds]]) # Build a time object
    |-- date(year,month,day) # Build a date object
    |-- datetime submodule

    

8, datetime submodule

    import datetime
        datetime.datetime.xxxx()
    from datetime import datetime

    'astimezone', 'combine', 'ctime', 'date', 'day', 'dst', 'fold',
    'fromisocalendar', 'fromisoformat', 'fromordinal', 'fromtimestamp',
    'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'microsecond', 
    'min', 'minute', 'month', 'now', 'replace', 'resolution', 'second',
    'strftime', 'strptime', 'time', 'timestamp', 'timetuple', 'timetz', 
    'today', 'toordinal', 'tzinfo', 'tzname', 'utcfromtimestamp', 
    'utcnow', 'utcoffset', 'utctimetuple', 'weekday', 'year']

|-- astimezone(时间object) # Return the time object corresponding to the current time zone
|-- datetime.now() # Return the current time object
|-- ctime(时间object) # A time string that meets European and American standards
|-- time (Time object) # Return the time part of the time object
|-- date(时间object) # Return the date part of the time object
|-- day #week
|-- now #time
|-- minute #minute
|-- second #second
|-- microsecond # microsecond

9, calendar module

Calendar module

10 、 hashlib

Cryptography:
        If you pass the same secret key, you can change the encryption algorithm:
            |-- symmetric encryption
                DES

            |--
                The secret key and decryption key of asymmetric encryption are not used to                 generate a pair of secret keys, public key and private key using the same
                rsa             |-- Hash encryption (hash), hash encryption,                 irreversible encryption, one-way, The result is the only                 md5                 shal256                 shal384

            




11、hmac

    import hmac
    m = hmac.new("123456".encode("utf-8"), "liujianhong".encode("utf-8"), "MD5")
    m.hexdigest()

12、uuid:

Unique

import uuid
s = uuid.uuid4()
s.hex
 

 

 

Guess you like

Origin blog.csdn.net/weixin_45802686/article/details/109151278