Skip to content

Instantly share code, notes, and snippets.

@ironpythonbot
Created December 9, 2014 17:59
Show Gist options
  • Save ironpythonbot/f95d5d9d840e2f8061c2 to your computer and use it in GitHub Desktop.
Save ironpythonbot/f95d5d9d840e2f8061c2 to your computer and use it in GitHub Desktop.
CodePlex Issue #32393 Plain Text Attachments
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using IronPython;
using IronPython.Hosting;
using IronPython.Modules;
using IronPython.Runtime;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
namespace PythonTest
{
[Activity (Label = "PythonTest", MainLauncher = true)]
public class Activity1 : Activity
{
private ScriptEngine engine;
private ScriptScope scope;
int count = 1;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
/*engine = Python.CreateEngine();
scope = engine.CreateScope();
object result = null;
try
{
ScriptSource source = engine.CreateScriptSourceFromString(@"a=5");
result = source.Execute(scope);
}
catch(Exception ex)
{
result = ex;
}*/
//String result2 = result.ToString();
/*ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromFile("Calculator.py");
ScriptScope scope = engine.CreateScope();
ObjectOperations op = engine.Operations;
//source.Execute(scope); // class object created
object klaz = scope.GetVariable("Calculator"); // get the class object
object instance = op.Invoke(klaz); // create the instance
object method = op.GetMember(instance, "add"); // get a method
int result = (int)op.Invoke(method, 4, 5); // call method and get result (9)*/
ScriptEngine pyEngine = Python.CreateEngine();
ScriptScope pyScope = pyEngine.CreateScope();
/*string code = @"print 'test = '
class MyClass:
def __init__(self):
pass
def somemethod(self):
print 'in some method'
def isodd(self, n):
return 1 == n % 2
";*/
String code = @"
import clr
";
ScriptSource source = pyEngine.CreateScriptSourceFromString(code);
CompiledCode compiled = source.Compile();
compiled.Execute(pyScope);
// Get the Python Class
object MyClass = pyEngine.Operations.Invoke(pyScope.GetVariable("MyClass"));
// Invoke a method of the class
pyEngine.Operations.InvokeMember(MyClass, "somemethod", new object[0]);
// create a callable function to 'somemethod'
Action SomeMethod2 = pyEngine.Operations.GetMember<Action>(MyClass, "somemethod");
SomeMethod2();
Func<int, bool> IsOdd = pyEngine.Operations.GetMember<Func<int, bool>>(MyClass, "isodd");
String result = IsOdd(2).ToString ();
var aLabel = new TextView (this);
aLabel.Text = result.ToString();
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button> (Resource.Id.myButton);
button.Click += delegate {
button.Text = string.Format (result, count++); };
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment