Python-使用两个相互依赖的类和一个for循环



我的问题基本如下:我有两个类:

  • 在循环中执行某些操作的类1(web抓取(
  • 类2,它具有不同的功能,可以自动调整某人在这个循环和将来类似循环中必须做的事情

这个想法是尽可能多地抽象类1。

示例:类2具有一个函数;像人一样停顿";在某些迭代之后

(摘录(

def pause_like_human(self):
''' Pausing long every x-th iteration or else short '''
# every 20th iteration - wait for 5-10 minutes before continuing
if self.index_no % self.index_counter == 0:
timestamp = time.localtime()
return print("Let's wait " + str(self.waiting_time_long) + " seconds! The time is: "
+ str(timestamp.tm_hour) + ":" + str(timestamp.tm_min) + ":"
+ str(timestamp.tm_sec)), time.sleep(self.waiting_time_long)
else:
return print("Let's continue! ... after " + str(self.waiting_time_short) + " seconds..."), time.sleep(self.waiting_time_short)

现在我的问题是,我需要导入";index_no";从我在第二节课上使用的循环…一遍又一遍。

(index_no在此循环中:(

for index_no, city in enumerate(self.df['column'][len(self.dic)-1:], start = len(self.dic)-1):

对我来说,这听起来效率很低——有人有更好的主意吗?

因此,对我来说,答案似乎是我只是对代码进行了更多的抽象。这将使我的";复杂的";循环成一个非常简单的循环,我的两个类变成一个类。第1类转向一个带有链接的较长列表,但实际上已经做好了充分准备。类2变成了一个更通用的类,其中已经继承了前一类1的大部分功能。

如果有人提出一般性的帮助或提示,我不介意把这篇文章打开。。。

相关内容

最新更新