@@ -428,11 +428,6 @@ def __repr__(self) -> str:
428
428
"""
429
429
Return a string representation of the Set that reconstructs the set with eval().
430
430
"""
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
-
436
431
if self .domain is not None :
437
432
return f"{ self .domain ._name } .{ self .name } " # type: ignore
438
433
return f"Set(({ self .func .__qualname__ } )"
@@ -461,19 +456,28 @@ class Rule:
461
456
462
457
type T = Rule
463
458
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."""
465
464
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__ } ." )
470
475
471
476
def __add__ (self , other : Rule ) -> Rule :
472
477
return Rule ({** self .conditions , ** other .conditions })
473
478
474
479
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 + ...
477
481
return self
478
482
return Rule ({** self .conditions , ** other .conditions })
479
483
0 commit comments