def filterByQuery(arr, ors, caseSensitive=False): if isinstance(ors, tuple): ors = [[ors]] # pprint(ors) if len(ors) < 1: return arr results = [] for item in arr: for ands in ors: andValid = True for key, comparator, value in ands: value = str(value) itemValue = str(item[key]) if not caseSensitive: value = value.lower() itemValue = itemValue.lower() if comparator not in ["CONTAINS", "EXCLUDES", "EXISTS"]: value = mu.parseNumber(value) itemValue = mu.parseNumber(itemValue) if comparator == "<=" and itemValue > value: andValid = False break elif comparator == ">=" and itemValue < value: andValid = False break elif comparator == "<" and itemValue >= value: andValid = False break elif comparator == ">" and itemValue <= value: andValid = False break elif comparator == "CONTAINS" and value not in itemValue: andValid = False break elif comparator == "EXCLUDES" and value in itemValue: andValid = False break elif comparator == "!=" and itemValue == value: andValid = False break elif comparator == "=" and itemValue != value: andValid = False break elif comparator == "EXISTS" and value == "true" and itemValue == "": andValid = False break elif comparator == "EXISTS" and value == "false" and itemValue != "": andValid = False break if andValid: results.append(item) break return results
def validateLon(value): value = mu.parseNumber(value, alwaysFloat=True, invalidValue=None) if value is None: return None if value < -180.0 or value > 180.0: return None return value
def parseQueryString(queryStr, parseNumbers=True): query = dict([tuple(c.split("=")) for c in queryStr.strip().split("&")]) if parseNumbers: for key in query: query[key] = mu.parseNumber(query[key]) return query
def isValidYear(year): global validYearEnd validYearStart = 1800 return isinstance(year, (int, )) and validYearStart <= year <= validYearEnd # lu.countValues(items, "Acquisition Year") # sys.exit() datePattern = re.compile("([12][0-9]{3})(\-[12][0-9]{3})?( \[[A-Z,\? ]+\])?") acquisitionTypeSynonyms = [("MUSEUM EXPEDITION", "EXPEDITION"), ("FIELD PURCHASE", "PURCHASE"), ("MUSEUM TRANSFER", "TRANSFER")] for i, item in enumerate(items): year = mu.parseNumber(item["Acquisition Year"]) yearStr = str(year).strip() if len(yearStr) < 1: continue if isValidYear(year): cleanedItems[i]["Acquisition Year"] = year cleanedItems[i]["Acquisition Year Confidence"] = 1.0 cleanedItems[i]["Acquisition Era"] = str(year)[:2] + "00s" continue # Try to match against pattern matches = datePattern.match(yearStr) if not matches: print("Could not match pattern against string: %s" % yearStr)