Lawrence DĀ¢Oliveiro wrote:
> You do this sequence of execute/fetch calls 4 times in your script.
Now, turning to your second set of suggestions regarding a more Pythonic
coding philosophy and perhaps making use of the db_iter helper: ...
It's a good idea where using a generator function to wrap repetitive
execute() and fetching patterns certainly keeps code DRY (Don't Repeat
Yourself) & I can see the advantage of putting the lookup in the loop.
It will take a while for me to fully digest your helpful suggestions, as I
noted in the prior response that I suffer from lack of SQL experience.
In fact, in the original thread, I originally tried to disassemble the
homescreen using SQL queries in DB Browser for SQLite v3.13.1 but gave up.
Message-ID: <[email protected]>
If I were to adapt your db_iter concept while keeping parameterized queries
safe, I'd probably modify it to accept a parameters tuple like this:
Python
def db_iter(conn, cmd, params=(), mapfn=lambda x: x):
"""Executes cmd with parameters and yields mapped results."""
for item in conn.cursor().execute(cmd, params):
yield mapfn(item)
That way, I get the clean generator loop style you recommended and maintain
strict parameter safety. Thanks again for taking the time to review the
code and thank you for offering suggestions such as that refactoring idea.--
https://mail.python.org/mailman3//lists/python-list.python.org