国庆期间我也在浏览微信公众号,今天继续为大家推荐一些有趣使用的Python库。
pyfiglet——制作炫酷的艺术字
pyfiglet可以将只包含字母和数字的字符串转换成具有立体感的艺术字。使用pip即可安装它。
先来一段简单的代码,看一下它的美化效果。
python 代码:import pyfiglet
text = "QI1.ZONE"
fmt = pyfiglet.figlet_format(text, font="slant")
print(fmt)

slant的意思为斜体,通过这种方式转换成的艺术字可以用作程序运行时的开始logo,给使用者个深刻的印象。
可以通过pyfiglet.FigletFont.getFonts()查看所有支持的艺术字类型。
运行结果如下。
['1943____', '1row', '3-d', '3d-ascii', '3d_diagonal', '3x5', '4max', '4x4_offr', '5lineoblique', '5x7', '5x8', '64f1____', '6x10', '6x9', 'acrobatic', 'advenger', 'alligator', 'alligator2', 'alpha', 'alphabet', 'amc_3_line', 'amc_3_liv1', 'amc_aaa01', 'amc_neko', 'amc_razor', 'amc_razor2', 'amc_slash', 'amc_slider', 'amc_thin', 'amc_tubes', 'amc_untitled', 'ansi_regular', 'ansi_shadow', 'aquaplan', 'arrows', 'ascii_new_roman', ...
我挑了几个,可以看看运行结果。
python 代码:import pyfiglet
text = 'QI1.ZONE'
print('3d-ascii', pyfiglet.figlet_format(text, '3d-ascii'), sep='\n')
print('sketch_s', pyfiglet.figlet_format(text, 'sketch_s'), sep='\n')
print('avatar', pyfiglet.figlet_format(text, 'avatar'), sep='\n')
print('hollywood', pyfiglet.figlet_format(text, 'hollywood'), sep='\n')

coloredlogs——彩色日志
它可以美化Python自带的logging模块,给日志带上颜色,使日志更个性化更易读。使用pip即可安装它。
在使用此模块之前,需要设置日志的级别为调试。然后也是需要定义日志的输出内容,为一个格式化字符串。下面给出一个简单的代码,用coloredlogs模块前后输出的日志有什么区别。
python 代码:import coloredlogs
import logging
coloredlogs.install(level='DEBUG', fmt='%(asctime)s %(levelname)s %(message)s')
logging.debug('debug message')
logging.info('info message')
logging.error('error message')
logging.warning('warning message')

当然我们也可以自定义颜色。
python 代码:import coloredlogs
import logging
def fun_name():
level_styles = coloredlogs.DEFAULT_LEVEL_STYLES.copy()
level_styles['debug'] = {'color': 'magenta'}
level_styles['info'] = {'color': 'yellow'}
level_styles['error'] = {'color': 'red'}
level_styles['warning'] = {'color': 'blue'}
coloredlogs.install(level="DEBUG", level_styles=level_styles,
fmt='%(asctime)s - %(module)s - %(funcName)s - %(levelname)s - %(message)s')
logging.debug('debug message')
logging.info('info message')
logging.error('error message')
logging.warning('warning message')
fun_name()

可以看出它对日志的美化还是很到位的,还可以自定义。
timeit——精确测量Python代码的执行效率
在使用timeit时,我们可以自定义一个装饰器,在函数执行前后分别记录一次时间,最后减一下就是这个函数的执行时间。Python其实提供了一个timeit模块,它可以重复执行Python代码,详细测量Python代码的执行时间。它在实现上运用了Python的time模块,在一个独立的进程中多次重复执行目标代码,计算总的执行时间。它是Python的内置模块,直接使用即可。
timeit提供了一个timeit函数,用来测量函数的执行时间。
python 代码:timeit(stmt, setup=None, timer=None, number=1)
各个参数意义如下:
- stmt 是需要测量执行时间的代码
- setup 是执行 stmt 之前需要执行的代码
- timer 是用于测量时间的函数。默认情况下,timer 使用的是 time.perf_counter() 函数
- number 是需要测量的代码的次数。默认情况下,number 是 1
接下来举一个简单的例子。
python 代码:import timeit
def test_func():
print("Hello, world!")
print(timeit.timeit(test_func, number=1000))
该代码会运行test_func函数1000次,然后计算平均时间。
运行结果如下。
Hello, world!
...
0.007531400000000001
其中最后一列数字就是平均执行时间。
除了 timeit 方法,timeit 模块还提供了 repeat() 方法,这个方法可以多次执行一个函数,并返回每次执行的执行时间。timeit.repeat() 方法的语法如下:
python 代码:timeit.repeat(stmt, repeat=3, setup=None, timer=None, number=1)
- stmt 是需要测量执行时间的代码
- repeat 是需要执行的次数
- setup 是执行 stmt 之前需要执行的代码
- timer 是用于测量时间的函数。默认情况下,timer 使用的是 time.perf_counter() 函数
- number 是需要测量的代码的次数。默认情况下,number 是 1
如:
python 代码:import timeit
def test_func():
return 10 ** 2
print(timeit.repeat(test_func, repeat=3))
该代码会得出以下结果:
[0.03199690004112199, 0.03228519996628165, 0.0314113000058569]
turtle——使用画笔绘制可爱的图案
Python的turtle模块是一个非常有趣的模块,正如它的名字一样,画板上会出现一个乌龟的图案,然后乌龟爬过的路径绘制成图案。
在实际绘图之前,需要设定一下绘图的参数。turcle提供了一个方法,可以设置这些参数。
python 代码:def setting():
pensize(4) # 画笔大小
hideturtle() # 隐藏乌龟,使绘制图像的过程更美观
colormode(255) # 设置颜色模式
color((255, 155, 192)) # 设置颜色
setup(840, 500) # 设置绘图窗体的大小
speed(10) # 画笔爬行速度
下面使用这个库绘制一个爱心。
这个是有动画的,感兴趣的可以复制这个代码运行一下。
python 代码:from turtle import *
def curvemove():
for i in range(200):
right(1)
forward(1)
setup(600,600,600,600)
hideturtle()
pencolor('black')
fillcolor("red")
pensize(2)
begin_fill()
left(140)
forward(111.65)
curvemove()
left(120)
curvemove()
forward(111.65)
end_fill()
penup()
goto(-27, 85)
pendown()
done()

那个第一个文字真的好常见,一会去试试
哈哈,去试试,很好看!
暂无点赞
暂无点赞
我学的第一个Python代码,就是画心~
我学的第一个代码是HelloWorld哈哈
暂无点赞
暂无点赞
我现在的文章质量怎么样?
以前你的站点类似于个人日记类,不像是博客,我是只加博客的。现在反正无所谓了
现在的文章质量怎么样?
现在挺好的,还有很大进步空间。
暂无点赞
暂无点赞
暂无点赞
1