【问题标题】:Two python(3) classes with the same input (extending a python class)具有相同输入的两个 python(3) 类(扩展一个 python 类)
【发布时间】:2018-05-31 14:34:44
【问题描述】:

我一直在尝试实施一项设计更改,但收效甚微,因为我似乎无法在任何地方找到我的问题。 目前我有一个 python 类,它创建一个数据库连接、存储索引名称(表)和其他属性(特别是它的一个 Elasticsearch 数据库连接,但这对于这个问题应该无关紧要)。

class Create:
# Functions to manipulate Index Objects

def __init__(self, index, type, host, shards=3, replicas=1):
# Create Index Object (OcrBook or OcrPage)
    self.index = index
    self.type = type
    self.shards = shards
    self.replicas = replicas
    self.es_connection = Elasticsearch([{'host': host, 'port': 9200}])

与该类相关的是操作索引对象的函数,例如在数据库(集群)上创建索引(表)或以某种方式修改该表。

def create_index(self):
# Creates/Executes Index
    try:
        self.es_connection.indices.create(
            index=self.index,
            body={
                'settings' : {
                    'number_of_shards' : self.shards,
                    'number_of_replicas': self.replicas,
                }
            })
    except Exception:
        CreateLog.write_log(Exception, 'Create Index Exception')

这些在同一个类中对我来说很有意义,因为与表/数据库的连接以及创建或修改该表/数据库是相互连接的。 我还有一组搜索该特定表的其他函数。我认为这些应该在一个单独的类中,而不是创建或修改表/数据库,它们只是搜索表/数据库,理想情况下可以采用由创建类初始化的任何表/数据库。目前我尝试通过执行以下操作来分解它们:

class Search(Create):

    def find_book(self, bookkey):
        """  Finds a Book """
        try:
            results = self.es_connection.search(self.index, self.type, body={
                "query": {
                    "match": {
                        "BookKey": bookkey
                    }
                }
            })
            return results['hits']['hits']

        except Exception:
            CreateLog.write_log(Exception, 'Could Not Find Book')

这适用于 Windows,但不能移植到“linux”,因为当我尝试使用搜索功能时,“类尚未初始化”。我知道这里存在设计问题,我可以将两个类合二为一来解决问题。但我想把它们分开。有没有更好的方法来“继承”(在这种情况下,我认为这不是正确的词)搜索类在“创建”类中创建的对象,有没有人有更好的方法来逻辑分离这些对象,或者是有没有更好的方法来扩展带有搜索功能的创建类?所有输入都是有帮助的!谢谢。

【问题讨论】:

    标签: python-3.x class elasticsearch inheritance


    【解决方案1】:

    您似乎在 OOP 路径上,但为什么 Search 必须是一个类?你有一个独立功能的完美任务find_book(index_object, bookkey)。它在内部不存储任何东西,我不明白为什么这必须是一个类,而不是一个函数。

    类命名也可以暗示您的设计决策(或问题)。类名通常是名词,函数名往往是动词。 Create 对我来说不是一个完美的班级名称。

    在您的设置中,我将使用 IndexObjects 类(即 Create 重命名)和函数 find_book(index_object, bookkey)。一旦这个设计启动并运行,您就可以切换到更多的 OOP。

    我想到的另一个职责划分如下。在这里你注入,而不是继承,这让你的零件更加独立。

    class IndexObject:
    
    #    ...
    
        def query(self, query_dict):
            return self.es_connection.search(self.index, self.type, body=query_dict)       
    
    
    class BookSearcher():    
        def __init__(self, index_object):
            self.index_object = index_object
    
        def find(self, book_key):
            """  Finds a Book """
            query_dict = {"query": {
                        "match": {
                            "BookKey": book_key
                        }
                      }
                    }        
            try:
                results = self.index_object.query(query_dict)
                return results['hits']['hits']        
    
            # FIXME: looks lile bare exception, not great
            except Exception:
                CreateLog.write_log(Exception, 'Could Not Find Book')
    

    【讨论】:

    • 我喜欢注入而不是继承的想法。我会使用独立函数方法,但鉴于我至少有几十个不同的搜索函数,我想将它们归为一类。现在接受这个答案,因为我认为它应该可以解决问题。在我进行更改后将再次发表评论。谢谢!
    • 按类对相似的函数进行分组是有意义的,尤其是当它们共享一些参数时!随时将我的提交标记为答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 2015-01-27
    • 2012-01-31
    相关资源
    最近更新 更多