また始まるpython-day 06-20200723-閉包-装飾器-モジュール-os-sys-math-random-datetime-time-calendar-hashlib-hmac

50157 ワード

# 1.                
# 2.               
# 3.                               

#   
#                      
#   =   +    
def outer():
    x = 10
    def inner():
        y = x + 1
        return y
    return inner
print(outer()())

#            
import time # time           

#           
#      1970-1-1 00-00-00 UTC       
# start = time.time() # time     time                    
# print('start=',start)
# x = 0
# for i in range(1,10000000):
    # x += i
# print(x)

# end = time.time()
# print('      {} '.format(end - start))
# time.sleep(3)
# def acl_time(fn):
    # start = time.time() 
    # print('start=',start)
    # fn()
    # end = time.time()
    # print('      {} '.format(end - start))
# def foo():
    # print('hello')
    # time.sleep(10)
    # print('world')

# acl_time(foo) #                



#         
import time
def acl_time(fn):
    def inner(x,*args,**kwargs): #       
        start = time.time()
        s = fn(x)
        end = time.time()
        print('    ',end - start)
        return s
    return inner #       
@acl_time #        acl_time;              fn
def demo(n):
    x = 0
    for i in range(1,n):
        x += i
    return x
#           demo        demo                  
print(demo(100000,5,7 ))
# @acl_time #     
def foo():
    print('1')
    time.sleep(3)
    print('2')
# foo() #    foo    inner
#     22               
#             

def can_play(fn):
    def inner(x,y,*args,**kwargs):
        clock = kwargs.get('clock',23) #                     
        if clock <= 22:
        # if args[0] <= 22:
            fn(x,y)
        else:
            print('   ',x,'   ',y   )
    
    return inner

#       
#                
@can_play
def play_game(name,game):
    print('{}  {}'.format(name,game))
play_game('  ','  ',clock=18)
play_game('  ','  ')


#          
all=[]
def all_job():
    print('---------------')
    print('       v1.0')
    print('1:    ')
    print('2:    ')
    print('3:    ')
    print('4:    ')
    print('5:      ')
    print('6:    ')
    print('---------------')
    input_num = int(input("         (  ):"))
    if input_num == 1:
    #           
        only = {}
        name = input('     :')
        for s in all:
            if name in s['name']:
                print('     ')
                all_job()
        only['name'] = name
        phone = input('      :')
        only['phone'] = phone
        QQ =  input('   QQ :')
        only['QQ'] = QQ
        all.append(only)
        print(all)
        all_job()
        #       
        #                   
    elif input_num == 2:
        num = int(input('           :'))
        del all[num]
        all_job()
    elif input_num == 3:
        pass
        num = int(input('          :'))
        print('name:{} phone:{} QQ:{}'.format(all[num]['name'],all[num]['phone'],all[num]['QQ']))
        name = input('       :')
        all[num]['name'] = name
        phone = input('        :')
        all[num]['phone'] = phone
        QQ = input('     QQ :')
        all[num]['QQ'] = QQ
        print(all)
        all_job()
    elif input_num == 4:
        name = input('         :')
        for s in all:
            if name in s['name']:
                print('name:{} phone:{} QQ:{}'.format(s['name'],s['phone'],s['QQ']))
                
            else:
                print('     ')
        all_job()       
        #          
        #         
        #              
    elif input_num == 5:
        print('                        QQ')
        for i in all:
            print(' {}     {}        {}        {}'.format(all.index(i),i['name'],i['phone'],i['QQ']))
        all_job()
        #      
    elif input_num == 6:
        num = input('         (yes/no):')
        if num == 'yes' or num == 'y':
            exit()
        else:
           all_job() 
        #       
        
# all_job()



#      
#   : python   py             
#      py            
#        py                            
# python                  

import time # 1.  time.py   
print(time.time()) #                   

from random import randint # 2.form     import                   
print(randint(0,2)) #      

from math import * # 3.from     import *        '  '     
print(pi)

import datetime as dt # 4.            
print(dt.MAXYEAR)

from copy import deepcopy as dp # 5.form     import     as   
print(dp([1,2,3,4,]))


#        

# OS    OperationSystem    

