Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fe0fec0

Browse files
refactor: improve error messages and assertions in Set class
1 parent 9366a96 commit fe0fec0

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

‎src/fuzzylogic/classes.py‎

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ class FuzzyWarning(UserWarning):
3939
pass
4040

4141

42-
NO_DOMAIN_TO_COMPARE = FuzzyWarning("No domains to compare.")
42+
NO_DOMAIN_TO_COMPARE = "No domains to compare to."
4343
CANT_COMPARE_DOMAINS = "Can't compare different domains."
44-
NO_DOMAIN = FuzzyWarning("No domain.")
44+
NO_DOMAIN = "No domain defined."
4545

4646

4747
class Domain:
@@ -296,65 +296,56 @@ def f(x: float):
296296
def __eq__(self, other: object) -> bool:
297297
"""A set is equal with another if both return the same values over the same range."""
298298
if self.domain is None or not isinstance(other, Set) or other.domain is None:
299-
# It would require complete AST analysis to check whether both Sets
299+
# It would require complete bytecode analysis to check whether both Sets
300300
# represent the same recursive functions -
301301
# additionally, there are infinitely many mathematically equivalent
302302
# functions that don't have the same bytecode...
303303
raise FuzzyWarning("Impossible to determine.")
304-
else:
305-
# however, if domains ARE assigned (whether or not it's the same domain),
306-
# we simply can check if they map to the same values
307-
return np.array_equal(self.array(), other.array())
304+
305+
# however, if domains ARE assigned (whether or not it's the same domain),
306+
# we simply can check if they map to the same values
307+
return np.array_equal(self.array(), other.array())
308308

309309
def __le__(self, other: Set) -> bool:
310310
"""If this <= other, it means this is a subset of the other."""
311+
assert self.domain is not None and other.domain is not None, NO_DOMAIN_TO_COMPARE
311312
assert self.domain == other.domain, CANT_COMPARE_DOMAINS
312-
if self.domain is None or other.domain is None:
313-
raise NO_DOMAIN_TO_COMPARE
314313
return all(np.less_equal(self.array(), other.array()))
315314

316315
def __lt__(self, other: Set) -> bool:
317316
"""If this < other, it means this is a proper subset of the other."""
317+
assert self.domain is not None and other.domain is not None, NO_DOMAIN_TO_COMPARE
318318
assert self.domain == other.domain, CANT_COMPARE_DOMAINS
319-
if self.domain is None or other.domain is None:
320-
raise NO_DOMAIN_TO_COMPARE
321319
return all(np.less(self.array(), other.array()))
322320

323321
def __ge__(self, other: Set) -> bool:
324322
"""If this >= other, it means this is a superset of the other."""
323+
assert self.domain is not None and other.domain is not None, NO_DOMAIN_TO_COMPARE
325324
assert self.domain == other.domain, CANT_COMPARE_DOMAINS
326-
if self.domain is None or other.domain is None:
327-
raise NO_DOMAIN_TO_COMPARE
328325
return all(np.greater_equal(self.array(), other.array()))
329326

330327
def __gt__(self, other: Set) -> bool:
331328
"""If this > other, it means this is a proper superset of the other."""
329+
assert self.domain is not None and other.domain is not None, NO_DOMAIN_TO_COMPARE
332330
assert self.domain == other.domain, CANT_COMPARE_DOMAINS
333-
if self.domain is None or other.domain is None:
334-
raise NO_DOMAIN_TO_COMPARE
335331
return all(np.greater(self.array(), other.array()))
336332

337333
def __len__(self) -> int:
338334
"""Number of membership values in the set, defined by bounds and resolution of domain."""
339-
if self.domain is None:
340-
raise NO_DOMAIN
335+
assert self.domain is not None, NO_DOMAIN
341336
return len(self.array())
342337

343338
@property
344339
def cardinality(self) -> float:
345340
"""The sum of all values in the set."""
346-
if self.domain is None:
347-
raise NO_DOMAIN
341+
assert self.domain is not None, NO_DOMAIN
348342
return sum(self.array())
349343

350344
@property
351345
def relative_cardinality(self) -> float:
352346
"""Relative cardinality is the sum of all membership values by float of all values."""
353-
if self.domain is None:
354-
raise NO_DOMAIN
355-
if len(self) == 0:
356-
# this is highly unlikely and only possible with res=inf but still..
357-
raise FuzzyWarning("The domain has no element.")
347+
assert self.domain is not None, NO_DOMAIN
348+
assert len(self) > 0, "The domain has no element." # only possible with step=inf, but still..
358349
return self.cardinality / len(self)
359350

360351
def concentrated(self) -> Set:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /