本文整理汇总了Python中dao.Dao.get_player_by_alias方法的典型用法代码示例。如果您正苦于以下问题:Python Dao.get_player_by_alias方法的具体用法?Python Dao.get_player_by_alias怎么用?Python Dao.get_player_by_alias使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dao.Dao
的用法示例。
在下文中一共展示了Dao.get_player_by_alias方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: bulk_alias_merge
# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import get_player_by_alias [as 别名]
def bulk_alias_merge(path, region):
config = parse_config()
username = config.get('database', 'user')
host = config.get('database', 'host')
auth_db = config.get('database', 'auth_db')
password = getpass.getpass()
mongo_client = MongoClient(host='mongodb://%s:%[email protected]%s/%s' % (username, password, host, auth_db))
dao = Dao(region, mongo_client=mongo_client)
with open(path) as f:
reader = csv.reader(f)
for row in reader:
target_alias = row[0]
db_target = dao.get_player_by_alias(target_alias)
if db_target is None:
print '%s is not a valid alias' % target_alias
continue
for source_alias in row[1:]:
source_alias = source_alias.strip()
if source_alias:
db_source = dao.get_player_by_alias(source_alias)
if db_source is None:
print '%s is not a valid alias' % source_alias
continue
print source_alias, '->', target_alias
dao.merge_players(source=db_source, target=db_target)
示例2: get
# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import get_player_by_alias [as 别名]
def get(self, region):
args = player_list_get_parser.parse_args()
dao = Dao(region, mongo_client=mongo_client)
return_dict = {}
# single player matching alias within region
if args['alias'] is not None:
return_dict['players'] = []
db_player = dao.get_player_by_alias(args['alias'])
if db_player:
return_dict['players'].append(db_player.get_json_dict())
# search multiple players by name across all regions
elif args['query'] is not None:
all_players = dao.get_all_players(all_regions=True)
return_dict['players'] = [p.get_json_dict() for p in self._get_players_matching_query(all_players, args['query'])]
# all players within region
else:
return_dict['players'] = [p.get_json_dict() for p in dao.get_all_players()]
convert_object_id_list(return_dict['players'])
# remove extra fields
for player in return_dict['players']:
del player['regions']
del player['aliases']
del player['ratings']
return return_dict
示例3: bulk_player_exclude
# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import get_player_by_alias [as 别名]
def bulk_player_exclude(path, region):
config = parse_config()
username = config.get('database', 'user')
host = config.get('database', 'host')
auth_db = config.get('database', 'auth_db')
password = getpass.getpass()
mongo_client = MongoClient(host='mongodb://%s:%[email protected]%s/%s' % (username, password, host, auth_db))
dao = Dao(region, mongo_client=mongo_client)
with open(path) as f:
for line in f:
line = line.strip()
if line:
db_player = dao.get_player_by_alias(line)
if db_player is None:
raise Exception('%s not found' % line)
print line
dao.exclude_player(db_player)
示例4: get
# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import get_player_by_alias [as 别名]
def get(self, region):
args = player_list_get_parser.parse_args()
dao = Dao(region, mongo_client=mongo_client)
return_dict = {}
if args['alias'] is not None:
return_dict['players'] = []
db_player = dao.get_player_by_alias(args['alias'])
if db_player:
return_dict['players'].append(db_player.get_json_dict())
else:
return_dict['players'] = [p.get_json_dict() for p in dao.get_all_players()]
convert_object_id_list(return_dict['players'])
# remove extra fields
for player in return_dict['players']:
del player['regions']
del player['aliases']
del player['ratings']
return return_dict
示例5: get
# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import get_player_by_alias [as 别名]
def get(self, region):
args = player_list_get_parser.parse_args()
dao = Dao(region, mongo_client=mongo_client)
if not dao:
return 'Dao not found', 404
return_dict = {}
exclude_properties = ['aliases']
# single player matching alias within region
if args['alias']:
return_dict['players'] = []
db_player = dao.get_player_by_alias(args['alias'])
if db_player:
return_dict['players'].append(db_player.dump(context='web',
exclude=exclude_properties))
# search multiple players by name across all regions
elif args['query']:
# TODO: none checks on below list comprehensions
all_players = dao.get_all_players(all_regions=True)
return_dict['players'] = [p.dump(context='web',
exclude=exclude_properties)
for p in self._get_players_matching_query(all_players, args['query'])]
# get all players in all regions
elif args['all']:
all_players = dao.get_all_players(all_regions=True)
return_dict['players'] = [p.dump(context='web',
exclude=exclude_properties)
for p in sorted(all_players, key=lambda player: player.name.lower())]
# all players within region
else:
print 'test'
return_dict['players'] = [p.dump(context='web',
exclude=exclude_properties)
for p in sorted(dao.get_all_players(),
key=lambda player: player.name.lower())]
return return_dict
示例6: TestDAO
# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import get_player_by_alias [as 别名]
#.........这里部分代码省略.........
self.norcal_dao.insert_ranking(ranking)
for user in self.users:
self.norcal_dao.insert_user(user)
def tearDown(self):
self.mongo_client.drop_database(DATABASE_NAME)
def test_init_with_invalid_region(self):
# create a dao with an existing region
Dao('norcal', self.mongo_client, database_name=DATABASE_NAME)
# create a dao with a new region
with self.assertRaises(RegionNotFoundException):
Dao('newregion', self.mongo_client, database_name=DATABASE_NAME)
def test_get_all_regions(self):
# add another region
region = Region('newregion', 'New Region')
Dao.insert_region(region, self.mongo_client, database_name=DATABASE_NAME)
regions = Dao.get_all_regions(self.mongo_client, database_name=DATABASE_NAME)
self.assertEquals(len(regions), 3)
self.assertEquals(regions[0], region)
self.assertEquals(regions[1], self.region_1)
self.assertEquals(regions[2], self.region_2)
def test_get_player_by_id(self):
self.assertEquals(self.norcal_dao.get_player_by_id(self.player_1_id), self.player_1)
self.assertEquals(self.norcal_dao.get_player_by_id(self.player_2_id), self.player_2)
self.assertEquals(self.norcal_dao.get_player_by_id(self.player_3_id), self.player_3)
self.assertIsNone(self.norcal_dao.get_player_by_id(ObjectId()))
def test_get_player_by_alias(self):
self.assertEquals(self.norcal_dao.get_player_by_alias('gar'), self.player_1)
self.assertEquals(self.norcal_dao.get_player_by_alias('GAR'), self.player_1)
self.assertEquals(self.norcal_dao.get_player_by_alias('garr'), self.player_1)
self.assertEquals(self.norcal_dao.get_player_by_alias('sfat'), self.player_2)
self.assertEquals(self.norcal_dao.get_player_by_alias('miom | sfat'), self.player_2)
self.assertIsNone(self.norcal_dao.get_player_by_alias('mango'))
self.assertIsNone(self.norcal_dao.get_player_by_alias('miom|sfat'))
self.assertIsNone(self.norcal_dao.get_player_by_alias(''))
def test_get_players_by_alias_from_all_regions(self):
self.assertEquals(self.norcal_dao.get_players_by_alias_from_all_regions('gar'), [self.player_1, self.player_3])
self.assertEquals(self.norcal_dao.get_players_by_alias_from_all_regions('GAR'), [self.player_1, self.player_3])
self.assertEquals(self.norcal_dao.get_players_by_alias_from_all_regions('garr'), [self.player_1])
self.assertEquals(self.norcal_dao.get_players_by_alias_from_all_regions('sfat'), [self.player_2])
self.assertEquals(self.norcal_dao.get_players_by_alias_from_all_regions('miom | sfat'), [self.player_2])
self.assertEquals(self.norcal_dao.get_players_by_alias_from_all_regions('mango'), [self.player_3])
self.assertEquals(self.norcal_dao.get_players_by_alias_from_all_regions('miom|sfat'), [])
self.assertEquals(self.norcal_dao.get_players_by_alias_from_all_regions(''), [])
def test_get_player_id_map_from_player_aliases(self):
aliases = ['GAR', 'sfat', 'asdf', 'mango']
expected_map = [
{'player_alias': 'GAR', 'player_id': self.player_1_id},
{'player_alias': 'sfat', 'player_id': self.player_2_id},
{'player_alias': 'asdf', 'player_id': None},
{'player_alias': 'mango', 'player_id': None},
]
map = self.norcal_dao.get_player_id_map_from_player_aliases(aliases)
self.assertEquals(map, expected_map)
示例7: TestDAO
# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import get_player_by_alias [as 别名]
#.........这里部分代码省略.........
self.rankings = [self.ranking_1, self.ranking_2]
for player in self.players:
self.dao.add_player(player)
for tournament in self.tournaments:
self.dao.insert_tournament(tournament)
for ranking in self.rankings:
self.dao.insert_ranking(ranking)
def test_init_with_invalid_region(self):
print Dao.get_all_regions(mongo_client=self.mongo_client)
# create a dao with an existing region
Dao(self.region, mongo_client=self.mongo_client, new=False)
# create a dao with a new region
with self.assertRaises(RegionNotFoundException):
Dao('newregion', mongo_client=self.mongo_client, new=False)
def test_get_all_regions(self):
# add another region
region = 'newregion'
Dao(region, mongo_client=self.mongo_client, new=True)
self.assertEquals(set(Dao.get_all_regions(mongo_client=self.mongo_client)), set([self.region, region]))
def test_get_player_by_id(self):
self.assertEquals(self.dao.get_player_by_id(self.player_1_id), self.player_1)
self.assertEquals(self.dao.get_player_by_id(self.player_2_id), self.player_2)
self.assertEquals(self.dao.get_player_by_id(self.player_3_id), self.player_3)
self.assertIsNone(self.dao.get_player_by_id(ObjectId()))
def test_get_player_by_alias(self):
self.assertEquals(self.dao.get_player_by_alias('gar'), self.player_1)
self.assertEquals(self.dao.get_player_by_alias('GAR'), self.player_1)
self.assertEquals(self.dao.get_player_by_alias('garr'), self.player_1)
self.assertEquals(self.dao.get_player_by_alias('sfat'), self.player_2)
self.assertEquals(self.dao.get_player_by_alias('miom | sfat'), self.player_2)
self.assertEquals(self.dao.get_player_by_alias('mango'), self.player_3)
self.assertIsNone(self.dao.get_player_by_alias('miom|sfat'))
self.assertIsNone(self.dao.get_player_by_alias(''))
def test_get_all_players(self):
self.assertEquals(self.dao.get_all_players(), [self.player_1, self.player_3, self.player_2])
def test_add_player_duplicate(self):
with self.assertRaises(DuplicateKeyError):
self.dao.add_player(self.player_1)
def test_delete_player(self):
self.assertEquals(self.dao.get_player_by_id(self.player_1_id), self.player_1)
self.assertEquals(self.dao.get_player_by_id(self.player_2_id), self.player_2)
self.assertEquals(self.dao.get_player_by_id(self.player_3_id), self.player_3)
self.dao.delete_player(self.player_2)
self.assertEquals(self.dao.get_player_by_id(self.player_1_id), self.player_1)
self.assertIsNone(self.dao.get_player_by_id(self.player_2_id))
self.assertEquals(self.dao.get_player_by_id(self.player_3_id), self.player_3)
self.dao.delete_player(self.player_3)
self.assertEquals(self.dao.get_player_by_id(self.player_1_id), self.player_1)
self.assertIsNone(self.dao.get_player_by_id(self.player_2_id))
self.assertIsNone(self.dao.get_player_by_id(self.player_3_id))
示例8: TestDAO
# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import get_player_by_alias [as 别名]
#.........这里部分代码省略.........
def tearDown(self):
self.mongo_client.drop_database(DATABASE_NAME)
def test_init_with_invalid_region(self):
# create a dao with an existing region
Dao('norcal', self.mongo_client, database_name=DATABASE_NAME)
# create a dao with a non existant region, should return none
self.assertIsNone(Dao('newregion', self.mongo_client,
database_name=DATABASE_NAME))
def test_get_all_regions(self):
# add another region
region = Region(id='newregion', display_name='New Region')
Dao.insert_region(region, self.mongo_client,
database_name=DATABASE_NAME)
regions = Dao.get_all_regions(
self.mongo_client, database_name=DATABASE_NAME)
self.assertEquals(len(regions), 3)
self.assertEquals(regions[0], region)
self.assertEquals(regions[1], self.region_1)
self.assertEquals(regions[2], self.region_2)
def test_get_player_by_id(self):
self.assertEquals(self.norcal_dao.get_player_by_id(
self.player_1_id), self.player_1)
self.assertEquals(self.norcal_dao.get_player_by_id(
self.player_2_id), self.player_2)
self.assertEquals(self.norcal_dao.get_player_by_id(
self.player_3_id), self.player_3)
self.assertIsNone(self.norcal_dao.get_player_by_id(ObjectId()))
def test_get_player_by_alias(self):
self.assertEquals(
self.norcal_dao.get_player_by_alias('gar'), self.player_1)
self.assertEquals(
self.norcal_dao.get_player_by_alias('GAR'), self.player_1)
self.assertEquals(
self.norcal_dao.get_player_by_alias('garr'), self.player_1)
self.assertEquals(
self.norcal_dao.get_player_by_alias('sfat'), self.player_2)
self.assertEquals(self.norcal_dao.get_player_by_alias(
'miom | sfat'), self.player_2)
self.assertIsNone(self.norcal_dao.get_player_by_alias('mango'))
self.assertIsNone(self.norcal_dao.get_player_by_alias('miom|sfat'))
self.assertIsNone(self.norcal_dao.get_player_by_alias(''))
def test_get_players_by_alias_from_all_regions(self):
self.assertEquals(self.norcal_dao.get_players_by_alias_from_all_regions(
'gar'), [self.player_1, self.player_3])
self.assertEquals(self.norcal_dao.get_players_by_alias_from_all_regions(
'GAR'), [self.player_1, self.player_3])
self.assertEquals(self.norcal_dao.get_players_by_alias_from_all_regions(
'garr'), [self.player_1])
self.assertEquals(self.norcal_dao.get_players_by_alias_from_all_regions(
'sfat'), [self.player_2])
self.assertEquals(self.norcal_dao.get_players_by_alias_from_all_regions(
'miom | sfat'), [self.player_2])
self.assertEquals(self.norcal_dao.get_players_by_alias_from_all_regions(
'mango'), [self.player_3])
self.assertEquals(
self.norcal_dao.get_players_by_alias_from_all_regions('miom|sfat'), [])
self.assertEquals(
示例9: TestServer
# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import get_player_by_alias [as 别名]
class TestServer(unittest.TestCase):
def setUp(self):
self.mongo_client_patcher = patch('server.mongo_client', new=mongomock.MongoClient())
self.mongo_client = self.mongo_client_patcher.start()
server.app.config['TESTING'] = True
self.app = server.app.test_client()
self.norcal_dao = Dao(NORCAL_REGION_NAME, mongo_client=self.mongo_client, new=True)
self.texas_dao = Dao(TEXAS_REGION_NAME, mongo_client=self.mongo_client, new=True)
self._import_files()
rankings.generate_ranking(self.norcal_dao)
rankings.generate_ranking(self.texas_dao)
def _import_files(self):
for f in NORCAL_FILES:
scraper = TioScraper(f[0], f[1])
self._import_players(scraper, self.norcal_dao)
self.norcal_dao.insert_tournament(Tournament.from_scraper('tio', scraper, self.norcal_dao))
for f in TEXAS_FILES:
scraper = TioScraper(f[0], f[1])
self._import_players(scraper, self.texas_dao)
self.texas_dao.insert_tournament(Tournament.from_scraper('tio', scraper, self.texas_dao))
def _import_players(self, scraper, dao):
for player in scraper.get_players():
db_player = dao.get_player_by_alias(player)
if db_player is None:
db_player = Player(player, [player.lower()], TrueskillRating(), False)
dao.add_player(db_player)
def test_get_region_list(self):
data = self.app.get('/regions').data
self.assertEquals(json.loads(data), {'regions': ['norcal', 'texas']})
def test_get_player_list(self):
def for_region(data, dao):
json_data = json.loads(data)
self.assertEquals(json_data.keys(), ['players'])
players_list = json_data['players']
players_from_db = dao.get_all_players()
self.assertEquals(len(players_list), len(players_from_db))
for player in players_list:
expected_keys = set(['id', 'name'])
self.assertEquals(set(player.keys()), expected_keys)
self.assertEquals(ObjectId(player['id']), dao.get_player_by_alias(player['name']).id)
data = self.app.get('/norcal/players').data
for_region(data, self.norcal_dao)
data = self.app.get('/texas/players').data
for_region(data, self.texas_dao)
def test_get_player(self):
player = self.norcal_dao.get_player_by_alias('gar')
data = self.app.get('/norcal/players/' + str(player.id)).data
json_data = json.loads(data)
self.assertEquals(len(json_data.keys()), 5)
self.assertEquals(json_data['id'], str(player.id))
self.assertEquals(json_data['name'], 'gar')
self.assertEquals(json_data['aliases'], ['gar'])
self.assertEquals(json_data['exclude'], False)
self.assertTrue(json_data['rating']['mu'] > 25.9)
self.assertTrue(json_data['rating']['sigma'] > 3.89)
player = self.texas_dao.get_player_by_alias('wobbles')
data = self.app.get('/texas/players/' + str(player.id)).data
json_data = json.loads(data)
self.assertEquals(len(json_data.keys()), 5)
self.assertEquals(json_data['id'], str(player.id))
self.assertEquals(json_data['name'], 'Wobbles')
self.assertEquals(json_data['aliases'], ['wobbles'])
self.assertEquals(json_data['exclude'], False)
self.assertTrue(json_data['rating']['mu'] > 44.5)
self.assertTrue(json_data['rating']['sigma'] > 3.53)
def test_get_tournament_list(self):
def for_region(data, dao):
json_data = json.loads(data)
self.assertEquals(json_data.keys(), ['tournaments'])
tournaments_list = json_data['tournaments']
tournaments_from_db = dao.get_all_tournaments()
self.assertEquals(len(tournaments_list), len(tournaments_from_db))
for tournament in tournaments_list:
tournament_from_db = dao.get_tournament_by_id(ObjectId(tournament['id']))
expected_keys = set(['id', 'name', 'date'])
self.assertEquals(set(tournament.keys()), expected_keys)
self.assertEquals(tournament['id'], str(tournament_from_db.id))
self.assertEquals(tournament['name'], tournament_from_db.name)
self.assertEquals(tournament['date'], tournament_from_db.date.strftime('%x'))
data = self.app.get('/norcal/tournaments').data
#.........这里部分代码省略.........
示例10: TestServer
# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import get_player_by_alias [as 别名]
class TestServer(unittest.TestCase):
def setUp(self):
self.mongo_client_patcher = patch('server.mongo_client', new=mongomock.MongoClient())
self.mongo_client = self.mongo_client_patcher.start()
server.app.config['TESTING'] = True
self.app = server.app.test_client()
self.norcal_region = Region('norcal', 'Norcal')
self.texas_region = Region('texas', 'Texas')
Dao.insert_region(self.norcal_region, self.mongo_client)
Dao.insert_region(self.texas_region, self.mongo_client)
self.norcal_dao = Dao(NORCAL_REGION_NAME, mongo_client=self.mongo_client)
self.texas_dao = Dao(TEXAS_REGION_NAME, mongo_client=self.mongo_client)
self._import_files()
now = datetime(2014, 11, 1)
rankings.generate_ranking(self.norcal_dao, now=now)
rankings.generate_ranking(self.texas_dao, now=now)
self.user_id = 'asdf'
self.user_full_name = 'full name'
self.user_admin_regions = ['norcal', 'nyc']
self.user = User(self.user_id, self.user_admin_regions, full_name=self.user_full_name)
self.norcal_dao.insert_user(self.user)
def _import_files(self):
for f in NORCAL_FILES:
scraper = TioScraper(f[0], f[1])
self._import_players(scraper, self.norcal_dao)
player_map = self.norcal_dao.get_player_id_map_from_player_aliases(scraper.get_players())
self.norcal_dao.insert_tournament(Tournament.from_scraper('tio', scraper, player_map, self.norcal_dao.region_id))
for f in TEXAS_FILES:
scraper = TioScraper(f[0], f[1])
self._import_players(scraper, self.texas_dao)
player_map = self.texas_dao.get_player_id_map_from_player_aliases(scraper.get_players())
self.texas_dao.insert_tournament(Tournament.from_scraper('tio', scraper, player_map, self.texas_dao.region_id))
def _import_players(self, scraper, dao):
for player in scraper.get_players():
db_player = dao.get_player_by_alias(player)
if db_player is None:
db_player = Player(
player,
[player.lower()],
{dao.region_id: TrueskillRating()},
[dao.region_id])
dao.insert_player(db_player)
def test_get_region_list(self):
data = self.app.get('/regions').data
expected_region_dict = {
'regions': [
{'id': 'norcal', 'display_name': 'Norcal'},
{'id': 'texas', 'display_name': 'Texas'}
]
}
self.assertEquals(json.loads(data), expected_region_dict)
def test_get_player_list(self):
def for_region(json_data, dao):
self.assertEquals(json_data.keys(), ['players'])
players_list = json_data['players']
players_from_db = dao.get_all_players()
self.assertEquals(len(players_list), len(players_from_db))
for player in players_list:
expected_keys = set(['id', 'name'])
self.assertEquals(set(player.keys()), expected_keys)
self.assertEquals(ObjectId(player['id']), dao.get_player_by_alias(player['name']).id)
data = self.app.get('/norcal/players').data
json_data = json.loads(data)
self.assertEquals(len(json_data['players']), 65)
for_region(json_data, self.norcal_dao)
data = self.app.get('/texas/players').data
json_data = json.loads(data)
self.assertEquals(len(json_data['players']), 41)
for_region(json_data, self.texas_dao)
def test_get_player_list_with_alias(self):
player = self.norcal_dao.get_player_by_alias('gar')
data = self.app.get('/norcal/players?alias=gar').data
json_data = json.loads(data)
self.assertEquals(len(json_data['players']), 1)
json_player = json_data['players'][0]
expected_keys = set(['id', 'name'])
self.assertEquals(set(json_player.keys()), expected_keys)
self.assertEquals(ObjectId(json_player['id']), player.id)
def test_get_player_list_case_insensitive(self):
player = self.norcal_dao.get_player_by_alias('gar')
#.........这里部分代码省略.........