# os                      
# os.name()          win nt -----    posix
# os.sep()       win \ ----    /
# path     
# os.path.abspath()          
# os.path.isdir()         
# os.path.isfile()        
# os.path.exists()       
# os.path.splitext()        
# os.getcwd()         
# os.chdir(../)                
# os.rename('a','b')      
# os.remove('a')     
# os.rmdir('a')       
# removedirs        mkdir       listdir           
# envison        
import os
print(os.name) 
print(os.sep) 
print(os.path.abspath('D:\Python\Python\day06-0723.py')) 
print(os.path.isdir('D:\Python\Python\day06-0723.py')) 
print(os.path.isfile('D:\Python\Python\day06-0723.py')) 
print(os.path.exists('D:\Python\Python\day06-0723.py'))
print(os.path.splitext('D:\Python\Python\2020.txt'))
print(os.getcwd())
os.chdir('D:\Python')
print(os.getcwd())
# os.mkdir('zsssssss')
print(os.listdir(os.getcwd()))
# os.removedirs('zss')
# print(os.listdir(os.getcwd()))

# sys          
# sys.exit()      exit()    
# sys.path        
import sys
# sys.exit()     
print('111')
# sys.exit()
print('222')
print(sys.path)
# sys.stdin #    input          
# sys.stdout #   sys.stdout            
# sys.stderr #   sys.stdout                
# input('   :')

# math            
import math
print(math.factorial(6)) # 6   
print(math.pow(2,10)) # 1024     2 ** 10 pow(2,10)
print(math.floor(12.98)) # 12     
print(math.ceil(12.001)) # 13     
print(math.sin(math.pi / 2 )) # sin90    Π = 180°
print(math.cos(math.pi / 2 )) # cos90
print(math.tan(math.pi / 2 )) # tan90

# random            
# random.randint(1,3) [1,3]
# random.random() [0,1)      
# random.randrange(2,9) [2,9)     
# random.choice(['11','22','33','44'])                  
# random.sample(['11','22','33','44'],2)               n   
import random
print(random.randint(1,3)) #   a-b        randrange(a,b+1)
print(random.random()) # [0,1)      
print(random.randrange(2,9))
print(random.choice(['11','22','33','44']))
print(random.sample(['11','22','33','44'],2))

# datetime         
import datetime as dt
# datetime.now()       
# date()       
# time()       
# timedelta(3) 3  
# _x __x __x__
print(dt.datetime.now()) #       
print(dt.date(2020,1,1)) #       
print(dt.time(12,22,33)) #       
print(dt.datetime.now() + dt.timedelta(3)) #   3        

# time        
import time
print(time.time()) #    1970-1-1-00:00 UTC             
print(time.strftime('%Y-%m-%d %H:%M:%S')) #         
print(time.asctime()) # Fri Jul 24 17:34:13 2020 asctime    
print(time.ctime()) # Fri Jul 24 17:34:13 2020              ctime     
print('2020')
# time.sleep(5) #   5 
print('2021')

# calendar        
import calendar
calendar.setfirstweekday(calendar.SUNDAY) #                    0-6
print(calendar.calendar(2020)) #   2020               
print(calendar.firstweekday()) #                       0
print(calendar.isleap(2021)) #     True    False
print(calendar.leapdays(2000,2020)) #    2000 2020        
print(calendar.month(2020,7)) #   2020 7    

# hashlib  
# hmac  
#        
# hashlib   2    md5   sha   
#     :     :       ,    (md5/sha)           ras
#               

import hashlib
x = hashlib.md5() #     md5   
x.update('abc'.encode()) # abc ==> 900150983cd24fb0d6963f7d28e17f72
print(x.hexdigest()) # 900150983cd24fb0d6963f7d28e17f72

h1 = hashlib.sha1('123456'.encode())
print(h1.hexdigest()) # 7c4a8d09ca3762af61e59520943dc26494f8941b
h1 = hashlib.sha224('123456'.encode()) # 224         4 
print(h1.hexdigest()) # f8cdb04495ded47615258f9dc6a3f4707fd2405434fefc3cbf4ef4e6
h1 = hashlib.sha256('123456'.encode()) # 256 
print(h1.hexdigest()) # 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
h1 = hashlib.sha384('123456'.encode()) # 384 
print(h1.hexdigest()) # 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92

import hmac
# hmac         
import hmac
message = b'Hello world'
key = b'secret'
h = hmac.new(key,message,digestmod='MD5')
print(h.hexdigest())