Function module
Functions of function modules (why there is a function module)
1, function module can reduce the amount of code
2, function module for easy reading
3, Function module maintenance strong
The essence of function module and the method of invocation
1. The essence of the function module is a. py file, which writes a number of functions inside the file.
2. The function module must be referenced with import, and the function module must be called with "module name. Function name" when calling function modules.
3, sometimes we just need to use a function in the module, you can also use the "from module name Improt function 1, function 2" way to reference the module, but only one reference, not recommended.
Iii. Classification of function modules
1, built-in function module: The system comes with the function module, we just call it can, in the Python environment is not see the built-in module, is written in the Python interpreter.
2, third-party function module: The system comes with some other people to write the module, called a third-party module, the module stores the path in the Python software installation path under the Lib (stored in the system environment).
3, custom modules: Their own definition of the module
4. Import the load order when importing the module: find it from the Python interpreter, then look it up from the system environment (LIB), and then look under the custom path.
Four, time built-in module detailed
1. Time Built-in module
1.1 Time Expression Form
There are three types of time expressions in Python, namely timestamp, structured time (Struct_time), and formatted time string.
Timestamp: The offset is calculated in seconds from 0 minutes and 0 seconds on January 1, 1970, and the value returned is a float type.
Tuples: Struct_time tuples Total 9 elements Total nine elements: (year, month, day, time, minute, second, week of the year, Day of the year, etc.)
Formatted time string: As ' 1998-10-10 ' (delimiter format can be freely defined by itself)
1.2 Summary: Time stamp is the time that the computer can recognize, the formatted time string is the time that the person can recognize, the structured time is used to manipulate the time.
2, three kinds of time expression forms on the mutual conversion ()
1.1 The string time and timestamp cannot be converted to each other.
1.2 Time.localtime structured time object is Beijing standard time, while the object of Time.gmtime structured time is the international time
Five, Random module detailed
1. The random module in Python is used to randomly generate an arbitrary number
2, Random.random
Random.random () is used to randomly generate a floating-point number from 0 to 1.0.
3, Random.randint
Random.randint (x, y) is used to randomly generate an integer greater than or equal to x less than or equal to Y, and X must be greater than Y. (Gu Tou and Gu Wei)
4, Random.randrange
Random.randrange (x, y) is used to randomly generate an integer greater than or equal to x less than Y, and X must be greater than Y. (Gu Tou regardless of tail)
5, Random.choice
Random.choice (sequence x) is an element that is randomly obtained from a sequence, and the returned result is an integer type. The sequence x represents an ordered sequence.
Random.choices (sequence y) is an element that is randomly fetched from a sequence, and the returned result is a list type. The sequence y represents an ordered sequence.
6, Random.sample
Random.sample (sequence, k) randomly fetches k elements from the specified sequence, and the returned result is a list. The sample function does not modify the original sequence sequence.
7, Random.uniform
Random.uniform (x, y) is used to randomly generate a floating-point number greater than x less than Y, and X can be greater than Y or less than Y.
8, Random.shuffle
Random.shuffle (x) is used to disrupt the elements of a list, changing the position of the elements of the list X.
Liu, Hashlib
1, the role of Hashlib: Hashlib is a special hash algorithm to provide a library (digest algorithm), now includes MD5, SHA1, sha224, SHA256, sha384, sha512.
2, Abstract algorithm: Abstract algorithm, also known as hashing algorithm, hashing algorithm. He passed a function. Data of arbitrary length into (corresponding) a fixed-length data string (usually represented by a 16-binary string)
3, hashlib the way of execution
HASHLIB.MD5 (). Update (b ' x ') where x is the encrypted object, the encryption is complete. (If the encrypted object is large you can call multiple update, the result of encryption is the same)
HASHLIB.MD5 (). hexdigest()
View Encrypted results (32-bit 16 binary string)
4. Salt-adding operation of MD5
4.1 Why add salt: Because the encrypted object and calculated MD5 value is one by one corresponding to the relationship, through the way to crack the database can be cracked user password, so the encryption object to add salt operation.
4.2 Add Salt Method: Add a string of data to the original string before MD5 encryption
The format is: hashlib.md5 (' Salt ', encod (Utf-8))
5, if two users of the same encryption objects, salt consistency, the MD5 value obtained is the same, the solution is through the login as a part of the salt to calculate the MD5, so that the user to achieve the same password also stores a different MD5. (If the user name is inconsistent)
Note: The result of the SHA1 is a bit byte, usually represented by a 40-bit 16 binary string. Algorithms that are more secure than SHA1 are SHA256 and SHA512, but the more secure the algorithm, the slower it is, and the longer the digest length. Abstract algorithms are widely used in many places. Note that the digest algorithm is not an encryption algorithm and cannot be used for encryption (because plaintext cannot be reversed by the digest), but it is only used for tamper-proof, but its one-way computing feature determines that the user's password can be verified without storing the plaintext password.
Seven, OS module detailed
1, OS module provides a unified operating system interface functions, these interface functions are usually specified by the platform, OS modules can be in different operating system platforms, such as NT or POSIX between the specific functions of automatic switching, so as to achieve cross-platform operation. (OS module is a module in the Python standard library for accessing operating system functionality.)
2. Detailed parameters of OS module
OS.GETCWD () Gets the current working directory, which is the directory path of the current Python script work
Os.chdir ("dirname") changes the current script working directory, equivalent to the shell under the CD Os.curdir return to the current directory: ('. ')
Os.pardir Gets the parent directory string name of the current directory: (' ... ')
Os.makedirs (' dirname1/dirname2 ') can generate multi-level recursive directories
Os.removedirs (' dirname1 ') if the directory is empty, then delete, and recursively to the previous level of the directory, if also empty, then delete, and so on
Os.mkdir (' dirname ') generates a single-level directory, equivalent to mkdir dirname in the shell
Os.rmdir (' dirname ') delete the single-level empty directory, if the directory is not empty can not be deleted, error, equivalent to the shell rmdir dirname
Os.listdir (' dirname ') lists all files and subdirectories in the specified directory, including hidden files, and prints as a list
Os.remove () Delete a file Os.rename ("Oldname", "newname") Rename File/directory
Os.stat (' path/filename ') get File/directory information OS.SEP output operating system-specific path delimiter, win under "\ \", Linux for "/"
OS.LINESEP output The line terminator used by the current platform, win under "\t\n", Linux "\ n"
OS.PATHSEP output is used to split the file path of the string win down for;, Linux for:
The Os.name output string indicates the current usage platform. Win-> ' NT '; Linux-> ' POSIX '
Os.system ("Bash command") runs a shell command that directly displays
Os.environ Getting system environment variables
Os.path.abspath (path) returns the absolute path normalized by path
Os.path.split (path) splits path into directory and file name two tuples returned
Os.path.dirname (path) returns the directory of path. is actually the first element of Os.path.split (path)
Os.path.basename (Path) returns the last file name of path. If path ends with a/or \, then a null value is returned. That
The second element of Os.path.split (path) os.path.exists (path) returns true if path exists, or false if path does not exist
Os.path.isabs (path) If path is an absolute path, returns true Os.path.isfile (path) If path is an existing file, returns True. otherwise returns false
Os.path.isdir (path) returns True if path is a directory that exists. otherwise returns false
Os.path.join (path1[, path2[, ...]) returns a combination of multiple paths, and the parameters before the first absolute path are ignored
Os.path.getatime (Path) returns the last access time of the file or directory to which path is pointing
Os.path.getmtime (Path) returns the last modified time of the file or directory to which path is pointing
Os.path.getsize (path) returns the size of path
Python Basics---function Module 1