I happen to write a lot of Python code to do file processing/parsing lately. A thing that often bugs me, especially in code like this, is that keeping lines below a certain length (hello PEP8) becomes increasingly difficult as the level of nesting increases.
My parsing code often has
- a class
- that has a method
- that uses
with
- and loops over all lines in a file
- which can lead to exceptions (try...except)
In pseudo code, for example roughly like this (the line marks the suggested maximum line length):
class file_parser(): |
def do_parse(): |
with open(f_in, "r") as fh: |
for line in fh: |
try: |
if "test" in line: |
parse_test(line) |
else: |
parse_real(line) |
except ParseError as e: |
log.warning(f"failed to parse line {line} from file {f_in}: {e})
That is already five levels deep; with an indentation width of four, that is 25% of the line width.
I was very happy when I learned that
Also, the ability to
helps to reduce the number of levels of nesting.
Potentially
can also be used to avoid another level of nesting; although I do not see a specific use case yet.
In an extreme case (in my opinion), I factored out parts of the processing into a separate method, just to get a "fresh start" with the indentation. That felt wrong, somehow.
So, here is my question:
Apart from ignoring the (or a) recommended maximum line length, what tricks, constructs, strategies or features of Python do experienced developers use to address this "loss of line width real estate"? Or is this not a problem after all? While I do not seem to be the only one thinking about it, I have not found much that addresses the specific case of nested code (segments).