1 class QChoicesAtom(QAtom): 2 """This class represents a query for an attribute with a discrete fixed 3 set of choices. 4 """ 5 def __init__(self, **kwargs): 6 QAtom.__init__(self, **kwargs) 7 8 # group all the search terms into one string 9 searchString = " ".join(self.search) 10 11 # if it's a numeric choice, then spit back the number 12 if searchString[0] in "1234556789": 13 search = int(search) 14 15 # if it's not numeric, test the choices until one matches 16 # and return its index 17 else: 18 for i, choice in enumerate(choicesDict[self.attribute]): 19 if choice.match(searchString): 20 search = i 21 break 22 23 # we should have assigned the search to a number, based on it either 24 # being a number, or one of the listed choices. Otherwise, raise 25 # an exception. 26 try: 27 self.search = search 28 except NameError: 29 raise ValueError( 30 "search not among defined choices for attribute") 31 32 33 def rawQuery(self): 34 35 if self.operator == "exact": 36 operator = (self.negative and " != " or " == ") 37 else: 38 raise ValueError( 39 "operator makes no sense for type/creator code query") 40 41 return self.attribute + operator + str(self.search)