Python common modules

Source: Internet
Author: User
Tags local time

Time

#Current time stamptime.time ()#deviations from standard Time (GMT)Time.altzone#return time format Sat 15:11:10time.asctime ()#return time format Sat 15:11:10time.ctime ()#returns the local time objecttime.localtime () T=time.localtime ()Print(T.tm_year,t.tm_mon) T= Time.localtime (Time.time ()-(60*60*24))#returns the UTC time (GMT) objecttime.gmtime () time.strftime ("%y-%m-%d%h:%m:%s") 2017-02-18 15:24:25#showing 10 days agoStruct_time = Time.localtime (Time.time ()-86400) Time.strftime ("%y-%m-%d%h:%m:%s", Struct_time) s_time= Time.strptime ("2017-02-17","%y-%d-%m") Time.mktime (s_time) time.strptime () time.mktime ()"2016/05/22"----------------> Time Object--------------> Timestamp

Datetime

# return results 2017-18 15:24:25 Datetime.datetime.now () # Timestamp is converted directly into a date format  -Datetime.timedelta (hours=3,minutes=20)#  time to replace Datetime.datetime.now (). Replace (year=2016,month=3)

Random

Print(Random.randint (1,10))Print(Random.randrange (1,10)) does not contain 10, can be added stepPrint(Random.sample ([1,2,3,4,5],2) randomly take a list of two elements--------------------------------------------------------------------The first kind of random verification codeImportRandomcheckcode="' forIinchRange (4): current= Random.randrange (0,4)    ifCurrent! =i:temp= Chr (Random.randint (65,90))    Else: Temp= Random.randint (0,9) Checkcode+=Str (temp)Print(Checkcode) the second typeImportRandom,stringsource= String.digits +String.ascii_lowercasePrint("". Join (Random.sample (source,6)))

Os

Import Platform Print (Platform.system ()) displays the current operating system Os.popen ('pwd'). Read () can get the execution result of the command Os.system Returns the status of the command execution result only

Sys

Sys.argvsys.path

Pickle & JSON

Account = {    'ID': 62253454873,    ' Credit': 15000,    'Balance': 8000,    'expire_date':'2020-05-21',    'Password':'FDSJKL'}f= Open ('account.db','W') F.write (str (account)) F.close ()------------------------------F= Open ('account.db','R') Account=eval (F.readlines ()) Serialization of memory-String Deserialization string-memory Pickle.dumps, pickle.dump serialization pickle.loads, Pickle.load deserialization JSON only supports STR, int, float, set, dict, list, tuple data types

Logging

Log format

% (name) s

Logger's name.

% (Levelno) s

Log level in digital form

% (LevelName) s

Log level in text form

% (pathname) s

The full path name of the module that called the log output function may not have

% (filename) s

The file name of the module that called the log output function

% (module) s

Call the module name of the log output function

% (FuncName) s

Function name of the call log output function

% (Lineno) d

The line of code where the statement that called the log output function is located

% (created) f

Current time, represented by the UNIX standard floating-point number representing the time

% (relativecreated) d

The number of milliseconds since logger was created when the log information is output

% (Asctime) s

The current time in string form. The default format is "2003-07-08 16:49:45,896". The comma is followed by milliseconds

% (thread) d

The thread ID. Probably not.

% (ThreadName) s

The name of the thread. Probably not.

% (process) d

The process ID. Probably not.

% (message) s

User-Output messages

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.