This is some interesting code, thanks for posting it here. Good work Mawg.
Nitpicking
# +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ # Given the lines and current line number, this function reads # from the current line (or the first following line containing the start string,
Please get rid of banners like this. If you are using an IDE such as PyCharm it can generate documentation comments for you. Current python industry standard regarding documentation is PEP-287.
lines,lineNumber,
Use consistent spacing. I personally like to use black to format my code. But you can use anything that formats to PEP8.
GetLinesBetween, lineNumber, ...
Please use correct PEP8 style such as get_lines_between
and line_number
?
#----------------------- except Exception as err: print('Exception:') exc_type, exc_value, exc_tb = sys.exc_info()
This is sloppy exception handling.
- If you must use a catch-all: Maybe use a higher level method that captures exceptions and change this to
try_get_lines_between
or_get_lines_between
as a more readable alternative. Why: separation of error handling and application logic - It is up to you to decide if this is best course of action. - You can also create your own exceptions if that is more suitable to you. You can for example raise them instead of returning empty list if it is more suitable.
- Use a logger instead of print to log exceptions.
logger.exception
is more suitable for this. - Instead of
sys.exit(0)
- maybe exit with non zero value so you know that there was an error.
emmpty, serach
Please use a spell checker plugin in your IDE.
# ToDo: consider (a) parameter(s) to indicate that lines must start with, # or be equal to, a search string, rather than just containing? # That would make this even more generic, but would require (an) extra parameter(s) #
People sometimes forget to do to-dos. Use a software like JIRA, Github Projects to track your tasks and remove them from code. It's OK to create tasks for yourself.
How would I design this better?
- Use generators instead of creating lists, so you can create pipeline of functions and can even read multiple files, and also save some memory.
- Maybe use Aho-Corasick algorithm (Trie based) to match against multiple strings.
- Sensible defaults - there are lot of parameters for this function. If you add default values then API would be easier to use.