Posts: 4
Threads: 1
Joined: Dec 2022
Dec-18-2022, 05:01 PM
(This post was last modified: Dec-18-2022, 05:01 PM by txrx.)
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)
Posts: 4
Threads: 1
Joined: Dec 2022
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)
Posts: 1,145
Threads: 114
Joined: Sep 2019
Dec-18-2022, 05:31 PM
(This post was last modified: Dec-18-2022, 05:31 PM by menator01.)
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)
Posts: 4
Threads: 1
Joined: Dec 2022
(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.
Posts: 6,798
Threads: 20
Joined: Feb 2020
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.
Posts: 4
Threads: 1
Joined: Dec 2022
Dec-18-2022, 07:50 PM
(This post was last modified: Dec-18-2022, 07:50 PM by txrx.)
(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.
|