views:

58

answers:

1

Hi,

I have one question, I have such code

this._engine = Python.CreateEngine();
ScriptSource source = this._engine.CreateScriptSourceFromString(script.SourceCode);
ScriptScope scope = this._engine.CreateScope();
ObjectOperations operations = this._engine.Operations;
source.Execute(scope);

And I am trying to execute IronPython code from tutorial:

from System.Collections import BitArray
ba = BitArray(5)
ba.Set(0, True) # call the Set method
print ba[0]

So it executes correctly, but I can't understand if i have print ba[0] why it doesn't output this value to the console? By the way, could you advice some good articles about using IronPython for applications scripting?

And another question is if I have some Python class and I do not know the name of this class, because the client can define it manually, is it possible to create an instance of this class? For example

class SomeClass(object):
    def some(self, a, b):
        return <something>

Now I'm trying to use this code

dynamic SomeClass = scope.GetVariable("SomeClass");
dynamic something = SomeClass();
something.some(x,y);

Should I iterate through ScriptScope.GetItems() collection and search for PythonType objects?

+1  A: 

I think you just need to call:

this._engine.Runtime.IO.RedirectToConsole();

Otherwise have a look here for a similar question about redirecting.

About python class instantiation in C#, your code seems to be ok, as shown in this question (look at the 3rd answer)

EDIT:

Sorry I didn't read your question with enough attention...

Anyway, to look for available classes in Python, just do as you said,

i.e. something like:

var availDynClasses = items.Where(x => x.Value is IronPython.Runtime.Types.PythonType);
digEmAll
edited, sorry :)
digEmAll
Ok, thanks! I don't really understand with instantiation, it works OK when I exactly know what is the class name. But if clients define their own custom class with some custom name, which implements some interface (for example with one method Do()) and I want to instantiate this class and run this Do() method, but I can't use 'scope.GetVariable()', because I don't know the class name.
Jevgenij Nekrasov
Ok, thanks!! :)
Jevgenij Nekrasov