Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vmuriart merged 2 commits intopythonnet:masterfromvmuriart:fix_readme
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsREADME.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/embed_tests/Python.EmbeddingTest.csproj
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/embed_tests/TestExample.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/runtime/converter.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 toPyList.

471673a

ButIEnumerable 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.

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

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 theIEnumberable 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(valueisIEnumerable)

Copy link
ContributorAuthor

@vmuriartvmuriartMar 12, 2017
edited
Loading

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

The correct thing there is probably to delete that section of code altogether though (or deletetypecode.Object). The remaining typecodes areEmpty,DBNull,Decimal andDateTime and i don't think any of those should be converted toPyList.
Also, I can't think of a situation in which thedefault branch of the switch would be hit because we haveTypecode.Object defined; It would always be called instead of thedefault. Thisexample is probably closer to how it should be written.

Either way though, all those changes aren't needed for the purposes of fixing thenumpy 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

[8]ページ先頭

©2009-2025 Movatter.jp