Is there anyway to selectively inherit/import a class/module?

P

Peter

I have a program that talks to a device via a serial interface.
Structurally it looks like this:

Program A -> module B -> Serial

I want to add a protocol layer around the serial port without
modifying any of the modules above and I want to be able to use BOTH
cases of the program whenever convenient, so at first it seemed like a
simple case of sub-classing the Serial class and then (this is the
problem) somehow use either the original serial class definition when
I wanted the original program functionality or the new class when I
wanted the extra layer present.

Module B has

import serial

so my first thought was to put the new module in the current directory
and call it "serial.py". This would allow me to run the program with
the new Serial class and when I didn't want that extra layer, all I
had to do was rename the module to something else and let Module B
pick up serial from the site-package area. But (obviously) this
doesn't work because the new class's first few statements are:

import serial

Class Serial(serial.Serial):

So when Module B does the import (and gets the new class definition),
I get an error message (as the new definition is loaded):

AttributeError: 'module' object has no attribute 'Serial' on the Class
declaration statement.

Is there anyway to have Module B use the new class without modifying
the content of Module B?

The only other alternative I can think of is to create a copy of the
original program, rename the new class file and have two complete
copies of the program but that is not ideal :)

Thanks
Peter
 
S

Steven D'Aprano

I have a program that talks to a device via a serial interface.
Structurally it looks like this:

Program A -> module B -> Serial

I don't understand what this means. Program A points to module B, which
points to ... what is Serial? Where is it? Is it a class inside the
module, or another module?

I want to add a protocol layer around the serial port without modifying
any of the modules above and I want to be able to use BOTH cases of the
program whenever convenient,

Where are you using them from? Program A, or another program?

I don't see any way to add the extra functionality to Program A without
modifying Program A. But if you're okay with that, here's a way to
conditionally define a class to use:

# inside Program A
import moduleB

if condition:
Serial = moduleB.Serial # I assume moduleB has a Serial class
else:
class Serial(moduleB.Serial): # subclass
def mymethod(self):
pass


connection = Serial(...)


so at first it seemed like a simple case of
sub-classing the Serial class and then (this is the problem) somehow use
either the original serial class definition when I wanted the original
program functionality or the new class when I wanted the extra layer
present.

No problem. Here's another way to do it:

from moduleB import Serial as SimpleSerial
class ComplexSerial(SimpleSerial):
...

And now you can use both at the same time.
 
P

Peter

I don't understand what this means. Program A points to module B, which
points to ... what is Serial? Where is it? Is it a class inside the
module, or another module?


Where are you using them from? Program A, or another program?

I don't see any way to add the extra functionality to Program A without
modifying Program A. But if you're okay with that, here's a way to
conditionally define a class to use:

# inside Program A
import moduleB

if condition:
    Serial = moduleB.Serial  # I assume moduleB has a Serial class
else:
    class Serial(moduleB.Serial):  # subclass
        def mymethod(self):
            pass

connection = Serial(...)


No problem. Here's another way to do it:

from moduleB import Serial as SimpleSerial
class ComplexSerial(SimpleSerial):
    ...

And now you can use both at the same time.

Thanks :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top