Need cleanup advice for multiline string

R

Robert Dailey

Hey guys. Being a C++ programmer, I like to keep variable definitions
close to the location in which they will be used. This improves
readability in many ways. However, when I have a multi-line string
definition at function level scope, things get tricky because of the
indents. In this case indents are serving two purposes: For syntax and
actual text output. The tabs for function scope should not be included
in the contents of the string. Below is the code I am trying to
improve. Notice how it looks ugly/unreadable because of the way the
string contents are shifted all the way to the left edge of the
document. This breaks the flow of scope. Would you guys solve this
problem by moving failMsg into global scope? Perhaps through some
other type of syntax?

Help is appreciated!

def RunCommand( commandList ):
commandString =
print( 'Running Command:', )
cmd = subprocess.Popen( commandList )
returnCode = cmd.wait()
if returnCode:
failMsg = '''\
*************************************************
The following command returned exit code [{:#x}].
This represents failure of some form. Please review
the command output for more details on the issue.
------------
{}
*************************************************
'''
commandString = ' '.join( commandList )
raise CommandFailure( failMsg.format( returnCode,
commandString ) )
 
R

Robert Dailey

Hey guys. Being a C++ programmer, I like to keep variable definitions
close to the location in which they will be used. This improves
readability in many ways. However, when I have a multi-line string
definition at function level scope, things get tricky because of the
indents. In this case indents are serving two purposes: For syntax and
actual text output. The tabs for function scope should not be included
in the contents of the string. Below is the code I am trying to
improve. Notice how it looks ugly/unreadable because of the way the
string contents are shifted all the way to the left edge of the
document. This breaks the flow of scope. Would you guys solve this
problem by moving failMsg into global scope? Perhaps through some
other type of syntax?

Help is appreciated!

def RunCommand( commandList ):
   commandString =
   print( 'Running Command:',  )
   cmd = subprocess.Popen( commandList )
   returnCode = cmd.wait()
   if returnCode:
      failMsg = '''\
*************************************************
The following command returned exit code [{:#x}].
This represents failure of some form. Please review
the command output for more details on the issue.
------------
{}
*************************************************
'''
      commandString = ' '.join( commandList )
      raise CommandFailure( failMsg.format( returnCode,
commandString ) )

And yes, I recognize there are syntax errors. Ignore those for now.
 
M

Mark Lawrence

Robert said:
Hey guys. Being a C++ programmer, I like to keep variable definitions
close to the location in which they will be used. This improves
readability in many ways. However, when I have a multi-line string
definition at function level scope, things get tricky because of the
indents. In this case indents are serving two purposes: For syntax and
actual text output. The tabs for function scope should not be included
in the contents of the string. Below is the code I am trying to
improve. Notice how it looks ugly/unreadable because of the way the
string contents are shifted all the way to the left edge of the
document. This breaks the flow of scope. Would you guys solve this
problem by moving failMsg into global scope? Perhaps through some
other type of syntax?

Help is appreciated!

def RunCommand( commandList ):
commandString =
print( 'Running Command:', )
cmd = subprocess.Popen( commandList )
returnCode = cmd.wait()
if returnCode:
failMsg = '''\
*************************************************
The following command returned exit code [{:#x}].
This represents failure of some form. Please review
the command output for more details on the issue.
------------
{}
*************************************************
'''
commandString = ' '.join( commandList )
raise CommandFailure( failMsg.format( returnCode,
commandString ) )

And yes, I recognize there are syntax errors. Ignore those for now.
For starters take a look at http://tinyurl.com/o2o8r8 , just about every
combination of string concatenation going there. I assume that one of
these will let you leave failMsg where it belongs.
 
D

Dave Angel

Robert said:
Hey guys. Being a C++ programmer, I like to keep variable definitions
close to the location in which they will be used. This improves
readability in many ways. However, when I have a multi-line string
definition at function level scope, things get tricky because of the
indents. In this case indents are serving two purposes: For syntax and
actual text output. The tabs for function scope should not be included
in the contents of the string. Below is the code I am trying to
improve. Notice how it looks ugly/unreadable because of the way the
string contents are shifted all the way to the left edge of the
document. This breaks the flow of scope. Would you guys solve this
problem by moving failMsg into global scope? Perhaps through some
other type of syntax?

