Equivalent code in IronPython from C# code

Hi @stevebaer @brian @piac

I am developing a .NET application that requires the use of Iron Python to read an HDF5 file format and I have found a piece of code written in C# that solve my problem.
I am using the HDF5-CSharp library HDF5-CSharp Repo but I struggling to translate the code for Iron Python.

C# CODE

public class Coordinate
{
       [Hdf5EntryName("COORDINATES")] public double[,] COORDINATES { get; set; }
}

string filename = @"recorder.hdf5"; \\full path
long fileId = -1;
try
{
        fileId = Hdf5.OpenFile(filename, true);
        var result = Hdf5.ReadObject<Coordinate>(fileId, "/MODEL_STAGE[1]/MODEL/NODES");
}
 finally
 {
        if (fileId > 0)
           {
             Hdf5.CloseFile(fileId);
            }
 }

IronPython CODE (it doesn’t work)

  1. Solution exception:Property set method not found.
import clr
clr.AddReferenceToFileAndPath(r"C:\Users\FORMAT\Desktop\hdf5Test\HDF5CSharp.dll")

from System.Collections import ArrayList
from HDF5CSharp import Hdf5

filename = r"C:\Users\FORMAT\Desktop\hdf5Test\recorder.hdf5"

fileId = Hdf5.OpenFile(filename, True)


print("Group exist: {}".format( Hdf5.GroupExists(fileId, "//MODEL_STAGE[1]//MODEL//NODES")))


result = Hdf5.ReadObject[ArrayList](fileId, "//MODEL_STAGE[1]//MODEL//NODES//COORDINATES")

I attached a “recorder.hdf5” and .dll in case you want to try to help.

Download: hdf5 and dll

Do you get a line number on where the error occurs?

It seems the library relies on the reflection structure of customized classes. I don’t believe it’s possible with IronPython.

ArrayList isn’t bound to a HDF type and you’ll need a customized type.

I have done some research around and I have (almost) made it working.
The actual problem is that I can not run the script twice and I am struggling to find a solution.
I have notice that I can just change the Class Name to run it one more time.

Error


Traceback:
  line 537, in __clrtype__, "C:\Program Files\Rhino 6\Plug-ins\IronPython\Lib\clrtype.py"
  line 15, in script

A possible work around is to create a different class name everytime that the script run but there should be a better way :frowning:

IronPython Code that I have tried

import System

import clr
import clrtype

clr.AddReferenceToFileAndPath(r"C:\Users\FORMAT\Desktop\hdf5Test\HDF5CSharp.dll")

from HDF5CSharp import Hdf5

type_2d = type(System.Array.CreateInstance(float, 1, 1))



class Coord(object):
    __metaclass__ = clrtype.ClrClass

    @property
    @clrtype.accepts()
    @clrtype.returns(type_2d)
    def COORDINATES(self): return self.__coords
    
    @COORDINATES.setter
    @clrtype.accepts(type_2d)
    @clrtype.returns()
    def COORDINATES(self, value): self.__coords = value    


a = []
b= []
c = []


fileId = 0

fileId = Hdf5.OpenFile(r"C:\Users\FORMAT\Desktop\hdf5Test\recorder.hdf5", True)
print(fileId)

result = Hdf5.ReadObject[Coord](fileId, "/MODEL_STAGE[1]/MODEL/NODES")
try:
    for i in range(1000):
        a.append( result.COORDINATES[i,0])
        b.append( result.COORDINATES[i,1])
        c.append( result.COORDINATES[i,2])
    Hdf5.CloseFile(fileId)
except:
    pass

Gif showing the process and error

1 Like