Creating Python Classes Derived from C# Class

Sorry if this is a repeat, but I don’t think it is.

I’m wondering if there’s a way to create an OpenTAP Python class that gets included in a plugin (deriving from Instrument, TestStep, DUT, etc.) and is made available in TAP, that derives directly from a C# class. For example, if I want to create a Python Instrument class that derives from the C# ScpiInstrument class, it doesn’t get included (compiled? transpiled?) in the plugin. If I’m unable to do this, this means that all the SCPI features of OpenTAP are unavailable in Python, if I’m not mistaken.

1 Like

@john.berlien I’m not sure I am completely following. In general you can mix C# and Python, either calling C# from Python or Python from C#. That is the main benefit provided from Python.NET, and part of the Python plugin provides this wrapper. But for ScpiInstrument specifically, you can see this somewhat done in this example:
TapApiFromPython/ · master · OpenTAP / Plugins / python · GitLab

Thanks for the link Brennen! That is interesting - but not what I was referring to. I meant something like this:

from PythonTap import *

import OpenTap

from OpenTap import ScpiInstrument

@Attribute(OpenTap.DisplayAttribute, "SCPI VNA", "VNA driver based on ScpiInstrument class.", "My Python Plugin", "VNA")

class ScpiVNA(ScpiInstrument):

    """SCPI-based VNA class"""

    def __init__(self):

        super(ScpiVNA, self).__init__()

        self.Name = "ScpiVNA"

        self.VisaAddress = "TCPIP0::localhost::hislip0::INSTR"

It’s not getting included in the plugin build process. From reading through, doesn’t seem like it’s meant to. It doesn’t look like Python subclasses of C# classes are meant to be included as part of the “plugin types”.

1 Like

Yeah this might be a gap from the instrument side specifically. What we have seen more so far is that an Instrument Plugin is entirely C#, like it could be for a VNA for instance, and then the Test Steps are Python. But to be honest I’m not sure if this was intentional/limitation, or if it is just a gap since someone hasn’t needed it yet. @rolf_madsen ideas?

Yes I figured that’s how you’d get around it, since you can use C# classes in Python. To me, this is a more significant limitation, as this would apply to any OpenTAP classes written in C#; you wouldn’t be able to build on them in a Python plugin. I think ScpiInstrument is just the most obvious example. But this means an existing OpenTAP code base couldn’t be built on in Python.

1 Like

Creating a new base class that is supported in all aspects in OpenTap is quite complicated.

It is however possible to build on plugins created in OpenTap, you’ll just have to not use inheritance.

1 Like

Oh - so you mean essentially create a wrapper around the class? Simply expose the methods and properties written in C# through Python with a new Python class, inheriting one of the existing supported base classes? Not elegant, like using inheritance itself… But doable I suppose.

What would you think about using ScpiInstrument as an example of doing this? Put that into the Python plugin somewhere.

1 Like

That is not exactly what I mean, but yes, if what you want is a PythonScpiInstrument that is maybe exactly what I am saying… I didn’t think it through that way. Anyway, how many methods do you really need on there? :slight_smile:

1 Like

Just the public ones of course would need a wrapper method in Python.

1 Like