Help is appreciated!

def RunCommand( commandList ):
commandString =
print( 'Running Command:', )
cmd = subprocess.Popen( commandList )
returnCode = cmd.wait()
if returnCode:
failMsg = '''\
*************************************************
The following command returned exit code [{:#x}].
This represents failure of some form. Please review
the command output for more details on the issue.
------------
{}
*************************************************
'''
commandString = ' '.join( commandList )
raise CommandFailure( failMsg.format( returnCode,
commandString ) )
No, don't put it in global scope. Put it externally, so it can be
readily localized for international markets.

DaveA
 
S

Simon Brunning

2009/8/11 Robert Dailey said:
It's a figure of speech. And besides, why would I want programming
advice from a woman? lol. Thanks for the help.

Give the attitudes still prevalent in our industry (cf
<http://tinyurl.com/c5nqju> and many more), I'm sorry to say that I
don't think this is funny.
 
H

Hendrik van Rooyen

It's a figure of speech. And besides, why would I want programming
advice from a woman? lol. Thanks for the help.

Well it may come as a surprise to you, but it was a woman who
wrote one of the first compilers.

She became an Admiral in the US navy as a result.

If I recall correctly, her name was Grace Hooper.

How many compilers have you written from scratch,
without a compiler to help you?

:)

- Hendrik
 
M

MRAB

Hendrik said:
Well it may come as a surprise to you, but it was a woman who
wrote one of the first compilers.

She became an Admiral in the US navy as a result.

If I recall correctly, her name was Grace Hooper.
Grace Hopper. The saying "It's easier to ask forgiveness than it is to
get permission" is attributed to her.
 
R

Robert Dailey

It's lots of little things like this which combine to create an
environment which is less friendly towards women than it is towards
others.  You might read it as a joke, others might not.  Even if it is a
joke, it's in poor taste and doesn't really belong on python-list.

There's a difference between pointing out inappropriate behavior and
being unfriendly.  Hopefully Robert got help with his problem.  That's
what the list is here for.  Having accomplished that, it is not
unfriendly to ask him not to make disparaging comments, "jokes" or
otherwise, about groups of people.

Jean-Paul

Hey everyone,

I was actually joking about my remark, I was making fun of the fact
that Bearophile took my figure of speech literally. I have worked with
a lot of women in the past and they even use "guys" to refer to
everyone in a room (When there were obviously other females in that
room as well).

On a more serious note, I do apologize to those offended by my remark.
I realize that these things can be a touchy subject for some people. I
expected more of a laid-back attitude from everyone. No need to be so
serious all the time. I cannot completely doubt that there are logical
women out there. I just haven't seen one yet. But that doesn't mean
I'm a sexist.

With my apology presented, I would like to propose that we end the
discussion here. As I said, this is a very sensitive subject and this
thread could spin way out of control if we don't just ignore the
issue. For those that took it as a friendly, harmless joke, hopefully
you had a laugh. For those that took it seriously or as an offense,
please take my apology to heart. Thanks once again to everyone for
your help. I've long been a member of this community and I really
appreciate the continuous support I've been receiving!

Take care everyone!
 
R

Robert Dailey

Hey everyone,

I was actually joking about my remark, I was making fun of the fact
that Bearophile took my figure of speech literally. I have worked with
a lot of women in the past and they even use "guys" to refer to
everyone in a room (When there were obviously other females in that
room as well).

On a more serious note, I do apologize to those offended by my remark.
I realize that these things can be a touchy subject for some people. I
expected more of a laid-back attitude from everyone. No need to be so
serious all the time. I cannot completely doubt that there are logical
women out there. I just haven't seen one yet. But that doesn't mean
I'm a sexist.

With my apology presented, I would like to propose that we end the
discussion here. As I said, this is a very sensitive subject and this
thread could spin way out of control if we don't just ignore the
issue. For those that took it as a friendly, harmless joke, hopefully
you had a laugh. For those that took it seriously or as an offense,
please take my apology to heart. Thanks once again to everyone for
your help. I've long been a member of this community and I really
appreciate the continuous support I've been receiving!

Take care everyone!

Oh, one last thing... So everyone knows, I chose the following
formatting solution to multiline strings:

