Python: dynamic set variables?

Label printing integration (NiceWatch middleware and ActiveX programming interface)

Moderators: Georges, milos, NiceLabel Support Team

Post Reply
FunDeckHermit
Enthusiast
Posts: 14
Joined: Fri Apr 20, 2018 1:58 pm

Python: dynamic set variables?

Post by FunDeckHermit » Tue Jun 19, 2018 9:31 am

Hello everyone,

I have the following data:
['TotalFat','vet','800']
['Protein','eiwit','400']
['Salt','zout','4']
etc etc

And the following variables:
LBL_TBL_TotalFat_Text
LBL_TBL_TotalFat_Value
LBL_TBL_Protein_Text
LBL_TBL_Protein_Value
LBL_TBL_Salt_Text
LBL_TBL_Salt_Value
etc etc

Parsing
I run a SQL query to retrieve the data and store them inside a variable
Then I parse that string:

Code: Select all

rows = DB_NutritionalData.Value.splitlines()
del rows[0]

for row in rows:
	row = row.strip('"')
	a,b,c = row.split('","')
my question
How do I dynamically assign the result of the parsing like this:

Code: Select all

rows = DB_NutritionalData.Value.splitlines()
del rows[0]

for row in rows:
	row = row.strip('"')
	a,b,c = row.split('","')
	"LBL_TBL_"+a+"_Text.Value" = b
	"LBL_TBL_"+a+"_Value.Value" = c

User avatar
Saso
NiceLabel
NiceLabel
Posts: 2933
Joined: Mon Sep 04, 2006 8:09 am
Contact:

Re: Python: dynamic set variables?

Post by Saso » Tue Jun 19, 2018 10:03 am

Hmm, no that will not work. [-(

I would use Python to create a text file that can be used with the action Load Variable Data.
In NiceLabel software, you have the actions par Save Variable Data / Load Variable Data to exchange data between NiceLabel applications / solutions / etc.

Do the following:
  1. Use the action Save Variable Data to learn the structure of data file it creates. Basically, it is a CSV-like file with names of fields in the first row, followed by rows of semicolon-delimited data values.
  2. Update your Python code to write the parsed data into such text file.
  3. Use the action Load Variable Data to load the data into variables.
Saso Fleiser
Senior Technical Product Specialist

FunDeckHermit
Enthusiast
Posts: 14
Joined: Fri Apr 20, 2018 1:58 pm

Re: Python: dynamic set variables?

Post by FunDeckHermit » Tue Jun 19, 2018 10:41 am

Hallo Saso,

Thanks for your reply.

After some tweaking I almost have a working solution:

Code: Select all

rows = DB_NutritionalData.Value.splitlines()
del rows[0]

for row in rows:
	row = row.strip('"')
	a,b,c = row.split('","')
	exec('LBL_TBL_'+a+'_Value.Value = c')
	exec('LBL_TBL_'+a+'_Text.Value = b')
Error:
Image

The variable name is created correctly. Only the assign operation does not work.

Other options:
Do you know how the NiceLabel variables are stored? Are they stored in Globals()?

Is there another way to assign variables in Nicelabel and Python?

Working solution, not pretty:

Code: Select all

rows = DB_NutritionalData.Value.splitlines()
del rows[0]

for row in rows:
	row = row.strip('"')
	a,b,c = row.split('","')
		
	if a == "Protein":
		LBL_TBL_Protein_Value.Value = c
		LBL_TBL_Protein_Text.Value = b
	elif a == "TotalFat":
		LBL_TBL_TotalFat_Value.Value = c
		LBL_TBL_TotalFat_Text.Value = b
	elif a == "Sugar":
		LBL_TBL_Sugar_Value.Value = c
		LBL_TBL_Sugar_Text.Value = b	
	elif a == "SaturatedFat":
		LBL_TBL_SaturatedFat_Value.Value = c
		LBL_TBL_SaturatedFat_Text.Value = b	
	elif a == "Salt":
		LBL_TBL_Salt_Value.Value = c
		LBL_TBL_Salt_Text.Value = b	
	elif a == "Energy":
		LBL_TBL_Energy_Value.Value = c
		LBL_TBL_Energy_Text.Value = b	
	elif a == "Carbohydrate":
		LBL_TBL_Carbohydrate_Value.Value = c
		LBL_TBL_Carbohydrate_Text.Value = b	

sdwarwick
Valued Contributor
Posts: 26
Joined: Fri Jul 20, 2018 6:09 pm

Re: Python: dynamic set variables?

Post by sdwarwick » Wed Aug 01, 2018 4:48 pm

this is a python question

to obtain a variable by a string reference, you use

k = locals()["variablename"]

then k will point to the variable you're interested in

Post Reply