1 回答

TA貢獻1869條經(jīng)驗 獲得超4個贊
你的分析是對的:你把問題想得太多了。
首先,將你正在做的事情分成簡單的步驟
+--------------------------+
+-----------> |Get the data from the page| <-----------------+
| +-------------+------------+ |
| | |
| v |
| |
| +-------------------------------------------+ |
| |Get the data into a nice format (e.g. list)| |
| +------------------+------------------------+ +------+
| | | |
| | +--------+ |
| +--+ | |
| | | +-------------+
| v | |wait for data|
| | +-------------+
| +--------------------------+ |
| |Do we actually have data? | | ^
| +------+-----+-------------+ |flick |no data
| | | | |
+-+------+ | | +-------------------+-----------------+-----+
|do stuff| <----+ +----> |Is this a flick or is there really no data?|
+--------+ yes no +-------------------------------------------+
您會看到輕彈和沒有數(shù)據(jù)最終返回以獲取數(shù)據(jù)。
如果你把上面的步驟放到python代碼中,你會得到這樣的東西:
def get_data(): # just an example
r = requests.get(...)
r.raise_for_status()
return r
def parse_data(text, _old=[]): # list is mutable => preserved across calls
"""convert the text to a list of names. If there are no (new) names, the list is empty"""
...
if new_data not in _old:
_old.append(new_data)
return new_data
else:
return []
def do_stuff(names):
...
像這樣拆分不僅可以讓您的代碼更好地被閱讀和理解,而且還為您提供了一種更改各個部分的方法:如果您想使用本地文件/數(shù)據(jù)進行測試,您只需重寫get_data而不更改任何其他內(nèi)容:
def get_data():
if random.randint(0, 1): # simulate flicks
return empty_sample_data
else:
return random.choice(sample_data_with_names)
按照上面的結(jié)構(gòu)圖,您現(xiàn)在可以通過counter在一個循環(huán)中查看來首先獲取數(shù)據(jù),檢查它并在獲取新數(shù)據(jù)之前執(zhí)行正確的操作:
WAIT_TIME = 10*60
counter = 0
while True:
data = parse_data(get_data())
if data:
counter = 0
do_stuff(data)
print("I got the data and did {stuff}. Now I'll wait and repeat with new names")
time.sleep(WAIT_TIME)
else:
counter += 1
if counter >= 5: # cosmic rays may increase ``counter`` by 2 instead of 1
counter = 0
print("I think there actually isn't any data. I'll wait and then check again")
time.sleep(WAIT_TIME)
如果新數(shù)據(jù)很快出現(xiàn)并且您決定不需要那么多日志記錄,您也可以直接counter取出。
添加回答
舉報