API - 常用工具箱¶
mytools.Error (value) |
Base class for exceptions in this module. |
mytools.StringPatternError (value) |
Exception raised for errors in the pattern of string args. |
mytools.MyEncoder ([skipkeys, ensure_ascii, ...]) |
解决json.dumps不能序列化datetime类型的问题:使用Python自带的json.dumps方法 |
mytools.get_mac_address () |
Get mac address. |
mytools.get_hostname () |
Get hostname. |
mytools.get_ip_address (hostname) |
Get host ip address. |
mytools.get_host_ip () |
Get host ip address. |
mytools.get_current_function_name () |
Get current function name. |
mytools.Walk ([db]) |
Walk directory to batch processing. |
mytools.time_me ([info, format_string]) |
Performance analysis - time |
mytools.get_timestamp ([s, style, pattern]) |
Get timestamp. |
mytools.get_current_time ([format_string, info]) |
Get current time with specific format_string. |
mytools.get_age ([format_string, info]) |
Get age with specific format_string. |
mytools.random_item (mylist) |
Get random item of data. |
mytools.file_replace (source_file, ...) |
File replace. |
mytools.read_excel (filepath) |
Get excel source |
mytools.set_excel_style (name, height[, bold]) |
Set excel style. |
mytools.write_excel ([filename, sheets]) |
Write excel from data. |
mytools.write_excel_sql ([filename, sheets]) |
Write excel from data. |
mytools.generate_dict (dictpath, sourcepath) |
Generate dictionary file from sourcefile. |
mytools.waitting () |
Print waitting. |
自定义字符串格式异常¶
MyEncoder 解决 json.dumps 不能序列化 datetime 类型的问题¶
-
mytools.
MyEncoder
(skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]¶ 解决json.dumps不能序列化datetime类型的问题:使用Python自带的json.dumps方法 转换数据为json的时候,如果格式化的数据中有datetime类型的数据时会报错。 TypeError: datetime.datetime(2014, 03, 20, 12, 10, 44) is not JSON serializable Usage: json.dumps(data, cls=MyEncoder)
获取IP地址¶
遍历目录进行批处理¶
-
mytools.
Walk
(db=None)[source]¶ Walk directory to batch processing. 遍历目录进行批处理。
Subclasses may override the ‘handle_file’ method to provide custom file processing mode. 子类可以重写’handle_file’方法来实现自定义的文件处理方式。
Public attributes: - filelist: All filenames with full path in directory. - fnamelist: All filenames in directory. - dirlist: All dirnames with full path in directory. - dnamelist: All dirnames in directory.
程序时间性能分析¶
-
mytools.
time_me
(info='used', format_string='ms')[source]¶ Performance analysis - time
Decorator of time performance analysis. 性能分析——计时统计 系统时间(wall clock time, elapsed time)是指一段程序从运行到终止,系统时钟走过的时间。 一般系统时间都是要大于CPU时间的。通常可以由系统提供,在C++/Windows中,可以由<time.h>提供。 注意得到的时间精度是和系统有关系的。 1.time.clock()以浮点数计算的秒数返回当前的CPU时间。用来衡量不同程序的耗时,比time.time()更有用。 time.clock()在不同的系统上含义不同。在UNIX系统上,它返回的是”进程时间”,它是用秒表示的浮点数(时间戳)。 而在WINDOWS中,第一次调用,返回的是进程运行的实际时间。而第二次之后的调用是自第一次 调用以后到现在的运行时间。(实际上是以WIN32上QueryPerformanceCounter()为基础,它比毫秒表示更为精确) 2.time.perf_counter()能够提供给定平台上精度最高的计时器。计算的仍然是系统时间, 这会受到许多不同因素的影响,例如机器当前负载。 3.time.process_time()提供进程时间。
- Args:
info: Customize print info. 自定义提示信息。 format_string: Specifies the timing unit. 指定计时单位,例如’s’: 秒,’ms’: 毫秒。
Defaults to ‘s’.