Python Forum

Full Version: New to Python and oop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am attempting to write a Python object-oriented program to control the state of three relays. My partial solution isn't eloquent. Can you please show me how to duplicate the below code using proper oop code?
Thank you in advance.
Bob

# relay1 = is controlled via pin 37
# relay2 = is controlled via pin 38
# relay3 = is controlled via pin 40

class Relays:
    def __init__(self, relay_num, relay_state):
        self.relay_num = relay_num # relay1, relay2 or relay3
        self.relay_state = relay_state # 1 = on, 2 = off

relay1 = Relays(1, 0)
relay2 = Relays(2, 1)
relay3 = Relays(3, 1)

print(relay1.relay_num)
print(relay1.relay_state)
print('-----')

print(relay2.relay_num)
print(relay2.relay_state)
print('-----')

print(relay3.relay_num)
print(relay3.relay_state)
print('-----')
You can remove the duplicated code
# relay1 = is controlled via pin 37
# relay2 = is controlled via pin 38
# relay3 = is controlled via pin 40
 
class Relays:
    def __init__(self, relay_num, relay_state):
        self.relay_num = relay_num # relay1, relay2 or relay3
        self.relay_state = relay_state # 1 = on, 2 = off
 
relay1 = Relays(1, 0)
relay2 = Relays(2, 1)
relay3 = Relays(3, 1)

for relay in (relay1, relay2, relay3):
    print(relay.relay_num)
    print(relay.relay_state)
    print('-----')
Thank you!

In a perfect opp world, should each relay be a method of class Relay? Perhaps, I asked the wrong question. How should one model a three relay system using oop techniques?
No each relay should not be a method of its self.
Methods in relay will do something with their own instance of relay.

Define a three relay system
A three relay system as defined in my original post.
The original post is just creating 3 instances of relay and outputting their attributes, i don't see any sign of a system or what you are referring to when you say system.
Understood. Thank you again for your assistance.
I would say your current class is actually the system. So from OOP perspective I would say you need a class for the relay and a class for the system (i.e. some changes to your current class).

Very basic example - i.e. a lot can be changed, improved, made differently, you can add extra helper attributes, etc.

class Relay:
    def __init__(self, relay_id=None, state=False):
        self.id = relay_id
        self.state = state
        
    def toggle(self):
        self.state = not self.state
        
        
class RelaySystem:
    def __init__(self, num_relays=0):
        self.relays = tuple(Relay(relay_id=f'{rel_id}') for rel_id in range(num_relays))
        
    def set_all(self, state=False):
        for relay in self.relays:
            relay.state = state
            
    def print_report(self):
        print(f'Relay system with {len(self.relays)} relays.')
        for relay in self.relays:
            print(f"Relay id={relay.id} is {'ON' if relay.state else 'OFF'}")
        print()



if __name__ == '__main__':          
    relay_board = RelaySystem(3)
    
    relay_board.relays[0].state=True
    relay_board.print_report()

    relay_board.set_all(True)
    relay_board.print_report()
                
    relay_board.relays[2].toggle()
    relay_board.print_report()
Output:
Relay system with 3 relays. Relay id=0 is ON Relay id=1 is OFF Relay id=2 is OFF Relay system with 3 relays. Relay id=0 is ON Relay id=1 is ON Relay id=2 is ON Relay system with 3 relays. Relay id=0 is ON Relay id=1 is ON Relay id=2 is OFF >>>
Thank you Buran!