use context managers in RecordCollection.__init__() and RecordCollection.write()
This commit is contained in:
parent
f66776c9bb
commit
a0ebba802e
|
@ -28,13 +28,10 @@ class Record:
|
||||||
|
|
||||||
class RecordCollection():
|
class RecordCollection():
|
||||||
def __init__(self, file):
|
def __init__(self, file):
|
||||||
fd = filewrapper(file, "r")
|
with filewrapper(file, "r") as fd:
|
||||||
|
|
||||||
self._fromFile(fd)
|
self._fromFile(fd)
|
||||||
self._parent()
|
self._parent()
|
||||||
|
|
||||||
fd.close()
|
|
||||||
|
|
||||||
def _fromFile(self, fd):
|
def _fromFile(self, fd):
|
||||||
lines = [i.rstrip() for i in fd.readlines()]
|
lines = [i.rstrip() for i in fd.readlines()]
|
||||||
self.objects = {}
|
self.objects = {}
|
||||||
|
@ -85,7 +82,7 @@ class RecordCollection():
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def write(self, file):
|
def write(self, file):
|
||||||
fd = filewrapper(file, "w")
|
with filewrapper(file, "w") as fd:
|
||||||
|
|
||||||
lines = []
|
lines = []
|
||||||
for i in self.objects:
|
for i in self.objects:
|
||||||
|
@ -107,4 +104,3 @@ class RecordCollection():
|
||||||
lines[i] += "\n"
|
lines[i] += "\n"
|
||||||
|
|
||||||
fd.writelines(lines)
|
fd.writelines(lines)
|
||||||
fd.close()
|
|
||||||
|
|
Loading…
Reference in New Issue