MCPcopy Index your code
hub / github.com/clips/pattern / Group

Class Group

pattern/db/__init__.py:1104–1138  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1102 return (field, value, comparison)
1103
1104class Group(list):
1105
1106 def __init__(self, *args, **kwargs):
1107 """ A list of SQL WHERE filters combined with AND/OR logical operator.
1108 """
1109 list.__init__(self, args)
1110 self.operator = kwargs.get("operator", AND)
1111
1112 def SQL(self, **kwargs):
1113 """ For example, filter for small pets with tails or wings
1114 (which is not the same as small pets with tails or pets with wings):
1115 >>> Group(
1116 >>> filter("type", "pet"),
1117 >>> filter("weight", (4,6), ":"),
1118 >>> Group(
1119 >>> filter("tail", True),
1120 >>> filter("wing", True), operator=OR))
1121 Yields:
1122 "type='pet' and weight between 4 and 6 and (tail=1 or wing=1)"
1123 """
1124 # Remember to pass the right escape() function as optional parameter.
1125 a = []
1126 for filter in self:
1127 # Traverse subgroups recursively.
1128 if isinstance(filter, Group):
1129 a.append("(%s)" % filter.SQL(**kwargs))
1130 continue
1131 # Convert filter() to string with cmp() - see above.
1132 if isinstance(filter, (list, tuple)):
1133 a.append(cmp(*filter, **kwargs))
1134 continue
1135 raise TypeError, "Group can contain other Group or filter(), not %s" % type(filter)
1136 return (" %s " % self.operator).join(a)
1137
1138 sql = SQL
1139
1140def all(*args):
1141 """ Returns a group of filters combined with AND.

Callers 3

allFunction · 0.70
anyFunction · 0.70
__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…