Best way to split up lines - RE: About the 79 character linerecommendation

Roy Smith roy at panix.com
Thu Dec 7 13:47:02 EST 2006


In article <mailman.1248.1165493129.32031.python-list at python.org>,
 "Fredrik Lundh" <fredrik at pythonware.com> wrote:

> Michael Yanowitz wrote:
> 
> > What would be the best way to split the following line (Python doesn't like
> > me to split it up between the comma-separated parameters):
> >
> >    top, ip1, ip2, ip3, ip4, messageCounter, ackRequired, dataType, utc1,
> > utc2, utc3, utc4, utc5, utc6, utc7, utc8, utc9, utc10, utc11, utc12, st1,
> > st2, st3, st4, st5, st6, numberOfLabels, dataWord =
> > struct.unpack("!H4BH20BHI", strMessage)
> 
>     data = struct.unpack("!H4BH20BHI", strMessage)
> 
>     (top, ip1, ip2, ip3, ip4, messageCounter, ackRequired,
>     dataType, utc1, utc2, utc3, utc4, utc5, utc6, utc7, utc8,
>     utc9, utc10, utc11, utc12, st1, st2, st3, st4, st5, st6,
>     numberOfLabels, dataWord) = data
> 
> </F> 

My general rule of thumb for formatting things like argument lists is 
either they all fit on one line, or they go one item per line.  So, I would 
do it:

    (top,
     ip1,
     ip2,
     ip3,
     ip4,
     messageCounter,
     ackRequired,
     dataType,
     utc1,
     utc2,
     utc3,
     utc4,
     utc5,
     utc6,
     utc7,
     utc8,
     utc9,
     utc10,
     utc11,
     utc12,
     st1,
     st2,
     st3,
     st4,
     st5,
     st6,
     numberOfLabels,
     dataWord) = data

All one one line, it's just a mess of text to read through.  Formatting 
like this, it immediately (at least to my eyes) jumps out that you've got a 
bunch of ips, a few other things, a bunch of utcs, a bunch of sts, and then 
some more random data.  Much easier to read.

The downside, of course, it it takes up mumble lines instead of 4.  I'm 
cool with that, but I could see how it might annoy some people.

It would be nice if struct.unpack() had a way to specify unpacking repeated 
items as a list, ie:

   data = struct.unpack ("! H 4(B) H 2B 12(B) 6(B) H I", strMessage)
   (top,
    ip,
    messageCounter,
    ackRequired,
    dataType,
    utc,
    st,
    numberOfLables,
    dataWord) = data

and you'd end up with ip, utc, and st being lists.  Hmmm, maybe that's 
worth a PEP?



More information about the Python-list mailing list