python-- keyword parameter / variable parameters - built-in function - Time Module - Module Common

--- --- restore the contents of the start key parameter / variable parameter

A knowledge point, built-in functions

def send_msm2 (* args): # variable parameter, the parameter set 
Print ( 'Phone', args)
send_msm2 () Returns the value of the tuple #

def send_msm2 (* args): # variable parameter, the parameter set 
Print ( 'Phone', args)
send_msm2 (110, 113, 333)

# Parameter set can not be used word = "haha" a form directly transmitted 
DEF say (Word, * args):
Print (Word)
Print (args)
say ( 'NiHao', 34444,666)

# Keyword arguments passed parameter = value format Key 
DEF kwfun (** kwargs):
Print (kwargs)
kwfun () # Empty dictionary

def kwfun(**kwargs):
print(kwargs)
kwfun()#空字典
kwfun('222')

def kwfun(**kwargs):
print(kwargs)
kwfun()#空字典
kwfun(age = '123')
kwfun(age = '123',name = 'xiaohei')

def t1 (word, country = ' china', * args, ** kwargs): # ( mandatory parameter, the default value of the parameter, the parameter set, the keyword parameter) must be defined four types of parameters, a fixed order to 
print (word )
Print (Country)
parameter print (args) # no parameters gave claimed

Print (kwargs)
T1 ( 'HAH', 'Japan', 134,333, name = 'DDE', Age = 44 is)

def redis(ip,pwd,post = 5666):
print('连接redis')
print(ip)
print(post)
print(pwd)
print('='*10)
redis('127.0.0.01','23344')

Redis DEF (IP, pwd, POST = 5666): 
Print ( 'connected Redis')
Print (IP)
Print (POST)
Print (pwd)
Print (' = '* 10)
redis_info = (' 127.0.0.1 ',' 12356 ', 777)
Redis (redis_info [0], redis_info [. 1], redis_info [2])
Redis (* redis_info) # unpacking, automatically tuple apart, with the former two meaning codes

 

 def redis(ip,pwd,post = 5666):

    print ( 'connected Redis') 
Print (IP)
Print (POST)
Print (pwd)
print (' = '* 10)
redis_info2 = {' IP ':' 127.0.0.1 ',' pwd ':' 5667 ',' POST ': 444}
Redis (redis_info2 [' IP '], redis_info2 [' pwd '], redis_info2 [' POST '])
Redis (** redis_info2) alternatively on two codes #

 Knowledge point two, built-in functions

# Coding = UTF-. 8 
# INPUT ()
# Print ()
# len ()
# type ()
# STR ()
# tuple ()
# SET ()
# dict ()
# List ()
L = 'asdfgwert3r'
# the sorted ( l) # sort
# print (all ([1,2,3,4, ''])) # If Li
# print (the any ([0,0,0,1])) # can be determined for the iteration
# print (bin (10)) # decimal transfected binary
# print (bool (0)) # convert an object into a boolean
# print (chr (66)) # print number corresponding ASCII
# Print (the ord ( 'B')) # print string corresponding to ASCII
# Print (dict (a =. 1, B = 2)) # conversion dictionary
# S = 'a'
# Print (the dir (S)) # printing method can be called incoming object
= S '[l, 2,3]' 

Print (the eval (S)) # python code execution, only perform simple data type definitions and calculation, 1 + 1, is defined dictionaries

# Print (exec ( 'def a (): pass')) # execute python code 
# print (filter (lambda x: x> 5, [12,3,12,2,1,2,35])) # the behind the object of the preceding iteration method
# Print (Map (the lambda X: X>. 5, [1,2,3,4,5,6]))
#Print (max ([12,3,4,5]) ) takes a maximum value #
#min ([12,3,4,5])
#Print (OCT (. 9)) is converted into digital # octal
#print (round (3.1415926,3)) # take several decimal places
# print (eval ( '[]' )) # python code execution, only perform simple arithmetic and defined data types
# print (exec ( 'def a (): pass')) # execute python code
# print (filter ( lambda x: x> 5, [ 12,3,12,2,1,2,35])) # the back of the object of the preceding iteration method
# print (map (lambda x: x> 5, [1,2 , 3,4,5,6]))
#zip ()


# = func_str '' '
# Import Time
# DEF Fun ():
# return' func_name '
#' ''
# Exec (func_str)
# Result = Fun ()

#print (result)

# f = open('作业.py',encoding='utf-8')
# result = f.read()
# exec(result)

# l = [1,2,3,4,5,6,7,8,9,10]
#
# def t(num):
# if num % 2 == 0:
# return True

#l2 = list( filter(t,l) )
# l3 = list( map(t,l) )
# print(l3)
# print(l2)

# l1=[1,2,3,4]
# l2=['a','b','c','d']
# l3=['a','b','c','d','f']
# for k1,k2,k3 in zip(l1,l2,l3):
# print(k1,k2,k3)

Knowledge-thirty, module

1, a module is a python file
1, write your own python file
2, the standard module, comes with python
3, third-party modules
installed:
PIP install # xxx install
pip uninstall xx # uninstall
pip install xx -U # Update
pip freeze > export .txt # third-party modules installed third-place module
pip install -r third-party modules .txt # read from the file inside the module, batch installation
pip problems:
1, pip command does not exist, the installation of python under Scripts adding environment variables
when 2, pip pip useless under the python directory, use the command know where the other pip, the name was changed to other
manual installation:
1, the end of the WHL
pip install /Users/PyMySQL-0.9.3-py2.py3 -none-any.whl # absolute path behind whl document
2, the end of the tar.gz
1, extract
2, the command line into which the extracted directory
3, performed python setup.py install

