Why does Python show the whole array?

J

John Posner

andrew said:
> i don't know why i get involved in this type of discussion, but....

====
I sense growing exasperation, and I do *not* have strong feelings on
this matter, so I promise this will be my last message on this topic.

I don't really prefer 'contains(belly, beer)' over 'beer in belly'.
Rather, it's a completeness/consistency argument: a language that
includes these methods for string objects:

belly.startswith(arg)
belly.endswith(arg)

.... should also include:

belly.contains(arg)

The lack of this method increases the likelihood that a user will
mistakenly use the find() method as a predicate.
 
C

Chris Rebert

Hi Python users,

I ran into a problem with python coding in ARCGIS. Does anybody have the
experience in dealing with this?

I need to calculate NEWFIELD based on OLDFIELD under condition: if  OLDFIELD
== 0 then return string "B" otherwise return "".

codeblock = "def codefun(code): if code == 0: return \"B\" else: return \"\"
"

gp.CalculateField_management("INFILE", "OLDFIELD", "codefun(!NEWFIELD!",
"PYTHON", codeblock)
I got error:

RuntimeError:
exceptions.SyntaxError: invalid syntax (line 1)
Failed to execute (CalculateField).

Might I recommend you try using the multiline equivalent (assuming
ArcGIS supports C-style escape sequences):

codeblock = "def codefun(code):\n\tif code == 0:\n\t\treturn
\"B\"\n\telse:\n\t\treturn \"\" "

Cheers,
Chris
 
L

Lydia

Thanks for the suggestion.

But I guess under Python this doesn't work. I tried putting the code in
different ways. But still not worked.

codeblock = "def codefun(code): \\
if code == 0: \\
return \"B\" \\
else: return \"\" "


----- Original Message -----
From: "Chris Rebert" <[email protected]>
To: "Lydia" <[email protected]>
Cc: <[email protected]>
Sent: Thursday, April 09, 2009 1:55 PM
Subject: Re: calculate field in ARCGIS


Hi Python users,

I ran into a problem with python coding in ARCGIS. Does anybody have the
experience in dealing with this?

I need to calculate NEWFIELD based on OLDFIELD under condition: if
OLDFIELD
== 0 then return string "B" otherwise return "".

codeblock = "def codefun(code): if code == 0: return \"B\" else: return
\"\"
"

gp.CalculateField_management("INFILE", "OLDFIELD", "codefun(!NEWFIELD!",
"PYTHON", codeblock)
I got error:

RuntimeError:
exceptions.SyntaxError: invalid syntax (line 1)
Failed to execute (CalculateField).

Might I recommend you try using the multiline equivalent (assuming
ArcGIS supports C-style escape sequences):

codeblock = "def codefun(code):\n\tif code == 0:\n\t\treturn
\"B\"\n\telse:\n\t\treturn \"\" "

Cheers,
Chris
 
M

MRAB

Lydia said:
Thanks for the suggestion.

But I guess under Python this doesn't work. I tried putting the code in
different ways. But still not worked.

codeblock = "def codefun(code): \\
if code == 0: \\
return \"B\" \\
else: return \"\" "
You might also want to try a triple-quoted string, which makes it
clearer:

codeblock = """def codefun(code):
if code == 0:
return "B"
else:
return ""
"""
 
J

Jason Scheirer

Might I recommend you try using the multiline equivalent (assuming
ArcGIS supports C-style escape sequences):

codeblock = "def codefun(code):\n\tif code == 0:\n\t\treturn
\"B\"\n\telse:\n\t\treturn \"\" "

Cheers,
Chris

Looks like an error in your code:

gp.CalculateField_management("INFILE", "OLDFIELD", "codefun(!
NEWFIELD!", "PYTHON", codeblock)

Should be:

gp.CalculateField_management("INFILE", "OLDFIELD", "codefun(!
NEWFIELD!)", "PYTHON", codeblock)

Or you could fold the function into the expression using the ternary
and get rid of your code block param:

gp.CalculateField_management("INFILE", "OLDFIELD", "'B' if !OLDFIELD!
== 0 else ''", "PYTHON")
 
J

John Machin

Hrvoje Niksic wrote:

 >  if test.contains(item)     # would return a Boolean value
 >

 >> That's a string method, not a function in the string module.

Oops, of course.

 >>>> import operator
 >>>> operator.contains('foo', 'o')

That's pretty good, and IMHO a bit better than John Machin's suggestion
to use the __contains__() method. (I have this prejudice that using the
__XXX__ methods in "everyday code" is cheating.)

I "suggested" no such thing. You asked whether anyone had discussed
such a thing. A reply pointing out that such a thing exists already is
in no way inciting people to use it.
Given how common string maniuplations are, I guess I'm surprised that
Python hasn't yet made "contains()" into both a "string"-module function
*and* a string-object method.

Perhaps because there's already a good way to do it: stringa in stringb
 
L

Lawrence D'Oliveiro

Lawrence said:
Gilles Ganault said:
test = "(e-mail address removed)"
isp = ["gmail.com", "yahoo.com"]
for item in isp:
if test.find(item):
print item
======= output
gmail.com
yahoo.com
=======

This is why conditional constructs should not accept any values other
than True and False.

So you think

if test.find(item) == True: ...

would have been better?

That won't work either. Can anyone tell us what he's done wrong?
 

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
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top