def name_usage(self, name, start_year=MAX_YEAR, stop_year=MAX_YEAR): year_gender = dict() for i in xrange(start_year, stop_year + 1): year_gender[i] = {'M': 0, 'F': 0} for gender in ['M', 'F']: q = Query(and_args(eq('name', name), eq('gender', gender), gte('year', start_year), lte('year', stop_year))) categorize = CategorizeSpec() categorize.stats = StatsSpec('year', 'count') q.add_categorization(categorize) q.limit(0) result = self.names.find(q.build()) try: for year, stat in result.categories['year'].items(): year_gender[int(float(year))][gender] = stat['sum'] except KeyError: # No stats exists for year meaning no resuls for that name pass return year_gender
def test_or(self): print "\nTest or with 2 terms\n" q = Query(or_args(eq('age', 1), eq('age', 2))) result = self.test_collection.find(q.build()) try: self.assertEqual(len(result.raw_data), 2, 'Should be 2, actually {0}'.format(len(result.raw_data))) except KeyError as e: self.fail("Unexpected result, {0}, {1}".format(e, result))
def test_and(self): print "\nTest and with 2 terms\n" q = Query(and_args(eq('name', 'Drew'), eq('age', 1))) result = self.test_collection.find(q.build()) try: self.assertEqual(len(result.raw_data), 1, 'Should be 1, actually {0}'.format(len(result.raw_data))) except KeyError as e: self.fail("Unexpected result, {0}, {1}".format(e, result))
def test_or(self): print "\nTest or with 2 terms\n" q = Query(or_args(eq('age', 1), eq('age', 2))) result = self.test_collection.find(q.build()) try: self.assertEqual( len(result.raw_data), 2, 'Should be 2, actually {0}'.format(len(result.raw_data))) except KeyError as e: self.fail("Unexpected result, {0}, {1}".format(e, result))
def test_and(self): print "\nTest and with 2 terms\n" q = Query(and_args(eq('name', 'Drew'), eq('age', 1))) result = self.test_collection.find(q.build()) try: self.assertEqual( len(result.raw_data), 1, 'Should be 1, actually {0}'.format(len(result.raw_data))) except KeyError as e: self.fail("Unexpected result, {0}, {1}".format(e, result))
def find_or(field, value1, value2, value3): print '\n Query with boolean or' q = Query(or_args(eq(field, value1), eq(field, value2), eq(field, value3))) query = json.dumps(q.build()) query_pprint(q.build()) print '\n' + curl('PUT', 'query/all', data=query, accept_header=True) result = demo_collection.find(q.build()) json_pprint(result.response_doc)
def find_or(field, value1, value2, value3): print '\n Query with boolean or' q = Query(or_args(eq(field, value1), eq(field, value2), eq(field, value3))) query = json.dumps(q.build()) query_pprint(q.build()) print '\n' + curl('PUT', 'query/all', data=query, accept_header=True) result = demo_collection.find(q.build()) json_pprint(result.response_doc)
def user_favorites(self, username): q = Query(eq('username', username)) q.sort({'name', ASCENDING}) result = self.users.find(q.build()) return list(result)
def popularity_by_state(self, state, count, sort_order, start_year=MAX_YEAR, stop_year=MAX_YEAR): popular = [dict() for i in xrange(count)] q = Query(and_args(eq('state', state), eq('gender', gender), gte('year', start_year), lte('year', stop_year))) q.limit(count) q.order({'count': sort_order}) result = self.names.find(q.build()) for i, item in enumerate(result.objects(result_limit=count)): popular[i][gender] = [item['name'], item['count']] return popular
def test_find(self): print "\nTest find (exact match)\n" q = Query(eq('name', 'Dan')) result = self.test_collection.find(q.build()) try: self.assertEqual(len(result.raw_data), 1, 'Should be 1, actually {0}'.format(len(result.raw_data))) except KeyError as e: self.fail("Unexpected result, {0}, {1}".format(e, result))
def equality_query(field, value): print '\nEquality Query:' q = Query(eq(field, value)) query = json.dumps(q.build()) query_pprint(q.build()) print '\n' + curl('PUT', 'query/all', data=query, accept_header=True) result = demo_collection.find(q.build()) json_pprint(result.response_doc)
def test_no_match(self): print "\nTest no match\n" q = Query(eq('name', 'notfound')) result = self.test_collection.find(q.build()) try: self.assertEqual(len(result.raw_data), 0, 'Should be 0, actually {0}'.format(len(result.raw_data))) except KeyError as e: self.fail("Unexpected result, {0}, {1}".format(e, result))
def find_one_equality(field, value): print '\nFind one record, Equality Query:' q = Query(eq(field, value)) query = json.dumps(q.build()) query_pprint(q.build()) print '\n' + curl('PUT', 'query/one', data=query, accept_header=True) result = demo_collection.find_one(q.build()) json_pprint(result)
def test_find(self): print "\nTest find (exact match)\n" q = Query(eq('name', 'Dan')) result = self.test_collection.find(q.build()) try: self.assertEqual( len(result.raw_data), 1, 'Should be 1, actually {0}'.format(len(result.raw_data))) except KeyError as e: self.fail("Unexpected result, {0}, {1}".format(e, result))
def test_categorize_by_query(self): q = Query(regex('name', '.*')) cat_spec = CategorizeSpec() cat_spec.add_categorize_query(eq('name', 'Dan')) q.add_categorization(cat_spec) q.limit(0) result = self.test_collection.find(q.build()) self.assertTrue(len(result.categories) > 0)
def find_one_equality(field, value): print '\nFind one record, Equality Query:' q = Query(eq(field, value)) query = json.dumps(q.build()) query_pprint(q.build()) print '\n' + curl('PUT', 'query/one', data=query, accept_header=True) result = demo_collection.find_one(q.build()) json_pprint(result)
def test_categorize_by_query(self): q = Query(regex('name', '.*')) cat_spec = CategorizeSpec() cat_spec.add_categorize_query(eq('name', 'Dan')) q.add_categorization(cat_spec) q.limit(0) result = self.test_collection.find(q.build()) self.assertTrue(len(result.categories) > 0)
def equality_query(field, value): print '\nEquality Query:' q = Query(eq(field, value)) query = json.dumps(q.build()) query_pprint(q.build()) print '\n' + curl('PUT', 'query/all', data=query, accept_header=True) result = demo_collection.find(q.build()) json_pprint(result.response_doc)
def test_find_one(self): print "\nTest find one\n" q = Query(eq('name', 'Dan')) results = self.test_collection.find_one(q.build()) try: self.assertEqual(results['name'], 'Dan', 'name field in retrieved record did not match, {0}, actually, {1}'.format('Dan', results['name'])) except KeyError as e: self.fail("Unexpected result, {0}, {1}".format(e, results))
def test_no_match(self): print "\nTest no match\n" q = Query(eq('name', 'notfound')) result = self.test_collection.find(q.build()) try: self.assertEqual( len(result.raw_data), 0, 'Should be 0, actually {0}'.format(len(result.raw_data))) except KeyError as e: self.fail("Unexpected result, {0}, {1}".format(e, result))
def test_find_one(self): print "\nTest find one\n" q = Query(eq('name', 'Dan')) results = self.test_collection.find_one(q.build()) try: self.assertEqual( results['name'], 'Dan', 'name field in retrieved record did not match, {0}, actually, {1}' .format('Dan', results['name'])) except KeyError as e: self.fail("Unexpected result, {0}, {1}".format(e, results))
def state_popularity2(self, count, sort_order, start_year=MAX_YEAR, stop_year=MAX_YEAR): popular = dict() for state in states: popular[state] = {'M': 0, 'F': 0} for gender in ['M', 'F']: q = Query(and_args(eq('gender', gender), gte('year', start_year), lte('year', stop_year))) q.add_grouping(GroupSpec(field='state', limit=count, start=0, sort={'count': sort_order})) q.limit = 1000 result = self.names.find(q.build()) for state, names in result.groups['state'].items(): popular[state][gender] = [[name['name'], name['count']] for name in names] return popular