Label Expression via Python

2680
4
Jump to solution
04-21-2021 09:59 AM
Labels (1)
PeteJordan
Occasional Contributor III

  I'm not the greatest with Python so I can easily get confused with indentations and such among other things.

  Currently I want to create a label expression to do the following and am only able to get it to work with 2 conditions and not the 3rd.

Field Names

[NAME]

[ALT_NAME]

[WIDTH] = Numeric

  I want to display these 3 fields only if they are not null or any combination of them that aren't null.

 

Originally I was only doing this with [NAME] and [ALT_NAME], and the code for this allows me to display either NAME or ALT_NAME or Both if they aren't (None, "", "<Null>"

def FindLabel ( [ALT_NAME], [NAME] ):
    if [ALT_NAME] in (None, "") or [ALT_NAME] in (None, "0"):
        return [NAME]
    
    elif [NAME] in (None, "") or [NAME] in (None, "<Null>"):
        return [ALT_NAME]
    
    else: 
      return [NAME] +'\r\n'+ [ALT_NAME]

This works fine.  However when trying to add in the [WIDTH], I want it to only display that if it is not null or equal to 0.

  So far I've been trying too many things to list on here and nothing seems to work.  I'm not sure if I'm indenting things wrong or using the .strip() wrong (either using it or not using it) and so on.

  I just want a code that only shows any combination or any of these that don't have blank or <Null> values (or 0 as well for the [WIDTH]).

  I know the code is probably a little messed up from the start, but again, I'm not all that familiar with Python...

0 Kudos
1 Solution

Accepted Solutions
ZacharyUhlmann1
Occasional Contributor III

Hi Pete.  I took a different approach with your desired outcome.  Let me know if this displays what you wanted (it worked for me with NAME and ALT_NAME type = string, and WIDTH type = int (or single?):

 

def FindLabel([NAME],[ALT_NAME],[WIDTH]):
  # initiate empty list to append to if val == NULL
  str_list = []
  if [NAME] not in (None, "<NULL>", "0"):
    str_list.append([NAME])
  if [ALT_NAME] not in (None,"<NULL>", "0"):
     str_list.append([ALT_NAME])
  if [WIDTH] not in (None, "<NULL>", "0"):
     str_list.append(str([WIDTH]))
  # create one string from all non-NULL values with "\n" in between
  label_str = '\n'.join(str_list)
  return(label_str)

 

My understanding is that you ONLY want to display labels for values that do NOT == 0, NULL, etc.  If that's not the case, we can adjust logic.  I honestly don't do fancy label action often, so thanks for showing me the "<NULL>" syntax.  Just curious, the "0" in (None, "<NULL>", "0") is actually a string not the integer 0.  If you want to hunt for the integer, then remove the "".  If you want both string and int, just add a comma and 0 within the parenthesis constructing your tuple. 

Let me know if that worked for you...Zach 

View solution in original post

4 Replies
ZacharyUhlmann1
Occasional Contributor III

Hi Pete.  I took a different approach with your desired outcome.  Let me know if this displays what you wanted (it worked for me with NAME and ALT_NAME type = string, and WIDTH type = int (or single?):

 

def FindLabel([NAME],[ALT_NAME],[WIDTH]):
  # initiate empty list to append to if val == NULL
  str_list = []
  if [NAME] not in (None, "<NULL>", "0"):
    str_list.append([NAME])
  if [ALT_NAME] not in (None,"<NULL>", "0"):
     str_list.append([ALT_NAME])
  if [WIDTH] not in (None, "<NULL>", "0"):
     str_list.append(str([WIDTH]))
  # create one string from all non-NULL values with "\n" in between
  label_str = '\n'.join(str_list)
  return(label_str)

 

My understanding is that you ONLY want to display labels for values that do NOT == 0, NULL, etc.  If that's not the case, we can adjust logic.  I honestly don't do fancy label action often, so thanks for showing me the "<NULL>" syntax.  Just curious, the "0" in (None, "<NULL>", "0") is actually a string not the integer 0.  If you want to hunt for the integer, then remove the "".  If you want both string and int, just add a comma and 0 within the parenthesis constructing your tuple. 

Let me know if that worked for you...Zach 

PeteJordan
Occasional Contributor III

That works Zach, thanks for that.

Now the next step would be to add some text for the width field to just say "Width = " and then the value.

In my original code (not shown here) I had 

return [NAME] +'\r\n'+ [ALT_NAME] +'\r\n'+ "Width = " + [WIDTH] + "'"

as my final line to at least have that text.  How would I add that to your script?  I tried to create a new string 

  str_Widthtxt = "Width = "

 but wasn't sure how to add it to the 

str_list.append(str([WIDTH]))

line.  Tried using the + and then commas and various other things, but no luck.

 

  Finally do you know how to italicize results or text in python in Pro?   I swore I had done this in the past, but I'm not finding anything that works now and not sure if its even possible?

 

Thanks

0 Kudos
ZacharyUhlmann1
Occasional Contributor III

Hi Pete.  Glad it worked out.  We can add those lines.  First though, here is a quick demo hopefully illustrating a few of the tougher tricks and functionality in that original code.  Run this using a Notebook in Pro.  Just cut and paste this into a cell and hit the Run arrow - see what prints.

 

player = ['Steve Nash','Steph Curry', 'Kawhi']
stat = ['Assists','Threes','FG pctg']
num = [11,500,60]
apex_statistics = []
# zip basically runs a for loop with multiple lists simultaneously
for player, stat, num in zip(player, stat, num):
  # different formatting for fg pctg
  if stat in ['FG pctg']:
      num = '{}%'.format(num)
  # note this syntax (.format)is for more current Python versions.
  # Google / StackExchange from older posts will be similar but different
  # each {} will be populated by locationally matched item in ()  
  str = '{} master of {}:\n{} per year!'.format(player, stat, num)
  # add str to list one at a time each iteration of for loop
  apex_statistics.append(str)
# creates single string from the 3 strings in the list apex_statistics
full_str = '\n\n'.join(apex_statistics)
print(full_str)

 

 So to get the width string formatted, replace existing lines in if statement block for width with those shown below.  Note use either Option A or B (delete the other).  

 

  if [WIDTH] not in (None, "<NULL>", "0"):
    # option A basic
    width_str = 'Width: {}'.format([WIDTH])
    # option B - add units if you want too
    width_str = 'Width: {} Feet'.format([WIDTH])
    str_list.append(width_str)

 

 let me know if that works.

0 Kudos
PeteJordan
Occasional Contributor III

Great thanks again Zach.  I just have issues with the indentation a lot, so that's something I need to spend more time getting to know.

  I like that little tutorial you gave as well and will use that as a sample code for my own personal Python doc I'm making for myself so I can learn how things work better in Python.

  Thanks again

0 Kudos