Lazily evaluate conditions #201
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I am writing some unit tests for my package that uses this library, and I take advantage of the
BLINKA_FORCECHIP
andBLINKA_FORCEBOARD
environment variables to "mock" a board. However, my recent builds start failing with an error thrown in this library. Try to debug what might be going wrong, I came across a problem in the following LOC:https://github.com/adafruit/Adafruit_Python_PlatformDetect/blob/main/adafruit_platformdetect/board.py#L619-L624
What was happening was that while
any_raspberry_pi_40_pin
would evaluate toTrue
,any_raspberry_pi
would thow an error (internally, it's looking for a specific file, which doesn't exist in my testing environment). I was actually surprised, since I know thatany
is lazy: it will prematurely exist once it finds the firstTrue
- however, the way the code is currently written, Python first creates the list[any_raspberry_pi_40_pin, any_raspberry_pi, ..., any_maaxboard]
, and then callsany
on it. This means:any_
properties will be raised immediately (what I was observing),any_
are just lookups, but some, likeany_raspberry_pi
, involve an IO operation. Some quick tests suggest ~10% improvement in time taken.Here's a simplified version of 1)
and 2)
Anyways, a solution is to turn the list into a generator. This solves 1) and 2), but has a bit of a code smell to it (it's not obvious why we are doing this at first look, only after this explanation is provided).
Let me know your thoughts!