Skip to content

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

Merged
merged 2 commits into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ static void Main(string[] args)

double c = np.cos(5) + sin(5);
Console.WriteLine(c);
/* this block is temporarily disabled due to regression #249

dynamic a = np.array(new List<float> { 1, 2, 3 });
Console.WriteLine(a.dtype);

dynamic b = np.array(new List<float> { 6, 5, 4 }, Py.kw("dtype", np.int32));
Console.WriteLine(b.dtype);

Console.WriteLine(a * b);
*/
Console.ReadKey();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/embed_tests/Python.EmbeddingTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<Compile Include="pyinitialize.cs" />
<Compile Include="pyrunstring.cs" />
<Compile Include="TestCustomMarshal.cs" />
<Compile Include="TestExample.cs" />
<Compile Include="TestPyAnsiString.cs" />
<Compile Include="TestPyFloat.cs" />
<Compile Include="TestPyInt.cs" />
Expand Down
53 changes: 53 additions & 0 deletions src/embed_tests/TestExample.cs
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());
}
}
}
17 changes: 17 additions & 0 deletions src/runtime/converter.cs
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;
Expand Down Expand Up @@ -133,6 +134,22 @@ internal static IntPtr ToPython(object value, Type type)
return result;
}

if (value is IList && value.GetType().IsGenericType)
{
Copy link
Contributor

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 to PyList anyway. IList is the closest interface like you selected.
Other example which are IEnumerable, but not IList besides strings are HashSet, maybe even HashTable.

using (var resultlist = new PyList())
{
foreach (object o in (IEnumerable)value)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@denfromufa are you talking about the IEnumberable here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I meant this place:

if (value is IEnumerable)

Copy link
Contributor Author

@vmuriart vmuriart Mar 12, 2017

Choose a reason for hiding this comment

The 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 pr is trying to do.

The correct thing there is probably to delete that section of code altogether though (or delete typecode.Object). The remaining typecodes are Empty, DBNull, Decimal and DateTime and i don't think any of those should be converted to PyList.
Also, I can't think of a situation in which the default branch of the switch would be hit because we have Typecode.Object defined; It would always be called instead of the default. This example is probably closer to how it should be written.

Either way though, all those changes aren't needed for the purposes of fixing the numpy arrays which is why I didn't include them. I understand where you're coming from though, and thought about doing the change but for the sake of having atomic commits/pull requests I avoided it.

Copy link
Contributor

Choose a reason for hiding this comment

The 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;
Expand Down