peterhinch / micropython_remote

Capture and replay 433MHz remote control codes. Control remote switched power adaptors.
MIT License
66 stars 11 forks source link

Throw exception if file is missing/unreadable or key not available #2

Closed kevinkk525 closed 4 years ago

kevinkk525 commented 4 years ago

As the TX() class is of not much use if the file was unreadable or is missing, throwing an exception would be better than printing a debug output to the repl and just returning. This way I could catch that exception in my program without having to check hasattr(TX(), "_data") https://github.com/peterhinch/micropython_remote/blob/e14e6212580640c40a7b54ca8b1e259acd529a04/tx/__init__.py#L42

Also I noticed that getitem(self,key) also just prints an error message if the key was not found but doesn't raise the exception. This means I can't catch a failed sending attempt in my program as those errors just get silently ignored.

Was there a reason to implement it like this?

peterhinch commented 4 years ago

It was really for testing at the REPL. I guess the best answer is just to let the exception occur

    def __init__(self, pin, fname, reps=5):
        self._pin = pin
        self._reps = reps
        with open(fname, 'r') as f:
            self._data = ujson.load(f)

and likewise with __getitem__. I'll push an update today.