def MyFunction():
multilineString = (
'This is a string that spans '
'multiple lines.'
)
print( multilineString )

I think this is as good as it is going to get for my personal needs.
However, I do not like having to put a space at the end of each
string. I've also done this in the past, which is slightly more ugly:

multilineString = (
'This is a string that spans',
'multiple lines.'
)
print( ' '.join( multilineString ) )

This will add the spaces between lines for you. However, in a
production quality application I would always have strings external to
the scripts and have an advanced localization system. However this is
useful for quick little scripts that I want to keep tidy.
 
S

Simon Forman

Hey everyone,

I was actually joking about my remark, I was making fun of the fact
that Bearophile took my figure of speech literally. I have worked with
a lot of women in the past and they even use "guys" to refer to
everyone in a room (When there were obviously other females in that
room as well).

On a more serious note, I do apologize to those offended by my remark.
I realize that these things can be a touchy subject for some people. I
expected more of a laid-back attitude from everyone. No need to be so
serious all the time. I cannot completely doubt that there are logical
women out there. I just haven't seen one yet. But that doesn't mean
I'm a sexist.

Oh my. And you were doing so well. You haven't seen a logical
woman? Perhaps you're blind because your eyes were torn out by a
raging marmoset?

Guess what? Thinking (or just saying) that /does/ mean you're a
sexist. (Even if it was just another "friendly, harmless joke".)
 
S

Steven D'Aprano

Keep in mind that the Internet is a global forum, and not everyone here
speaks English as a first language. I believe Bearophile is one of those.
Although his, or possibly her, English is excellent, it wouldn't surprise
me that (s)he would misinterpret "guys" as just referring to men. I'm a
native English speaker, and I would have done the same.


Yes, I've seen this myself, but it's still uncommon enough to surprise me
every time I see it.


That's okay, I haven't seen terribly many logical men out there either.

Oh, one last thing... So everyone knows, I chose the following
formatting solution to multiline strings:

def MyFunction():
multilineString = (
'This is a string that spans '
'multiple lines.'
)
print( multilineString )

I think this is as good as it is going to get for my personal needs.
However, I do not like having to put a space at the end of each
string.

So put them at the beginning of the next line. It makes the space more
obvious, so it's clearer what you have done. That's what I sometimes do.

I've also done this in the past, which is slightly more ugly:

multilineString = (
'This is a string that spans',
'multiple lines.'
)
print( ' '.join( multilineString ) )


It's also less efficient, as it does the concatenation at runtime instead
of compile time. But for a small script, that's not likely to be a
problem worth worrying about.
 
S

Steven D'Aprano

On Wed, 12 Aug 2009 08:11:43 -0700, Simon Forman wrote:

[quoting Robert Dailey]
Oh my. And you were doing so well. You haven't seen a logical woman?
Perhaps you're blind because your eyes were torn out by a raging
marmoset?

Guess what? Thinking (or just saying) that /does/ mean you're a sexist.
(Even if it was just another "friendly, harmless joke".)

It was an incredibly insensitive thing for Robert to say, having just
been slapped for a previous insensitive "joke" about women. But still,
most people, male or female, *aren't* logical. I know I've never met
somebody who is entirely logical, of either sex, and I'm pretty sure I've
not met very many people who are even mostly logical. Vulcans we are not.
Does this mean I'm equally sexist against men *and* women? ("I'm not
biased, I hate everyone equally!" *wink*)

Hell, here I am, at 2am, defending somebody I don't know, for saying
something I don't approve of, against somebody who is saying something I
agree with, out of some sort of misguided sense of fairness. Logic? Ha,
what's logic got to do with it?
 
D

David Bolen

Robert Dailey said:
Hey guys. Being a C++ programmer, I like to keep variable definitions
close to the location in which they will be used. This improves
readability in many ways. However, when I have a multi-line string
definition at function level scope, things get tricky because of the
indents. In this case indents are serving two purposes: For syntax and
actual text output. The tabs for function scope should not be included
in the contents of the string. (...)

Personally I'm in the camp that something like this should be hoisted
out of the code path (whether to global scope, a dedicated message
module or configuration file is a design choice).

