Skip to content

Commit bd7a3a1

Browse files
committed
cleaned up classes.py
1 parent b05052c commit bd7a3a1

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/fuzzylogic/classes.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class FuzzyWarning(UserWarning):
3939
pass
4040

4141

42+
NO_DOMAIN_TO_COMPARE = FuzzyWarning("No domains to compare.")
43+
CANT_COMPARE_DOMAINS = "Can't compare different domains."
44+
NO_DOMAIN = FuzzyWarning("No domain.")
45+
46+
4247
class Domain:
4348
"""
4449
A domain is a 'measurable' dimension of 'real' values like temperature.
@@ -83,8 +88,8 @@ def __init__(
8388
"""Define a domain."""
8489
assert low < high, "higher bound must be greater than lower."
8590
assert res > 0, "resolution can't be negative or zero"
86-
assert isinstance(name, str), "Name must be a string."
87-
assert str.isidentifier(name), "Name must be a valid identifier."
91+
assert isinstance(name, str), "Domain Name must be a string."
92+
assert str.isidentifier(name), "Domain Name must be a valid identifier."
8893
self._name = name
8994
self._high = high
9095
self._low = low
@@ -303,50 +308,50 @@ def __eq__(self, other: object) -> bool:
303308

304309
def __le__(self, other: Set) -> bool:
305310
"""If this <= other, it means this is a subset of the other."""
306-
assert self.domain == other.domain
311+
assert self.domain == other.domain, CANT_COMPARE_DOMAINS
307312
if self.domain is None or other.domain is None:
308-
raise FuzzyWarning("Can't compare without Domains.")
313+
raise NO_DOMAIN_TO_COMPARE
309314
return all(np.less_equal(self.array(), other.array()))
310315

311316
def __lt__(self, other: Set) -> bool:
312317
"""If this < other, it means this is a proper subset of the other."""
313-
assert self.domain == other.domain
318+
assert self.domain == other.domain, CANT_COMPARE_DOMAINS
314319
if self.domain is None or other.domain is None:
315-
raise FuzzyWarning("Can't compare without Domains.")
320+
raise NO_DOMAIN_TO_COMPARE
316321
return all(np.less(self.array(), other.array()))
317322

318323
def __ge__(self, other: Set) -> bool:
319324
"""If this >= other, it means this is a superset of the other."""
320-
assert self.domain == other.domain
325+
assert self.domain == other.domain, CANT_COMPARE_DOMAINS
321326
if self.domain is None or other.domain is None:
322-
raise FuzzyWarning("Can't compare without Domains.")
327+
raise NO_DOMAIN_TO_COMPARE
323328
return all(np.greater_equal(self.array(), other.array()))
324329

325330
def __gt__(self, other: Set) -> bool:
326331
"""If this > other, it means this is a proper superset of the other."""
327-
assert self.domain == other.domain
332+
assert self.domain == other.domain, CANT_COMPARE_DOMAINS
328333
if self.domain is None or other.domain is None:
329-
raise FuzzyWarning("Can't compare without Domains.")
334+
raise NO_DOMAIN_TO_COMPARE
330335
return all(np.greater(self.array(), other.array()))
331336

332337
def __len__(self) -> int:
333338
"""Number of membership values in the set, defined by bounds and resolution of domain."""
334339
if self.domain is None:
335-
raise FuzzyWarning("No domain.")
340+
raise NO_DOMAIN
336341
return len(self.array())
337342

338343
@property
339344
def cardinality(self) -> float:
340345
"""The sum of all values in the set."""
341346
if self.domain is None:
342-
raise FuzzyWarning("No domain.")
347+
raise NO_DOMAIN
343348
return sum(self.array())
344349

345350
@property
346351
def relative_cardinality(self) -> float:
347352
"""Relative cardinality is the sum of all membership values by float of all values."""
348353
if self.domain is None:
349-
raise FuzzyWarning("No domain.")
354+
raise NO_DOMAIN
350355
if len(self) == 0:
351356
# this is highly unlikely and only possible with res=inf but still..
352357
raise FuzzyWarning("The domain has no element.")

0 commit comments

Comments
 (0)