__iter__
and __next__
Instead of re-opening the file each time and seeking to the previous position, you could write __iter__
as a generator function by using yield
instead of needing to define __next__
. Something like:
def __iter__(self):
with open(file) as f:
yield from f
Error handling
In case of error, you're returning a string 'Cant read next line'
.
What if you have a file that contains text 'Cant read next line'
? How do you differentiate between these?
Well... you can't.
Instead of returning a string, throw an exception on error - that's what exceptions are meant for.
Also, this error message of yours isn't really correct. You most likely get IOError when opening a file... before even attempting to read first line from it.
It seems like your error handling code doesn't really do anything very useful with the IOError. So why not instead just let it bubble upwards and drop the try-catch block completely.
Duplication
You can define your own open method, so you don't have to repeat passing the file_path to it:
def open(self, mode='r'):
open(self.file_path, mode)
You could also investigate writing you own context manager for opening the file with your custom error handling.
__iter__
and __next__
Instead of re-opening the file each time and seeking to the previous position, you could write __iter__
as a generator function by using yield
instead of needing to define __next__
. Something like:
def __iter__(self):
with open(file) as f:
yield from f
Error handling
In case of error, you're returning a string 'Cant read next line'
.
What if you have a file that contains text 'Cant read next line'
? How do you differentiate between these?
Well... you can't.
Instead of returning a string, throw an exception on error - that's what exceptions are meant for.
Also, this error message of yours isn't really correct. You most likely get IOError when opening a file... before even attempting to read first line from it.
It seems like your error handling code doesn't really do anything very useful with the IOError. So why not instead just let it bubble upwards and drop the try-catch block completely.
Duplication
You can define your own open method, so you don't have to repeat passing the file_path to it:
def open(self, mode='r'):
open(self.file_path, mode)
You could also investigate writing you own context manager for opening the file with your custom error handling.
__iter__
and __next__
Instead of re-opening the file each time and seeking to the previous position, you could write __iter__
as a generator function by using yield
instead of needing to define __next__
. Something like:
def __iter__(self):
with open(file) as f:
yield from f
Error handling
In case of error, you're returning a string 'Cant read next line'
.
What if you have a file that contains text 'Cant read next line'
? How do you differentiate between these?
Well... you can't.
Instead of returning a string, throw an exception on error - that's what exceptions are meant for.
Also, this error message of yours isn't really correct. You most likely get IOError when opening a file... before even attempting to read first line from it.
It seems like your error handling code doesn't really do anything very useful with the IOError. So why not instead just let it bubble upwards and drop the try-catch block completely.
Duplication
You can define your own open method, so you don't have to repeat passing the file_path to it:
def open(self, mode='r'):
open(self.file_path, mode)
You could also investigate writing you own context manager for opening the file with your custom error handling.
__iter__
and __next__
Instead of re-opening the file each time and seeking to the previous position, you could write __iter__
as a generator function by using yield
instead of needing to define __next__
. Something like:
def __iter__(self):
with open(file) as f:
yield from f.readline()
Error handling
In case of error, you're returning a string 'Cant read next line'
.
What if you have a file that contains text 'Cant read next line'
? How do you differentiate between these?
Well... you can't.
Instead of returning a string, throw an exception on error - that's what exceptions are meant for.
Also, this error message of yours isn't really correct. You most likely get IOError when opening a file... before even attempting to read first line from it.
It seems like your error handling code doesn't really do anything very useful with the IOError. So why not instead just let it bubble upwards and drop the try-catch block completely.
Duplication
You can define your own open method, so you don't have to repeat passing the file_path to it:
def open(self, mode='r'):
open(self.file_path, mode)
You could also investigate writing you own context manager for opening the file with your custom error handling.
__iter__
and __next__
Instead of re-opening the file each time and seeking to the previous position, you could write __iter__
as a generator function by using yield
instead of needing to define __next__
. Something like:
def __iter__(self):
with open(file) as f:
yield f.readline()
Error handling
In case of error, you're returning a string 'Cant read next line'
.
What if you have a file that contains text 'Cant read next line'
? How do you differentiate between these?
Well... you can't.
Instead of returning a string, throw an exception on error - that's what exceptions are meant for.
Also, this error message of yours isn't really correct. You most likely get IOError when opening a file... before even attempting to read first line from it.
It seems like your error handling code doesn't really do anything very useful with the IOError. So why not instead just let it bubble upwards and drop the try-catch block completely.
Duplication
You can define your own open method, so you don't have to repeat passing the file_path to it:
def open(self, mode='r'):
open(self.file_path, mode)
You could also investigate writing you own context manager for opening the file with your custom error handling.
__iter__
and __next__
Instead of re-opening the file each time and seeking to the previous position, you could write __iter__
as a generator function by using yield
instead of needing to define __next__
. Something like:
def __iter__(self):
with open(file) as f:
yield from f
Error handling
In case of error, you're returning a string 'Cant read next line'
.
What if you have a file that contains text 'Cant read next line'
? How do you differentiate between these?
Well... you can't.
Instead of returning a string, throw an exception on error - that's what exceptions are meant for.
Also, this error message of yours isn't really correct. You most likely get IOError when opening a file... before even attempting to read first line from it.
It seems like your error handling code doesn't really do anything very useful with the IOError. So why not instead just let it bubble upwards and drop the try-catch block completely.
Duplication
You can define your own open method, so you don't have to repeat passing the file_path to it:
def open(self, mode='r'):
open(self.file_path, mode)
You could also investigate writing you own context manager for opening the file with your custom error handling.
__iter__
and __next__
Instead of re-opening the file each time and seeking to the previous position, you could write __iter__
as a generator function by using yield
instead of needing to define __next__
. Something like:
def __iter__(self):
with open(file) as f:
yield f.readline()
Error handling
In case of error, you're returning a string 'Cant read next line'
.
What if you have a file that contains text 'Cant read next line'
? How do you differentiate between these?
Well... you can't.
Instead of returning a string, throw an exception on error - that's what exceptions are meant for.
Also, this error message of yours isn't really correct. You most likely get IOError when opening a file... before even attempting to read first line from it.
It seems like your error handling code doesn't really do anything very useful with the IOError. So why not instead just let it bubble upwards and drop the try-catch block completely.
Duplication
You can define your own open method, so you don't have to repeat passing the file_path to it:
def open(self, mode='r'):
open(self.file_path, mode)
You could also investigate writing you own context manager for opening the file opening with your custom error handling.
__iter__
and __next__
Instead of re-opening the file each time and seeking to the previous position, you could write __iter__
as a generator function by using yield
instead of needing to define __next__
. Something like:
def __iter__(self):
with open(file) as f:
yield f.readline()
Error handling
In case of error, you're returning a string 'Cant read next line'
.
What if you have a file that contains text 'Cant read next line'
? How do you differentiate between these?
Well... you can't.
Instead of returning a string, throw an exception on error - that's what exceptions are meant for.
Also, this error message of yours isn't really correct. You most likely get IOError when opening a file... before even attempting to read first line from it.
It seems like your error handling code doesn't really do anything very useful with the IOError. So why not instead just let it bubble upwards and drop the try-catch block completely.
Duplication
You can define your own open method, so you don't have to repeat passing the file_path to it:
def open(self, mode='r'):
open(self.file_path, mode)
You could also investigate writing you own context manager the file opening with your custom error handling.
__iter__
and __next__
Instead of re-opening the file each time and seeking to the previous position, you could write __iter__
as a generator function by using yield
instead of needing to define __next__
. Something like:
def __iter__(self):
with open(file) as f:
yield f.readline()
Error handling
In case of error, you're returning a string 'Cant read next line'
.
What if you have a file that contains text 'Cant read next line'
? How do you differentiate between these?
Well... you can't.
Instead of returning a string, throw an exception on error - that's what exceptions are meant for.
Also, this error message of yours isn't really correct. You most likely get IOError when opening a file... before even attempting to read first line from it.
It seems like your error handling code doesn't really do anything very useful with the IOError. So why not instead just let it bubble upwards and drop the try-catch block completely.
Duplication
You can define your own open method, so you don't have to repeat passing the file_path to it:
def open(self, mode='r'):
open(self.file_path, mode)
You could also investigate writing you own context manager for opening the file with your custom error handling.