But if it's going to stay inline, one approach that can maintain some
of the attractive qualities of a triple quoted string is to make use
of the textwrap module:

import textwrap

def RunCommand( commandList ):
# ...
if returnCode:
failMsg = textwrap.dedent('''\
*************************************************
The following command returned exit code [{:#x}].
This represents failure of some form. Please review
the command output for more details on the issue.
------------
{}
*************************************************
''')

which removes any common leading whitespace (must be identical in terms
of any tabs/spaces).

This is still additional run-time processing, and most likely less
efficient than the joining of individual strings, but it does permit a
clean triple-quoted string so IMO is easier to read/maintain in the
source - providing the code indentation level doesn't get in the way
of the desired line length of the string. You can also choose to
dedent the string a bit (say to the level of "failMsg") if needed
without being forced all the way back to the left margin.

You can also combine textwrap.dedent with some of the other options if
where the strings are defined makes it nicer if they still have some
indentation (say in a global Python module). In that case, you'd most
likely just process them once when the module was imported, so any
inefficiency in textwrap.dedent is far less important.

-- David
 
J

Jean-Michel Pichavant

Grant said:
Straying a bit OT, but I find this particular issue rather
fascinating.

At least in the US, "guys" is now pretty much gender-neutral
according to my casual research (mostly just paying attention
to informal speach).

Oddly, it still seems to be masculine when singular. Though one
commonly hears a group of females addressed as "you guys" or
refered to as "those guys", one never hears a single female
referred to as "a guy" or "that guy".

It is a bit tricky, however, since a phrase like "a group of
guys" still seems to refer to just males since the word "guys"
in that case is being applied individually to a plurality of
persons rather being applied collectivelly to a single group --
if that makes any sense.

I've actually discussed this with a a number of female friends,
and they almost all thought the term "gals" was condescending
and actually preferred to be referred to collectively as
"guys".
I'm no English native, but I already heard women/men referring to a
group as "guys", no matter that group gender configuration. It's even
used for group composed exclusively of women. Moreover it looks like a
*very* friendly form, so there is really nothing to worry about it.

Forms like:
"Hi guys", "You guys should do something...", "Come on guys..." are very
friendly and gender-neutral.

JM
 
P

Paul Boddie

Are you suggesting this list reject part of the community regarding its
sexual orientation, ethnicity, size, culture? If that was the case I'd
like to know about it.

Careful: you probably meant to write "rejects", not "reject". That
changes the meaning of what you've written somewhat.
I would really want to know how you'd guess my gender (could be some
clue somewhere), my sexual orientation, my religion and so on.
How can you reject someone regarding informations you don't have ?

Well, everyone can of course hide their actual identity on the
Internet, but when someone references a group of people with a
juvenile remark (if we are being charitable about the matter), it has
nothing to do with guessing the characteristics of individuals. The
whole excuse that anonymity defends against insults and harassment is
a bit like saying that slinging mud at everyone is acceptable as long
as everyone is encouraged to do it and nobody is wearing their nicest
clothes. And unless your idea of a Python-related conference is
something close to a fancy-dress event with everyone "in character" -
which would obviously limit the effectiveness of such an event - you
presumably understand that there is a genuine need for continuity
between interactions on and off the Internet. This somewhat undermines
your argument.
That's the beauty of this mailing list, it has diversity, by design.

An explanation is needed here for this not to sound like
conversational padding.
We even welcome people that mixes up joke with sexist aggression, not to
mention how open minded we are :eek:)

Well, jokes actually need an amusing side, regardless of how
"edgy" ("juvenile" is typically the more accurate term) the joke-
teller is trying to be, and that was completely absent from the remark
in question. There's little room for error in communication over a
medium like this one, as I pointed out with your opening sentence. And
much as it probably upsets the "unfettered free speech" advocates, we
should be able to assert that "sexist aggression" is not acceptable
behaviour amongst those who seek to participate in our community.

Paul
 
C

Carl Banks

I'm no English native, but I already heard women/men referring to a
group as "guys", no matter that group gender configuration. It's even
used for group composed exclusively of women. Moreover it looks like a
*very* friendly form, so there is really nothing to worry about it.

I like how being very friendly means calling people after a guy who
tried to blow up the English Parliament.


Carl Banks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top