def find(collection, query, sort=[]): """ Parameters ---------- collection : list of dicts query: dict sort : list example: [('time', ASCENDING)] More than one element is not supported. Returns ------- results : list """ match = Query(query).match result = filter(match, collection) if sort is None: return result elif len(sort) > 2: raise NotImplementedError("Only one sort key is supported.") else: key, reverse = sort sorted_result = sorted(result, key=lambda x: x[key], reverse=reverse) # Make it a generator so it is the same as the unsorted code path. return (elem for elem in sorted_result)
def upsert(self, start_doc, stop_doc, gen_func, gen_args, gen_kwargs): if not Query(self._query).match(start_doc): return uid = start_doc['uid'] self._uid_to_run_start_doc[uid] = start_doc entry = SafeLocalCatalogEntry( name=start_doc['uid'], description={}, # TODO driver='intake_bluesky.core.BlueskyRunFromGenerator', direct_access='forbid', args={ 'gen_func': gen_func, 'gen_args': gen_args, 'gen_kwargs': gen_kwargs, 'filler': self.filler }, cache=None, # ??? parameters=[], metadata={ 'start': start_doc, 'stop': stop_doc }, catalog_dir=None, getenv=True, getshell=True, catalog=self) self._entries[uid] = entry
def find_taken_time(self): if not self.validate_query(): return False else: self.load_query() query = Query(self.query) selected_lessons = filter(query.match, self.timetable['lessons']) return selected_lessons
def search_asset(self, filter_values: Any) -> List[str]: """ :param dict filter_values: can be a dict or a mongo type query :returns: a list of asset_ids found """ if not isinstance(filter_values, dict): raise TypeError('Filter values must be a type dict') asset_list = self.get_metadata_list() filter_query = Query(filter_values) result = [] if asset_list: for asset_id, metadata in asset_list.items(): if filter_query.match(metadata): result.append(asset_id) return result
def find(self, query, sort=None): match = Query(query).match result = filter(match, self._docs) if sort is None: return (copy.deepcopy(elem) for elem in result) elif len(sort) > 2: raise NotImplementedError("Only one sort key is supported.") else: sort, = sort # ascending_or_descending is -1 (descending) or 1 (ascending) key, ascending_or_descending = sort reverse = (ascending_or_descending == DESCENDING) sorted_result = sorted(result, key=lambda x: x[key], reverse=reverse) # Make it a generator so it is the same as the unsorted code path. return (copy.deepcopy(elem) for elem in sorted_result)
def find_one(collection, query): """ Parameters ---------- collection : list of dicts query: dict Returns ------- result : dict """ match = Query(query).match for doc in collection: if match(doc): return doc return None
def generate_timetable_by_day(self, week): timetable_data = TimetableData() days = timetable_data.get_week_days() hours = timetable_data.get_hours() timetable_by_day = {} for day_num, val in days.items(): timetable_by_day[day_num] = {} for hour in hours: query = Query({"$and": [{"day": day_num}, {"hour": hour}]}) lessons_to_append = filter(query.match, self.filtered_timetable) timetable_by_day[day_num][hour] = [] add_to_timetable_by_day = False for lesson in lessons_to_append: if lesson['teacher'] is not None: if week in lesson['teacher']: lesson['teacher'] = re.sub("(-p.*)|(-n.*)", "", lesson['teacher']) add_to_timetable_by_day = True if lesson['classroom'] is not None: if week in lesson['classroom']: lesson['classroom'] = re.sub( "(-p.*)|(-n.*)", "", lesson['classroom']) add_to_timetable_by_day = True if lesson['subject'] is not None: if week in lesson['subject']: lesson['subject'] = re.sub("(-p.*)|(-n.*)", "", lesson['subject']) add_to_timetable_by_day = True elif re.match("^(WF)", lesson['subject']): add_to_timetable_by_day = True if add_to_timetable_by_day: timetable_by_day[day_num][hour].append(lesson) return timetable_by_day
def upsert(self, gen_func, gen_args, gen_kwargs): gen = gen_func(*gen_args, **gen_kwargs) name, run_start_doc = next(gen) if name != 'start': raise ValueError("Expected a generator of (name, doc) pairs where " "the first entry was ('start', {...}).") if not Query(self._query).match(run_start_doc): return uid = run_start_doc['uid'] self._uid_to_run_start_doc[uid] = run_start_doc entry = SafeLocalCatalogEntry( name=run_start_doc['uid'], description={}, # TODO driver='intake_bluesky.core.BlueskyRunFromGenerator', direct_access='forbid', args={ 'gen_func': gen_func, 'gen_args': gen_args, 'gen_kwargs': gen_kwargs, 'filler': self.filler }, cache=None, # ??? parameters=[], metadata={ 'start': run_start_doc, 'stop': None }, catalog_dir=None, getenv=True, getshell=True, catalog=self) self._entries[uid] = entry
def find_one(self, query): match = Query(query).match for doc in self._docs: if match(doc): return copy.deepcopy(doc) return None
def _query(self, definition, collection=_ALL): return list(filter( Query(definition).match, collection ))
def find_one(self, query): match = Query(query).match for doc in self._docs: if match(doc): return doc return None
def _query(self, definition, collection=_ALL, flattened=False): return list( filter(Query(definition, flattened=flattened).match, collection))
def match(self, template): return Parser(None, list(filter(Query(template).match, self.nodes)))
def match(obj, query): """Returns True if object matches the mongo query, else False""" if not isinstance(obj, Sequence) and not isinstance(obj, Mapping): return False query = Query(query) return query.match(obj)