2016-12-05 6 views
0

私は、ExcelFileという名前のクラスを持っています。彼の仕事は、Excelファイルを管理することです(スタックの読み込み、データの抽出、および異なるものの管理)。例外を処理する

エラー/例外を管理するシステムを実装したいと考えています。例えば

、方法負荷としてExcelFile()、あなたが見ることができるように "セットアップ"

def load(self): 
     """ 
      Setup for excel file 
      Load workbook, worksheet and others characteristics (data lines, header...) 
      :return: Setup successfully or not 
      :rtype: bool 

      Current usage 
      :Example: 

      > excefile = ExcelFile('test.xls') 
      > excefile.load() 
       True 
      > excefile.nb_rows() 
       4 
     """ 
     self.workbook = xlrd.open_workbook(self.url) 
     self.sheet = self.workbook.sheet_by_index(0) 
     self.header_row_index = self.get_header_row_index() 

     if self.header_row_index == None: # If file doesn't have header (or not valid) 
      return False 

     self.header_fields = self.sheet.row_values(self.header_row_index) 
     self.header_fields_col_ids = self.get_col_ids(self.header_fields) # Mapping between header fields and col ids 
     self.nb_rows = self.count_rows() 
     self.row_start_data = self.header_row_index + self.HEADER_ROWS 
     return True 

のように、私は2つの貴様エラーが発生することができます。

  1. ファイルはExcelファイルではありません(xlrd.XLRDErrorを上げる)
  2. ファイルには無効なヘッダーが含まれているため(Falseを返します)

ExcelFileエラーの管理システムを実装したいのは、このクラスがスタックで多く使われているからです。

これは、処理のための私の最初のアイデアです:

は標準の例外

class ExcelFileException(Exception): 

    def __init__(self, message, type=None): 
     self.message = message 
     self.type = type 

    def __str__(self): 
     return "{} : {} ({})".format(self.__class__.__name__, self.message, self.type) 

書き換え負荷方法

def load(self): 
    """ 
     Setup for excel file 
     Load workbook, worksheet and others characteristics (data lines, header...) 
     :return: Setup successfully or not 
     :rtype: bool 

     Current usage 
     :Example: 

     > excefile = ExcelFile('test.xls') 
     > excefile.load() 
      True 
     > excefile.nb_rows() 
      4 
    """ 
    try: 
     self.workbook = xlrd.open_workbook(self.url) 
    except xlrd.XLRDError as e: 
     raise ExcelFileException("Unsupported file type", e.__class__.__name__) 

    self.sheet = self.workbook.sheet_by_index(0) 
    self.header_row_index = self.get_header_row_index() 

    if self.header_row_index == None: # If file doesn't have header (or not valid) 
     raise ExcelFileException("Invalid or empty header") 

    self.header_fields = self.sheet.row_values(self.header_row_index) 
    self.header_fields_col_ids = self.get_col_ids(self.header_fields) # Mapping between header fields and col ids 
    self.nb_rows = self.count_rows() 
    self.row_start_data = self.header_row_index + self.HEADER_ROWS 
    return True 

を実装して、呼び出し元のメソッドでは、この例では、大きな問題私はフランス語で、お客様の成功と他のエラーのエラーと "レポート"というdictを管理する必要があります。

... 
def foo(): 
    ... 
    file = ExcelFile(location) 

    try: 
     file.load() 
    except ExcelFileException as e: 
     log.warn(e.__str__()) 
     if e.type == 'XLRDError' 
      self.report['errors'] = 'Long description of the error, in french (error is about invalid file type)' 
     else: 
      self.report['errors'] = 'Long description of the error, in french (error is about invalid header)' 
... 

あなたはどう思いますか?あなたはより良い方法を持っていますか? はありがとう

あなたがにエラーを記録するためにあなたの例外を変更することができ

答えて

1

あなたdict

class ExcelFileException(Exception): 

    def __init__(self, message, report, type=None): 
     report['errors'].append(message) 
     self.message = message 
     self.type = type 

    def __str__(self): 
     return "{} : {} ({})".format(self.__class__.__name__, self.message, self.type) 

あなたはraiseexceptionますとき:

raise ExcelFileException("Invalid or empty header", report) 

エラーがself.dictionnary['errors']

で存在するであろう
+0

ありがとうございましたが、私は条件を持っている:1)レポートのメッセージ)が、お客様の成功とチームの他のメンバーのために(フランス語、非常に明示的でなければなりません – Alexandre

+0

'ExcelFileException("エラーメッセージ "、"フランス語の説明 ")' – Alexandre

+0

エラーメッセージがどこかに保存されている場合は、frenchの説明で 'dict'を使うことができますエラーメッセージ、 'report ['errors']のようなものです。append(your_dict_with_french_descriptions [message])' – user312016