-
Notifications
You must be signed in to change notification settings - Fork 748
Fix numpy array and README example #427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using NUnit.Framework; | ||
using Python.Runtime; | ||
|
||
namespace Python.EmbeddingTest | ||
{ | ||
public class TestExample | ||
{ | ||
[OneTimeSetUp] | ||
public void SetUp() | ||
{ | ||
PythonEngine.Initialize(); | ||
} | ||
|
||
[OneTimeTearDown] | ||
public void Dispose() | ||
{ | ||
PythonEngine.Shutdown(); | ||
} | ||
|
||
[Test] | ||
public void TestReadme() | ||
{ | ||
dynamic np; | ||
try | ||
{ | ||
np = Py.Import("numpy"); | ||
} | ||
catch (PythonException) | ||
{ | ||
Assert.Inconclusive("Numpy or dependency not installed"); | ||
return; | ||
} | ||
|
||
Assert.AreEqual("1.0", np.cos(np.pi * 2).ToString()); | ||
|
||
dynamic sin = np.sin; | ||
StringAssert.StartsWith("-0.95892", sin(5).ToString()); | ||
|
||
double c = np.cos(5) + sin(5); | ||
Assert.AreEqual(-0.675262, c, 0.01); | ||
|
||
dynamic a = np.array(new List<float> { 1, 2, 3 }); | ||
Assert.AreEqual("float64", a.dtype.ToString()); | ||
|
||
dynamic b = np.array(new List<float> { 6, 5, 4 }, Py.kw("dtype", np.int32)); | ||
Assert.AreEqual("int32", b.dtype.ToString()); | ||
|
||
Assert.AreEqual("[ 6. 10. 12.]", (a * b).ToString()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,5 +1,6 @@ | ||||
using System; | ||||
using System.Collections; | ||||
using System.Collections.Generic; | ||||
using System.Globalization; | ||||
using System.Reflection; | ||||
using System.Runtime.InteropServices; | ||||
|
@@ -133,6 +134,22 @@ internal static IntPtr ToPython(object value, Type type) | |||
return result; | ||||
} | ||||
|
||||
if (value is IList && value.GetType().IsGenericType) | ||||
{ | ||||
using (var resultlist = new PyList()) | ||||
{ | ||||
foreach (object o in (IEnumerable)value) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @denfromufa are you talking about the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I meant this place: pythonnet/src/runtime/converter.cs Line 208 in 423407a
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I left that alone on purpose as it wasn't necessary to modify for what this The correct thing there is probably to delete that section of code altogether though (or delete Either way though, all those changes aren't needed for the purposes of fixing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, I opened a separate issue for this dead code: #428 |
||||
{ | ||||
using (var p = new PyObject(ToPython(o, o?.GetType()))) | ||||
{ | ||||
resultlist.Append(p); | ||||
} | ||||
} | ||||
Runtime.XIncref(resultlist.Handle); | ||||
return resultlist.Handle; | ||||
} | ||||
} | ||||
|
||||
// it the type is a python subclass of a managed type then return the | ||||
// underlying python object rather than construct a new wrapper object. | ||||
var pyderived = value as IPythonDerivedType; | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be completely consistent with the converter code the IEnumerable should be replaced with IList like you did this pull request.
The commit message mentioned that strings should not be converted to
PyList
.471673a
But
IEnumerable
is not quite right when converting toPyList
anyway.IList
is the closest interface like you selected.Other example which are IEnumerable, but not IList besides strings are HashSet, maybe even HashTable.