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

  • Thread starter Michael Yanowitz
  • Start date
M

Michael Yanowitz

Hello:

I too don't like large lines. However in the following case, and
multi-level indentations, I find it unavoidable.
Here is one huge statement I haven't been able to split onto multiple
lines.
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)


Thanks in advance:
Michael Yanowitz


-----Original Message-----
From: [email protected]
[mailto:p[email protected]]On Behalf
Of Ramon Diaz-Uriarte
Sent: Wednesday, December 06, 2006 5:12 PM
To: Steve Bergman
Cc: (e-mail address removed)
Subject: Re: About the 79 character line recommendation



(...)
I'm finding 100 to be a nice balance. It forces me not to be lazy and
allow really long lines, but allows me to format so as to make the
meaning most clear.



But if you use some advanced editors (such as Emacs) that easily allow
you to see/edit the same file in two buffers side by side, then going
beyond 80 chars is often a bad idea, specially if you use a laptop.
(And, of course, there is the eternal issue of doing a simple "a2ps"
to print some code; longer than 80 and you often have hard to read
pages).

Best,

R.

--
Ramon Diaz-Uriarte
Statistical Computing Team
Structural Biology and Biocomputing Programme
Spanish National Cancer Centre (CNIO)
http://ligarto.org/rdiaz
 
R

Roel Schroeven

Michael Yanowitz schreef:
Hello:

I too don't like large lines. However in the following case, and
multi-level indentations, I find it unavoidable.
Here is one huge statement I haven't been able to split onto multiple
lines.
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)

I see three possibilities:

1. Avoid the one-liner alltogether

u = struct.unpack("!H4BH20BHI", strMessage)
top = u[0]
ip1, ip2, ip3, ip4 = u[1:5]
messageCounter = u[6]
# ... and so on ...

In this approach with your example it seems to me to be a good idea to
put ip, utc and st in separate tuples like this:

u = struct.unpack("!H4BH20BHI", strMessage)
top = u[0]
ip = u[1:5]
messageCounter = u[5]
ackRequired = u[6]
dataType = u[7]
utc = u[8:20]
st = u[20:26]
numberOfLabels = u[26]
dataWord = u[27]


2. Use parens around the tuple on the left hand side

(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)


3. Use '\' to break the line

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)
 

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top