Skip to main content
Question

Cannot get PythonCreator script to expose attribute.


amc
Contributor
Forum|alt.badge.img+1
  • Contributor

Hi there,

I am running below script succesfully in the PYTHON Creator but i can't get it to expose a created value into and attrribute. the script creates a 3 digit number in ICP_GEN_ICP based on a parameter I provide for CheckICP, but it won't get exposed. i need to know how I can assign this value to an attribute and use it in an XML templater. thanks!

ps i am new to coding ( both FME and Python)

 

 

script:

D = int('0x180f', 0)

H100 = int('0x100', 0)

H1000 = int('0x1000', 0)

HFFFF = int('0xffff', 0)

 

def Asc(bytes_obj):

  '''Get the int value of a char'''

  if type(bytes_obj) == bytes:

    return bytes_obj[0]

  else:

    return ord(bytes_obj[0])

 

def Mid(string, integer, integer2):

  '''Select a substring from a string'''

  integer = integer - 1

  return string[integer : integer+integer2]

 

def gen_crc(icp_number):

  """ Generate a checksum for an ICP"""

  R = int('0', 0)   

  CheckICP = FME_MacroValues['icp_number']

  ICP_GEN_CRC = ""

  IChar = 0

  if len(CheckICP.strip()) < 12:

    ICP_GEN_CRC = "Insufficient Data"

  else:   

    CheckICP = CheckICP[:12] + "\\0\\0\\0"   

    #print('CheckICP: ' + str(CheckICP))

    for I in range(0,108):

      #print(I)

      if I%8 == 0:

        IChar = Asc(Mid(CheckICP, int((I/8)+1), 1))

        #print('IChar: ' + str(IChar))

      IChar = IChar * 2

      #print('IChar: ' + str(IChar))

      R = R * 2

      #print('R: ' + str(R))

      if int((IChar & H100) / 256.0) == 1:       

        R = R + 1

      if (R & H1000) > 0:

        R = R ^ D

    R = R & HFFFF

    #print('R: ' + str(R))

    CheckICP = ("000" + format(R,'x'))[-3:]

    #print('CheckICP: ' + str(CheckICP))

    ICP_GEN_CRC = CheckICP.upper()

    # below ICP_GEN_CRC should be put in an attribute that i can use in an XML-creator. 

    print('ICP_GEN_CRC: ' + str(ICP_GEN_CRC))

  return ICP_GEN_CRC

 

i expose ICP_GEN_CRC as output attribute , but it returns empty.

2 replies

virtualcitymatt
Celebrity
Forum|alt.badge.img+38

Hmm, Perhaps you want to use a Python Scripted Parameter instead? https://community.safe.com/s/article/python-scripted-parameters

 

Otherwise be sure you are creating FMEFeatures and setting the attribute value on a feature (http://docs.safe.com/fme/html/fmepython/api/fmeobjects/_feature/fmeobjects.FMEFeature.html).


david_r
Celebrity
  • November 2, 2020

If this is from a PythonCreator, then you're missing the part where you create the FME Feature and set the attribute 'ICP_GEN_CRC' accordingly. Simply doing this is not enough:

return ICP_GEN_CRC

Is this the entire code, of have you skipped some of it?


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings