Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why does Python show the whole array?

Reply
Thread Tools

Why does Python show the whole array?

 
 
John Posner
Guest
Posts: n/a
 
      04-09-2009
> Peter Otten wrote:

>> Could you explain why you prefer 'contains(belly, beer)'
>> or 'belly.contains(beer)' over 'beer in belly'? The last form may be

a bit
>> harder to find in the documentation, but once a newbie has learned about
>> it he'll find it easy to remember.


andrew cooke wrote:

> 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.

 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      04-09-2009
On Thu, Apr 9, 2009 at 12:42 PM, Lydia <(E-Mail Removed)> wrote:
> 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

--
I have a blog:
http://blog.rebertia.com
 
Reply With Quote
 
 
 
 
Lydia
Guest
Posts: n/a
 
      04-09-2009
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" <(E-Mail Removed)>
To: "Lydia" <(E-Mail Removed)>
Cc: <(E-Mail Removed)>
Sent: Thursday, April 09, 2009 1:55 PM
Subject: Re: calculate field in ARCGIS


On Thu, Apr 9, 2009 at 12:42 PM, Lydia <(E-Mail Removed)> wrote:
> 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

--
I have a blog:
http://blog.rebertia.com

 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      04-09-2009
Lydia wrote:
> 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 ""
"""

> ----- Original Message ----- From: "Chris Rebert" <(E-Mail Removed)>
> To: "Lydia" <(E-Mail Removed)>
> Cc: <(E-Mail Removed)>
> Sent: Thursday, April 09, 2009 1:55 PM
> Subject: Re: calculate field in ARCGIS
>
>
> On Thu, Apr 9, 2009 at 12:42 PM, Lydia <(E-Mail Removed)> wrote:
>> 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
>


 
Reply With Quote
 
Jason Scheirer
Guest
Posts: n/a
 
      04-09-2009
On Apr 9, 12:55*pm, Chris Rebert <(E-Mail Removed)> wrote:
> On Thu, Apr 9, 2009 at 12:42 PM, Lydia <(E-Mail Removed)> wrote:
> > 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
>
> --
> I have a blog:http://blog.rebertia.com


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")
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      04-09-2009
On Apr 10, 2:36*am, John Posner <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      04-10-2009
In message <grk6ch$73v$00$(E-Mail Removed)-online.com>, Peter Otten wrote:

> Lawrence D'Oliveiro wrote:
>
>> In message <(E-Mail Removed)>, Gilles Ganault
>> wrote:
>>
>>> test = "(E-Mail 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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does the crypto key show in "show run" on some switches and notothers? ttripp Cisco 2 10-19-2010 08:45 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
*WITHOUT* using: ValidateRequest="False" for the whole page (or my whole site).... \A_Michigan_User\ ASP .Net 2 08-21-2006 02:13 PM
why does the show function not show anything? greenflame Javascript 6 06-18-2005 08:57 PM



Advertisments