newbie - variable "buried in quotes"

P

plsullivan

I've got a variable deep inside some quotes needed by the application I
am using. I can't figure out how to make this work. (Also, is there a
line continuation character?)
Thanks in advance,
Phil

luser = win32api.GetUserName()

gp.FeatureclassToCoverage_conversion("'Database
Connections\\%s@GIS_srv-earth.sde\\GIS.GIS.Cadastral\\GIS.GIS.Parcels'
POLYGON", prcl83, "", "DOUBLE") % luser
 
B

Bernd Nawothnig

I've got a variable deep inside some quotes needed by the application I
am using. I can't figure out how to make this work. (Also, is there a
line continuation character?)
luser = win32api.GetUserName()
gp.FeatureclassToCoverage_conversion("'Database
Connections\\%s@GIS_srv-earth.sde\\GIS.GIS.Cadastral\\GIS.GIS.Parcels'
POLYGON", prcl83, "", "DOUBLE") % luser

You poosibly meant something like that:

#v+
gp.FeatureclassToCoverage_conversion("'Database"
"Connections\\%s@GIS_srv-earth.sde\\GIS.GIS.Cadastral\\GIS.GIS.Parcels'"
"POLYGON" % luser, prcl83, "", "DOUBLE")
#v-



Bernd
 
P

plsullivan

If I follow your response Bernd, it looks like you interpreted that as
several lines. It actually should all be on one line. That's what made
me wonder if there is a line continuation character.
Phil
 
P

Peter Hansen

plsullivan said:
I've got a variable deep inside some quotes needed by the application I
am using. I can't figure out how to make this work. (Also, is there a
line continuation character?)
Thanks in advance,
Phil

luser = win32api.GetUserName()

gp.FeatureclassToCoverage_conversion("'Database
Connections\\%s@GIS_srv-earth.sde\\GIS.GIS.Cadastral\\GIS.GIS.Parcels'
POLYGON", prcl83, "", "DOUBLE") % luser

The % operator goes immediately after the string on which it operates:

gp.FeatureclassToCoverage_conversion("'Database
Connections\\%s@GIS_srv-earth.sde\\GIS.GIS.Cadastral\\GIS.GIS.Parcels'
POLYGON" % luser, prcl83, "", "DOUBLE")


-Peter
 
P

Peter Hansen

Peter said:
The % operator goes immediately after the string on which it operates:

gp.FeatureclassToCoverage_conversion("'Database
Connections\\%s@GIS_srv-earth.sde\\GIS.GIS.Cadastral\\GIS.GIS.Parcels'
POLYGON" % luser, prcl83, "", "DOUBLE")

Missed your buried question about line continuations. Yes, there is
one, and it's the conventional \ at the end of the line.

On the other hand, there are better approaches:

gp.FeatureclassToCoverage_conversion("'Database Connections"
"\\%s@GIS_srv-earth.sde\\GIS.GIS.Cadastral\\GIS.GIS.Parcels'"
"POLYGON" % luser, prcl83, "", "DOUBLE")

That, for example, uses the fact that adjacent strings are combined into
one by the parser. Since they are all enclosed in parentheses, they are
considered "adjacent" even though they come on different lines without
line continuation characters.

It's pretty rare to need to use \ to continue lines, unless it's just a
style you don't mind or prefer.

(Personally, I'd write this differently. Create a variable called path
or something just above that line, doing the string substitution when
you build it. Then the call to FeatureclassToCoverage_conversion() can
come all on one line using "path", even with its unwieldy name. :)

-Peter
 
P

plsullivan

Thanks guys but I am still not getting it. This part -->
gp.FeatureclassToCoverage_conv­ersion("'Database
Connections\\%s@GIS_srv-earth.­sde\\GIS.GIS.Cadastral\\GIS.GI­S.Parcels'

POLYGON", prcl83, "", "DOUBLE") <-- % luser is one long command.
I need to be able to insert the luser variable deep in the middle of
that.
Phil
 
B

Bernd Nawothnig

If I follow your response Bernd, it looks like you interpreted that as
several lines. It actually should all be on one line. That's what made
me wonder if there is a line continuation character.

The lines are concatenated to one string as I wrote it.

See also fup from Peter Hansen for more explanations.



Bernd
 
B

Bernd Nawothnig

Thanks guys but I am still not getting it. This part -->
gp.FeatureclassToCoverage_conv­ersion("'Database
Connections\\%s@GIS_srv-earth.­sde\\GIS.GIS.Cadastral\\GIS.GI­S.Parcels'
POLYGON", prcl83, "", "DOUBLE") <-- % luser is one long command.

Yes, I understood you perfectly well.
I need to be able to insert the luser variable deep in the middle of
that.

luser will replace the '%s' in my version. Just try it!



Bernd
 
P

plsullivan

Ok, I see how concatenation is called for here. I'll try to use the
tutorial to figure that one out. Look out I'll probably be back! Thanks
for the help.
Phil
 
D

Dennis Lee Bieber

Ok, I see how concatenation is called for here. I'll try to use the
tutorial to figure that one out. Look out I'll probably be back! Thanks

What may not have been mentioned is that Python will consider an
unclosed paren to be a "continuation"

x = (a, b, c)

and
x = ( a,
b,
c )

are the same semantically.

Same applies to lists and dictionaries:

x = [ a,
b,
c ]

x = { "a" : 1,
"c" : 2,
"b" : 3 }

--
 
D

Dennis Lee Bieber

Thanks guys but I am still not getting it. This part -->
gp.FeatureclassToCoverage_conv­ersion("'Database
Connections\\%s@GIS_srv-earth.­sde\\GIS.GIS.Cadastral\\GIS.GI­S.Parcels'

POLYGON", prcl83, "", "DOUBLE") <-- % luser is one long command.
I need to be able to insert the luser variable deep in the middle of
that.
Phil
Nonetheless, it is a function call with four arguments. The %
substitution has to be applied to AN argument; it does not go outside
the call's closing )

But... Let's try something else (note: untested):

gp.FeatueclassToCoverage_conversion(
"\\".join( [ "'Database Connections",
"%s@GIS_srv-earth.-sde" % luser,
"GIS.GIS.Cadastral",
"GIS.GI-S.Parcels'POLYGON" ] ),
prcl83,
"",
"DOUBLE" )

No line continuation is needed as Python is looking for the matching )
for the first (. And the same applies to the join( looking for ), and
the [ looking for ]

The first argument is a string join. The join() is taking a list
of four items, and making one long string using the backslash as the
separator. The second item in the list is a string with % substitution.

Relying upon Python's implicit string concatenation for adjacent
strings (and remembering that the function ( means no line continuation
mark is needed until the matching ) ) you could change the join to just:

"'Database Connections"
"\\%s@GIS_srv-earth.-sde"
"\\GIS.GIS.Cadastral"
"\\GIS.GI-S.Parcels'POLYGON" % luser,

--
 

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

No members online now.

Forum statistics

Threads
474,239
Messages
2,571,200
Members
47,838
Latest member
elibuskamoSeAve

Latest Threads

Top