-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.py
57 lines (51 loc) · 1.57 KB
/
db.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
#
import pymongo
import json
import time
import matplotlib.pyplot as plt
class DB:
def __init__(self,password="111",db_name="run_log"):
client = pymongo.MongoClient("mongodb://terry:"+password+"@cluster0-shard-00-00.b434c.mongodb.net:27017,cluster0-shard-00-01.b434c.mongodb.net:27017,cluster0-shard-00-02.b434c.mongodb.net:27017/"+db_name+"?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority")
self.db = client[db_name]
# print(db)
def add_one(self,data,name="log"):
"""
添加一条数据
"""
# print(type(data))
data["_id"]=time.time()
self.db[name].insert_one(data)
def clear_col(self,name="log"):
"""
清空单个表
"""
self.db[name].remove({})
def get_col(self,name="log"):
"""
遍历表
"""
for it in self.db[name].find({}):
# print((it))
yield it
if __name__ == '__main__':
db_name=input("db_name:")
password=input("password:")
Db=DB(password=password,db_name=db_name)
# Db.add_one({'xx':11})
plt.figure()
plt.ion()
while True:
x=[]
y=[]
for i,it in enumerate( Db.get_col()):
print(it)
# x.append(i)
x.append(it['step'])
y.append(it['loss'])
plt.plot(x,y)
plt.xlabel("step") #X轴标签
plt.ylabel("loss") #Y轴标签
plt.title("run loss") #标题
plt.show() #显示图
plt.savefig("data/plot.jpg")
time.sleep(10)