[IronPython] Two questions regarding exposing an API.

TraumaPony pontheangelofdeath at gmail.com
Tue Jul 8 04:40:55 PDT 2008


Hi, I have two questions regarding the IronPython 2.0 B3.

1. I'm trying to expose only a few methods from my assembly, but I haven't
the faintest idea how to do it. The best I can come up with is the
following:

import clr
clr.AddReference("CoolEngine")
def randomIntNoParams():
    from CoolEngine.Engine.Scripting import ScriptManager
    return CoolEngine.Engine.Scripting.ScriptManager.RandomInt()
def randomIntOneParam(i):
    from CoolEngine.Engine.Scripting import ScriptManager
    return CoolEngine.Engine.Scripting.ScriptManager.RandomInt(i)
def randomIntTwoParams(i, j):
    from CoolEngine.Engine.Scripting import ScriptManager
    return CoolEngine.Engine.Scripting.ScriptManager.RandomInt(i, j)
def randomIntThreeParams(i, j, k):
    from CoolEngine.Engine.Scripting import ScriptManager
    return CoolEngine.Engine.Scripting.ScriptManager.RandomInt(i, j, k)
def randomIntThreeParamsNoReturn(i, j, k):
    from CoolEngine.Engine.Scripting import ScriptManager
    CoolEngine.Engine.Scripting.ScriptManager.RandomIntNoReturn(i, j, k)

But alas, it says "name 'CoolEngine' is not defined" . What am I doing wrong
there?

2. I'm trying to create a module dynamically at runtime. I have the
following code which I want to be placed in a module called "Script":

import clr
clr.AddReference("System.Threading")
def Pause(milliseconds):
    from System.Threading.Tasks import Task
    Task.CurrentTask.Wait(milliseconds)

However, I have no idea how to get it into its own module from there, and
including it in its own file called Script.py is out of the question; I have
that code as a string resource in my assembly.

I'm doing the following:
*

Engine.CreateScriptSourceFromString(

"import imp\n"+

"import sys\n"+

"newModule = imp.new_module(moduleName)\n"+

"exec(moduleSource, newModule.__dict__)\n" +

"sys.modules[moduleName] = newModule", *SourceCodeKind*
.Statements).Execute(scope);
*

where the moduleName variable is "Script" and the moduleSource is the above
source code. However, I get the following message:

An unhandled exception of type 'Microsoft.Scripting.SyntaxErrorException'
occurred in Unknown Module.

Additional information: unexpected token '\r'

Any ideas?



-- 
/"\
\ / ASCII RIBBON CAMPAIGN
X AGAINST HTML MAIL
/ \

This message has been encrypted with ROT-26. Any unauthorised decryption
will result in prosecution to the full extent of the law.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ironpython.com/pipermail/users-ironpython.com/attachments/20080708/3e6bbc07/attachment.htm>


More information about the Users mailing list