Nesting and join more files

I have 3 files in python. The goal is to change the color of the LED on my hardware connected through the serial port to the PC.

  1. MainProgram.py - UI - user will choose color and brightness there.
  2. LEDClass.py - There are methods like setColor(self, color), setBrightness(self, bright..)
  3. SerialLinkClass.py - Low layer for sending the data to my HW. sendDataToHW(self,byteArray...)

My actual solution is to create an instance for the serial port and for LED in MainProgram.py.
But I am not sure what is the best practise in python to do that - especially passing the SerialPort to setColor function. Do you think this is right solution of my problem?

I just have this:

SerialPort = SerialLink()
SerialPort.Open(9600)
    
LED = LEDClass()
LED.setColor("blue",SerialPort)

… and the setColor is like:

def setColor(self,color,serialPort):
    serialPort.sendDataToHW(color)

Thank you very much.

I have 3 files in python. The goal is to change the color of the LED on
my hardware connected through the serial port to the PC.

  1. MainProgram.py - UI - user will choose color and brightness there.
  2. LEDClass.py - There are methods like setColor(self, color), setBrightness(self, bright..)
  3. SerialLinkClass.py - Low layer for sending the data to my HW. sendDataToHW(self,byteArray...)

My actual solution is to create an instance for the serial port and for LED in MainProgram.py.
But I am not sure what is the best practise in python to do that - especially passing the SerialPort to setColor function. Do you think this is right solution of my problem?

I just have this:

I presume you’ve got some import statements to pull in the other files.

SerialPort = SerialLink()
SerialPort.Open(9600)

LED = LEDClass()
LED.setColor(“blue”,SerialPort)

… and the setColor is like:

def setColor(self,color,serialPort):
serialPort.sendDataToHW(color)

Looks basicly ok to me.

However, notice that you’re not using “self” above. Normally the purpose
of a class is twofold: to group closely related functions together in a
nice namespace, but also to construct instances which have some state.

Not using “self” is a clue that you’re not making use of the class
instance (class is LEDClass, the instance is LED). Sometimes that is
reasonable, but usually it implies that there’s some instance state
you’re ignoring (or have not implemented).

In your case, it might be reasonable for your LED class to represent a
specific LED by virtue of a specific serial port. It’s kind of
irrelevant that your code only has one right now.

So the init for your LED class might look like this:

def __init__(self, serial_port):
    self.serial_port = serial_port

and the matching setColor method like this:

def setColor(self, color):
    self.serial_port.sendDataToHW(color)

which means your setup would look like this:

SerialPort = SerialLink()
SerialPort.Open(9600)

LED = LEDClass(SerialPort)

and your color setting like this:

LED.setColor("blue")

because the LED now knows how it talks (via the serial port).

Similarly your SerialLink might be told what device to use, though it
may well make that optional, and have a sensible default such as
whatever device it already uses implicitly.

Cheers,
Cameron Simpson cs@cskk.id.au

Hi Cameron,

Thank you very much, this is exactly what I needed to know. I will edit my code :slight_smile:

best regards, Jan.