-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEndput.py
More file actions
27 lines (24 loc) · 761 Bytes
/
Endput.py
File metadata and controls
27 lines (24 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Endput():
def __init__(self, number_lines, tofile=''):
self.number_lines = number_lines
self.n = 1
if tofile:
self.tofile = open(tofile, 'a')
else:
self.tofile = tofile
def generator(self, line):
if self.n == self.number_lines:
self.n +=1
yield line
elif self.n < self.number_lines:
self.n +=1
yield line+'\n'
else:
raise StopIteration
def __call__(self, line):
if not self.tofile:
return self.generator(line).__next__()
else:
self.tofile.write(self.generator(line).__next__())
if self.n == self.number_lines+1:
self.tofile.close()