Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

__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.

deleted 6 characters in body
Source Link
Rene Saarsoo
  • 2.1k
  • 11
  • 14

__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.

added 4 characters in body
Source Link
Rene Saarsoo
  • 2.1k
  • 11
  • 14

__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.

added 4 characters in body
Source Link
Daniel
  • 4.6k
  • 2
  • 18
  • 40
Loading
added 1194 characters in body
Source Link
Rene Saarsoo
  • 2.1k
  • 11
  • 14
Loading
Source Link
Rene Saarsoo
  • 2.1k
  • 11
  • 14
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /