美文网首页简友广场想法散文
python实现抽象类和适配类

python实现抽象类和适配类

作者: Cache_wood | 来源:发表于2021-12-08 09:06 被阅读0次

第一部分构造抽象类Plotter,实现抽象方法plot()
第二部分继承元类Plotter实现子类,实现元类的方法plot()
第三部分分别实现子类的适配类Adapter
import os
import cv2
import abc
import glob
import jieba
import imageio
import librosa
import librosa.display
from PIL import Image
from celluloid import Camera
import matplotlib.pyplot as plt
from wordcloud import WordCloud

class Point:
    def __init__(self,x,y):
        lis = []
        for i in range(len((x))):
            lis.append((x[i],y[i]))
        self.lis = lis

class Plotter(metaclass=abc.ABCMeta):#class Plotter(abc.ABC)  #构造元类Plotter
    @abc.abstractmethod
    def plot(self,*arg,**kwargs):
        pass

class PointPlotter(Plotter): #点型数据绘制
    def __init__(self,data):
        self._data = data

    def plot(self):
        for x,y in self._data:
            plt.scatter(x,y)
        plt.show()

class ArrayPlotter(Plotter):  #数组型数据绘制
    def __init__(self,data):
        self._data = data

    def plot(self):
        plt.scatter(self._data[0],self._data[1])
        plt.show()
            
class TextPlotter(Plotter):  #文本型数据绘制
    def __init__(self,data):
        self._data = data

    def plot(self):
        txt = jieba.lcut(self._data)
        string = ''.join(txt)
        w = WordCloud(max_font_size=100,
                        background_color='white',
                        scale=3,font_path='C:/Windows/SIMLI.TTF')
        w.generate(string)    #生成词云
        w.to_file('axwordcloud.png')

class ImagePlotter(Plotter):  #图像型数据绘制
    def __init__(self,data):
        self._data = data

    def plot(self,row=3,column=4):
        for num in range(0,len(self._data),row*column):
            print(num)
            for each in range(1,row*column+1): #控制每张子图展示图片数量
                if num+each-1<len(self._data):
                    img = self._data[num+each-1]
                    img = Image.open(img)
                    img = img.resize((640,480))
                    plt.subplot(row,column,each)
                    plt.imshow(img)
            plt.show()

class GifPlotter(Plotter):  #Gif图片的输出绘制
    def __init__(self,data):
        self._data = data

    def plot(self):
        fig = plt.figure()
        camera = Camera(fig)
        for i in range(10):
            img = self._data[i]
            img = Image.open(img)
            img = img.resize((640,480))
            plt.imshow(img)
            camera.snap()
        animation = camera.animate()
        animation.save('celluloid_minimal.gif')

class AudioPlotter(Plotter):   #音频型数据绘制
    def __init__(self,data):
        self._data = data

    def plot(self):
        x , sr = librosa.load(self._data)
        #print(type(x), type(sr))  
        #plt.figure(figsize=(10, 5))
        librosa.display.waveplot(x, sr=sr)
        plt.savefig('audio.png')
        plt.show()  

class VideoPlotter(Plotter):    #视频型数据绘制
    def __init__(self,data):
        self._data = data

    def plot(self):
        video_cap = cv2.VideoCapture(self._data)
        frame_count = 0
        all_frames = []
        while True:
            ret, frame = video_cap.read()
            if ret is False:
                break
            frame = frame[..., ::-1]   # opencv读取BGR,转成RGB
            all_frames.append(frame)
            cv2.imshow('frame', frame)
            cv2.waitKey(1)
            frame_count += 1
        video_cap.release()
        cv2.destroyAllWindows()
        print('===>', len(all_frames))

        imageio.mimsave('./result.gif', all_frames, 'GIF', duration=0.001)  

class PointPlotterAdapter(PointPlotter):   #点型数据适配类
    def __init__(self,pointplotter):
        self.pointplotter=pointplotter

    def plot(self):
        self.pointplotter.plot()

class ArrayPlotterAdapter(ArrayPlotter):   #数组型数据适配类
    def __init__(self,arrayplotter):
        self.arrayplotter=arrayplotter

    def plot(self):
        self.arrayplotter.plot()

