Skip to content

Commit 158e34d

Browse files
committed
cleaned up Rules
1 parent bd7a3a1 commit 158e34d

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/fuzzylogic/classes.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,6 @@ def __repr__(self) -> str:
428428
"""
429429
Return a string representation of the Set that reconstructs the set with eval().
430430
"""
431-
432-
# experimental
433-
# x = f"{self.func.__qualname__.split('.')[0]}({self.func.__closure__[0].cell_contents.__code__.co_nlocals}))" # noqa: E501
434-
# print(x)
435-
436431
if self.domain is not None:
437432
return f"{self.domain._name}.{self.name}" # type: ignore
438433
return f"Set(({self.func.__qualname__})"
@@ -461,19 +456,28 @@ class Rule:
461456

462457
type T = Rule
463458

464-
def __init__(self, conditions_in: dict[Iterable[Set] | Set, Set]):
459+
def __init__(
460+
self,
461+
*args: Rule | dict[Iterable[Set] | Set, Set],
462+
) -> None:
463+
"""Define a rule with conditions and a target set."""
465464
self.conditions: dict[frozenset[Set], Set] = {}
466-
for if_sets, then_set in conditions_in.items():
467-
if isinstance(if_sets, Set):
468-
if_sets = (if_sets,)
469-
self.conditions[frozenset(if_sets)] = then_set
465+
for arg in args:
466+
if isinstance(arg, Rule):
467+
self.conditions |= arg.conditions
468+
elif isinstance(arg, dict):
469+
for if_sets, then_set in arg.items():
470+
if isinstance(if_sets, Set):
471+
if_sets = (if_sets,)
472+
self.conditions[frozenset(if_sets)] = then_set # type: ignore
473+
else:
474+
raise TypeError(f"Expected any number of Rule or dict[Set, Set], got {type(arg).__name__}.")
470475

471476
def __add__(self, other: Rule) -> Rule:
472477
return Rule({**self.conditions, **other.conditions})
473478

474479
def __radd__(self, other: Rule | int) -> Rule:
475-
# we're using sum(..)
476-
if isinstance(other, int):
480+
if isinstance(other, int): # as sum(...) does implicitely 0 + ...
477481
return self
478482
return Rule({**self.conditions, **other.conditions})
479483

0 commit comments

Comments
 (0)