Giter Club home page Giter Club logo

quanttide-data-python's Introduction

quanttide-data-python

量潮数据工程Python工具包

安装

通过pip安装

pip install quanttide-data

通过poetry安装

poetry add quanttide-data

测试

poetry install

quanttide-data-python's People

Contributors

guo-zhang avatar

Watchers

 avatar

quanttide-data-python's Issues

抽象类的定义原则

在Python语言Base类中有两种思路:

  1. 接口类,比如目前的BaseProcessor和Processor。和其他语言的interface类差不多。
  2. 元类,比如ProcessorMeta。更Pythonic,不过其他语言不一定支持。

如果只要类型,就使用1;如果需要自定义类的创建行为,就需要2。也可以三个类同时定义

By ChatGPT:

在理论上,ProcessorMeta 元类可以用于创建 BaseProcessor 类和 Processor 类。可以通过在 ProcessorMeta 的 new 方法中进行逻辑判断,根据不同的情况来创建不同的类。但是,在实际应用中,这种方式可能会导致代码的可读性和可维护性变差,因此不是推荐的做法。

更好的做法是将 ProcessorMeta 元类用于创建 BaseProcessor 类,然后将 BaseProcessor 作为 Processor 类的基类。这样可以使代码更加清晰和易于理解。

下面是一个示例,演示了如何使用 ProcessorMeta 元类创建 BaseProcessor 类,然后将 BaseProcessor 作为 Processor 类的基类:

class ProcessorMeta(type):
    def __new__(cls, name, bases, attrs):
        # 在创建类的过程中,自动添加一个 process 方法
        if 'process' not in attrs:
            raise NotImplementedError('process method not implemented')
        # 返回新创建的类
        return super().__new__(cls, name, bases, attrs)

class BaseProcessor(metaclass=ProcessorMeta):
    def __init__(self):
        self.name = 'BaseProcessor'

    def process(self, data):
        raise NotImplementedError('process method not implemented')

class Processor(BaseProcessor):
    def __init__(self):
        super().__init__()
        self.name = 'Processor'

    def process(self, data):
        print(f'{self.name} processing {data}')

processor = Processor()
processor.process('some data')

在这个示例中,ProcessorMeta 元类用于创建 BaseProcessor 类。BaseProcessor 类中包含了一个名为 process 的抽象方法,该方法在 ProcessorMeta 元类的 new 方法中被自动添加。Processor 类继承自 BaseProcessor,并重写了 process 方法,实现了具体的处理逻辑。

需要注意的是,ProcessorMeta 元类、BaseProcessor 类和 Processor 类这三个类只是一个示例,实际中我们可以根据需求定义不同的元类、基类和继承类,来实现不同的功能。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.