From 952d7256392b4175f79b6bab8d635cc8d335349f Mon Sep 17 00:00:00 2001 From: iou1name Date: Sat, 8 Jun 2019 03:12:03 -0400 Subject: [PATCH] refactor from_dict() --- juice.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/juice.py b/juice.py index a83a166..30980ed 100644 --- a/juice.py +++ b/juice.py @@ -60,11 +60,8 @@ class RelayDevice: """ Initializes self with data from JSON dict. """ - self.id = data['id'] - self.description = data['description'] - self.location = data['location'] - self.ip_address = data['ip_address'] - self.sub_devices = {} + for key, value in data.items(): + setattr(self, key, value) for sub_dev_type, sub_devs in data['sub_devices'].items(): if sub_dev_type == 'RelayOutlet': self.sub_devices[sub_dev_type] = [] @@ -90,10 +87,8 @@ class RelayOutlet: """ Initializes self with data from JSON dict. """ - self.id = data['id'] - self.description = data['description'] - self.gpio = data['gpio'] - self.state = data['state'] + for key, value in data.items(): + setattr(self, key, value) return self