2016-04-06 5 views
0

このpython関数を書きました。プロジェクト仕様は、私がエラーを処理するためにtry/exceptを使用することを許可していません。 doctstringごとに私は成功すればFalseを返します。失敗したらdeclare_error関数を呼び出します。主な機能で扱われる行番号。Python関数でtry/exceptを使用しないエラー処理

sysとosのほかには何もインポートできないので、正規表現を使用するとテーブルから外れます。

ここに私のコードです。 if/elseステートメントに何を使用すべきかに関する提案はありますか?

#=============================================================================== 
def read_subsequent_lines(file_object, line_number, simulation_obj_list): 
#=============================================================================== 
    '''Read and parse the next line of the file, confirm it matches one of the 
     line signatures, such as ('sim_object_type=World', 'size=') 

     Parameters: file_object, the input file object. 
        line_number, the current line number being read in. 
        simulation_obj_list, the list of converted lines. 
     Returns: False for success, True for failure 

     Convert the line in the file to a list of strings, with one string per 
      name=value pair (such as "name=Joe"). Make sure that each line matches 
      up with one of the line "signatures" in the constant definitions. 
      Modify this line_list to only include the value portion of the pair, 
      calling extract_line_using_signature (...) to get the each line list. 
      Append each modified line_list to the simulation_obj_list(). 

     If success: Return False. 
     If failure: Call declare_error (...) and Return True. 
    ''' 
    #List of lists to contain each line of the input file 
    line_list = [] 
    #Read in Lines and append to a list of lines containing strings 
    for line in file_object: 
     # print(line.strip().split(',')) 
     line_list.append(line.strip().split()) 

    #Compare line to signature constants and append to simulation_obj_list 
    for i in line_list: 
     #World 
     if len(i) == NUM_WORLD_TOKENS: 
      if i[0].startswith(LINE_SIGNATURE_TUPLE[0][0]) and i[1].startswith(LINE_SIGNATURE_TUPLE[0][1]): 
       simulation_obj_list.append(extract_line_using_signature(LINE_SIGNATURE_TUPLE[0],i)) 
     #Person 
     if len(i) == NUM_PERSON_TOKENS: 
      if i[0].startswith(LINE_SIGNATURE_TUPLE[1][0]) and i[1].startswith(LINE_SIGNATURE_TUPLE[1][1]) and \ 
       i[2].startswith(LINE_SIGNATURE_TUPLE[1][2]): 
        simulation_obj_list.append(extract_line_using_signature(LINE_SIGNATURE_TUPLE[1],i)) 
     #Robot 
     if len(i) == NUM_ROBOT_TOKENS: 
      if i[0].startswith(LINE_SIGNATURE_TUPLE[2][0]) and i[1].startswith(LINE_SIGNATURE_TUPLE[2][1]) and \ 
       i[2].startswith(LINE_SIGNATURE_TUPLE[2][2]) and i[3].startswith(LINE_SIGNATURE_TUPLE[2][3]): 
       simulation_obj_list.append(extract_line_using_signature(LINE_SIGNATURE_TUPLE[2],i)) 

答えて

0

迅速な醜い修正はelif sおよびelse S(私はNUM_WORLD_TOKENSNUM_PERSON_TOKENSを仮定している、とNUM_ROBOT_TOKENSは、すべての一意の値です)を使用することである。しかし非常に臭いのコードである

#Compare line to signature constants and append to simulation_obj_list 
for i in line_list: 
    #World 
    if len(i) == NUM_WORLD_TOKENS: 
     if i[0].startswith(LINE_SIGNATURE_TUPLE[0][0]) and i[1].startswith(LINE_SIGNATURE_TUPLE[0][1]): 
      simulation_obj_list.append(extract_line_using_signature(LINE_SIGNATURE_TUPLE[0],i)) 
     else: 
      declare_error() 
      return True 
    #Person 
    elif len(i) == NUM_PERSON_TOKENS: 
     if i[0].startswith(LINE_SIGNATURE_TUPLE[1][0]) and i[1].startswith(LINE_SIGNATURE_TUPLE[1][1]) and \ 
      i[2].startswith(LINE_SIGNATURE_TUPLE[1][2]): 
       simulation_obj_list.append(extract_line_using_signature(LINE_SIGNATURE_TUPLE[1],i)) 
     else: 
      declare_error() 
      return True 
    #Robot 
    elif len(i) == NUM_ROBOT_TOKENS: 
     if i[0].startswith(LINE_SIGNATURE_TUPLE[2][0]) and i[1].startswith(LINE_SIGNATURE_TUPLE[2][1]) and \ 
      i[2].startswith(LINE_SIGNATURE_TUPLE[2][2]) and i[3].startswith(LINE_SIGNATURE_TUPLE[2][3]): 
      simulation_obj_list.append(extract_line_using_signature(LINE_SIGNATURE_TUPLE[2],i)) 
     else: 
      declare_error() 
      return True 

    return False 

を。正規表現の使用はどうですか?

for line in lines: 
    if re.match(WORLD_REGEX, line): 
     simulation_obj_list.append(
      extract_line_using_signature(LINE_SIGNATURE_TUPLE[0], line)) 
    elif re.match(PERSON_REGEX, line): 
     # etc 

    else: 
     declare_error() 
     return True 

    return False 
+0

あああまりにも悪いです。しかし、私は最初に投稿したコードを使用することができます。 'main関数で扱われる行番号 'が意味することについても興味があります。forループで' enumerate'を使用すると、フリーの行番号が与えられます。 –

+0

残念ながら、私のインストラクターは、私たちにスターターコードを使用する必要があります。したがって、line_numberはmain関数でインクリメントされ、宣言とエラーの際にread_subsequent_linesでのみ使用されます。教授はまた、Pythonに比較的新しいので、自分で使っていないので、正規表現の使用を除外しました。 – RR84

+0

これは悪いインストラクターです。 私の最初の解決策はまったく役に立ちますか? –

関連する問題