使用configparser从配置文件获取参数
相关链接:
使用getopt从命令行获取参数
从命令行读取的参数默认是str类型,这里用int()
函数将它转化为int类型
- https://www.geeksforgeeks.org/getopt-module-in-python/
- https://docs.python.org/zh-cn/3/library/getopt.html
使用loguru记录日志
代码:
@logger.catch
def my_function(x, y, z):
# An error? It's caught anyway!
return 1 / (x + y + z)
my_function(1, -1, 0)
日志:
2022-07-17 16:58:34.814 | ERROR | __main__:<module>:21 - An error has been caught in function '<module>', process '
MainProcess' (14074), thread 'MainThread' (4335273344):
Traceback (most recent call last):
> File "/Users/suwenjin/Documents/GitWarehouse/python_factory/common/loguru_example.py", line 21, in <module>
my_function(1, -1, 0)
└ <function my_function at 0x126cac790>
File "/Users/suwenjin/Documents/GitWarehouse/python_factory/common/loguru_example.py", line 18, in my_function
return 1 / (x + y + z)
│ │ └ 0
│ └ -1
└ 1
ZeroDivisionError: division by zero
Process finished with exit code 0
loguru日志太多时,可以根据不同的策略保存日志
- 每500MB创建一个文件
logger.add('runtime_{time}.log', rotation="500 MB")
- 每周创建一个日志
logger.add('runtime_{time}.log', rotation='1 week')
- 每天零点创建日志
logger.add('runtime_{time}.log', rotation='00:00')
import math
# 创建无穷大、负无穷大的浮点值
a = float('inf')
b = float('-inf')
# 创建非数字的nan
c = float('nan')
print(a, b, c)
# 判断inf、 -inf、 nan
print(math.isinf(a))
print(math.isinf(b))
print(math.isnan(c))
使用Databricks SQL Connector查询databricks数据
返回string中所有与pattern匹配的全部字符串,返回形式为数组。
import re
print(re.findall("123", ""))