Is there a standard pythonic way to return exceptions from threaded classes? /u/OhGodSoManyQuestions Python Education

I’m [re]writing a few dozen modules that wrap communications to devices connected via RPi.GPIO. I’m about to go back and add exception catching to all of the IO/OS communications. But the mix of synchronous and asynchronous methods is making it feel like a mess. I’d like to have just one clean technique for all cases, including errors in the __init__ method of classes. I’m leaning toward an async callback for everything but that’s going to complicate exception when calling synchronous methods.

As an example: here’s the meat of the simplest module. The get_value() method may be called in synchronous and asynchronous contexts. And it’s called when the class is instantiated. Is there and especially Pythonic way to return exception data to the code that uses this module?

# module: binary_input.py class Input(threading.Thread): def __init__( self, pin_number, data_callback=lambda x: None, pull_up_down=0, poll_interval=0, ): self.pin_number = pin_number self.data_callback = data_callback self.poll_interval = poll_interval match pull_up_down: case -1: GPIO.setup(self.pin_number, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) case 0: GPIO.setup(self.pin_number, GPIO.IN) case 1: GPIO.setup(self.pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP) self.pin_access_lock = threading.Lock() self.last_value = self.get_value() if poll_interval > 0: self.data_callback(self.last_value) threading.Thread.__init__(self) self.start() def get_value(self): with self.pin_access_lock: return GPIO.input(self.pin_number) def get_change(self): current_value = self.get_value() if current_value != self.last_value: self.last_value = current_value return (True, current_value) return (False, current_value) def run(self): while True: time.sleep(self.poll_interval) current_value = self.get_value() if current_value != self.last_value: self.data_callback(current_value) self.last_value = current_value 

submitted by /u/OhGodSoManyQuestions
[link] [comments]

​r/learnpython I’m [re]writing a few dozen modules that wrap communications to devices connected via RPi.GPIO. I’m about to go back and add exception catching to all of the IO/OS communications. But the mix of synchronous and asynchronous methods is making it feel like a mess. I’d like to have just one clean technique for all cases, including errors in the __init__ method of classes. I’m leaning toward an async callback for everything but that’s going to complicate exception when calling synchronous methods. As an example: here’s the meat of the simplest module. The get_value() method may be called in synchronous and asynchronous contexts. And it’s called when the class is instantiated. Is there and especially Pythonic way to return exception data to the code that uses this module? # module: binary_input.py class Input(threading.Thread): def __init__( self, pin_number, data_callback=lambda x: None, pull_up_down=0, poll_interval=0, ): self.pin_number = pin_number self.data_callback = data_callback self.poll_interval = poll_interval match pull_up_down: case -1: GPIO.setup(self.pin_number, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) case 0: GPIO.setup(self.pin_number, GPIO.IN) case 1: GPIO.setup(self.pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP) self.pin_access_lock = threading.Lock() self.last_value = self.get_value() if poll_interval > 0: self.data_callback(self.last_value) threading.Thread.__init__(self) self.start() def get_value(self): with self.pin_access_lock: return GPIO.input(self.pin_number) def get_change(self): current_value = self.get_value() if current_value != self.last_value: self.last_value = current_value return (True, current_value) return (False, current_value) def run(self): while True: time.sleep(self.poll_interval) current_value = self.get_value() if current_value != self.last_value: self.data_callback(current_value) self.last_value = current_value submitted by /u/OhGodSoManyQuestions [link] [comments] 

I’m [re]writing a few dozen modules that wrap communications to devices connected via RPi.GPIO. I’m about to go back and add exception catching to all of the IO/OS communications. But the mix of synchronous and asynchronous methods is making it feel like a mess. I’d like to have just one clean technique for all cases, including errors in the __init__ method of classes. I’m leaning toward an async callback for everything but that’s going to complicate exception when calling synchronous methods.

As an example: here’s the meat of the simplest module. The get_value() method may be called in synchronous and asynchronous contexts. And it’s called when the class is instantiated. Is there and especially Pythonic way to return exception data to the code that uses this module?

# module: binary_input.py class Input(threading.Thread): def __init__( self, pin_number, data_callback=lambda x: None, pull_up_down=0, poll_interval=0, ): self.pin_number = pin_number self.data_callback = data_callback self.poll_interval = poll_interval match pull_up_down: case -1: GPIO.setup(self.pin_number, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) case 0: GPIO.setup(self.pin_number, GPIO.IN) case 1: GPIO.setup(self.pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP) self.pin_access_lock = threading.Lock() self.last_value = self.get_value() if poll_interval > 0: self.data_callback(self.last_value) threading.Thread.__init__(self) self.start() def get_value(self): with self.pin_access_lock: return GPIO.input(self.pin_number) def get_change(self): current_value = self.get_value() if current_value != self.last_value: self.last_value = current_value return (True, current_value) return (False, current_value) def run(self): while True: time.sleep(self.poll_interval) current_value = self.get_value() if current_value != self.last_value: self.data_callback(current_value) self.last_value = current_value 

submitted by /u/OhGodSoManyQuestions
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *