-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.py
111 lines (87 loc) · 3.11 KB
/
mongo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import time
from trueskill import Rating
import player as p
from settings import Settings
import pymongo
__author__ = 'mocsar'
### Standard URI format: mongodb://[dbuser:dbpassword@]host:port/dbname
class Mongo(object):
def __init__(self):
db_uri = Settings.get('db-uri', '---')
db_collection = Settings.get('mongo-db-collection', 'players')
self.client = pymongo.MongoClient(db_uri)
db = self.client.get_default_database()
self.collection = db[db_collection]
self.log_collection = db['logs_' + db_collection]
def _get_rating(self, name):
if self.collection:
query = {'name': name}
player = self.collection.find_one(query)
if player:
return player
return None
def _set_rating(self, name, mu, sigma):
if self.collection:
query = {'name': name}
self.collection.update_one(query, {'$set': {'mu': float(mu), 'sigma': float(sigma)}}, upsert=True)
def get_all_players(self):
"""
:rtype : list[Player]
"""
res = []
if self.collection:
all_data = self.collection.find()
for values in all_data:
res.append(p.Player(values['name'], Rating(float(values['mu']), float(values['sigma']))))
return res
def get_player(self, name):
"""
:rtype : Player
"""
values = self._get_rating(name)
if not values: return None
return p.Player(name, Rating(float(values['mu']), float(values['sigma'])))
def set_player(self, player):
"""
:type player: Player
"""
self._set_rating(player.name, player.rating.mu, player.rating.sigma)
def log_results(self, names):
if self.log_collection:
self.log_collection.insert_one({'time': time.time(), 'names': names})
def close(self):
if self.client:
self.client.close()
def test1():
db_uri = Settings.get('mongo-db-uri', '---')
db_collection = Settings.get('mongo-db-collection', '---')
client = pymongo.MongoClient(db_uri)
db = client.get_default_database()
print 'collections', db.collection_names()
collection = db[db_collection]
print 'count', collection.count()
players = collection.find()
for player in players:
print 'find', player
# # Note that the insert method can take either an array or a single dict.
# SEED_DATA = [
# {'player': 'miki', 'score': 1.0},
# {'player': 'tomi', 'score': 1.1}
# ]
# collection.insert_many(SEED_DATA)
# query = {'player': 'maki'}
# player = collection.find_one(query)
# print 'find_one', player
# players = collection.find(query)
# for player in players:
# print 'find', player
# print 'update...'
# collection.update_one(query, {'$set': {'score': 2.0}}, upsert=True)
#
# player = collection.find_one(query)
# print 'find_one', player
# players = collection.find(query)
# for player in players:
# print 'find', player
# db.drop_collection(db_collection)
client.close()