4 ms·
What do you see as mistakes? I see some weirdness, but the spec is just not complete - there was no requirement for rewinding, multiple users, etc. in the reque
by viraptor 2y ago
What do you see as mistakes? I see some weirdness, but the spec is just not complete - there was no requirement for rewinding, multiple users, etc. in the request so it's not implemented.
The only thing I'd call an actual mistake is using an empty list to mean both an empty file and an uninitialised value.
- yifanl 2y agofor line in f: is multiple mistakes in a single line.
- falcor84 2y agoWhat are the mistakes there?
- galbar 2y agoThe most obvious one: with open(self.file_path, 'r') as openFile: for line in f: `f` does not exist. It should be `openFile`.
- dpassens 2y agoOne is that the variable is called openFile and not f. I don't know enough python to see something else wrong with that but would love to know too, since I've written such a line just last week.
- yifanl 2y agof doesn't refer to anything. iterating over the file object at all instead of just calling self.cache = openFile.readlines() means that calling strip() the line below removes data beyond just the trailing newlines.
- shakna 2y agoThe file object is named "openFile", but used as "f". The class is defined as "CachedFileIterator", but used as "CacheingFileIterator". That's two typos, before discussing the actual code.
- harpiaharpyja 2y agoWell, there's also the fact that the entire thing could be replaced with... def cached_file_iterator(file_path): with open(file_path, 'r') as f: lines = [ line.strip() for line in f.readlines() ] yield from iter(lines) # Example usage: if __name__ == "__main__": file_path = 'example.txt' # Replace with your file path iterator = cached_file_iterator(file_path) for line in iterator: print(line) Which is functionally identical and FAR less code to maintain.