python-Common Modules

Source: Internet
Author: User

Can name Meta-ancestor Namedtuple
namedtuple Example

Deque Dual-ended queue
deque Example

Queues Queue
Queue Example

An ordered dictionary (key is kept in order) ordereddict
ordereddict Example

When using a dictionary, if key does not exist, return a default value, you can use the Defaultdict
defaultdict Example

The counter count, which is an unordered container type, is stored in the form of a dictionary key-value pair, where the element is counted as the key and its count as value. The count value can be any Interger (including 0 and negative numbers)
Counter Example

Time Module
Time Module
Import Time#ret = Time.time () #时间戳, the timestamp represents the offset in seconds, starting January 1, 1970 00:00:00#print (ret) #1510647685.4264479#print (Type (ret))#formatted time string strftime#ret = time.strftime ('%y-%m-%d%A%h-%m-%s ')#print (ret)#ret = time.localtime (1510647685.4264479)#print (ret)#tuple (struct_time): There are nine elements in a total of 9 elements: (year, month, day, time, minute, second, week of the year, Day of the year, etc.)#s = time.time ()## print (s)#ret = time.struct_time (s)#print (ret)#ret = time.gmtime ()#print (ret)#Ret1 = Time.localtime ()#print (RET1)#Print (Time.gmtime (1222222222))#Print (Time.localtime (1222222222))#print (Time.localtime ())#ret = time.localtime (1222222222)#print (ret)#Print (Time.mktime (ret))#ret = time.strftime ('%y-%m-%d ')#print (ret)#Ret1 = Time.strptime ("2017-03-16", "%y-%m-%d")#print (RET1)## Ret2 = Time.mktime (ret1)## Print (Ret2)##Ret2 = time.strftime ('%y-%m ', Ret1)#print (Ret2)#true_time=time.mktime (Time.strptime (' 2017-09-11 08:30:00 ', '%y-%m-%d%h:%m:%s '))#time_now=time.mktime (Time.strptime (' 2017-09-12 11:00:00 ', '%y-%m-%d%h:%m:%s '))#Dif_time=time_now-true_time#struct_time=time.gmtime (dif_time)#print (struct_time)#print ('%d '%d '%d '%d '%d '% (struct_time.tm_year-1970,struct_time.tm_mon-1,#Struct_time.tm_mday-1,struct_time.tm_hour,#struct_time.tm_min,struct_time.tm_sec))#ret = Time.asctime (Time.gmtime ())#print (ret)#Ret1 = Time.ctime (2000000000)#print (RET1)

SYS module
Sys

Random Module

Random Explanations and examples
#Random Decimals#print (Random.random ()) #0-decimal between 1#Print (Random.uniform (10,20)) #n, decimal between M## generate random integers#print (Random.randint )#Print (Random.randrange (2))#print (Random.randrange )#Print (Random.randrange (1,5,2))## randomly selected from a sequence: one choice, multiple sample#randomly select a return choice#print (random.choice) #1或者2或者3#Randomly select multiple returns, the number returned is the second parameter of the function sample#Print (Random.sample ([1,25,[1,2]],2)) #列表元素任意2个组合#disrupts the order of a sequence shuffle#item = [1,3,5,7,9]#random.shuffle (item) #改变了原列表#print (item) #应用: Shuffle, draw, test some sort algorithm#exercise: Generate Random verification codes#Method One#Print (Random.randint (100000,999999))#Method Two:#Print (Random.randrange (100000,1000000))#Method Three#L = []#For I in range (6):#num = random.randint (0,9)#l.append (num)#print (". Join (L)")#generates a 6-bit digital random verification code. There can be no repetition, there will be a lot less possibilities.#Print (Random.sample (range (0,10), 6))#L = []#For I in range (6):#alpha = Chr (Random.randint (65,90))#num = str (random.randint (0,9))#ret = Random.choice ([alpha,num])#l.append (ret)#print (". Join (L)")

OS Module

An OS module is an interface that interacts with the operating system

OS-related
" "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 Os.popen ("Bash command") to run the shell command to get execution results Os.environ Get system environment variable Os.pathos.path.abspath (PATH) returns path normalized absolute path Os.path.split (path) splits path into directory and file name two tuples return Os.path.dirname (path) Returns the directory of path. In fact, the first element of Os.path.split (path) os.path.basename returns the last file name of path. How PAth ends with a/or \, then a null value is returned. The second element of Os.path.split (path), os.path.exists (path), returns True if path exists, or if path does not exist, returns Falseos.path.isabs if path is an absolute path, Returns Trueos.path.isfile (path) If path is an existing file and returns True. 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 size of the file or directory to which path was last modified Os.path.getsize (path) returned path" "Note: Os.stat ('Path/filename') Get File/the structure of the directory information describes the STAT structure: St_mode:inode Protected Mode st_ino:inode node number. The device that the St_dev:inode resides on. St_nlink:inode number of links. St_uid: The owner's user ID. St_gid: The group ID of the owner. St_size: The size of the normal file in bytes, including data waiting for certain special files. St_atime: Time of last visit. St_mtime: The time of the last modification. St_ctime: reported by the operating system"CTime"。 On some systems, such as UNIX, is the time of the most recent metadata change, and on other systems (such as Windows) is the time of creation (see the documentation for the platform for more information).





python-Common Modules

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.