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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of TraumaPony
Sent: 2008年7月8日 19:41
To: [email protected]
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.
_______________________________________________
Users mailing list
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com