API - 常用工具箱

../_images/mytools.ico
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.

自定义异常

mytools.Error(value)[source]

Base class for exceptions in this module.

自定义字符串格式异常

mytools.StringPatternError(value)[source]

Exception raised for errors in the pattern of string args.

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)

获取Mac地址

mytools.get_mac_address()[source]

Get mac address.

获取主机名

mytools.get_hostname()[source]

Get hostname.

获取IP地址

mytools.get_ip_address(hostname)[source]

Get host ip address.

mytools.get_host_ip()[source]

Get host ip address.

获取当前运行函数名

mytools.get_current_function_name()[source]

Get current function name.

遍历目录进行批处理

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’.

获取指定日期表示方式的时间戳或者当前时间戳

mytools.get_timestamp(s=None, style='%Y-%m-%d %H:%M:%S', pattern='s')[source]

Get timestamp. 获取指定日期表示方式的时间戳或者当前时间戳。

Args:
style: Specifies the format of time. 指定日期表示方式。
Defaults to ‘%Y-%m-%d %H:%M:%S’.
pattern: Specifies the timestamp unit. 指定时间戳单位,’s’: 秒,’ms’: 毫秒。
Defaults to ‘s’.

获取指定日期表示方式的当前时间

获取指定日期表示方式的年龄

mytools.get_age(format_string='%s年%s个月%s天', info='2016-7-25')[source]

Get age with specific format_string. 获取指定日期表示方式的年龄。

Args:
format_string: Specifies the format of time. 指定日期表示方式。
Defaults to ‘{y}年{m}个月{d}天’.

列表随机元素

mytools.random_item(mylist)[source]

Get random item of data. 从数据中获取随机元素。

Returns:
item: Random item of data. 数据随机项。

文件替换

mytools.file_replace(source_file, destination_file)[source]

File replace. 文件替换。

Args:
source_file: The full path of source file. 原始文件完整路径。 destination_file:The full path of destination file. 目标文件完整路径。

读取excel

mytools.read_excel(filepath)[source]

Get excel source

Args:
filepath: The full path of excel file. excel文件完整路径。
Returns:
data: Data of excel. excel数据。

设置excel样式

mytools.set_excel_style(name, height, bold=False)[source]

Set excel style.

写入excel

mytools.write_excel(filename='demo.xlsx', sheets=None)[source]

Write excel from data.

mytools.write_excel_sql(filename='demo.xlsx', sheets=None)[source]

Write excel from data.

生成词典

mytools.generate_dict(dictpath, sourcepath)[source]

Generate dictionary file from sourcefile.

Args:
dictpath: The full path of dict file. 源文件完整路径。 sourcepath: The full path of source file. 源文件完整路径。

显示等待中

mytools.waitting()[source]

Print waitting.