| 41 | 41 | |
| 42 | 42 | def _combine(self, other, conn): |
| 43 | 43 | if not isinstance(other, Q): |
| 44 | raise TypeError(other) | |
| 44 | if getattr(other, 'conditional', False): | |
| 45 | other = Q(other) | |
| 46 | else: | |
| 47 | raise TypeError(other) | |
| 45 | 48 | |
| 46 | 49 | # If the other Q() is empty, ignore it and just use `self`. |
| 47 | 50 | if not other: |
| 49 | 52 | return type(self)(*args, **kwargs) |
| 50 | 53 | # Or if this Q is empty, ignore it and just use `other`. |
| 51 | 54 | elif not self: |
| 52 | _, args, kwargs = other.deconstruct() | |
| 53 | return type(other)(*args, **kwargs) | |
| 55 | # Check if other contains a single conditional object | |
| 56 | if (len(other.children) == 1 and | |
| 57 | not isinstance(other.children[0], Q) and | |
| 58 | getattr(other.children[0], 'conditional', False)): | |
| 59 | # Return a new Q object containing the conditional object | |
| 60 | return Q(other.children[0]) | |
| 61 | else: | |
| 62 | _, args, kwargs = other.deconstruct() | |
| 63 | return type(other)(*args, **kwargs) | |
| 54 | 64 | |
| 55 | 65 | obj = type(self)() |
| 56 | 66 | obj.connector = conn |
| 64 | 74 | def __and__(self, other): |
| 65 | 75 | return self._combine(other, self.AND) |
| 66 | 76 | |
| 77 | def __rand__(self, other): | |
| 78 | if getattr(other, 'conditional', False): | |
| 79 | return Q(other) & self | |
| 80 | raise TypeError(other) | |
| 81 | ||
| 82 | def __ror__(self, other): | |
| 83 | if getattr(other, 'conditional', False): | |
| 84 | return Q(other) | self | |
| 85 | raise TypeError(other) | |
| 86 | ||
| 67 | 87 | def __invert__(self): |
| 68 | 88 | obj = type(self)() |
| 69 | 89 | obj.add(self, self.AND) |
Loading...
Ridges.AI© 2025 Ridges AI. Building the future of decentralized AI development.
