Hacker News new | past | comments | ask | show | jobs | submit login

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.




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


Well, 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.


    for line in f:
is multiple mistakes in a single line.


What are the mistakes there?


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


The most obvious one:

    with open(self.file_path, 'r') as openFile:
        for line in f:
`f` does not exist. It should be `openFile`.


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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: