I am trying to set a local variable in a python library script function with this syntax:
tareWeight = session.custom.intTare
I get this error:
excError: (<type ‘exceptions.NameError’>, NameError(“global name ‘session’ is not defined”,), <traceback object at 0x20e>)
tareWeight = self.session.custom.intTare
I get this error:
excError: (<type ‘exceptions.NameError’>, NameError(“global name ‘self’ is not defined”,), <traceback object at 0x212>)
Do I need to pass the session id in as a parameter to the function? Does anybody know how to reference a session custom property in a library script?
Your function doesn’t have a session to operate on unless you pass one in as a parameter. And then any callers will need to actually pass it in, so callers will have to be originating from somewhere in perspective land where you have access to the current session.
# old code
# calculations.fctBuildBc_Label_Inventory(system.tag.read('[default]scales/strProductID').value, system.tag.read('[default]scales/strLotNumber').value)
# I would like to pass the session in here:
# something like :
# new code
logger.info('session.id: ' + str(session.id))
calculations.fctBuildBc_Label_Inventory(system.tag.read('[default]scales/strProductID').value, system.tag.read('[default]scales/strLotNumber').value, session.id)
and then pull it into function like so:
def fctBuildBc_Label_Inventory(strProductID, strLotNumber, sessionID):
if I use the system.perspective.getSessionInfo(id) to try and get the session id i get an empty list:
calculations.fctBuildBc_Label_Inventory(system.tag.read(’[default]scales/strProductID’).value, system.tag.read(’[default]scales/strLotNumber’).value, system.perspective.getSessionInfo(id))
shows: sessionID:
getSessionInfo() only expects up to two arguments, and neither of those is a session id (see Docs). Your button code should be called in the following manner:
# note that you need to reference session as an attribute of the current component by using "self"
logger.info('session.id: ' + str(self.session.id))
calculations.fctBuildBc_Label_Inventory(system.tag.read('[default]scales/strProductID').value, system.tag.read('[default]scales/strLotNumber').value, self.session)
Then, your custom function can directly reference the session object:
You're getting an empty list because the function is interpreting the passed id arg as the username to filter on; since there are no users with a username which matches the session's id you are getting no results.
Not having any luck capturing the session to pass to a function in a python library, trying to use the:
logger.info('''self.sesion.info.id : ''' + str(self.sesion.info.id))
returns nothing, in fact in the code in a button component on a perspective view, it does not show anything and does not throw and error.
any suggestions?
Or to read a session variable in side a function in a python library: Preformatted text
strBarcode = strBarcode + str(sID.session.custom.strUPCCode) Preformatted text