forked from jiangpinglei/BERT_ChineseWordSegment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
66 lines (55 loc) · 1.82 KB
/
database.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
from sqlalchemy import Column, Integer, String, DateTime
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import os
import datetime
Base = declarative_base()
import sys
class Record(Base):
__tablename__ = 'logs'
id = Column(Integer, primary_key=True)
timestamp = Column(DateTime, default=datetime.datetime.utcnow)
application = Column(String)
category = Column(String)
key = Column(String)
value = Column(String)
class Pair(Base):
__tablename__ = 'results'
id = Column(Integer, primary_key=True)
timestamp = Column(DateTime, default=datetime.datetime.utcnow)
application = Column(String)
token = Column(String)
ori_seg = Column(String)
des_seg = Column(String)
class Logger(object):
def __init__(self, application, ignore=False):
self.application = application
self.ignore = ignore
try:
database_string = os.environ['DATABASE']
self.engine = create_engine(database_string)
Session = sessionmaker(bind=self.engine)
self.session = Session()
Base.metadata.create_all(self.engine)
except:
if not self.ignore:
raise Exception()
def add(self, params):
try:
params['application'] = self.application
record = Record(**params)
self.session.add(record)
self.session.commit()
except:
if not self.ignore:
raise Exception()
def add_pair(self, params):
try:
params['application'] = self.application
pair = Pair(**params)
self.session.add(pair)
self.session.commit()
except:
if not self.ignore:
raise Exception()