Reading a session variable from a python scipt

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?image

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.

Cool, Any idea the syntax for the function call from a component. In this case it is a btn on a container on a view in Perspective land?

btn code:

	# 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:

Does not like sesson.props.id either.
Been looking thru the user manual, just not finding the correct syntax.

logger.info('session.props.id: ’ + str(session.props.id))

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:

def fctBuildBc_Label_Inventory(strProductID, strLotNumber, session):
    tareWeight = session.custom.intTare

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?

Tried a few ways to get the session…
i.e.

logger.info('system.perspective.getSessionInfo(id): ’ + str(system.perspective.getSessionInfo(id)))

logger.info('session.props.id: ’ + str(session.props.id))

logger.info('session.pageId: ’ + str(session.pageId))

logger.info('session: ’ + str(session))

logger.info('self.session.info(id): ’ + str(self.session.info(id)))

logger.info(’’‘self.session.info(‘id’): ‘’’ + str(self.session.info(‘id’)))

			logger.info('Hello Line 43: ')
			logger.info('''self.sesion.info.id : ''' + str(self.sesion.info.id))

@Greg.Bogard I moved you other post into this thread because both posts are a result of the same incorrect function usage.

cmallonee, Kevin.Herron, Kevin McCluskey,

Thank you very much for the help with the syntax of sending a session object into a python script library from a Perspective component on a view.

This is how I solved it:

call to python library function from perspective component on a view - send in self.

Preformatted text
calculations.fctBuildBc_Label_Inventory(self)
Preformatted text

in the function in side of python library. Define the input parameter sID, reference the session variables with sID.session.custom.Variable name:

i.e.
Preformatted text
def fctBuildBc_Label_Inventory(sID):
Preformatted text

`Preformatted text

sID.session.custom.strSerialNumber = calluspGetNewSerialNumber.getOutParamValue("strSerialNumber") Preformatted text`

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

Pssst, Greg! Use the code formatting button for pasted code, log files, and other plain text. This one: </>

Will Do, Thanks for the tip!