Python and PEP8 - Recommendations on breaking up long lines?

Walter Hurry walterhurry at lavabit.com
Thu Nov 28 18:38:38 CET 2013


On Thu, 28 Nov 2013 10:08:30 -0500, Roy Smith wrote:

> In article <34479463-b8a8-4417-9989-cd29369461c2 at googlegroups.com>,
>  Victor Hooi <victorhooi at gmail.com> wrote:
> 
>>     cur.executemany("INSERT INTO foobar_foobar_files VALUES (?)",
>>                     [[os.path.relpath(filename, foobar_input_folder)]
>>                     for filename in filenames])
> 
> I don't often write raw SQL embedded in Python.  I'm much more likely to
> use some sort of ORM layer.  But, if I were doing this, I would break it
> up something like:
> 
> 
> 
> There's a few different strategies I employed there.  My first thought
> was a logical break of computing the list of pathnames vs. inserting
> them into the database.  That got me here:
> 
>     paths = [[os.path.relpath(filename, foobar_input_folder)] \
>       for filename in filenames]
>     cur.executemany("INSERT INTO foobar_foobar_files VALUES (?)",
>                     paths)
> 
> I wouldn't have actually broken the first line with the backslash, but
> I'm doing that to appease my news posting software.
> 
> My next step would be some simple textual changes; I'd get rid of the
> overly-line variable names.  In general, I don't like very short
> variable names, but I'm OK with them as long as the scope is very small,
> as it is here:
> 
>     paths = [[os.path.relpath(fn, folder)] for fn in filenames]
>     cur.executemany("INSERT INTO foobar_foobar_files VALUES (?)",
>                     paths)
> 
> I'd probably factor out the double lookup of os.path.relpath.  I think
> this is easier to read:
> 
>     relpath = os.path.relpath paths = [[relpath(fn, folder)] for fn in
>     filenames] cur.executemany("INSERT INTO foobar_foobar_files VALUES
>     (?)",
>                     paths)
> 
> If filenames was a very long list, it would also be a little bit faster
> to execute, but that's such a small factor as to probably be
> unmeasurable.
> 
> And, finally, I'd probably move one set of square brackets down into the
> SQL statement.  It really makes more sense there anyway; the bundling up
> of the arguments into a sequence is more a part of the database API than
> it is inherent to the data.
> 
>     relpath = os.path.relpath paths = [relpath(fn, foobar_input_folder)
>     for fn in filenames]
>     cur.executemany("INSERT INTO foobar_foobar_files VALUES (?)",
>                     [paths])

I had a problem similar to this some time ago. Someone (here) suggested 
textwrap.dedent, which for me was just the ticket.




More information about the Python-list mailing list