Skip to content

Commit 67fe25d

Browse files
committed
refactor: defensive runtime checks are now asserts
1 parent fe0fec0 commit 67fe25d

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/fuzzylogic/classes.py

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

4141

42-
NO_DOMAIN_TO_COMPARE = "No domains to compare to."
43-
CANT_COMPARE_DOMAINS = "Can't compare different domains."
42+
NO_DOMAIN_TO_COMPARE = "Domain can't work with no domain."
43+
CANT_COMPARE_DOMAINS = "Can't work with different domains."
4444
NO_DOMAIN = "No domain defined."
4545

4646

@@ -253,14 +253,17 @@ def __call__(
253253

254254
def __invert__(self) -> Set:
255255
"""Return a new set with 1 - function."""
256+
assert self.domain is not None, NO_DOMAIN
256257
return Set(inv(self.func), domain=self.domain)
257258

258259
def __neg__(self) -> Set:
259260
"""Synonyme for invert."""
261+
assert self.domain is not None, NO_DOMAIN
260262
return Set(inv(self.func), domain=self.domain)
261263

262264
def __and__(self, other: Set) -> Set:
263265
"""Return a new set with modified function."""
266+
assert self.domain is not None and other.domain is not None, NO_DOMAIN_TO_COMPARE
264267
assert self.domain == other.domain
265268
return Set(MIN(self.func, other.func), domain=self.domain)
266269

@@ -380,8 +383,7 @@ def multiplied(self, n: float) -> Set:
380383

381384
def plot(self) -> None:
382385
"""Graph the set in the given domain."""
383-
if self.domain is None:
384-
raise FuzzyWarning("No domain assigned, cannot plot.")
386+
assert self.domain is not None, NO_DOMAIN
385387
R = self.domain.range
386388
V = [self.func(x) for x in R]
387389
if plt:
@@ -393,21 +395,19 @@ def plot(self) -> None:
393395

394396
def array(self) -> Array:
395397
"""Return an array of all values for this set within the given domain."""
396-
if self.domain is None:
397-
raise FuzzyWarning("No domain assigned.")
398+
assert self.domain is not None, NO_DOMAIN
398399
return np.fromiter((self.func(x) for x in self.domain.range), float)
399400

400401
def range(self) -> Array:
401402
"""Return the range of the domain."""
402-
if self.domain is None:
403-
raise FuzzyWarning("No domain assigned.")
403+
assert self.domain is not None, NO_DOMAIN
404404
return self.domain.range
405405

406406
def center_of_gravity(self) -> float:
407407
"""Return the center of gravity for this distribution, within the given domain."""
408408
if self.__center_of_gravity is not None:
409409
return self.__center_of_gravity
410-
assert self.domain is not None, "No center of gravity with no domain."
410+
assert self.domain is not None, NO_DOMAIN
411411
weights = self.array()
412412
if sum(weights) == 0:
413413
return 0
@@ -431,8 +431,7 @@ def __str__(self) -> str:
431431

432432
def normalized(self) -> Set:
433433
"""Return a set that is normalized *for this domain* with 1 as max."""
434-
if self.domain is None:
435-
raise FuzzyWarning("Can't normalize without domain.")
434+
assert self.domain is not None, NO_DOMAIN
436435
return Set(normalize(max(self.array()), self.func), domain=self.domain)
437436

438437
def __hash__(self) -> int:

0 commit comments

Comments
 (0)