检测类实例变量值中的重复项

问题描述 投票:1回答:2
class Translator(object):
    def __init__(self, tracking_col='tracking_id', coding_col='coding', qualifying_code_col='qualifying_code',
                 translation_col='translation'):
        self._results = []
        self.tracking_col = tracking_col
        self.data_col = coding_col
        self.definition_col = qualifying_code_col
        self.translation_col = translation_col
        self.__validate_parameters(self.__dict__)

    def __validate_parameters(self, variable_values):
        class_values = {}
        for key, value in variable_values.items():
            if type(value) is str:
                class_values.setdefault(value, set()).add(key)
        for key, values in class_values.items():
            # If there is more than one value, there is a duplicate
            if len(values) > 1:
                raise Exception('Duplicate column names exist in parameters. \'{}\' are set to \'{}\'. '
                                'Do not use duplicate column names.'.format(values, key))

此类不能具有任何“col”变量的重复值。如果存在重复值,则类中的逻辑可能不会崩溃,但会产生不可预测的结果。

在实例化时,我的函数__validate_parameters将检测重复值并引发异常。问题是我将所有值转储到字典中,迭代创建另一个字典,最后手动引发一个异常(从我被告知在任何情况下都是错误的事情)。它也相当冗长。

是否有更简洁,更简洁的方法来验证重复项,同时在没有上述复杂性的情况下传播错误?

python class exception reflection exception-handling
2个回答
1
投票

手动引发异常没有错。在一些集合中收集cols将使验证更容易:

class Translator(object):
    def __init__(self, tracking_col=..., coding_col=..., qualifying_code_col=..., 
                 translation_col=...):
        self._results = []
        self.cols = [tracking_col, coding_col, qualifying_code_col, translation_col]
        self.validate_cols(self)

    def validate_cols(self):
        if len(self.cols) > len(set(self.cols)):
            raise ...

    @property
    def tracking_col(self):
        return cols[0]

    # ...

1
投票

您可以使构造函数使用字典而不是单个变量,例如

class Translator(object):
  def __init__(self, cols={}):
    defaults = { "tracking_col"        : "tracking_id", 
                 "coding_col"          : "coding",
                 "qualifying_code_col" : "qualifying_code", 
                 "translation_col"     : "translation" }

    for d in defaults:
      if d not in cols:
        cols[d] = defaults[d]

    self.__validate_parameters(cols)

  def __validate_parameters(self, d):
    import Counter
    c = Counter.Counter(d.values())
    if any(cnt > 1 for cnt in c.values()):
      raise Exception("Duplicate values found: '%s'" % str(c))

(代码未经测试)

© www.soinside.com 2019 - 2024. All rights reserved.