1 回答

TA贡献1869条经验 获得超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 +-------------------------------------------+
您会看到轻弹和没有数据最终返回以获取数据。
如果你把上面的步骤放到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):
...
像这样拆分不仅可以让您的代码更好地被阅读和理解,而且还为您提供了一种更改各个部分的方法:如果您想使用本地文件/数据进行测试,您只需重写get_data而不更改任何其他内容:
def get_data():
if random.randint(0, 1): # simulate flicks
return empty_sample_data
else:
return random.choice(sample_data_with_names)
按照上面的结构图,您现在可以通过counter在一个循环中查看来首先获取数据,检查它并在获取新数据之前执行正确的操作:
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)
如果新数据很快出现并且您决定不需要那么多日志记录,您也可以直接counter取出。
添加回答
举报