Script error when using type() function in project library script

I was having an issue with a value coming back from a data.getRowCount() function. I was trying to insert the value into a string like, ‘The count of parts is %d.’ %(rows), but I was getting the following error:
TypeError: %d format: a number is required, not unicode
I double checked the getRowCount function was spelled correctly and even tried using the %s placeholder for the row count variable. That gave me a “TypeError: not all arguments converted during string formatting” error.
So I tried using the type() function on the row count variable and got this error:
UnboundLocalError: local variable ‘type’ referenced before assignment
Seems to think that type is a variable.
Any idea why this may be happening?
I am using 8.0.16 and even re-installed the software and rebooted with no change in behavior.
One think I have also noticed in all of this is that my DB connection seems to drop out for a few seconds during my testing of this functionality. This was working yesterday, at least to the point of development where I had gotten it to. So I’m perplexed as to what might be causing this (malfunction between chair and keyboard?! :D)

Please post your code (use the “preformatted text” tool when pasting in your code so it is easier to read).

1 Like

Sounds like you’ve accidentally used the name type as a variable somewhere, wiping out its built-in implementation.

1 Like

Your original attempt was close, but the formatting expects a numeric value and you supplied unicode. This example illustrates the same issue but with a str:

>>> x = u"1"
>>> 'Something %d'%x
   Traceback (most recent call last):
     File "<input>", line 1, in <module>
   TypeError: %d format: a number is required, not str
>>> 'Something %d'%float(x)
   'Something 1'

Notice how x was a string value? by converting it to a numeric value instead of a string you can easily insert the value into your string.

I can’t explain the UnboundLocalError: local variable ‘type’ referenced before assignment error without seeing the code, but it does sound like you’re encountering what @pturmel suggested, in that you’ve used type incorrectly.

The code that caused the original error (TypeError: %d format: a number is required, not unicode) is below.

	params = {'aid': aid}
	#check if AID is Spark aligner or Spark template
	IDcheck = system.db.runNamedQuery('Print_IDcheck', params)
	chkCnt = IDcheck.getRowCount()
	util.log(ln, 'Records returned for Aligner "%s" is %d.' %(aid, chkCnt))

the use of the %d and the value of the getRowCount() method of the dataset, I would have thought, should have worked. That is when my other faux pas (which Phil as accurately pointed out), the fact that I had used “type” as a variable name, showed it’s unpleasant face! I’m still not sure why the original error occurred, but after removing my debug code, I have not seen it again. BTW, the IDcheck dataset should have 1 row max with 3 columns.

I am renaming my type variable to a non-system name to prevent this in the future.

Thanks!