def kwQuery(self, **kwargs): kwitems = list(kwargs.items()) key, value = kwitems[0] query = rc_query.Eq(key, value) for k, v in kwitems[1:]: query = query & rc_query.Eq(k, v) return query
def test_catalog_query(self): container = self.test_indexing() catalog = container.catalog from repoze.catalog import query rec1, rec2, rec3, rec4 = RECORDS query1 = query.Eq('field_name', 'Me') query2 = query.Any('keyword_keywords', 'that') query3 = query.Eq('field_when', datetime.date(2012, 1, 2)) query_empty_date = query.Eq('field_when', None) r = catalog.query(query1) assert len(r) == 1 assert r.values()[0] is rec1 assert r.keys()[0] is IUUID(rec1) r = catalog.query(query2) assert len(r) == 2 assert IUUID(rec1) in r assert rec1 in r.values() assert IUUID(rec2) in r assert rec2 in r.values() r = catalog.query(query1 & query1) assert rec2 not in r.values() assert rec1 in r.values() r = catalog.query(query3) assert len(r) == 1 assert IUUID(rec2) in r r = catalog.query(query_empty_date) assert len(r) == 2 assert IUUID(rec1) in r and IUUID(rec3) in r r = catalog.query(query3 & query2) assert len(r) == 1 assert IUUID(rec2) in r
class TestQueryWithPythonQueryObjects(unittest.TestCase, TestQueryBase): query = ( q.All('allowed', ['a', 'b']) & q.Any('name', ['name1', 'name2', 'name3', 'name4', 'name5']) & q.Not(q.Eq('title', 'title3')) & q.Contains('text', 'body') )
def main(): parser = argparse.ArgumentParser() parser.add_argument("config_uri", help="Paster ini file to load settings from") parser.add_argument( "path", help="from which path to clear likes (meeting or agenda item)") args = parser.parse_args() env = bootstrap(args.config_uri) root = env['root'] request = env['request'] context = traverse(root, args.path).get('context') if IMeeting.providedBy(context) or IAgendaItem.providedBy(context): print('Clearing likes on {}'.format(context.title)) path_query = query.Eq('path', args.path) cleared = False for type_name in ('Proposal', 'DiscussionPost'): count, docids = root.catalog.query( path_query & query.Eq('type_name', type_name)) response = input( 'Found {} {} on {}. Do you want to clear likes on these? (y/N) ' .format(count, type_name, context.title).encode('utf8')) if response.lower() in ('y', 'yes', 'j', 'ja'): cleared = True for obj in request.resolve_docids(docids, perm=None): like = request.registry.getAdapter(obj, IUserTags, name='like') like.storage.clear() like._notify() if cleared: transaction.commit() env['closer']() else: print('Path does not match a meeting or agenda item')
def _query_from_mapping(self, qdict): """ return a query.Query object given mapping of keys/values. Value normalization is not in scope (should happen to resulting query). """ r = [] for idxname, value in qdict.items(): if isinstance(value, tuple) and len(value) > 1: if issubclass(value[0], query.Query): comparator = value[0] r.append(comparator(idxname, value[1])) continue if idxname.startswith('text'): r.append(query.Contains(idxname, value)) elif idxname.startswith('keyword'): r.append(query.Any(idxname, value)) else: r.append(query.Eq(idxname, value)) if len(r) == 1: return r[0] return query.And(*r)
snake = zg.addNode(Node(['Snake', 'Animal'], name='Snake', text='sss')) adam = zg.addNode(Node(['Human', 'Male'], name='Adam')) eve = zg.addNode(Node(['Human', 'Female'], name='Eve')) zg.addEdge(Edge(jehova, 'creates', tree)) zg.addEdge(Edge(jehova, 'creates', apple)) zg.addEdge(Edge(jehova, 'creates', snake)) zg.addEdge(Edge(jehova, 'creates', adam)) zg.addEdge(Edge(jehova, 'creates', eve)) zg.addEdge(Edge(snake, 'talks', eve)) zg.addEdge(Edge(eve, 'takes', apple)) zg.addEdge(Edge(eve, 'gives', apple, to=adam)) else: apple = zg.nodecatalog.q('name=="Apple"')[0] print zg.nodecatalog.query(query.Eq('name', 'Eve')) print print zg.nodecatalog.q(query.Eq('text', 'light')) print p = ResultSet(apple).incoming('takes').source._as( 'source').outgoing().target.name._as('name') print p.values # Initializing ZGraph # (ResultSetSize(1, 1), IFSet([6])) # # [ N1:God] # # [ N3:Apple:Plant:Fruit] # [ E7 6 takes 3] # [ N6:Human:Female]