Little scripting help

in my dictionary I have:

199: "zone A",
200: " ",

so in my script, I am trying to say something like

if value=" "
 value=" "
else
 value="located in "+value

How do I ask if value is equal to a space?

update
I was missing syntax stuff like colons and an additional equals

if value == " ":
		value=" "
else:
           value = "located in "+value

Make sure to make your thread title more descriptive than “Little scripting help.” This title does nothing to help someone else during a forum search or anyone that may answer your question.

myDict = { 199:"zone A", 200:""}
for key,value in myDict.items():
	location = ""
	if len(value):
		location = "%s Located In: %s" % (str(key),value)
	print location
2 Likes

If there’s a variable number of white spaces you might just want to change

if len(value):

to

if len(value.strip()):

Of course it’s probably best to just plan things out so you don’t have to do .strip(). If its truly nothing, you might want to consider using None instead of an empty string but I don’t know your full details.

If you end up going with None instead of ‘’, you could then do

if value is not None:

I noticed that there looked like a space in the value, but I figured that to be a typo. Definitely not a good practice, good to point that out.

2 Likes

Always better to squash a code smell before they turn into a code stench

3 Likes

I had no idea what to make the title.

what is

% (str(key),value)

https://docs.python.org/2.7/library/stdtypes.html#string-formatting-operations

Consider going through some Python tutorials.

3 Likes

Writing the phrase with the converter and then casting the key as a string is mental gymnastics for me.
I can see how it would be very useful though.

It was hard for me to understand what the docs.python.org was saying.

this was way more my speed

Casting the key to string in the formatting operation was actually superfluous, as the %s conversion format specifier would do that anyways.

But, anyways, whether via docs or tutorials, learning python’s basics will make Ignition much easier to navigate.

6 Likes

You can also use

"something {variable} format".format(variable="you want to")

Which I think was the recommended syntax before f-strings.

1 Like

The additional nicety is you can unpack a dictionary inside the format as well though I think this only came with jython 2.7. But its nicer than the old % style of string formatting as you don’t have to manually say what each type is

dataToPrint = {
	'intField':1,
	'strField':"hello",
	"lastField": None
}

f = "int field is {intField} strField is {strField} lastField is {lastField}".format(**dataToPrint)
print f
#prints out "int field is 1 strField is hello lastField is None"

To do similar in Ignition 7.* which uses jython 2.5 you can’t do the .format but you can do

dataToPrint = {
	'intField':1,
	'strField':"hello",
	"lastField": None
}

f = "int field is %(intField)i strField is %(strField)s lastField is %(lastField)s"%(dataToPrint)
print f
# prints out "int field is 1 strField is hello lastField is None"

No, it is equivalent to using %s everywhere. I’m not a fan of the curly braces.

Exactly, what I’m saying is you don’t have to specify this is an %i, this one is a %d, this one is %s because it implicitly is doing the %s for you. So its can be convenient.

I find them much more readable.

"{level} error happened in {device} at {time}"

vs

"%s error happened in %s at %s"

especially when you start applying formats to your variables:

"foo {bar:>10}".format(bar="bar")

Actually, I’m not even sure how you do this with %

Besides, specifying a type reminds me a bit too much of printf

1 Like

What about for dynamic paths?

Tag Paths - Ignition User Manual 8.1 - Ignition Documentation (inductiveautomation.com)

narrator =Clarkson
convoyNumber = PQ17
tagPath = ""[default]%s/%s" % Clarkson,PQ17

is that going to set the tagpath as:

[default]Clarkson/PQ17


I think % is throwing an illegal character in script console

I like the curly braces, but I think those only work in expression

You have a couple of errors in your script. It should be:

narrator = 'Clarkson'
convoyNumber = 'PQ17'
tagPath = '[default]%s/%s' % (narrator,convoyNumber)

This will set the tagPath to [default]Clarkson/PQ17

1 Like

I did something wrong before, seems to be working in script console.
Thanks

Why are you using ’ ’ instead of " "

Habit/laziness (don’t have to use the shift key) :rofl:.

Officially, python (jython by extension) will accept either. This allows you to do things like nested quotes without escape characters.

'This will allow "quotes" inside of a string'

2 Likes

Also true for Ignition’s expression language. But this is not true for SQL.

Using ‘’ will make many/most programming languages accept whats inside the ‘’ as litteral text/string and it will not try translate each character in between as a possible “other command”, while using “” the interpeter will look to see if you have like escape characters like /r/n or RegExp expression in it.
This is my main take on the difference.