If there are multiple versions of the python computer
1, the install xpinyin to python3 -m PIP
python2 the install xpinyin -m PIP
2, various versions of the following to find scripts directory, pip modify each pip2 / pip3
sequence introduced module
1, the current directory look for
2, sys.path

The essence of the import module
import the substance of the module is to execute this module from top to bottom again

 os module

os Import 
os.remove ()
os.rename ()
os.mkdir () # create folders, single
os.makedirs () # create the folder, create a multi-layer directory

os.listdir (r'Documents \ Tencent Files \ 2273747892 \ FileRecv \ day4 ') # r without translation plus

Files = the os.listdir (r'C: \ the Users \ yangmingyue \ Documents \ Tencent Files \ 2273747892 \ FileRecv \ Day4 \ Day4')
Print (Files)
Print (the os.path. isdir (r'C: \ Users \ yangmingyue \ documents \ Tencent files \ 2273747892 \ FileRecv \ day4 \ day4 ')) # judge is not a folder
print (os.path.isfile (r'C: \ Users \ yangmingyue \ documents \ Tencent files \ 2273747892 \ FileRecv \ day4 \ day4 ')) # judge is not a file
os.chdir (r'C: \ Users \ yangmingyue \ documents \ Tencent files \ 2273747892 \ FileRecv \ day4 \ day4') # to enter the file
print (os.listdir ()) # get the current directory
print (os.getcwd ()) # get the current path
# Judge folder or file 
# name = '.mp4'
# DEF search_file (path, name):
# for cur_dir, dirs, Files in os.walk (path):
# Files for File in:
# IF name in File:
# = the os.path.join abs_path with (cur_dir, file)
# Print ( '!% s file path% s'% (file, abs_path with))
#
# search_file ( '/', name)

result = os.system ( 'ifconfig') # execute operating system commands to help you execute the command, get command execution results 
#result = os.popen ( 'ifconfig') . read () # execute operating system commands, you can get results
Print ( 'the result ...', the result)
#Print (os.getcwd ()) # get the current path
# print (os.path.getsize ( 'products.json' )) # get the file size
# print ( os.path.exists ( 'products.json')) # if there is
# print (os.path.getatime ( 'products.json' )) # last access time
# print (os.path.getctime ( 'products. json ')) # createtime creation time
# print (os.path.getmtime (' products.json ' )) # modiyftime modification time
# print (os.path.split (r' / Users / nhy / PycharmProjects / mjz / day4 / products.json ')) # split file path and file name
#print (os.path.dirname (r' / Users / nhy / PycharmProjects / mjz / day4 / os module .py ')) # Get parent directory

#print (os .path.abspath (__ file __)) # get the absolute path of the relative path represents the current directory, ..A directory on behalf of

# os.rmdir () # delete empty folders
# os.removedirs () # delete empty folders
knowledge Fourth, time module
Time Import, datetime 
# 2019-10-19 13:23:38
# 13,245,232,453
# timestamp string of numbers
# from a computer that second birth to now the number of seconds

# print (time.time ()) # get the first timestamp
# print (time.strftime ( '% Y- % m-% d% H:% M:% S')) # formatted current time

# tuples time

# 1, the formatted time stamp transfer

timestamp = 1571476513
# time_tuple time.gmtime = (timestamp) time # standard time zone conversion
time_tuple = time.localtime (timestamp) # time to the current time zone conversion
result = time.strftime ( '% Y-% m-% d% H: % M:% S ', time_tuple ) # time format
Print (Result)

DEF timestamp_to_str (timestamp = None, the format ='% Y-M-% D%% H:% M:% S '):
' '' time turn formatted time stamp, the current time returned by default '' '
IF timestamp:
time_tuple time.localtime = (timestamp) time to the current time zone # conversion
result = time.strftime (format,time_tuple)
else:
The time.strftime = Result (the format)
return Result

DEF str_to_timestamp (String = None, the format = '% Y-M-% D%% H:% M:% S'):
'' 'formatted string rotation time stamp, the current timestamp returned by default '' '
IF String:
time_tuple the time.strptime = (String, the format) # formatted time, transfer time tuples
result = time.mktime (time_tuple) # time to turn into a tuple stamp
the else:
Result = the time.time ()
return int (Result)

# S = '2019-10-19 17:15:13'
# time_tuple the time.strptime = (S, '%% Y-M-% D% H:% M:% S ' ) # formatted time, transfer time tuples
# result = time.mktime (time_tuple) # converted into the time stamp tuples
# Print (Result)

T1 = str_to_timestamp () + 60 * 60 * 24 * 3

knowledge 4.5, create log
timestamp_to_str DEF (timestamp = None, the format = '% Y-M-% D%% H:% M:% S'): 
'' 'turn formatted time stamp, a time stamp if there is no transmission, to obtain the current formatting time '' '
IF timestamp:
time_tuple time.localtime = (timestamp) time is converted into time stamp # tuple
result = time.strftime (format, time_tuple) # time to turn into a tuple formatted time
return Result
the else:
return The time.strftime (the format)


Import Time, OS, Random
L = [ 'iOS', 'Android', 'Nginx', 'Tomcat', 'Python', 'Blog', 'Apache', 'MySQL' , 'Redis']
for I in L:
P = the os.path.join ( 'logs', I)
os.makdirs (P)
for J in Range (30):
T = int (the time.time ()) - 86400 J *
time_str timestamp_to_str = (T,'%Y-%m-%d')
log_name = '%s_%s.log'%(i,time_str)
= the os.path.join abs_file_path ( 'logs', I, log_name)
FW = Open (abs_file_path, 'W', encoding = 'UTF-. 8')
IF the random.randint (1,10)% 2 == 0:
FW .write ( 'wins more negative defensive play both sides')
fw.close ()

operation result: the current generation log file directory

Guess you like

Origin www.cnblogs.com/luoxinmoli/p/11737358.html
Recommended