class TextPlotterAdapter(TextPlotter):  #文本型数据适配类
    def __init__(self,textplotter):
        self.textplotter=textplotter

    def plot(self):
        self.textplotter.plot()

class ImagePlotterAdapter(ImagePlotter):    #图片型数据适配类
    def __init__(self,imageplotter):
        self.imageplotter=imageplotter

    def plot(self):
        self.imageplotter.plot()

class GifPlotterAdapter(GifPlotter):    #GIF图片型适配类
    def __init__(self,gifplotter):
        self.gifplotter=gifplotter

    def plot(self):
        self.gifplotter.plot()

class AudioPlotterAdapter(AudioPlotter):    #音频型数据适配类
    def __init__(self,audioplotter):
        self.audioplotter=audioplotter

    def plot(self):
        self.audioplotter.plot()

class VideoPlotterAdapter(VideoPlotter):    #视频型数据适配类
    def __init__(self,videoplotter):
        self.videoplotter=videoplotter

    def plot(self):
        self.videoplotter.plot()

def main():  #主函数
    x = [1,2,3,4,5]
    y = [2,4,6,8,10]
    p = Point(x,y)
    print(p.lis)
    objects = [PointPlotter(p.lis)]

    ar = ArrayPlotter([x,y])

    data = '我是一个粉刷匠,粉刷本领强。'
    te = TextPlotter(data)

    path = 'H:\图片\\'
    data = glob.glob(os.path.join(path + '*.jpg'))
    im = ImagePlotter(data)

    gi = GifPlotter(data)

    audio_path = 'summer.wav'
    au = AudioPlotter(audio_path)

    video_path = 'H:\\Captures\\我创建的视频\\feifei.mp4'
    vi = VideoPlotter(video_path)

    objects.append(ArrayPlotterAdapter(ar))
    objects.append(TextPlotterAdapter(te))
    objects.append(ImagePlotterAdapter(im))
    objects.append(GifPlotterAdapter(gi))
    objects.append(AudioPlotterAdapter(au))
    objects.append(VideoPlotterAdapter(vi))

    for obj in objects:
        obj.plot()

main()
  • 点型数据绘制


  • 数组型变量绘制


  • 文本型数据绘制


  • 图片型数据绘制


  • GIF动图数据绘制


  • 音频型数据绘制


  • 视频型数据绘制

相关文章

  • 21.适配器模式-接口适配器模式

    UML 代码实现 需要被适配的类【电源】(普通类)可以扩展成抽象类 抽象适配类【充电器】(抽象类) 适配类1【新充...

  • python实现抽象类和适配类

    第一部分构造抽象类 ,实现抽象方法 。 第二部分继承元类 实现子类,实现元类的方法 。 第三部分分别实现子类的适配...

  • 16.2、python初识面向对象(2)

    抽象类 什么是抽象类 与java一样,python也有抽象类的概念但是同样需要借助模块实现,抽象类是一个特殊的...

  • 深入理解类和对象

    1.1 抽象基类(abc模块) python的抽象类的写法,继承抽象类的类必须要实现抽象方法,否则会报错 1.2 ...

  • Kotlin抽象类与接口 (1)抽象类

    抽象类概念抽象类声明和实现 一、抽象类概念 抽象类:被 abstract 关键字修饰的类(class) 被称为抽象...

  • Android面试题

    接口和抽象类的区别: 抽象类只能单继承,接口能多实现(一个类只能继承一个抽象类,但是能实现多个接口) 抽象类是一个...

  • python基础(abc类)

    abc ABC是Abstract Base Class的缩写。Python本身不提供抽象类和接口机制,要想实现抽象...

  • js.pattern -h 模板方法模式

    基于复用技术、抽象类-->继承抽象类 创建抽象类 实现抽象类

  • 装饰者模式

    装饰角色抽象类或接口 装饰角色抽象类或接口实现类 装饰类抽象类 装饰类抽象类具体实现类 场景使用

  • 简述接口和抽象类

    接口和抽象类 定义 抽象类:有抽象方法的类就是抽象类 抽象类中可以有一般的变量和一般的方法 子类继承抽象类必须实现...

网友评论

    本文标题:python实现抽象类和适配类

    本文链接:https://www.haomeiwen.com/subject/egxoxrtx.html