Skip to content

jwsmai/python_factory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Factory

使用configparser从配置文件获取参数

相关链接:

使用getopt从命令行获取参数

Highline

从命令行读取的参数默认是str类型,这里用int()函数将它转化为int类型

相关链接

使用loguru记录日志

Highline

使用@logger.catch代替try catch跟踪变量的值

代码:

@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')

相关链接

创建和判断inf、nan

代码

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数据

相关链接

正则表达式

Highline

re.findall(pattern, string, flags=0)

返回string中所有与pattern匹配的全部字符串,返回形式为数组。

import re

print(re.findall("123", ""))

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages