From denis.akhiyarov at gmail.com Wed Sep 7 09:50:44 2016 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Wed, 7 Sep 2016 08:50:44 -0500 Subject: [Python.NET] pythonnet appdomain(s) In-Reply-To: <60CDCA83B420BB48BF95B8AF66CE7FFF45A36D89@NYMBX02.bcna.corp> References: <60CDCA83B420BB48BF95B8AF66CE7FFF45A36D89@NYMBX02.bcna.corp> Message-ID: Hi Adam, Did you manage to setup a separate AddDomain for pythonnet? If yes, then there is an open issue on github where you can comment with your approach. https://github.com/pythonnet/pythonnet/issues/213 Thanks, Denis On Fri, Nov 14, 2014 at 1:16 PM, Adam Klein wrote: > Hello, > > > > Is there a design reason that pythonnet loads C# assemblies into its own > AppDomain rather than establishing a separate AppDomain? It would be useful > for instance when developing in C# in parallel with writing python code > (say, in the ipython REPL) to be able to reload C# assemblies dynamically. > > > > Also, we?ve found it useful in Initialize() to enable shadow copying, to > prevent file locking, ie > > > > internal static void Initialize() { > > ... > > > > AppDomain domain = AppDomain.CurrentDomain; > > > > // prevent file locking > > domain.SetShadowCopyFiles(); > > domain.SetShadowCopyPath(null); > > > > Although these are deprecated methods, and really it should be > establishing a new AppDomain as above. > > > > Regards, > > Adam > > > > _________________________________ > > Adam D Klein > > BlueMountain Capital Management LLC > > 280 Park Ave, 5th Floor East > > New York, NY 10017 > > O:(212)905-2136 > > C:(917)331-8871 > > > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.akhiyarov at gmail.com Thu Sep 8 00:54:23 2016 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Wed, 7 Sep 2016 23:54:23 -0500 Subject: [Python.NET] cvs -> svn -> git history of Python for .NET Message-ID: It happened to me multiple times that the author of particular piece of code was needed to be known. But in the latest pythonnet repository on github we have seem to lost some of this history when transitioning from sourceforge. Are there any git experts, who know how to rebuild the history to include some missing commits? Here is a quick example. Based on a bug report [1] I'm looking at lost history [2] of this C# file in the master branch [3]: [1] https://github.com/pythonnet/pythonnet/issues/262 [2] https://github.com/pythonnet/pythonnet-rawsvnmig/blob/master/pythonnet/src/embed_tests/pyimport.cs [3] https://github.com/pythonnet/pythonnet/blob/master/src/embed_tests/pyimport.cs -------------- next part -------------- An HTML attachment was scrubbed... URL: From tony at pyxll.com Thu Sep 8 12:03:58 2016 From: tony at pyxll.com (Tony Roberts) Date: Thu, 08 Sep 2016 16:03:58 +0000 Subject: [Python.NET] cvs -> svn -> git history of Python for .NET In-Reply-To: References: Message-ID: Some files were moved some time after being imported from svn. You need to use --follow to follow renames/moves, i.e: git log --follow src/embed_tests/pyimport.cs All commits from svn were imported into git (and are still there). The one you mention is here: https://github.com/pythonnet/pythonnet/commit/217bb4847d3c181b8251e84d7eab26dba1341655 On Thu, Sep 8, 2016 at 4:18 PM Denis Akhiyarov wrote: > It happened to me multiple times that the author of particular piece of > code was needed to be known. > > But in the latest pythonnet repository on github we have seem to lost some > of this history when transitioning from sourceforge. > > Are there any git experts, who know how to rebuild the history to include > some missing commits? > > Here is a quick example. Based on a bug report [1] I'm looking at lost > history [2] of this C# file in the master branch [3]: > > [1] https://github.com/pythonnet/pythonnet/issues/262 > > [2] > https://github.com/pythonnet/pythonnet-rawsvnmig/blob/master/pythonnet/src/embed_tests/pyimport.cs > > [3] > https://github.com/pythonnet/pythonnet/blob/master/src/embed_tests/pyimport.cs > > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.akhiyarov at gmail.com Sun Sep 11 10:48:50 2016 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Sun, 11 Sep 2016 09:48:50 -0500 Subject: [Python.NET] cvs -> svn -> git history of Python for .NET In-Reply-To: References: Message-ID: Hi Tony, Thank you, this is exactly what I wanted. I can see the move below in the history. c:\Python\pythonnet>git clone https://github.com/pythonnet/pythonnet Cloning into 'pythonnet'... remote: Counting objects: 3000, done. remote: Total 3000 (delta 0), reused 0 (delta 0), pack-reused 2999 Receiving objects: 100% (3000/3000), 2.56 MiB | 1.58 MiB/s, done. Resolving deltas: 100% (2075/2075), done. Checking connectivity... done. c:\Python\pythonnet>cd pythonnet c:\Python\pythonnet\pythonnet>git log --follow src/embed_tests/pyimport.cs commit f41cf630e28858a9fc22766b5182fdb1253c94d9 Author: Victor Uriarte Date: Sun Apr 17 01:04:52 2016 -0700 Format, white-space .cs files commit d4882688df6cbd2454f344da6a2a573e2575a7e0 Author: David Anthoff Date: Fri Apr 11 14:34:26 2014 -0700 Move everything from pythonnet folder to root folder Main reason is that setup.py should be in root. commit 217bb4847d3c181b8251e84d7eab26dba1341655 Author: Barton Cline Date: Mon Apr 8 09:36:23 2013 +0000 # BUGFIX: Accommodate new output path (x86 vs. x64) # commit de5492d145195a23239cdd3c553fd1314b67b846 Author: Barton Cline Date: Mon Apr 8 08:55:03 2013 +0000 # FIXED: .sln and .csproj file configurations x86/x64 Mono/Win Debug/Release all reflected throughout # # Added Conditional Reference elements to Python.Runtime.csp roj => building for Mono on Windows # # Added Required reference assemblies => building for Mono on Windows # # R# Cleanup on pyimport.cs # # Remove reference to the Python.Runtime project from Console.csproj # # BUGFIX: TARGET_PLATFORM==x86 in buildclrmodule.bat # commit dc7614d227f77ae2db7071651d3d23b11bddc239 Author: Barton Cline Date: Mon Jan 23 07:27:32 2012 +0000 # Slashes in path are unix/linux (tested on Windows) # # Target Framework is 3.5 # # nUnit version is 2.5.9.10348 # # nUnit state storage added # commit 4f790456e751ede65fc5ead8a9aa609a7f66b247 Author: Barton Cline Date: Thu Oct 7 08:16:52 2010 +0000 I've been adding pieces to the UnitTest classes. This addition tests embedding nested imports. The Python parts are in the te sts directory and the managed parts are in the embed_tests directroy as are the nunit parts. This test also demonstrates some neat tricks that can be done when embedding a python interpreter in managed code by using reflection to call inte rnal members of the Python.Runtime. The project settings file is VS2008, which m eans that it's probably time to go ahead and bring the rest of the project files up to a more recent version of Visual Studio. c:\Python\pythonnet\pythonnet> On Thu, Sep 8, 2016 at 11:03 AM, Tony Roberts wrote: > Some files were moved some time after being imported from svn. You need to > use --follow to follow renames/moves, i.e: > > git log --follow src/embed_tests/pyimport.cs > > All commits from svn were imported into git (and are still there). The one > you mention is here: > https://github.com/pythonnet/pythonnet/commit/ > 217bb4847d3c181b8251e84d7eab26dba1341655 > > > On Thu, Sep 8, 2016 at 4:18 PM Denis Akhiyarov > wrote: > >> It happened to me multiple times that the author of particular piece of >> code was needed to be known. >> >> But in the latest pythonnet repository on github we have seem to lost >> some of this history when transitioning from sourceforge. >> >> Are there any git experts, who know how to rebuild the history to include >> some missing commits? >> >> Here is a quick example. Based on a bug report [1] I'm looking at lost >> history [2] of this C# file in the master branch [3]: >> >> [1] https://github.com/pythonnet/pythonnet/issues/262 >> >> [2] https://github.com/pythonnet/pythonnet-rawsvnmig/ >> blob/master/pythonnet/src/embed_tests/pyimport.cs >> >> [3] https://github.com/pythonnet/pythonnet/blob/master/src/embed_tests/ >> pyimport.cs >> >> >> _________________________________________________ >> Python.NET mailing list - PythonDotNet at python.org >> https://mail.python.org/mailman/listinfo/pythondotnet > > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.akhiyarov at gmail.com Tue Sep 13 01:33:08 2016 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Tue, 13 Sep 2016 00:33:08 -0500 Subject: [Python.NET] announcement - IPython cell magic for C# usage in jupyter notebooks (clrmagic) Message-ID: Last week I noticed very useful blog post on how to use C# magic cells with pythonnet from IPython by Xavier Dupr? @sdpython : http://www.xavierdupre.fr/blog/2014-09-20_nojs.html As a result of this, we put together a package clrmagic that can be installed and loaded into jupyter notebooks in 3 steps: *pip install clrmagic* *jupyter notebook* In open notebook enter: *%reload_ext clrmagic* Here is a small demo: %%CS mypower System.dll public static double mypower(double x, double y) { if (y == 0) return 1.0 ; return System.Math.Pow(x,y) ; } .> mypower(3.0,3.0) 27.0 Note that clrmagic works and is tested both on Windows and Linux! It should also work on OSX, but I have not tested. Currently the source code is hosted here: https://github.com/denfromufa/clrmagic I plan to host this code in pythonnet repository under: https://github.com/pythonnet/clrmagic Let's wait for one week to get any feedback from this mailing list and jupyter group (to be posted later this week). Thanks, Denis and Xavier -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.akhiyarov at gmail.com Tue Sep 13 17:18:27 2016 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Tue, 13 Sep 2016 16:18:27 -0500 Subject: [Python.NET] upcoming talk about pythonnet at PyOhio In-Reply-To: References: Message-ID: Attached is the PDF of the pythonnet presentation from PyOhio 2016 presented by Hussein Farran. Here is also a link to it, if you do not get the attachment: https://drive.google.com/file/d/0Bxa9pLe-A3FHcm15d25jdk5xYjA/view?usp= sharing Thanks, Denis On Thu, Jun 30, 2016 at 12:25 PM, Denis Akhiyarov wrote: > http://pyohio.org/schedule/presentation/240/ > > > Saturday 10:30 a.m.?11:20 a.m.Integrating Python into the CLR with Python > for .NETHussein Farran Audience > level:IntermediateDescription > > Python for .NET is a project dedicated to being able to call into Python > from the CLR. Where IronPython has fallen off as the .NET Python prodigal > son Python for .NET has picked up the slack. Python for .NET (or pythonnet > for short) is an integration of the C Python engine with .NET. We'll be > going through the uses and methods of integration of Python with your .NET > projects. > Abstract > > This talk covers embedding Python into the .NET runtime as well as running > Python from your .NET code. It will be in C# on the .NET side. It will > cover topics such as embedding Python, purposes for scripting, script > security, and script/embedded code speeds. I will also cover IronPython and > provide a comparison and analysis of how it compares with Python for .NET. > This may include talking notes about the implementation of IronPython as > well as benchmark comparisons. > > > Hussein Farran > > Hussein Farran is relatively new to the professional IT world, but he?s > doing his best to come in blazing. While building his experience Hussein > strives to forge connections with those more experienced and to pass on > what he knows to those less experienced. As a Software Developer at UseTech > Design, Hussein has the opportunity to dip into many project types and to > learn from a varied cast of co-workers and mentors. > -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Integrating Python with the CLR with Python for .NET.pdf Type: application/pdf Size: 642295 bytes Desc: not available URL: From denis.akhiyarov at gmail.com Sat Sep 17 02:48:45 2016 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Sat, 17 Sep 2016 01:48:45 -0500 Subject: [Python.NET] powershell + pythonnet = match made by DLR Message-ID: Today I came across this simple question about interacting with python from open-sourced *powershell*. It turns out it is so easy to do this using pythonnet! Note that powershell is also heavily using DLR. *http://stackoverflow.com/questions/39183614/how-system-consoleoutputencoding-inputencoding-with-python * Here is an old thread where using powershell was not possible. https://mail.python.org/pipermail/pythondotnet/2013-May/001371.html -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.akhiyarov at gmail.com Wed Sep 21 12:14:03 2016 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Wed, 21 Sep 2016 11:14:03 -0500 Subject: [Python.NET] Python for .NET (pythonnet) 2.2.0.dev1 new development release available Message-ID: Hello Python and .NET developers, New pythonnet 2.2.0.dev1 version with (pre-)release notes are available for download from PYPI and GitHub: https://pypi.python.org/pypi/pythonnet/2.2.0.dev1 Note that since this is pre-release, use this command for installation: pip install pythonnet --pre -U Detailed release notes: https://github.com/pythonnet/pythonnet/releases News - clrmagic for IPython kernel - presentation on PyOhio 2016 - 3 new pythonnet core developers @denfromufa @filmor @vmuriart - 26+1 contributors Changelog - Switch to C# 6.0 (#219 ) - Relative imports (#219 ) - Recursive types (#250 ) - Demo fix - stream reading (#225 ) - setup.py improvements for locating build tools (#208 ) - unmanaged exports updated (#206 ) - Mono update pinned to 4.2.4.4 (#233 ) - Documentation update (http://pythonnet.github.io/) - wiki added (https://github.com/pythonnet/pythonnet/wiki) Known Issues - Subclassing .NET classes (#264 ) - Mixing python and .NET constructors (#252 ) - License update (one blocking contributor @tiran out of 27) - Overloading on constructors, ref/out and subtypes (#265 ) - pythonhome and pythonpath (#186 ) - Regression bug on numpy arrays (#249 ) -------------- next part -------------- An HTML attachment was scrubbed... URL: From HMoeller at comprion.com Thu Sep 22 11:04:14 2016 From: HMoeller at comprion.com (Henning Moeller) Date: Thu, 22 Sep 2016 15:04:14 +0000 Subject: [Python.NET] Inheriting from abstract .NET class Message-ID: Hello out there, I'm trying to inherit from an abstract .NET base class in Python (2.7). I'm a Python n00b but from what I understood... Here's what I managed to do in Python only and which works fine: [File: room.py] ------- import abc class Room(object): def __init__(self, door): self.door = door def open(self): self.door.open() def close(self): self.door.close() def is_open(self): return self.door.is_open() class Door(object): __metaclass__ = abc.ABCMeta def open(self): if not self.is_open(): self.toggle() def close(self): if self.is_open(): self.toggle() @abc.abstractmethod def is_open(self): pass @abc.abstractmethod def toggle(self): pass class StringDoor(Door): def __init__(self): self.status = "closed" def is_open(self): return self.status == "open" def toggle(self): if self.status == "open": self.status = "closed" else: self.status = "open" class BooleanDoor(Door): def __init__(self): self.status = True def is_open(self): return self.status def toggle(self): self.status = not (self.status) Door.register(StringDoor) Door.register(BooleanDoor) ------- Now, all I did was to replace the abstract base class Door by a C# representation: [File: PythonAbstractBaseClass.dll] ------- namespace PythonAbstractBaseClass { public abstract class Door { public virtual void Open() { if (!IsOpen()) Toggle(); } public virtual void Close() { if (IsOpen()) Toggle(); } public abstract bool IsOpen(); public abstract void Toggle(); } } ------- Removing Door from the Python part and importing it from the .NET assembly instead, I end up with this: [File: room2.py] ------- import clr import abc from PythonAbstractBaseClass import Door class Room(object): def __init__(self, door): self.door = door def open(self): self.door.open() def close(self): self.door.close() def is_open(self): return self.door.is_open() class StringDoor(Door): def __init__(self): self.status = "closed" def is_open(self): return self.status == "open" def toggle(self): if self.status == "open": self.status = "closed" else: self.status = "open" class BooleanDoor(Door): def __init__(self): self.status = True def is_open(self): return self.status def toggle(self): self.status = not (self.status) Door.register(StringDoor) Door.register(BooleanDoor) ------- But this fails with the following error message: Door.register(StringDoor) AttributeError: type object 'Door' has no attribute 'register' >From what I understood about abc.ABCMeta, this metaclass contributes the 'register' method. It seems that abstract C# classes do not come with the same metaclass. They instead come with metaclass "CLR Metatype" which obviously does not provide 'register'. But if I drop the call to 'register', on instantiating one of the derived classes, I receive the error message sdoor = StringDoor() TypeError: cannot instantiate abstract class Is there a way to inherit from an abstract .NET class or is this is missing feature? Thanks in advance, Henning -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: PythonAbstractBaseClass.zip Type: application/x-zip-compressed Size: 2520 bytes Desc: PythonAbstractBaseClass.zip URL: From denis.akhiyarov at gmail.com Thu Sep 22 13:37:59 2016 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Thu, 22 Sep 2016 12:37:59 -0500 Subject: [Python.NET] Inheriting from abstract .NET class In-Reply-To: References: Message-ID: You are not a "Python n00b" based on metaclass usage! My recommendation is to keep simple integration layer between CPython and .NET, hence metaclasses were/are not supported. But contributions are welcome! Although I expect this to be a tremendous undertaking based on reviewing multiple sources. Note that metaclass is a very different low-level concept from higher-level abstract classes in .NET, hence direct mapping (e.g. `.register()` ) is not feasible. Thanks, Denis On Thu, Sep 22, 2016 at 10:04 AM, Henning Moeller wrote: > Hello out there, > > > > I?m trying to inherit from an abstract .NET base class in Python (2.7). > I?m a Python n00b but from what I understood? > > > > Here?s what I managed to do in Python only and which works fine: > > > > [File: room.py] ------- > > import abc > > > > class Room(object): > > def __init__(self, door): > > self.door = door > > > > def open(self): > > self.door.open() > > > > def close(self): > > self.door.close() > > > > def is_open(self): > > return self.door.is_open() > > > > class Door(object): > > __metaclass__ = abc.ABCMeta > > > > def open(self): > > if not self.is_open(): > > self.toggle() > > > > def close(self): > > if self.is_open(): > > self.toggle() > > > > @abc.abstractmethod > > def is_open(self): > > pass > > > > @abc.abstractmethod > > def toggle(self): > > pass > > > > class StringDoor(Door): > > def __init__(self): > > self.status = "closed" > > > > def is_open(self): > > return self.status == "open" > > > > def toggle(self): > > if self.status == "open": > > self.status = "closed" > > else: > > self.status = "open" > > > > class BooleanDoor(Door): > > def __init__(self): > > self.status = True > > > > def is_open(self): > > return self.status > > > > def toggle(self): > > self.status = not (self.status) > > > > Door.register(StringDoor) > > Door.register(BooleanDoor) > > ------- > > > > Now, all I did was to replace the abstract base class Door by a C# > representation: > > > > [File: PythonAbstractBaseClass.dll] ------- > > namespace PythonAbstractBaseClass > > { > > public abstract class Door > > { > > public virtual void Open() > > { > > if (!IsOpen()) > > Toggle(); > > } > > > > public virtual void Close() > > { > > if (IsOpen()) > > Toggle(); > > } > > > > public abstract bool IsOpen(); > > public abstract void Toggle(); > > } > > } > > ------- > > > > Removing Door from the Python part and importing it from the .NET assembly > instead, I end up with this: > > > > [File: room2.py] ------- > > import clr > > import abc > > from PythonAbstractBaseClass import Door > > > > class Room(object): > > def __init__(self, door): > > self.door = door > > > > def open(self): > > self.door.open() > > > > def close(self): > > self.door.close() > > > > def is_open(self): > > return self.door.is_open() > > > > class StringDoor(Door): > > def __init__(self): > > self.status = "closed" > > > > def is_open(self): > > return self.status == "open" > > > > def toggle(self): > > if self.status == "open": > > self.status = "closed" > > else: > > self.status = "open" > > > > class BooleanDoor(Door): > > def __init__(self): > > self.status = True > > > > def is_open(self): > > return self.status > > > > def toggle(self): > > self.status = not (self.status) > > > > Door.register(StringDoor) > > Door.register(BooleanDoor) > > ------- > > > > But this fails with the following error message: > > > > Door.register(StringDoor) > > AttributeError: type object 'Door' has no attribute 'register' > > > > From what I understood about abc.ABCMeta, this metaclass contributes the > ?register? method. It seems that abstract C# classes do not come with the > same metaclass. They instead come with metaclass ?CLR Metatype? which > obviously does not provide ?register?. > > > > But if I drop the call to ?register?, on instantiating one of the derived > classes, I receive the error message > > > > sdoor = StringDoor() > > TypeError: cannot instantiate abstract class > > > > Is there a way to inherit from an abstract .NET class or is this is > missing feature? > > > > Thanks in advance, > > > > Henning > > > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From solospirit at sohu.com Mon Sep 26 09:40:57 2016 From: solospirit at sohu.com (solospirit) Date: Mon, 26 Sep 2016 21:40:57 +0800 (CST) Subject: [Python.NET] call python in Visual C# WinForm application Message-ID: <1474897257.e1bbeb4db2f649f5a0e4be3d8a23d968.solospirit@sohu.com> An HTML attachment was scrubbed... URL: From HMoeller at comprion.com Mon Sep 26 10:19:27 2016 From: HMoeller at comprion.com (Henning Moeller) Date: Mon, 26 Sep 2016 14:19:27 +0000 Subject: [Python.NET] Inheriting from abstract .NET class In-Reply-To: References: Message-ID: <172ece7de07246a2a7f30d75f9aa0bb4@ex2013.orga-test.local> Hi Denis, Thanks for your fast response. Sounds completely reasonable to me. And this has not been too much of an issue for me. I got rid of the abstract base class simply by providing an ?empty? implementation and using a non-abstract base class. No need for meta classes any more. This way, I got rid of error messages. Great! Unfortunately, there seem to be an issue when calling a virtual method inside the C# implementation. Even when overriding this virtual method in Python, it?s still the base class? implementation which is called. See the new simple base class defined in C#: namespace PythonBaseClass { using System; public class Door { public virtual void Open() { if (!IsOpen()) Toggle(); } public virtual void Close() { if (IsOpen()) Toggle(); } public virtual bool IsOpen() { Console.WriteLine("Door.IsOpen()"); return false; } public virtual void Toggle() { Console.WriteLine("Door.Toggle()"); } } } The consuming Python code now looks like this: import clr from PythonBaseClass import Door class StringDoor(Door): def __init__(self): self.status = "closed" def IsOpen(self): print "StringDoor.IsOpen()" return self.status == "open" def Toggle(self): print "StringDoor.Toggle()" if self.status == "open": self.status = "closed" else: self.status = "open" But the output does not look like expected: Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from PythonInheritedClass import StringDoor >>> d = StringDoor() >>> d.status 'closed' >>> d.Open() Door.IsOpen() Door.Toggle() >>> d.status 'closed' >>> From my understanding, the output should rather read ?StringDoor.()? instead of ?Door.()?. Obviously, only the base implementation is called but the overridden methods are ignored. Also a limitation of inheriting C# classes in pythonnet? BR, Henning From: PythonDotNet [mailto:pythondotnet-bounces+hmoeller=comprion.com at python.org] On Behalf Of Denis Akhiyarov Sent: Donnerstag, 22. September 2016 19:38 To: A list for users and developers of Python for .NET Subject: Re: [Python.NET] Inheriting from abstract .NET class You are not a "Python n00b" based on metaclass usage! My recommendation is to keep simple integration layer between CPython and .NET, hence metaclasses were/are not supported. But contributions are welcome! Although I expect this to be a tremendous undertaking based on reviewing multiple sources. Note that metaclass is a very different low-level concept from higher-level abstract classes in .NET, hence direct mapping (e.g. `.register()` ) is not feasible. Thanks, Denis On Thu, Sep 22, 2016 at 10:04 AM, Henning Moeller > wrote: Hello out there, I?m trying to inherit from an abstract .NET base class in Python (2.7). I?m a Python n00b but from what I understood? Here?s what I managed to do in Python only and which works fine: [File: room.py] ------- import abc class Room(object): def __init__(self, door): self.door = door def open(self): self.door.open() def close(self): self.door.close() def is_open(self): return self.door.is_open() class Door(object): __metaclass__ = abc.ABCMeta def open(self): if not self.is_open(): self.toggle() def close(self): if self.is_open(): self.toggle() @abc.abstractmethod def is_open(self): pass @abc.abstractmethod def toggle(self): pass class StringDoor(Door): def __init__(self): self.status = "closed" def is_open(self): return self.status == "open" def toggle(self): if self.status == "open": self.status = "closed" else: self.status = "open" class BooleanDoor(Door): def __init__(self): self.status = True def is_open(self): return self.status def toggle(self): self.status = not (self.status) Door.register(StringDoor) Door.register(BooleanDoor) ------- Now, all I did was to replace the abstract base class Door by a C# representation: [File: PythonAbstractBaseClass.dll] ------- namespace PythonAbstractBaseClass { public abstract class Door { public virtual void Open() { if (!IsOpen()) Toggle(); } public virtual void Close() { if (IsOpen()) Toggle(); } public abstract bool IsOpen(); public abstract void Toggle(); } } ------- Removing Door from the Python part and importing it from the .NET assembly instead, I end up with this: [File: room2.py] ------- import clr import abc from PythonAbstractBaseClass import Door class Room(object): def __init__(self, door): self.door = door def open(self): self.door.open() def close(self): self.door.close() def is_open(self): return self.door.is_open() class StringDoor(Door): def __init__(self): self.status = "closed" def is_open(self): return self.status == "open" def toggle(self): if self.status == "open": self.status = "closed" else: self.status = "open" class BooleanDoor(Door): def __init__(self): self.status = True def is_open(self): return self.status def toggle(self): self.status = not (self.status) Door.register(StringDoor) Door.register(BooleanDoor) ------- But this fails with the following error message: Door.register(StringDoor) AttributeError: type object 'Door' has no attribute 'register' From what I understood about abc.ABCMeta, this metaclass contributes the ?register? method. It seems that abstract C# classes do not come with the same metaclass. They instead come with metaclass ?CLR Metatype? which obviously does not provide ?register?. But if I drop the call to ?register?, on instantiating one of the derived classes, I receive the error message sdoor = StringDoor() TypeError: cannot instantiate abstract class Is there a way to inherit from an abstract .NET class or is this is missing feature? Thanks in advance, Henning _________________________________________________ Python.NET mailing list - PythonDotNet at python.org https://mail.python.org/mailman/listinfo/pythondotnet -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.akhiyarov at gmail.com Mon Sep 26 13:54:22 2016 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Mon, 26 Sep 2016 12:54:22 -0500 Subject: [Python.NET] call python in Visual C# WinForm application In-Reply-To: <1474897257.e1bbeb4db2f649f5a0e4be3d8a23d968.solospirit@sohu.com> References: <1474897257.e1bbeb4db2f649f5a0e4be3d8a23d968.solospirit@sohu.com> Message-ID: @solospirit: You are doing illegal things with Python and believe me - it bites back :) Remember that in Python you cannot just import modules (.py) just from arbitrary paths, unless you are using importlib or sys.path. Hence here is the correct workflow using sys.path: *Microsoft (R) Roslyn C# Compiler version 1.2.0.60425* *Loading context from 'CSharpInteractive.rsp'.* *Type "#help" for more information.* *> #r "C:\Python\Python27_32b\Lib\site-packages\Python.Runtime.dll"* *> using Python.Runtime;* *> dynamic sys; using (Py.GIL()) { sys= Py.Import("sys"); }* *> using (Py.GIL()) { sys.path.append(@"C:\Users\denis.akhiyarov\Downloads") ; }* *> dynamic testmodule; using (Py.GIL()) { testmodule = Py.Import("network2"); }* *> testmodule.Network* *[]* Perhaps this should be documented here: https://github.com/pythonnet/pythonnet/wiki/pythonnet-troubleshooting-wiki-on-Windows Thanks, Denis On Mon, Sep 26, 2016 at 8:40 AM, solospirit wrote: > Hi, > > > I'm using windows 7 64bit + Visual C#2012 to develop WinForm 32bit > application with .Net Framework 4.0. I want to call some python script > within my winform application. > > > After read the document, i'm now able to call the following code within my > WinForm application, and the result is correct. > > using (Py.GIL()) > > { > > dynamic np = Py.Import("numpy"); > > dynamic sin = np.sin; > > double c = np.cos(5) + sin(5); > > MessageBox.Show(c.ToString()); > > } > > > I'm using a python script from https://github.com/ > mnielsen/neural-networks-and-deep-learning/blob/master/src/network2.py > by > git clone to my local machine c:\mypath\neural-networks-and-deep-learning, > it works inside python in my system. > > > I tried the following code, but the np returns NULL. > > using (Py.GIL()) > > { > > dynamic np = Py.Import(@"c:\mypath\neural- > networks-and-deep-learning\src\network2"); > > } > > > I also tried the following code, but pModule also returns NULL > > string workingdir = @"c:\mypath\neural-networks- > and-deep-learning\src"; > > PythonEngine.Initialize(); > > IntPtr pythonLock = PythonEngine.AcquireLock(); > > PythonEngine.RunSimpleString("import sys\n"); > > PythonEngine.RunSimpleString("sys.path.append('" + workingdir > + "')\n"); > > PyObject pModule = PythonEngine.ImportModule(workingdir + > "\\network2.py"); > > > How could I invoke such python script, thanks a lot! > > > > > > ------------------------------ > > > > > > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.akhiyarov at gmail.com Mon Sep 26 14:16:27 2016 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Mon, 26 Sep 2016 13:16:27 -0500 Subject: [Python.NET] Inheriting from abstract .NET class In-Reply-To: <172ece7de07246a2a7f30d75f9aa0bb4@ex2013.orga-test.local> References: <172ece7de07246a2a7f30d75f9aa0bb4@ex2013.orga-test.local> Message-ID: Hi Henning, This feature was implemented by @tonyroberts in 2013 and is tested in CI with test_subclass.py file. According to these tests, the missing piece in your code is defining explicitly __namespace__ on the .NET class derived in Python: In [11]: class StringDoor1(Door): ....: __namespace__ = "PythonBaseClass" ....: def __init__(self): ....: self.status = "closed" ....: ....: def IsOpen(self): ....: print "StringDoor.IsOpen()" ....: return self.status == "open" ....: ....: def Toggle(self): ....: print "StringDoor.Toggle()" ....: if self.status == "open": ....: self.status = "closed" ....: else: ....: self.status = "open" In [12]: d = StringDoor1() In [13]: d.status Out[13]: 'closed' In [14]: d.Open() StringDoor.IsOpen() StringDoor.Toggle() In [15]: d.status Out[15]: 'open' Thanks, Denis On Mon, Sep 26, 2016 at 9:19 AM, Henning Moeller wrote: > Hi Denis, > > > > Thanks for your fast response. Sounds completely reasonable to me. And > this has not been too much of an issue for me. > > > > I got rid of the abstract base class simply by providing an ?empty? > implementation and using a non-abstract base class. No need for meta > classes any more. This way, I got rid of error messages. Great! > > > > Unfortunately, there seem to be an issue when calling a virtual method > inside the C# implementation. Even when overriding this virtual method in > Python, it?s still the base class? implementation which is called. > > > > See the new simple base class defined in C#: > > > > namespace PythonBaseClass > > { > > using System; > > > > public class Door > > { > > public virtual void Open() > > { > > if (!IsOpen()) > > Toggle(); > > } > > > > public virtual void Close() > > { > > if (IsOpen()) > > Toggle(); > > } > > > > public virtual bool IsOpen() > > { > > Console.WriteLine("Door.IsOpen()"); > > return false; > > } > > > > public virtual void Toggle() > > { > > Console.WriteLine("Door.Toggle()"); > > } > > } > > } > > > > The consuming Python code now looks like this: > > > > import clr > > from PythonBaseClass import Door > > > > class StringDoor(Door): > > def __init__(self): > > self.status = "closed" > > > > def IsOpen(self): > > print "StringDoor.IsOpen()" > > return self.status == "open" > > > > def Toggle(self): > > print "StringDoor.Toggle()" > > if self.status == "open": > > self.status = "closed" > > else: > > self.status = "open" > > > > But the output does not look like expected: > > > > Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 > bit (Intel)] on win32 > > Type "help", "copyright", "credits" or "license" for more information. > > >>> from PythonInheritedClass import StringDoor > > >>> d = StringDoor() > > >>> d.status > > 'closed' > > >>> d.Open() > > *Door.IsOpen()* > > *Door.Toggle()* > > >>> d.status > > *'closed'* > > >>> > > > > From my understanding, the output should rather read ? > StringDoor.()? instead of ?Door.()?. Obviously, > only the base implementation is called but the overridden methods are > ignored. > > > > Also a limitation of inheriting C# classes in pythonnet? > > > > BR, > > > > Henning > > > > *From:* PythonDotNet [mailto:pythondotnet-bounces+hmoeller= > comprion.com at python.org] *On Behalf Of *Denis Akhiyarov > *Sent:* Donnerstag, 22. September 2016 19:38 > *To:* A list for users and developers of Python for .NET < > pythondotnet at python.org> > *Subject:* Re: [Python.NET] Inheriting from abstract .NET class > > > > You are not a "Python n00b" based on metaclass usage! > > > > My recommendation is to keep simple integration layer between CPython and > .NET, hence metaclasses were/are not supported. > > > > But contributions are welcome! Although I expect this to be a tremendous > undertaking based on reviewing multiple sources. > > > > Note that metaclass is a very different low-level concept from > higher-level abstract classes in .NET, hence direct mapping (e.g. > `.register()` ) is not feasible. > > > > Thanks, > > Denis > > > > On Thu, Sep 22, 2016 at 10:04 AM, Henning Moeller > wrote: > > Hello out there, > > > > I?m trying to inherit from an abstract .NET base class in Python (2.7). > I?m a Python n00b but from what I understood? > > > > Here?s what I managed to do in Python only and which works fine: > > > > [File: room.py] ------- > > import abc > > > > class Room(object): > > def __init__(self, door): > > self.door = door > > > > def open(self): > > self.door.open() > > > > def close(self): > > self.door.close() > > > > def is_open(self): > > return self.door.is_open() > > > > class Door(object): > > __metaclass__ = abc.ABCMeta > > > > def open(self): > > if not self.is_open(): > > self.toggle() > > > > def close(self): > > if self.is_open(): > > self.toggle() > > > > @abc.abstractmethod > > def is_open(self): > > pass > > > > @abc.abstractmethod > > def toggle(self): > > pass > > > > class StringDoor(Door): > > def __init__(self): > > self.status = "closed" > > > > def is_open(self): > > return self.status == "open" > > > > def toggle(self): > > if self.status == "open": > > self.status = "closed" > > else: > > self.status = "open" > > > > class BooleanDoor(Door): > > def __init__(self): > > self.status = True > > > > def is_open(self): > > return self.status > > > > def toggle(self): > > self.status = not (self.status) > > > > Door.register(StringDoor) > > Door.register(BooleanDoor) > > ------- > > > > Now, all I did was to replace the abstract base class Door by a C# > representation: > > > > [File: PythonAbstractBaseClass.dll] ------- > > namespace PythonAbstractBaseClass > > { > > public abstract class Door > > { > > public virtual void Open() > > { > > if (!IsOpen()) > > Toggle(); > > } > > > > public virtual void Close() > > { > > if (IsOpen()) > > Toggle(); > > } > > > > public abstract bool IsOpen(); > > public abstract void Toggle(); > > } > > } > > ------- > > > > Removing Door from the Python part and importing it from the .NET assembly > instead, I end up with this: > > > > [File: room2.py] ------- > > import clr > > import abc > > from PythonAbstractBaseClass import Door > > > > class Room(object): > > def __init__(self, door): > > self.door = door > > > > def open(self): > > self.door.open() > > > > def close(self): > > self.door.close() > > > > def is_open(self): > > return self.door.is_open() > > > > class StringDoor(Door): > > def __init__(self): > > self.status = "closed" > > > > def is_open(self): > > return self.status == "open" > > > > def toggle(self): > > if self.status == "open": > > self.status = "closed" > > else: > > self.status = "open" > > > > class BooleanDoor(Door): > > def __init__(self): > > self.status = True > > > > def is_open(self): > > return self.status > > > > def toggle(self): > > self.status = not (self.status) > > > > Door.register(StringDoor) > > Door.register(BooleanDoor) > > ------- > > > > But this fails with the following error message: > > > > Door.register(StringDoor) > > AttributeError: type object 'Door' has no attribute 'register' > > > > From what I understood about abc.ABCMeta, this metaclass contributes the > ?register? method. It seems that abstract C# classes do not come with the > same metaclass. They instead come with metaclass ?CLR Metatype? which > obviously does not provide ?register?. > > > > But if I drop the call to ?register?, on instantiating one of the derived > classes, I receive the error message > > > > sdoor = StringDoor() > > TypeError: cannot instantiate abstract class > > > > Is there a way to inherit from an abstract .NET class or is this is > missing feature? > > > > Thanks in advance, > > > > Henning > > > > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > > > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From HMoeller at comprion.com Tue Sep 27 03:49:02 2016 From: HMoeller at comprion.com (Henning Moeller) Date: Tue, 27 Sep 2016 07:49:02 +0000 Subject: [Python.NET] Inheriting from abstract .NET class In-Reply-To: References: <172ece7de07246a2a7f30d75f9aa0bb4@ex2013.orga-test.local> Message-ID: <7d1b1b6d0e5e47f59819d686b5606dbb@ex2013.orga-test.local> Hi Denis, thank you again. That helped a lot. But ? I?m sorry to bother you again. In the classes I really have to use, some of the virtual methods are protected. Those are not resolved by pythonnet. Only public virtual is recognized and overridden by the inherited class. See the (only slightly) altered example: namespace PythonBaseClass { using System; public class Door { public virtual void Open() { if (!IsOpen()) Toggle(); } public virtual void Close() { if (IsOpen()) Toggle(); } public virtual bool IsOpen() { Console.WriteLine("Door.IsOpen()"); return false; } protected virtual void Toggle() { Console.WriteLine("Door.Toggle()"); } } } The Python code has just been altered so that the namespace is properly set. Running the example comes with the following output: Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from PythonInheritedClass import * >>> d = StringDoor() >>> d.status 'closed' >>> d.Open() StringDoor.IsOpen() Door.Toggle() >>> Any ideas? This looks a little bit more ?doable? than the abstract thing. BR, Henning From: PythonDotNet [mailto:pythondotnet-bounces+hmoeller=comprion.com at python.org] On Behalf Of Denis Akhiyarov Sent: Montag, 26. September 2016 20:16 To: A list for users and developers of Python for .NET Subject: Re: [Python.NET] Inheriting from abstract .NET class Hi Henning, This feature was implemented by @tonyroberts in 2013 and is tested in CI with test_subclass.py file. According to these tests, the missing piece in your code is defining explicitly __namespace__ on the .NET class derived in Python: In [11]: class StringDoor1(Door): ....: __namespace__ = "PythonBaseClass" ....: def __init__(self): ....: self.status = "closed" ....: ....: def IsOpen(self): ....: print "StringDoor.IsOpen()" ....: return self.status == "open" ....: ....: def Toggle(self): ....: print "StringDoor.Toggle()" ....: if self.status == "open": ....: self.status = "closed" ....: else: ....: self.status = "open" In [12]: d = StringDoor1() In [13]: d.status Out[13]: 'closed' In [14]: d.Open() StringDoor.IsOpen() StringDoor.Toggle() In [15]: d.status Out[15]: 'open' Thanks, Denis On Mon, Sep 26, 2016 at 9:19 AM, Henning Moeller > wrote: Hi Denis, Thanks for your fast response. Sounds completely reasonable to me. And this has not been too much of an issue for me. I got rid of the abstract base class simply by providing an ?empty? implementation and using a non-abstract base class. No need for meta classes any more. This way, I got rid of error messages. Great! Unfortunately, there seem to be an issue when calling a virtual method inside the C# implementation. Even when overriding this virtual method in Python, it?s still the base class? implementation which is called. See the new simple base class defined in C#: namespace PythonBaseClass { using System; public class Door { public virtual void Open() { if (!IsOpen()) Toggle(); } public virtual void Close() { if (IsOpen()) Toggle(); } public virtual bool IsOpen() { Console.WriteLine("Door.IsOpen()"); return false; } public virtual void Toggle() { Console.WriteLine("Door.Toggle()"); } } } The consuming Python code now looks like this: import clr from PythonBaseClass import Door class StringDoor(Door): def __init__(self): self.status = "closed" def IsOpen(self): print "StringDoor.IsOpen()" return self.status == "open" def Toggle(self): print "StringDoor.Toggle()" if self.status == "open": self.status = "closed" else: self.status = "open" But the output does not look like expected: Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from PythonInheritedClass import StringDoor >>> d = StringDoor() >>> d.status 'closed' >>> d.Open() Door.IsOpen() Door.Toggle() >>> d.status 'closed' >>> From my understanding, the output should rather read ?StringDoor.()? instead of ?Door.()?. Obviously, only the base implementation is called but the overridden methods are ignored. Also a limitation of inheriting C# classes in pythonnet? BR, Henning From: PythonDotNet [mailto:pythondotnet-bounces+hmoeller=comprion.com at python.org] On Behalf Of Denis Akhiyarov Sent: Donnerstag, 22. September 2016 19:38 To: A list for users and developers of Python for .NET > Subject: Re: [Python.NET] Inheriting from abstract .NET class You are not a "Python n00b" based on metaclass usage! My recommendation is to keep simple integration layer between CPython and .NET, hence metaclasses were/are not supported. But contributions are welcome! Although I expect this to be a tremendous undertaking based on reviewing multiple sources. Note that metaclass is a very different low-level concept from higher-level abstract classes in .NET, hence direct mapping (e.g. `.register()` ) is not feasible. Thanks, Denis On Thu, Sep 22, 2016 at 10:04 AM, Henning Moeller > wrote: Hello out there, I?m trying to inherit from an abstract .NET base class in Python (2.7). I?m a Python n00b but from what I understood? Here?s what I managed to do in Python only and which works fine: [File: room.py] ------- import abc class Room(object): def __init__(self, door): self.door = door def open(self): self.door.open() def close(self): self.door.close() def is_open(self): return self.door.is_open() class Door(object): __metaclass__ = abc.ABCMeta def open(self): if not self.is_open(): self.toggle() def close(self): if self.is_open(): self.toggle() @abc.abstractmethod def is_open(self): pass @abc.abstractmethod def toggle(self): pass class StringDoor(Door): def __init__(self): self.status = "closed" def is_open(self): return self.status == "open" def toggle(self): if self.status == "open": self.status = "closed" else: self.status = "open" class BooleanDoor(Door): def __init__(self): self.status = True def is_open(self): return self.status def toggle(self): self.status = not (self.status) Door.register(StringDoor) Door.register(BooleanDoor) ------- Now, all I did was to replace the abstract base class Door by a C# representation: [File: PythonAbstractBaseClass.dll] ------- namespace PythonAbstractBaseClass { public abstract class Door { public virtual void Open() { if (!IsOpen()) Toggle(); } public virtual void Close() { if (IsOpen()) Toggle(); } public abstract bool IsOpen(); public abstract void Toggle(); } } ------- Removing Door from the Python part and importing it from the .NET assembly instead, I end up with this: [File: room2.py] ------- import clr import abc from PythonAbstractBaseClass import Door class Room(object): def __init__(self, door): self.door = door def open(self): self.door.open() def close(self): self.door.close() def is_open(self): return self.door.is_open() class StringDoor(Door): def __init__(self): self.status = "closed" def is_open(self): return self.status == "open" def toggle(self): if self.status == "open": self.status = "closed" else: self.status = "open" class BooleanDoor(Door): def __init__(self): self.status = True def is_open(self): return self.status def toggle(self): self.status = not (self.status) Door.register(StringDoor) Door.register(BooleanDoor) ------- But this fails with the following error message: Door.register(StringDoor) AttributeError: type object 'Door' has no attribute 'register' From what I understood about abc.ABCMeta, this metaclass contributes the ?register? method. It seems that abstract C# classes do not come with the same metaclass. They instead come with metaclass ?CLR Metatype? which obviously does not provide ?register?. But if I drop the call to ?register?, on instantiating one of the derived classes, I receive the error message sdoor = StringDoor() TypeError: cannot instantiate abstract class Is there a way to inherit from an abstract .NET class or is this is missing feature? Thanks in advance, Henning _________________________________________________ Python.NET mailing list - PythonDotNet at python.org https://mail.python.org/mailman/listinfo/pythondotnet _________________________________________________ Python.NET mailing list - PythonDotNet at python.org https://mail.python.org/mailman/listinfo/pythondotnet -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.akhiyarov at gmail.com Tue Sep 27 12:56:41 2016 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Tue, 27 Sep 2016 11:56:41 -0500 Subject: [Python.NET] Inheriting from abstract .NET class In-Reply-To: <7d1b1b6d0e5e47f59819d686b5606dbb@ex2013.orga-test.local> References: <172ece7de07246a2a7f30d75f9aa0bb4@ex2013.orga-test.local> <7d1b1b6d0e5e47f59819d686b5606dbb@ex2013.orga-test.local> Message-ID: Henning, It is very questionable if "protected" should be available for overriding in Python, since we are not really doing compile time overriding in a .NET class with all sorts of differences in class compilation vs runtime emission. But as a quick workaround you can add protected in this method in pythonnet: private static void AddVirtualMethod(MethodInfo method, Type baseType, TypeBuilder typeBuilder) Let us know about the result. Cheers, Denis On Tue, Sep 27, 2016 at 2:49 AM, Henning Moeller wrote: > Hi Denis, > > > > thank you again. That helped a lot. > > > > But ? I?m sorry to bother you again. In the classes I really have to use, > some of the virtual methods are protected. Those are not resolved by > pythonnet. Only public virtual is recognized and overridden by the > inherited class. See the (only slightly) altered example: > > > > namespace PythonBaseClass > > { > > using System; > > > > public class Door > > { > > public virtual void Open() > > { > > if (!IsOpen()) > > Toggle(); > > } > > > > public virtual void Close() > > { > > if (IsOpen()) > > Toggle(); > > } > > > > public virtual bool IsOpen() > > { > > Console.WriteLine("Door.IsOpen()"); > > return false; > > } > > > > *protected* virtual void Toggle() > > { > > Console.WriteLine("Door.Toggle()"); > > } > > } > > } > > > > The Python code has just been altered so that the namespace is properly > set. Running the example comes with the following output: > > > > Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 > bit (Intel)] on win32 > > Type "help", "copyright", "credits" or "license" for more information. > > >>> from PythonInheritedClass import * > > >>> d = StringDoor() > > >>> d.status > > 'closed' > > >>> d.Open() > > StringDoor.IsOpen() > > *Door.Toggle()* > > >>> > > > > Any ideas? This looks a little bit more ?doable? than the abstract thing. > > > > BR, > > > > Henning > > > > *From:* PythonDotNet [mailto:pythondotnet-bounces+hmoeller= > comprion.com at python.org] *On Behalf Of *Denis Akhiyarov > *Sent:* Montag, 26. September 2016 20:16 > *To:* A list for users and developers of Python for .NET < > pythondotnet at python.org> > *Subject:* Re: [Python.NET] Inheriting from abstract .NET class > > > > Hi Henning, > > > > This feature was implemented by @tonyroberts in 2013 and is tested in CI > with test_subclass.py file. > > > > According to these tests, the missing piece in your code is defining > explicitly __namespace__ on the .NET class derived in Python: > > > > In [11]: class StringDoor1(Door): > > ....: __namespace__ = "PythonBaseClass" > > ....: def __init__(self): > > ....: self.status = "closed" > > ....: > > ....: def IsOpen(self): > > ....: print "StringDoor.IsOpen()" > > ....: return self.status == "open" > > ....: > > ....: def Toggle(self): > > ....: print "StringDoor.Toggle()" > > ....: if self.status == "open": > > ....: self.status = "closed" > > ....: else: > > ....: self.status = "open" > > > > In [12]: d = StringDoor1() > > > > In [13]: d.status > > Out[13]: 'closed' > > > > In [14]: d.Open() > > StringDoor.IsOpen() > > StringDoor.Toggle() > > > > In [15]: d.status > > Out[15]: 'open' > > > > > > > > Thanks, > > Denis > > > > On Mon, Sep 26, 2016 at 9:19 AM, Henning Moeller > wrote: > > Hi Denis, > > > > Thanks for your fast response. Sounds completely reasonable to me. And > this has not been too much of an issue for me. > > > > I got rid of the abstract base class simply by providing an ?empty? > implementation and using a non-abstract base class. No need for meta > classes any more. This way, I got rid of error messages. Great! > > > > Unfortunately, there seem to be an issue when calling a virtual method > inside the C# implementation. Even when overriding this virtual method in > Python, it?s still the base class? implementation which is called. > > > > See the new simple base class defined in C#: > > > > namespace PythonBaseClass > > { > > using System; > > > > public class Door > > { > > public virtual void Open() > > { > > if (!IsOpen()) > > Toggle(); > > } > > > > public virtual void Close() > > { > > if (IsOpen()) > > Toggle(); > > } > > > > public virtual bool IsOpen() > > { > > Console.WriteLine("Door.IsOpen()"); > > return false; > > } > > > > public virtual void Toggle() > > { > > Console.WriteLine("Door.Toggle()"); > > } > > } > > } > > > > The consuming Python code now looks like this: > > > > import clr > > from PythonBaseClass import Door > > > > class StringDoor(Door): > > def __init__(self): > > self.status = "closed" > > > > def IsOpen(self): > > print "StringDoor.IsOpen()" > > return self.status == "open" > > > > def Toggle(self): > > print "StringDoor.Toggle()" > > if self.status == "open": > > self.status = "closed" > > else: > > self.status = "open" > > > > But the output does not look like expected: > > > > Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 > bit (Intel)] on win32 > > Type "help", "copyright", "credits" or "license" for more information. > > >>> from PythonInheritedClass import StringDoor > > >>> d = StringDoor() > > >>> d.status > > 'closed' > > >>> d.Open() > > *Door.IsOpen()* > > *Door.Toggle()* > > >>> d.status > > *'closed'* > > >>> > > > > From my understanding, the output should rather read ? > StringDoor.()? instead of ?Door.()?. Obviously, > only the base implementation is called but the overridden methods are > ignored. > > > > Also a limitation of inheriting C# classes in pythonnet? > > > > BR, > > > > Henning > > > > *From:* PythonDotNet [mailto:pythondotnet-bounces+hmoeller= > comprion.com at python.org] *On Behalf Of *Denis Akhiyarov > *Sent:* Donnerstag, 22. September 2016 19:38 > *To:* A list for users and developers of Python for .NET < > pythondotnet at python.org> > *Subject:* Re: [Python.NET] Inheriting from abstract .NET class > > > > You are not a "Python n00b" based on metaclass usage! > > > > My recommendation is to keep simple integration layer between CPython and > .NET, hence metaclasses were/are not supported. > > > > But contributions are welcome! Although I expect this to be a tremendous > undertaking based on reviewing multiple sources. > > > > Note that metaclass is a very different low-level concept from > higher-level abstract classes in .NET, hence direct mapping (e.g. > `.register()` ) is not feasible. > > > > Thanks, > > Denis > > > > On Thu, Sep 22, 2016 at 10:04 AM, Henning Moeller > wrote: > > Hello out there, > > > > I?m trying to inherit from an abstract .NET base class in Python (2.7). > I?m a Python n00b but from what I understood? > > > > Here?s what I managed to do in Python only and which works fine: > > > > [File: room.py] ------- > > import abc > > > > class Room(object): > > def __init__(self, door): > > self.door = door > > > > def open(self): > > self.door.open() > > > > def close(self): > > self.door.close() > > > > def is_open(self): > > return self.door.is_open() > > > > class Door(object): > > __metaclass__ = abc.ABCMeta > > > > def open(self): > > if not self.is_open(): > > self.toggle() > > > > def close(self): > > if self.is_open(): > > self.toggle() > > > > @abc.abstractmethod > > def is_open(self): > > pass > > > > @abc.abstractmethod > > def toggle(self): > > pass > > > > class StringDoor(Door): > > def __init__(self): > > self.status = "closed" > > > > def is_open(self): > > return self.status == "open" > > > > def toggle(self): > > if self.status == "open": > > self.status = "closed" > > else: > > self.status = "open" > > > > class BooleanDoor(Door): > > def __init__(self): > > self.status = True > > > > def is_open(self): > > return self.status > > > > def toggle(self): > > self.status = not (self.status) > > > > Door.register(StringDoor) > > Door.register(BooleanDoor) > > ------- > > > > Now, all I did was to replace the abstract base class Door by a C# > representation: > > > > [File: PythonAbstractBaseClass.dll] ------- > > namespace PythonAbstractBaseClass > > { > > public abstract class Door > > { > > public virtual void Open() > > { > > if (!IsOpen()) > > Toggle(); > > } > > > > public virtual void Close() > > { > > if (IsOpen()) > > Toggle(); > > } > > > > public abstract bool IsOpen(); > > public abstract void Toggle(); > > } > > } > > ------- > > > > Removing Door from the Python part and importing it from the .NET assembly > instead, I end up with this: > > > > [File: room2.py] ------- > > import clr > > import abc > > from PythonAbstractBaseClass import Door > > > > class Room(object): > > def __init__(self, door): > > self.door = door > > > > def open(self): > > self.door.open() > > > > def close(self): > > self.door.close() > > > > def is_open(self): > > return self.door.is_open() > > > > class StringDoor(Door): > > def __init__(self): > > self.status = "closed" > > > > def is_open(self): > > return self.status == "open" > > > > def toggle(self): > > if self.status == "open": > > self.status = "closed" > > else: > > self.status = "open" > > > > class BooleanDoor(Door): > > def __init__(self): > > self.status = True > > > > def is_open(self): > > return self.status > > > > def toggle(self): > > self.status = not (self.status) > > > > Door.register(StringDoor) > > Door.register(BooleanDoor) > > ------- > > > > But this fails with the following error message: > > > > Door.register(StringDoor) > > AttributeError: type object 'Door' has no attribute 'register' > > > > From what I understood about abc.ABCMeta, this metaclass contributes the > ?register? method. It seems that abstract C# classes do not come with the > same metaclass. They instead come with metaclass ?CLR Metatype? which > obviously does not provide ?register?. > > > > But if I drop the call to ?register?, on instantiating one of the derived > classes, I receive the error message > > > > sdoor = StringDoor() > > TypeError: cannot instantiate abstract class > > > > Is there a way to inherit from an abstract .NET class or is this is > missing feature? > > > > Thanks in advance, > > > > Henning > > > > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > > > > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > > > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.akhiyarov at gmail.com Tue Sep 27 22:37:13 2016 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Tue, 27 Sep 2016 21:37:13 -0500 Subject: [Python.NET] pythonnet to drop support for Python 2.6 Message-ID: We have Travis CI (Linux and OSX soon) for Python 2.6, but no CI for Python 2.6 on Appveyor (Windows). Python 2.6 was dropped by major packages (even numpy is dropping support in v1.12.0) and Python 3.6 is going to be added soon. If anyone is against dropping Python 2.6, then let's collect feedback. https://github.com/pythonnet/pythonnet/issues/270 -------------- next part -------------- An HTML attachment was scrubbed... URL: