From b7504a0d08c2e7e5fef1748e6581cea1b17f3029 Mon Sep 17 00:00:00 2001 From: Willem Van Onsem Date: Wed, 6 Mar 2024 11:27:52 +0100 Subject: [PATCH] add __bool__ for fsm --- interegular/fsm.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/interegular/fsm.py b/interegular/fsm.py index 67e5ca4..03ec230 100644 --- a/interegular/fsm.py +++ b/interegular/fsm.py @@ -761,6 +761,15 @@ def get_num_strings(state): return get_num_strings(self.initial) + def __bool__(self): + """ + Check if the FSM accepts at least one string (i.e. is not empty). This + is better than checking len(.), which is computationally more expensive, + and will raise an OverflowError in case there are an infinite amount of + strings, whereas bool(.) will just return True. + """ + return not self.empty() + def __len__(self): """ Consider the FSM as a set of strings and return the cardinality of that