其他
答应我,调试 Python 代码,不要再用 Print 了!
The following article is from 法纳斯特 Author 小F
关注上方“Python数据科学”,选择星标,
关键时间,第一时间送达!
num2 = 40
print(num1)
print(num2)
40
num2 = 40
print("num1" num1)
print("num2" num1)
num2 40
def plus_five(num):
return num + 5
ic(plus_five(4))
ic(plus_five(5))
ic| plus_five(5): 10
if user:
print("I'm user")
else:
print("I'm not user")
hello(user=True)
def hello(user:bool):
if user:
ic()
else:
ic()
hello(user=True)
from icecream import ic
import time
from datetime import datetime
def time_format():
return f'{datetime.now()}|> '
ic.configureOutput(prefix=time_format)
for _ in range(3):
time.sleep(1)
ic('Hello')
2021-01-24 10:38:24.545628|> 'Hello'
2021-01-24 10:38:25.550777|> 'Hello'
def plus_five(num):
return num + 5
ic.configureOutput(includeContext=True)
ic(plus_five(4))
ic(plus_five(5))
ic| ice_test.py:8 in <module>- plus_five(5): 10
prefix,自定义输出前缀
outputFunction,更改输出函数
argToStringFunction,自定义参数序列化字符串
includeContext,显示文件名、代码行、函数信息
def plus_five(num):
return num + 5
ic.configureOutput(includeContext=True)
ic(plus_five(4))
ic(plus_five(5))
for i in range(10):
print(f'****** Training model {i} ******')
ic| ice_1.py:8 in <module>- plus_five(5): 10
****** Training model 0 ******
****** Training model 1 ******
****** Training model 2 ******
****** Training model 3 ******
****** Training model 4 ******
****** Training model 5 ******
****** Training model 6 ******
****** Training model 7 ******
****** Training model 8 ******
****** Training model 9 ******
总结
到此,你就应该就学会了如何使用icecream去打印调试。更多功能可以访问「GitHub」,了解详情~
https://github.com/gruns/icecream
赠送新书《Python最优化算法实战》5 本!已经赠送多次了,这本书非常棒,由「北京大学出版社」赞助提供,感兴趣的朋友非常推荐入手一本。
介绍:本书以理论结合编程开发为原则,使用Python作为开发语言,讲解优化算法的原理和应用,详细介绍了Python基础、Gurobi 优化器、线性规划、整数规划、多目标优化、动态规划、图与网络分析、智能优化算法。对于算法部分的每一种算法都包含原理和编程实践,使读者对优化算法的认识更加深入。