-
Notifications
You must be signed in to change notification settings - Fork 752
Using F# (FSHARP) successfully #112
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
Comments
This seems a better fit for pythonnet mailing list or a wiki page, since I do not see any issue, is there? I also copied your answer here: http://stackoverflow.com/a/34207705/2230844 Folks from Deedle that is written in F# use pythonnet extensively. |
Not issue in the sense of bug but perhaps in the broader sense https://guides.github.com/features/issues/ I'm not sure if there is an appropriate label that could be applied to this issue. It is not an enhancement per se but might be useful in future planning as another case study in dynamic. Thanks for the Deedle/pythonnet connection |
The main problem I have with F# is not being able to use the standard arithmetic operators. I resorted to this for the time being but needless to say it's not an optimal solution:
The only other solution I can think of at this moment is forking pythonnet to define the respective static methods (e.g. op_Addition) in PyObject (so they become accessible from F#) and maybe using a custom ? operator that always returns a PyObject (as opposed to a raw obj) |
Another way would be to submit a pull request to pythonnet with f# specific BTW, why F# uses special arithmetic operators? On Friday, March 18, 2016, Steven notifications@github.com wrote:
|
It's actually the other way around: DynamicObject has direct support from the C# compiler, so the dot operator gets mapped to DynamicObject.TryInvokeMember/TryGetMember/etc and the arithmetic operators get mapped to DynamicObject.TryBinaryOperation/TryUnaryOperation/etc |
@matthid did you encounter the issues above in your F# code with pythonnet? If yes, how did you workaround this? |
I think @royalstream has said just about everything. There is no particular issue either on this (pythonnet) or the F# side. There is no (and probably wont be) dynamic support by the F# compiler. I like the suggestion to add the operators at PyObject (eg op_addition). |
I should note here that I immediately typed/casted/converted all results to static types. Therefore I did never notice this issue. https://github.com/matthid/googleMusicAllAccess/blob/develop/src/GMusicApi/GMusicApi.fs#L176 https://github.com/matthid/googleMusicAllAccess/blob/develop/src/GMusicApi/PythonInterop.fs#L742 And I build a computation expression to not forget to gather and release the GIL: https://github.com/matthid/googleMusicAllAccess/blob/develop/src/GMusicApi/PythonInterop.fs#L20 More details: https://yaaf.de/blog/post/2016-05-28/Having%20Fun%20with%20Computation%20Expressions |
@matthid thank you for the feedback, I included a link to your blog post in wiki: https://github.com/pythonnet/pythonnet/wiki BTW, congratulations on being featured in Microsoft weekly news! https://blogs.msdn.microsoft.com/dotnet/2016/05/31/the-week-in-net-5312016/ |
@aolney I found your blog post as well (Using NLTK from F#) and linked it in wiki. |
It would make sense to track this with an F# language suggestion. But the techniques you are using today make sense. |
FSharp.Interop.Dynamic has operators now for dynamic objects. Version 4.0 (current beta package on nuget. )
|
some F# related issues: #574 |
I've used F# to do some of the nice dynamic interop with pythonnet rather than the c# dynamic keyword approach. Not sure if it is truly helpful over c# dynamic, but it may be a bit more concise and interesting.
Here is some code that shows the functionality. The key is to use fsprojects/FSharp.Interop.Dynamic@318f50b (actually whatever is currently available in nuget). This helped me get closer to the goal behavior of fsprojects/FSharp.Interop.PythonProvider@f3cdabf but of course without the type provider mojo (meaning user perceived behavior rather than implementation behavior). I used pythonnet from tonyroberts/pythonnet@21270eb , CPython3.4, the matching NLTK distribution, VS2015, Windows 10.
As described below the most significant complaint is that you can't use F# primitive operators like (+) on numpy arrays without an error. The workaround is to use the corresponding numpy function as demonstrated.
Also if a complicated call is made like
nltk?corpus?brown?tagged_words(Py.kw("categories", "news") )
the debugger seems to time out the expression evaluation (requiring print statements). Though in general the expression evaluation on the dynamic types returned is not very helpful, so this isn't a major drawback.The text was updated successfully, but these errors were encountered: