current position:Home>Python notes-day13-system module

Python notes-day13-system module

2022-05-15 06:03:43Wandering mage 12

Preface
python Grammar learning , Leave it to those who need it , Understand everything !!

# coding=utf8
# @time:2022/4/21 20:14
# Author  Haoyu 

#  One 、 Time module 
# import time,datetime
# 1.python There are two modules related to time :time、datetime
# 2. Time stamp 
'''''''''
 Timestamp refers to the current time to 1970 year 1 month 1 Japan 0 when 0 branch 0 second ( It refers to Greenwich mean time ) The time difference ( The unit is seconds )
 benefits :
1) Saving time with a timestamp takes much less than saving time with a string 
2) It's easier to encrypt time with a timestamp 
1650544031.5202246  ->    Including the year \ month \ Japan \ when \ branch \ second \ millisecond 

'''''''''
#  One . Time stamp 
# 1.time() usage  -  Get the current time 
# t1 = time.time()
# print(t1,type(t1)) # 1650543825.3692148 <class 'float'> ( Time stamp )


# 2.locatime usage 
# 1)localtime() -  Get local time 
# t2 = time.localtime()
# print(t2,type(t2)) # time.struct_time(tm_year=2022, tm_mon=4, tm_mday=21, tm_hour=20, tm_min=40, tm_sec=15, tm_wday=3, tm_yday=111, tm_isdst=0)

# 2)localtime( Time stamp ) -  Convert timestamp to local structure time 
# t3 = time.localtime(0)
# print(t3)

# 3. Convert structure time to string time 
'''''''''
 Time format string  -    Contains the time placeholder string 
%Y  -    year 
%m  -    month 
%d  -    Japan 
%H  -    when 
%M  -    branch 
%S  -    second 
%a  -    week ( Abbreviations of English words )
%A  -    week ( Complete spelling of English words )
%b  -    month ( English abbreviation )
%B  -    month ( Spell it all in English )
'''''''''
# strftime( Time format string , Structure time )
# t5 = time.localtime()
# s_t5 = time.strftime('%Y year -%m month -%d Japan ',t5)
# print(s_t5)
# -----------------------------------------------


# 4.sleep( Time ) -  Program pause time ( Unit second )
# time.sleep(2)
# print('================')


# 4.datetime
'''''''''
from datetime import time,date,datetime,timedelta
# time -  Minutes and seconds 
# date -  Specific date 
# datetime -  Mm / DD / yyyy HHM / S 
t1 = date.today()
print(t1,t1.year,t1.month,t1.day)   # 2022-04-21 2022 4 21

t2 = datetime.now()
print(t2)   # 2022-04-21 22:50:40.328680
'''''''''


#  Two .hashlib modular 
import hashlib

# hashlib yes python It comes with a special one to provide hash Encrypted module 
# 1.hash Features of encryption 
'''''''''
1) The result of the same data through the same encryption algorithm is the same ( The encrypted result is called ciphertext or abstract )
2) The encrypted result is irreversible 
3) The length of the summary generated by the same algorithm is the same for data of different sizes 
'''''''''

# 2. Application scenarios 
'''''''''
1) Create data irreversible ciphertext ( encryption )
2) Verify the integrity of the data and whether it has been modified 
'''''''''

# 3. How to generate summary 
'''''''''
# 1) Create according to the encryption algorithm hash object 
hash = hashlib.md5()    #  common hash Algorithm :md5、sha relevant 
# 2) Add encrypted object 
# hash object .update( data )
#  data  -  Must be bytes object 
hash.update(bytes(' I am the best in the world ',encoding = 'utf--8'))  #  The added data needs to be converted into binary 
# 3) Generate summary ( Generating ciphertext )
# hash object .hexdigest()
result = hash.hexdigest()
print(result)   # 1bcbf481b5a6b208c8e3d8b2765f1146
'''''''''

#  Generate image summary 
'''''''''
hash = hashlib.md5()
with open(' beauty .jpg','rt') as f:
    hash.update(f.read())
    print(hash.hexdigest())
'''''''''

#  Generate a summary of the text file 
'''''''''
hash = hashlib.md5()
with open('01- Exception trapping .py','rb') as f:
    hash.update(f.read())
    print(hash.hexdigest())
'''''''''

#  Supplementary content :
'''''''''
# 1) String to binary 
#  Method 1 :bytes( character string ,encoding=utf-8)
str1='hello world'
b1=bytes(str1,encoding='utf-8')
print(b1,type(b1))

#  Method 2 : character string .encode()
b2=str1.encode()
print(b1,type(b1))

# 2) Binary to string 
#  Method 1 :a.str( Binary system ,encoding='utf-8')
s1=str(b1.encoding='utf-8')
print(s1,type(s1))

#  Method 2 : Binary system .decode()

'''''''''

More secure sharing , Please pay attention to 【 Security info】 WeChat official account !

copyright notice
author[Wandering mage 12],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/131/202205110607424233.html

Random recommended