[IronPython] Treat struct as tuple

Dino Viehland dinov at microsoft.com
Mon Nov 30 19:35:31 CET 2009


You could do something like the code below.  This is an example of an extension method on DateTime which enables indexing for read-only access to a couple of attributes.  You could also define methods like __iter__ so the iteration will work (although it should work w/ just __getitem__), __repr__ so that print will work more reasonable, and you should be able to do define "PythonTuple ConvertToPythonTuple(Point3D point)" w/ [SpecialName, ImplicitConversionMethod] attributes to get the ability to pass to something which expects a tuple.  The last one may not work as consistently as one would like though.

using System;
using IronPython.Hosting;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using IronPython.Runtime.Exceptions;
using System.Runtime.CompilerServices;
using Microsoft.Scripting.Runtime;

[assembly: ExtensionType(typeof(DateTime), typeof(DateTimeExtensions))]
public class DateTimeExtensions {
    [SpecialName, ImplicitConversionMethodAttribute]
    public static object __getitem__(DateTime dt, int index) {
        switch(index) {
           case 0:
            return dt.Hour;
           case 1:
            return dt.Second;
           default:
             throw new NotImplementedException();           
        }
    }
}

class Foo {

private static ScriptEngine pe;
private static ScriptScope scope;

public static void Main(string[] args){    
    
	pe = Python.CreateEngine();    
	pe.Runtime.LoadAssembly(typeof(DateTimeExtensions).Assembly);
	
	scope = pe.CreateScope();    
	scope.SetVariable("test", DateTime.Now);
	pe.Execute(@"print test[0]", scope);

}

	
}

> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-
> bounces at lists.ironpython.com] On Behalf Of Steve Baer
> Sent: Monday, November 30, 2009 10:08 AM
> To: Users at lists.ironpython.com
> Subject: [IronPython] Treat struct as tuple
> 
> Say you have a .NET struct for something like a 3D point
> public struct Point3d
> {
>   private double m_x, m_y, m_z;
>   public double X{ get {return m_x;} set {m_x = value;} }
>   public double Y{ get {return m_y;} set {m_y = value;} }
>   public double Z{ get {return m_z;} set {m_z = value;} }
>   // more functions...
> }
> 
> We have a user that wants to treat this struct like a tuple of 3 values in
> IronPython so he can do things like
> pt = Point3d(1,2,3)
> for a in pt:
>   print a
> -or-
> pass pt to a function that expects a tuple.
> 
> I recommended that the user write a function to coerce the struct to a tuple
> def coerceTuple( pt ):
>   return [pt.X, pt.Y, pt.Z]
> 
> The user said this works, but would like to have something a bit cleaner. Do
> any of you guys suggestions about what I could do to make our Point3d struct
> more "python friendly"?
> 
> Thanks,
> -Steve
> 
> Steve Baer
> Robert McNeel & Associates
> www.rhino3d.com
> www.mcneel.com
> 
> 
> 
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



More information about the Ironpython-users mailing list