Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class help
#1
Can anyone help with the code below. I was wondering whether the if statement should live within the class?. When I have tried this before I can't seem to find a way to print the status using the __str__ method

class Device:

    def __init__(self, dev_id, dev_status):
        self.dev_id = dev_id
        self.dev_status = dev_status

    def __str__(self):
        return f'{self.dev_id} - {self.dev_status}'


#output from monitoring device
output = 'DEVICE,281,3,4'

output_split = output.split(',')

#should/could this live within the class object?
if output_split[3] == '3':
    status = 'Active'
elif output_split[3] == '4':
    status = 'Inactive'

#here I was send output_split[3] rather than status
occupancy_sensor1 = Device(output_split[1], status)

print(occupancy_sensor1)
Reply
#2
I have managed to solve it...

class Device:

    def __init__(self, dev_id, dev_status):
        self.dev_id = dev_id
        self.dev_status = dev_status

    def __str__(self):
        if self.dev_status == '3':
            status = 'Active'
        elif self.dev_status == '4':
            status = 'Inactive'
        return f'{self.dev_id} - {status}'


output = 'DEVICE,281,3,3'
output_split = output.split(',')

occupancy_sensor1 = Device(output_split[1], output_split[3])
print(occupancy_sensor1)
Reply
#3
I don't know what each of the values are for in your output list but, here is another way

from random import choice
class Device:
    def __init__(self, output, id):
        self.output = output
        self.id = id
        self.status = 'Inactive'

        if self.output == '4':
            self.status = 'Active'

    def __str__(self):
        return f'ID: {self.id} | Status: {self.status}'

output = f'DEVICE,281,3,{choice(["3","4"])}'.split(',')

sensor = Device(output[3], output[1])
print(sensor)
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
(Dec-18-2022, 05:31 PM)menator01 Wrote: I don't know what each of the values are for in your output list but, here is another way

from random import choice
class Device:
    def __init__(self, output, id):
        self.output = output
        self.id = id
        self.status = 'Inactive'

        if self.output == '4':
            self.status = 'Active'

    def __str__(self):
        return f'ID: {self.id} | Status: {self.status}'

output = f'DEVICE,281,3,{choice(["3","4"])}'.split(',')

sensor = Device(output[3], output[1])
print(sensor)

Thanks, I've not seen the choice method before. Smile
Reply
#5
I prefer unpacking to indexing.
message_type, id, state, status = output.split(',')
occupancy_sensor1 = Device(id, status)
This code raises an exception if self.status is not '3' or '4'
return f'{self.dev_id} - {status}'
I agree with menator about how to save information like status. If status should be 'Active' or 'Inactive', use these in the class. If status should be 3 or 4, define an enumeration that gives these numerical values names. I don't see why status would ever be '3' or '4' other than that is how it is received in a message.
Reply
#6
(Dec-18-2022, 06:26 PM)deanhystad Wrote: I prefer unpacking to indexing.
message_type, id, state, status = output.split(',')
occupancy_sensor1 = Device(id, status)
This code raises an exception if self.status is not '3' or '4'
return f'{self.dev_id} - {status}'
I agree with menator about how to save information like status. If status should be 'Active' or 'Inactive', use these in the class. If status should be 3 or 4, define an enumeration that gives these numerical values names. I don't see why status would ever be '3' or '4' other than that is how it is received in a message.

I like the idea of unpacking. I'll have a look at that. The monitoring system I'm writing the code for uses a 3 to determine an Active state (or contact closed) and a 4 to determine the inactive state (contact open) so in theory it should never be anything else but I'm definitely a fan of build something that is robust.

I'm quite new to Python so all of this is great, helpful info. Thanks.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020