day 17 package, module time, datetime, os, sys, json, pickle, hashlib, hmac, logging

  package

Suppose a m1.py module 60 functions, 60 is subdivided into a plurality of function py file 

import mode converted
from m1 import f1

for human use package, exhausted
from M2 F1 Import
from Import F6 M3

appeared package, the module is divided into a plurality of files, but can be introduced to maintain the same manner. Followed by the package or from m1 import f1, users do not feel the change
package is actually a folder (this had to contain __init__.py file)

the init let ordinary folder into a package, the package is the lead guide _ ** _init __. py **


The first use (not recommended)
from m1.m2 Import f1

second use
from m1 import f1
Guide package is to import init, init run the file, create a namespace init this, then the variables within the inti thrown within this namespace init
Package of environmental variables executable file as a reference, init package can only begin importing the package name from 

the relative import :( can only be used in the package)
. Is the current directory
.. the parent directory

from .m2 Import F5


time module

time module: supports three different types of time, can be switched between the various forms of time

import time

stamp
print (time.time ()) # 1565922341.6068554

format Time
Print (The time.strftime ( 'm_Low%%% Y_% X-D')) # 2019_08_16 10:26:36

# structured time
print (time. localtime ()) # local time (China)
Print (time.gmtime ()) # standard time

converting three formats # time (no need to remember)

# structured format rotation time period
print (time.strftime ( '% Y - M-%%% X-D ', time.localtime ()))
# turn structured format time time
print (time.strptime (' 2019-08-16 10:30:44 ' ,'% Y-% m -%% X-D '))

# structured transfer time stamp
Print (time.mktime (time.localtime ()))
# time stamp transfected structured
Print (time.localtime (the time.time ()))

# key
the time.time ()
the time.sleep (2)


datetime module
datetime module: modification time 

Import datetime
Print (datetime.datetime.now ()) # print time

now datetime.datetime.now = ()
print (now + the datetime.timedelta (. 3)) # Day +3 default
print (now + datetime.timedelta (hours = 3)) # 3 hours
print (now + datetime.timedelta (minutes = 3)) # 3 minutes
print (now.replace (year = 1900) ) # change Year 1900
os module
os module: interaction with the operating system, you can manipulate files
import os
print (os.getcwd ()) # Get the current directory 

os.mkdir ( 'm2') # Create a folder
os.rmdir ( 'm2') # delete folders

************ * (often used)
RES = os.listdir (r'D: \ Shanghai python11 of video \ python11 of video \ day 17 ') # list all files
Print (RES)

os.rename (' test.py ',' test1.py ') # rename
os.remove (' test1.py ') # delete a file


# __file__ only pychamr was provided, python itself does not support
print (' os.path.abspath (__ file __ ): ', os.path .abspath (__ file__)) # support different platforms (Windows, ios, andirod, Linux, UNIX)

Print ( '__ __ file:', __ file__)

Print (os.path.exists ('01 package .py ')) # file does not there is False, there is True


Print (os.path.isfile ('01 package .py ')) # whether the file
print (os.path.isdir ('01 package .py')) # whether the folder


# *** ***** (often used)
to support a variety of platforms (windows, ios, andirod, linux ,unix)
res = os.path.join (r'D: \ Shanghai Python11 of video \ python11 of video \ day 17 \ m1 \ bbb ' ,' m5.py ') # splice file path
res = os.path.join (r' D: \ Shanghai python11 of video \ python11 of video \ day 17 \ m1 \ bbb ' ,' m5 ',' test.py ') # splicing file path
# print (r'D: \ Shanghai python11 of video \ python11 on video \. 17 Day \ M1 \ BB '+' \ m5.py ')
Print (RES)

# ******* (often used)
Print (os.path.abspath with (__ file__))
Print (os.path.dirname (os.path.dirname (os.path.abspath (__ file__) ))) # returns the parent directory path


g = os.walk (r'D: \ Shanghai Python11 of video \ python11 of video \ day 17 ') # returns three values, the first value is the path; the second value is the file in the folder path, the third value is the file path
for G in I:
Print (I)
application:
computing code function (given folder path, the path calculation folder for all files py; py given file, the number of lines calculated py file)


SYS module
sys module: with Python interactive interpreter 

Import sys
Print (sys.path) # lists the current directory, as well as the environment variable

print (sys.argv) # receive parameters (when running with cmd executable file is valid)

Print (sys.modules) # shows all the imported modules

Module json

json modules: a program written in python, using java to write a program, the program requires two exchanges between the data, a predetermined common data types of languages, json string

Serialization: json into python string from the dictionary (the most common), dump

deserialization: from json into python string dictionary (the most common), Load


Import json

DIC = { 'A':. 1, 'B': 'ABC', 'C':} None
Data = json.dumps (DIC) in memory # serialized
print (data, type (data) ) # single quotes into double quote all STR
Data = json.loads (Data) # Get json string from memory
Print (Data, type (Data)) # dict

with Open ( 'test.json', 'W', encoding = 'UTF8') AS FW:
  the json.dump (DIC, FW)

with Open (F '. { "Test"} JSON', 'R & lt', encoding = 'UTF8') AS fr:
Data the json.load = (fr)
Print (Data)

pickle module
pickle: python can serialize all objects (data types), but not cross-platform 

import pickle
import pickle
dic = {'a': 1, 'b': 'abc', 'c': None}
data = pickle.dumps(dic)
print(data,type(data)) # 二进制
data = pickle.loads(data)
print(data,type(data)) # dict

dic = {'a': 1, 'b': 'abc', 'c': None}
def func():
x = 3
print(x)

with open('test.pkl','wb') as fw:
  pickle.dump(func,fw)

with open('test.pkl', 'rb') as fr:
data = pickle.load(fr)
data()
print(data)

hashlib module
# Password encryption: No matter what you throw the string, he will return a string of a fixed length string 

Import hashlib

m = hashlib.md5 () # fixed wording
m.update (b'123456 ')
m.update (b'123 ')
m.update (b'456')
Print (m.hexdigest ())

# 123 456 - "e10adc3949ba59abbe56e057f20f883e
# 123456 -" e10adc3949ba59abbe56e057f20f883e

1. become a fixed character string
after the string 2. the same hash result Like
3. superposition

hmac module
# Hmac Module: encryption password, salt can 

Import HMAC

m = hmac.new (b'abc ') # salt
m.update (b'123456')
m.update (b'456 ')
Print (m.hexdigest ())

# 123 456 ABC -> 8c7498982f41b93eb0ce8216b48ba21d
# 123456 ABC -> 8c7498982f41b93eb0ce8216b48ba21d

1. The fixed character string into 
the same character string as the hash 2. Results
3. Superposition
4 above where it is added to the same 'salt' of change 'salt' results will be different


loggin module
logging - "Log # Reference nick blog 

Import logging

logging.debug ( 'debug') # 10
logging.info ( 'normal') # 20 # with this one
logging.critical ( 'serious error') # 30
logging.error ( 'error') # 40
logging.warning ( 'warning') # 50
default output 30 may be more




 

Guess you like

Origin www.cnblogs.com/wwei4332/p/11366022.html