Summary of common python small modules and python Module
Some easy-to-use small modules are often used during code writing. Here we will summarize them:
1. Obtain the current file name and directory name and add it to the system environment variable.
File = OS. path. abspath (_ file _) # obtain the absolute path of the file
File_name = OS. path. basename (file) # Get the file name
File_dir = OS. path. dirname (OS. path. dirname (file) # obtain the directory at the top of the file
OS. path. join (file_dir, "logs ")
Sys. path. append (file_dir)
2. Obtain the number of Chinese characters in a string
Def get_chinese_num (uchar): I = 0 for utext in uchar: if U' \ u4e00 '<= utext <= U' \ u9fa5 ': I + = 1 return istr = "2015 autumn and winter new velvet warm pullover sweater 15541707 BC17 gray floral gray" a = get_chinese_num (str) B = len (str) print (a, B)Obtain the number of Chinese Characters
3. encapsulate the print function and display different colors based on different types.
Def show_message (msg, msgtype): "encapsulates the print function. Different colors are displayed for different types: param msg: displayed Message Body: param msgtype: Message Type: return: returns formatted content "if msgtype =" NOTICE ": show_msg =" \ n \ 033 [1; 33 m {0} \ 033 [0m \ n ". format (msg) elif msgtype = "ERROR": show_msg = "\ n \ 033 [1; 31 m {0} \ 033 [0m \ n ". format (msg) elif msgtype = "INFORMATION": show_msg = "\ n \ 033 [1; 32 m {0} \ 033 [0m \ n ". format (msg) else: show_msg = "\ n {0} \ n ". format (msg) print (show_msg)Different types show different colors
4. Generate a non-repeated serial number based on the time
Import time
From datetime import datetime
Serno = "{0} {1 }". format (datetime. now (). strftime ("% Y % m % d % H % M % S"), str (int (time. time ())))
Print (serno)
5. Convert the digit week to a Chinese digit
Def numtochr (num_of_weekday): "converts a number week to a Chinese number: param num_of_weekday: the number of the day of the week (0, 1, 2, 3, 4, 5, 6): return: chinese day of the week "chrtuple = ('1', '2', '3', '4', '5', '6', 'day ') num = int (num_of_weekday) return chrtuple [num]Day of week to Chinese digit
6. A public detection function that determines whether the input information is null. If the input information is null, the input information is returned.
Def input_msg (message, limit_value = tuple (): "indicates a public detection function that determines whether the input information is null. If it is null, the input continues, if this parameter is not blank, the following information is returned: param limit_value: it has a limit on the input value. It must be the value of limit_value. ex :( "admin", "user"): param message: input () function prompt message: return the input information "is_null_flag = True while is_null_flag: input_value = input (message ). strip (). lower () if not input_value: show_message ("the input cannot be blank! "," ERROR ") continue elif len (limit_value)> 0: if input_value not in limit_value: show_message (" the input value is incorrect. Please enter it again! "," ERROR ") continue else: is_null_flag = False continue return input_valueWhether the input is null
Instance description:
Choose = common. input_msg ("select function number [1-5]:", ("1", "2", "3", "4", "5 ")). strip ()
7. Check whether the input date is correct: yyyy-mm-dd or yyyy-m-d.
Def input_date (msg, default_date): "determines whether the input date is correct. yyyy-mm-dd or yyyy-m-d: param msg: Enter the prompt message: param default_date: default date: return date str type "check_flag = False while not check_flag: strdate = input (msg ). strip () if not strdate: strdate = default_date try: date_list = strdate. split ("-") result = date (int (date_list [0]), int (date_list [1]), int (date_list [2]) check_flag = True distinct T ValueError: show _ Message ("the input date is invalid. Please enter it again! "," ERROR ") continue return result. strftime (" % Y-% m-% d ")The input date is correct