散文想法简友广场

python装饰器获取程序运行时间、新建文件夹及播放音乐

2021-12-02  本文已影响0人  Cache_wood

Process类实现了两个方法属性,generator函数生成大列表,用装饰器来测试内存占用情况,traverse函数遍历大列表,用tqdm来查看运行进度。
import os
import time
import pygame
from tqdm import tqdm
from functools import wraps
from memory_profiler import profile
from line_profiler import LineProfiler

class Process:
    def __init__(self):
        pass

    @profile  #查看内存时间装饰器
    def generator(self):  #大的列表生成
        a = [1] * (10 ** 6)
        b = [2] * (2 * 10 ** 7)
    
    def traverse(self):
        a = [1] * (10 ** 3) #大列表生成加遍历,tqdm查看进度
        for i in tqdm(a):
            time.sleep(0.01)
            
p = Process()
#使用memory_profile查看内存占用
p.generator()

#使用line_profile查看运行时间
lp = LineProfiler()
lp.enable_by_count()
lp_wrapper = lp(p.traverse)
p.traverse()
lp.print_stats()

Music装饰器类实现在程序执行完毕之后播放音乐,examine_path装饰器类实现检查文件夹路径,如果没有则自动创建的任务。两个装饰器装饰同一个函数,测试多个装饰器的装饰效果和装饰顺序。

class Music: #Music类,在程序结束之后播放音乐
    def __init__(self,func):
        self.func = func

    def __call__(self,*args,**kwargs):
        self.func(*args,**kwargs)
        pygame.init()
        pygame.mixer.init()

        pygame.mixer.music.load('stream.mp3') # 加载歌曲
        pygame.mixer.music.play() # 播放
        time.sleep(5)
        pygame.mixer.music.stop()

#用装饰器检查文件夹路径是否存在
def examine_path(path):
    def decorator(func):
        @wraps(func)

        def wrapper(*args, **kwargs):
            if os.path.exists(path) == True:
                print('the path exists!')
            else:
                os.mkdir(path)
                print('we have created the path for you!')
            return func(*args,**kwargs)
        return wrapper
    return decorator

path = 'E:\coding'
#多个装饰器,装饰顺序就近原则,调用顺序就远原则
@examine_path(path)
@Music
def printf(path):
    print(path)
printf(path)


pygame 2.0.1 (SDL 2.0.14, Python 3.8.8)
Hello from the pygame community. https://www.pygame.org/contribute.html
the path exists!
E:\coding

Line #    Mem usage    Increment  Occurences   Line Contents
============================================================
    13     49.3 MiB     49.3 MiB           1       @profile  #查看内存时间装饰器
    14                                             def generator(self):  #大的列表生成
    15     56.9 MiB      7.6 MiB           1           a = [1] * (10 ** 6)
    16    209.5 MiB    152.6 MiB           1           b = [2] * (2 * 10 ** 7)


100%|███████████████████████████████████████████████████████████████████████████████████| 1000/1000 [00:10<00:00, 93.36it/s] 
Timer unit: 1e-07 s

Total time: 10.7133 s
File: e:\coding\code_design\week9\test.py
Function: traverse at line 18

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    18                                               def traverse(self):
    19         1         56.0     56.0      0.0          a = [1] * (10 ** 3) #大列表生成加遍历,tqdm查看进度
    20      1001    1276175.0   1274.9      1.2          for i in tqdm(a):
    21      1000  105856496.0 105856.5     98.8              time.sleep(0.01)
上一篇下一篇

猜你喜欢

热点阅读