Python 自定义iterator生成器

#计数版
class countdown(object):
	def __init__(self,start):
		self.start = start
	def __iter__(self):
		return countdown_iter(self.start)

class countdown_iter(object):
	def __init__(self, count):
		self.count = count
	def __next__(self):
		if self.count <= 0:
			raise StopIteration
		r = self.count
		self.count -= 1
		return r

  

#pandas读取多个文件
import pandas as pd

class read_csv_paths(object):
	def __init__(self,paths):
		self.paths = paths
	def __iter__(self):
		return read_csv_iter(self.paths)

class read_csv_iter(object):
	def __init__(self, paths):
		self.paths = paths
	def __next__(self):
		if len(self.paths)<=0:
			raise StopIteration
		path=self.paths.pop()
		#print(path)
		df=pd.read_csv(path,low_memory=False,dtype='object')
		return df

 

#generator版
def countdown(n):
	print("Counting down from", n)
	while n > 0:
		yield n
		n -= 1

  

  

posted on 2019-01-02 11:32  hailuo  阅读(423)  评论(0编辑  收藏  举报