Skip to content

Commit 24ca23b

Browse files
committed
capture_exception for ensurer
This way an ensurer can be used in an if statement to see if a check passes
1 parent 14f4fc3 commit 24ca23b

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

puzzlepiece/piece.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ def ensurer(ensure_function):
160160
function - for example checking if a laser is connected ahead of trying to set its power.
161161
This way one ensurer can be written and used in multiple places easily.
162162
163-
For example, an ensurer can be defined as a Piece's method (in the main body of the class)::
163+
Note that **the method being decorated should raise an exception if the check fails!** This way
164+
execution will stop if the condition is not met. This is not mandatory though - custom behaviour
165+
is allowed.
166+
167+
For example, an ensurer can be defined as a Piece's method (in the main body of the class).::
164168
165169
@puzzlepiece.piece.ensurer
166170
def _ensure_connected(self):
@@ -179,14 +183,20 @@ def power(self, value):
179183
def wavelength(self, value):
180184
self.laser.set_wavelength(value)
181185
182-
It can also be called directly if preferred::
186+
It can also be called directly if preferred, optionally with `capture_exception=True`
187+
which will return True if the check passes, or False if the check raises an Exception::
183188
189+
# This should raise an Exception is the check fails
184190
self._ensure_connected()
191+
192+
# This will not raise an Exception is the check fails
193+
if self._ensure_connected(capture_exception=True):
194+
print("laser is connected!")
185195
"""
186196
# Decorating a class method with ensure makes it a decorator.
187197
# Here we create this decorator and return it.
188198
@wraps(ensure_function)
189-
def ensure_decorator(self, main_function=None):
199+
def ensure_decorator(self, main_function=None, capture_exception=False):
190200
if main_function is not None:
191201
# This means ensure_decorator was used as a decorator, and
192202
# main_function is the function being decorated. We therefore
@@ -200,5 +210,12 @@ def wrapped_main(self, *args, **kwargs):
200210
# If main_function is None, ensure_decorator has been called
201211
# directly instead of being used as a decorator, so we
202212
# just execute ensure_function
203-
ensure_function(self)
213+
if capture_exception:
214+
try:
215+
ensure_function(self)
216+
except:
217+
return False
218+
return True
219+
else:
220+
ensure_function(self)
204221
return ensure_decorator

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "puzzlepiece"
7-
version = "0.6.0"
7+
version = "0.6.1"
88
authors = [
99
{ name="Jakub Dranczewski", email="jakub.dranczewski@gmail.com" },
1010
]

0 commit comments

Comments
 (0)