def parse_expressions(): db = get_test_db() logs = db.logs one = list(logs.keys())[0] # l0 = logs[one] # print yaml_dump_pretty(l0._asdict()) query = one + "/{10:15}" res = dtu.fuzzy_match(query, logs, filters=filters_slice, raise_if_no_matches=True) assert len(res) == 1 l1 = res[list(res)[0]] assert l1.t0 == 10, l1.t0 assert l1.t1 == 15, l1.t1 assert l1.length == 5, l1.length query = one + "/{10:15}/{1:3}" res2 = dtu.fuzzy_match(query, logs, filters=filters_slice, raise_if_no_matches=True) assert len(res2) == 1 l2 = res2[list(res2)[0]] # print l2.t0, l2.t1, l2.length assert l2.t0 == 11 assert l2.t1 == 13 assert l2.length == 2, l1.length
def my_filter_test(): rc = re.compile(dtu.slice_regexp) m = rc.search("[1:2:3]") assert m.group("a") == "1" assert m.group("b") == "2" assert m.group("c") == "3" Species = namedtuple("Species", "name size weight") data = OrderedDict([ ("jeb", Species("A big horse", "large", 200)), ("fuffy", Species("A medium dog", "medium", 50)), ("ronny", Species("A medium cat", "medium", 30)), ]) expect(data, "all", ["jeb", "fuffy", "ronny"]) expect(data, "all/[0]", ["jeb"]) expect(data, "all/[0:]", ["jeb", "fuffy", "ronny"]) expect(data, "all/[0:1]", ["jeb"]) expect(data, "all/[:-1]", ["jeb", "fuffy"]) expect(data, "all/[::1]", ["jeb", "fuffy", "ronny"]) expect(data, "all/[::2]", ["jeb", "ronny"]) expect(data, "all/[0:3]", ["jeb", "fuffy", "ronny"]) res = dtu.fuzzy_match("all/shuffle", data) assert len(res) == 3
def parse_expressions4(): db = get_test_db() logs = db.logs one = list(logs.keys())[0] query = one + "/{1:}" res = dtu.fuzzy_match(query, logs, filters=filters_slice, raise_if_no_matches=True) logger.info(res) assert len(res) == 1 l1 = res[list(res)[0]] assert l1.t0 == 1
def parse_expressions2(): db = get_test_db() logs = db.logs one = list(logs.keys())[0] query = one + "/{10.5:15.5}" res = dtu.fuzzy_match(query, logs, filters=filters_slice, raise_if_no_matches=True) assert len(res) == 1 l1 = res[list(res)[0]] assert l1.t0 == 10.5, l1.t0 assert l1.t1 == 15.5, l1.t1 assert l1.length == 5, l1.length
def query(self, family_name, query, raise_if_no_matches=False): if isinstance(query, list): res = {} for q in query: res.update(self.query(family_name, q, raise_if_no_matches=False)) if raise_if_no_matches and not res: msg = "Could not find any match for the queries:" for q in query: msg += f"\n- {q}" raise dtu.DTNoMatches(msg) return res else: family = self.get_family(family_name) instances = family.instances result = dtu.fuzzy_match(query, instances, raise_if_no_matches=raise_if_no_matches) return result
def expect(data, query, result_keys, filters=None): result_keys = list(result_keys) check_isinstance(data, OrderedDict) if True: spec = dtu.parse_match_spec(query, filters=filters) print("-----") print("Query: %s" % query) print(dtu.indent(spec, "", "Spec: ")) res = dtu.fuzzy_match(query, data, filters=filters) check_isinstance(res, OrderedDict) if list(res) != result_keys: msg = "Error:" msg += "\n Data: %s" % data msg += "\n Query: %s" % query msg += "\n Result: %s" % list(res) msg += "\n Expect: %s" % list(result_keys) raise Exception(msg)
def query_logs(logs: Dict[str, PhysicalLog], query: QueryType, raise_if_no_matches=True) -> Dict[str, PhysicalLog]: """ query: a string or a list of strings Returns a dict str -> PhysicalLog. The query can also be a filename. """ if isinstance(query, list): res = {} for q in query: res.update(query_logs(logs, q, raise_if_no_matches=False)) if raise_if_no_matches and not res: msg = "Could not find any match for the queries:" for q in query: msg += f"\n- {q}" raise dtu.DTNoMatches(msg) return res else: dtu.check_isinstance(query, str) filters = {} filters.update(filters_slice) filters.update(dtu.filters0) aliases = {} aliases.update(logs) # adding aliases unless we are asking for everything if query != "*": # print('adding more (query = %s)' % query) for _, log in list(logs.items()): dtr = DTR.from_yaml(log.resources["bag"]) original_name = dtr.name # print ('alias: %s %s' % (original_name, dtr.name)) aliases[original_name] = log original_name = original_name.replace(".bag", "") aliases[original_name] = log result = dtu.fuzzy_match(query, aliases, filters=filters, raise_if_no_matches=raise_if_no_matches) # remove doubles after # XXX: this still has bugs present = defaultdict(set) for k, v in list(result.items()): present[id(v)].add(k) def choose(options): if len(options) == 1: return list(options)[0] else: options = sorted(options, key=len) return options[0] c = {} for k, v in list(result.items()): chosen = choose(present[id(v)]) if k == chosen: c[k] = v return c