[英]How would i go about handling multiple file exceptions?
基本上,我需要創建一個函數,該函數允許我從文件中加載計划行程的基本細節。 我們很簡短:
參數:包含文件路徑的字符串
返回值:3個字符串的字符串,其中包含從文件中讀取的旅程的開始位置,結束位置和到達時間;如果失敗,則返回(無,無,無)。
有多項測試,所有測試均使用幻數作為輸入。 測試如下:
這是不良數據測試的代碼,應為您提供以下測試之一的示例:
PATH = os.path.expanduser('~/test_prev_plan_spec.txt')
def test_missing_file_is_handled(self):
if os.path.exists(self.PATH):
os.unlink(self.PATH)
plan = utils.load_prev_plan_spec(self.PATH)
self.assertEqual(3, len(plan))
self.assertEqual(plan, (None, None, None))
def test_spec_loads_ok(self):
from_ = 'Bournemouth'
to = 'Southampton'
arrive_at = '2019/04/20 13:30'
with open(self.PATH, 'wt') as f:
f.write('{}\n{}\n{}\n'.format(from_, to, arrive_at))
plan = utils.load_prev_plan_spec(self.PATH)
self.assertEqual(3, len(plan))
self.assertEqual(from_, plan[0])
self.assertEqual(to, plan[1])
self.assertEqual(arrive_at, plan[2])
def test_short_spec_is_ignored(self):
from_ = 'Bournemouth'
to = 'Southampton'
with open(self.PATH, 'wt') as f:
f.write('{}\n{}\n'.format(from_, to))
plan = utils.load_prev_plan_spec(self.PATH)
self.assertEqual(3, len(plan))
self.assertEqual(plan, (None, None, None))
with open(self.PATH, 'wt') as f:
f.write('{}\n'.format(from_))
plan = utils.load_prev_plan_spec(self.PATH)
self.assertEqual(3, len(plan))
self.assertEqual(plan, (None, None, None))
def test_empty_line_is_handled(self):
from_ = 'Bournemouth'
to = ''
arrive_at = '2019/04/20 13:30'
with open(self.PATH, 'wt') as f:
f.write('{}\n{}\n{}\n'.format(from_, to, arrive_at))
plan = utils.load_prev_plan_spec(self.PATH)
self.assertEqual(3, len(plan))
self.assertEqual(plan, (None, None, None))
def test_bad_data_line_is_handled(self):
from_ = 'Bournemouth'
to = 'Southampton'
arrive_at = '2019/04/20 13:60'
with open(self.PATH, 'wt') as f:
f.write('{}\n{}\n{}\n'.format(from_, to, arrive_at))
plan = utils.load_prev_plan_spec(self.PATH)
self.assertEqual(3, len(plan))
self.assertEqual(plan, (None, None, None))
到目前為止,這是我所擁有的,我正在尋求與此相關的幫助,任何解釋都很棒!
我的代碼atm:
def load_prev_plan_spec(PATH):
'''
Function: utils.load_prev_plan_specLoads the basic details of a planned journey from a file.
Parameters: A string containing a file path
Returns: A 3-tuple of strings containing start location, end location and arrival time of a journey
read from the file, or (None, None, None) if unsuccessful.
'''
try:
if os.path.exists(PATH):
infomation = []
f = open(PATH, 'r', encoding='cp1252')
for line in f:
infomation.append([line.strip()])
if not line.strip():
infomation = (None, None, None)
tuple(infomation)
f.close()
return infomation
else:
pass
except IOError as err2:
print(err2)
raise IOError
else:
return infomation
第一次失敗的測試是因為,如果第一行或第二行為空,則將具有三個None
值的元組綁定到infomation
,然后進行下一次迭代,然后嘗試append()
某些內容append()
到該tuple中-但是元組沒有append()
方法。 如果遇到空行,則需要停止處理這些行並返回錯誤值。
第二個失敗的測試是因為您嘗試在函數的最后一行中返回infomation
,但是如果文件不存在,則沒有執行路徑可以為此名稱分配值。
第三次失敗無法識別13:60
不是有效的時間值。
第四個失敗返回兩個值,而不是三個值,因為您不檢查文件中是否實際上存在三行而不是更少。
第六個也是最后一個失敗是因為您將每個項目包裝在列表中。 為什么?
通過所有測試用例的函數可能如下所示:
from datetime import datetime as DateTime
def load_prev_plan_spec(path):
try:
with open(path, 'r', encoding='cp1252') as file:
lines = [line.strip() for line in file]
if len(lines) == 3 and all(lines):
try:
#
# Just for the `ValueError` to test if string is a valid
# timestamp.
#
DateTime.strptime(lines[-1], '%Y/%m/%d %H:%M')
except ValueError:
pass # Intentionally ignored.
else:
return tuple(lines)
except (OSError, UnicodeDecodeError):
pass # Intentionally ignored.
return (None, None, None)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.