[IronPython] Two questions regarding exposing an API.

Han Kejing Colin.Han at grapecity.com
Wed Jul 9 03:24:48 CEST 2008


About first question, I think it should be write as follows. Because you just input ScriptManager symbol to current scope. You had not import CoolEngine into your current scope.

 

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

 

 

----------------------------------------------------------------

Follow my heart

Colin Han @ MultiRow Developer Team (A Strong Team)

 

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of TraumaPony
Sent: 2008年7月8日 19:41
To: users at lists.ironpython.com
Subject: [IronPython] Two questions regarding exposing an API.

 

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://mail.python.org/pipermail/ironpython-users/attachments/20080709/9ef91372/attachment.html>


More information about the Ironpython-users mailing list