Closed den-run-ai closed 3 years ago
This function is similar to the function I proposed here #484. These two functions can be integrated into single one,“How to convert between python objects and CLR objects dynamically?”
Based on my code before, I just wrote a new PyConverter here. I can make a PR if this design is accepted.
It can be used in these ways,
For this issue from stackoverflow, we can use the PyConverter in this way,
First [define a PyConverter]() according to needs,
public PyConverter NewMyDictConverter()
{
using (Py.GIL())
{
var converter = new PyConverter();
converter.AddListType();
converter.Add(new StringType());
converter.Add(new Int64Type());
converter.Add(new Int32Type());
converter.Add(new FloatType());
converter.Add(new DoubleType());
converter.AddDictType<string, object>();
return converter;
}
}
Then, we can use this converter to convert a Clr Dictionary into a Python Dictionary, and vice versa.
using (Py.GIL())
{
//create a instance of PyConverter
var converter = NewMyDictConverter();
var scope = Py.CreateScope();
scope.Exec(
"a={'0': 1, '1': [1, \"2\"]}"
);
dynamic a = scope.Get("a"); // mock a python dictionary
var b = converter.ToClr(a); //Convert python dict to Clr Dictionary<string, object>
var c = converter.ToPython(b); // Convert Clr Dictionary to python dict
object c0 = c["0"];
object c1 = c["1"];
var d = converter.ToClr(c);
object d0 = d["0"];
object d1 = d["1"];
scope.Dispose();
}
@yagweb when you mention conversion happens "dynamically", do you mean the conversion happens at runtime or even later when the corresponding value/field is accessed?
@denfromufa "dynamically" here means the conversion happens at runtime.
It‘s opposite to "static": we know the type of the value/field in coding phase, so we directly use the PyInt etc. in the code.
There are three drawbacks of this hard coding method,
The work steps of PyConverter are,
@yagweb i have not tested your converter API yet, but this looks promising. regarding preparing a PR, there is too much activity currently. i would wait until we stabilize .net core support in few weeks.
Have you tested this yet? It looks quite promising and I would like to use it. Would this be licensed under any terms?
@kostjaaa feel free to submit a PR, but make a summary of what you would like to achieve first.
I had a similar problem but solved it using Python code, which works with the current release See the netdict example.
This should now be fixed in 3.0
This should now be fixed in 3.0
When working with a typing.Dict[str, str]
and Dictionary<string, string>
I still get
TypeError: 'dict' value cannot be converted to System.Collections.Generic.Dictionary`2[System.String,System.String]
on Python.NET 3.0.3
Looking at the source code it seems there is a PyDict wrapper, but it is not used in any of the Converter logic. Are there any plans to add built-in conversion support for dictionaries?
If it is supposed to work, an example show-casing the correct way to assign a python dictionary to an managed property accepting Dictionary<TKey, TValue> or IDictionary<TKey, TValue> or IReadOnlyDictionary<TKey, TValue> would be highly appreciated.
If only Python.NET was more "user-friendly"... don't get me wrong, it is doing an awesome job with most things, there are just some rough edges and unfortunately I already spent countless hours figuring out these things without much success...
Thanks!
I would also be interested in an example.
This should now be fixed in 3.0
Any details on how this was fixed in 3.0? How are we meant to take a dictionary from python and convert it into a C# Dictionary<T,T>
?
https://stackoverflow.com/questions/48689604/calling-into-python-code-using-pythonnet-from-c-sharp-and-custom-c-sharp-classes