Time.asctime (Time.localtime (1234324422))
python--Common Modules
1 What is a module:
Module is the py file
2 Import Time #导入时间模块
In python, there are usually three ways to represent time: timestamp, tuple (struct_time), formatted time string:
(1) timestamp (timestamp): Typically, a timestamp represents an offset that is calculated in seconds from 00:00:00 1970 January 1st. We run "type (Time.time ())" and return the float type.
(2) formatted time string (format string): '1988-03-16 '
(3) Tuple (struct_time):struct_time tuple total of 9 elements total nine elements :(year, month, day, time, minute, second, week of the year, Day of the year, etc.)
<1> time Stamp
Import time
Time.time () #返回当前时间的时间戳 >>>1493191228.100598
<2> Time string
Time.strftime ("%y-%m-%d"%x) >>> ' 2017-04-26 15:24:05 '
<3> Time Yuan zu
Time.localtime () >>> time.struct_time (tm_year=2017, tm_mon=4, tm_mdec=25, tm_wday=2, tm_yday=116, tm_isdst=0 )
Summary: Time stamp is the time that the computer can recognize; The time string is the time that a person can read; Tuples are used to manipulate time.
2.1 Conversion of several time forms
<1> timestamp converted to structured time
Time.localtime (Time.time ()) >>> time.struct_time (tm_year=2017, tm_mon=4, tm_mday=26, tm_hour=15, tm_min=48, tm_s=57, tm_wday=2, tm_yday=116, tm_isdst=0)
structured time converted to timestamp
Time.mktime (Time.localtime ()) 1493215930.0
<2> string time converted to structured time
Time.strftime ("%y-%m-%d%x", Time.localtime ()) ' 2017-04-26 22:16:05 '
structure time conversion into string time
Time.strptime ("2017-03-16", "%y-%m-%d")
2.2
Time.ctime ()
>>> ' Wed Apr 26 16:05:52 2017 '
Time.asctime (Time.localtime (1234324422))
1 #--------------------------Other methods
2 # Sleep (secs)
3 # Threads defer a specified time run, in seconds.
3 Random Stochastic module
Import Random #导入模块 random.random () #浮点小数print (Random.random ()) random.randint (1,4) print (Random.randint ( 1,4)) #1-4 numbers randomly appear between random.randrange (1,3) print (Random.randrange (1,3)) #顾头不顾尾取值 digital Random.choice ([1,2,5]) print ( Random.choice ([1,2,5])) #取1, the value list element of 2,5 random.sample ([1,3,5],2) print (Random.sample ([1,3,5],2)) #列表元素 Any 2 combination of random.uniform (1,4) print (Random.uniform (1,4)) #小于4大于1的小数item =[4,1,3,2]random.shuffle (item) #打乱次序print (item)
Practice Generate verification Code function
Import Randomdef V_code (): s= "" For I in range (5): rnum=random.randint (0,9) RA=CHR (Random.randint ( 65,90) rz=chr (Random.randint (97,122)) ret=random.choice ([Rnum,ra,rz]) s+=str (ret) return Sprint (V_code ())
4 hashilib #摘要算法
1, Algorithm Introduction
Python's hashlib provides a common digest algorithm, such as MD5,SHA1 and so on.
What is a digest algorithm? Abstract the algorithm is also called hash algorithm and hashing algorithm. It uses a function to convert any length of data into a fixed length data string (usually represented by a 16-binary string).
Abstract the algorithm is to calculate the fixed-length summary digest by the Digest function f () to the data of arbitrary length, in order to find out whether the original data has been tampered with.
Abstract the algorithm can point out whether the data has been tampered with, because the digest function is a one-way function, it is easy to calculate f (data), but it is very difficult to digest data by using it. Also, making a bit change to the original data will result in a completely different summary of the calculations.
We use the Common Digest algorithm MD5 as an example to calculate the MD5 value of a string:
#数据量大 can call Update () multiple times in chunks, and the last value is the same
Import hashlibm=hashlib.md5 () m.update (b ' Hello python ') m.update (b "Hello") print (M.hexdigest ())
#名字一样但是最后打印的值不一样
M=HASHLIB.MD5 () m.update (b "Alex") print (M.hexdigest ()) m.update (b "Alex") print (M.hexdigest ())
#两者用途一样 Shal and MD5
Import HASHLIBSHA1 = HASHLIB.SHA1 () sha1.update (b ' How to use SHA1 in ') sha1.update (b ' python hashlib? ') Print (Sha1.hexdigest ())
2. Abstract algorithm Application
Because the MD5 value of the common password is easy to calculate, so to ensure that the Stored user password is not the MD5 of the commonly used password, this method is implemented by adding a complex string to the original password, commonly known as "Add salt":
HASHLIB.MD5 ("Salt". Encode ("UTF8"))
5 OS module is an interface that interacts with the operating system
Import OS #导入模块
"' OS.GETCWD () gets the current working directory, that is, the directory path of the current Python script work os.chdir (" DirName ") changes the current script working directory, equivalent to the shell Cdos.curdir return the current directory: ('. ') Os.pardir Gets the parent directory string name of the current directory: (' ... ') Os.makedirs (' dirname1/dirname2 ') can generate a multi-level recursive directory Os.removedirs (' dirname1 ') if the directory is empty, then deleted, and recursively to the previous level of the directory, if also empty, then delete, and so on Os.mkdir (' DirName ') to generate a single-level directory, equivalent to the shell mkdir dirnameos.rmdir (' dirname ') delete the single-level empty directory, if the directory is not empty can not be deleted, error; equivalent to the shell rmdir Dirnameos.listdir (' dirname ') lists all files and subdirectories under the specified directory, including hidden files, and prints 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 current platform using the line terminator, win under "\t\n", Linux "\ N "os.pathsep output is used to split the file path of the string win under;, Linux: The Os.name output string indicates the current use of the platform. Win-> ' NT '; Linux-> ' POSIX ' Os.system ("Bash command") runs a shell command that directly displays the Os.environ get system environment variable Os.path.abspath (PATH) Return path normalized absolute path Os.path.split (path) splits path into directory and file name two tuples return Os.path.dirname (path) to the directory where path is returned. In fact, the first element of Os.path.split (path) os.path.basename returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path)Os.path.exists (path) If path exists, returns true if path does not exist, returns Falseos.path.isabs (path) If path is an absolute path, returns Trueos.path.isfile (path Returns true if path is a file that exists. Otherwise, return Falseos.path.isdir (path) True if path is a directory that exists. Otherwise return Falseos.path.join (path1[, path2[, ...]) When multiple paths are combined, 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 pointed to by Path Os.path.getsize (path) returns the size of path "
6 SYS module dealing with interpreters
Import SYS #导入模块
SYS.ARGV command line argument list, the first element is the program itself path Sys.exit (n) exits the program, exit normally (0) sys.version Gets the version information of the Python interpreter Sys.maxint the largest int value Sys.path returns the search path for the module, using the value of the PYTHONPATH environment variable when initializing Sys.platform Returns the operating system platform name
python--Common Modules