How to call type(string, bases, dict) instead of type( [PSMRL], text, [modifiers] )

Asked by Mark

The compiler is complaining that my second argument cannot be coerced to a string. How can I tell the compiler to call the other type method?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Are you talking about scripting with the Sikuli IDE (Python/Jython) or about programming in Java?

--- type(string, bases, dict)
What should that be.

--- second argument cannot be coerced to a string
paste your coding that fails

Revision history for this message
Mark (sp4m1) said :
#2

I'm talking about Jython (I'm using Sikuli t script in Jython) and it appears that the static, three parameter, type() function (from Python) has been overridden to work with matches and "type" keyboard characters.

The function I want to call is documented here: http://www.jython.org/docs/library/functions.html

However, when I try to call it, Jython emits the following:

    return type('Enum', tuple([object]), enumDict)
TypeError: type(): 2nd arg can't be coerced to String

This tells me that it is trying to dispatch to the type() function documented here: http://sikuli.org/wiki/reference-0.10#type

here's my code:

def enum(*sequential, **named):
    enumDict = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', tuple([object]), enumDict)

states = enum('ONE', 'TWO', 'THREE')

I'm new to Jython, so apologies in advance if some of this makes no sense. To summarize, how can I call the Jython type function of my choice?

TIA RaiMan

Revision history for this message
Best RaiMan (raimund-hocke) said :
#3

For being new to Jython you are doing rather cool stuff ;-)

--- I'm using Sikuli t script in Jython
Not really clear what that means, but I suppose, you are making Jython scripts, that use the features of Sikuli.

In dead, the Jython type command is hidden, since using "from sikuli import *" makes all methods of Screen (and the inherited Region) global. one of these is the Region.type().

This is a long known bug (bug 906343).

There are shown workarounds, but in your case (needs the dynamic class type()), the only easy way is to use the "import __builtin__" solution.

If you do not need to run Sikuli scripts (.sikuli) from within your Jython scripts using Sikuli's script runner, you might modify or comment out the initSikuli stuff in the Sikuli.py (unjar and rejar sikuli-script.jar). If you do this, this means , you have to qualify all Screen and Region methods like it has to be done, when using Java programming with Sikuli.

Revision history for this message
Mark (sp4m1) said :
#4

Thanks RaiMan, that solved my question.

Revision history for this message
Mark (sp4m1) said :
#5

Here's the working code for anyone who's interested

def enum(*sequential, **named):
 enumDict = dict(zip(sequential, range(len(sequential))), **named)
 module = __import__('__builtin__')
 value = module.__dict__['type']('Enum', tuple([object]), enumDict)
 return value

states = enum('ONE', 'TWO', 'THREE')