summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ausbutler/db.py15
-rw-r--r--ausbutler/interface.py10
-rw-r--r--ausbutler/model.py6
-rw-r--r--butler.py5
4 files changed, 19 insertions, 17 deletions
diff --git a/ausbutler/db.py b/ausbutler/db.py
index a53a273..940c69d 100644
--- a/ausbutler/db.py
+++ b/ausbutler/db.py
@@ -1,14 +1,15 @@
import json
from os import path
-from sqlalchemy import create_engine, event
+from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import __main__
-config = json.load(
- open(path.join(path.dirname(__main__.__file__), 'config', 'db.json')))
-engine = create_engine(
- "mysql://{0[user]}:{0[pass]}@{0[host]}/{0[db]}".format(config))
-
-Session = sessionmaker(bind=engine)
+def get_session():
+ session = sessionmaker(bind=create_engine(
+ "mysql://{0[user]}:{0[pass]}@{0[host]}/{0[db]}".format(
+ json.load(open(
+ path.join(path.dirname(__main__.__file__), 'config', 'db.json')
+ )))))
+ return session()
diff --git a/ausbutler/interface.py b/ausbutler/interface.py
index 94b61e4..dd58b76 100644
--- a/ausbutler/interface.py
+++ b/ausbutler/interface.py
@@ -1,14 +1,14 @@
import re
from .butler import cutoff, get_opponents, get_room, normalize
-from .db import Session
+from .db import get_session
from .model import AusButler, Butler
-class Interface:
+class Interface(object):
def __init__(self, config):
- self.session = Session()
+ self.session = get_session()
self.config = config
def init_db(self, force=False):
@@ -46,7 +46,9 @@ class Interface:
averages = {opps[0]: {'sum': 0.0, 'count': 0},
opps[1]: {'sum': 0.0, 'count': 0}}
for opp_butler in butlers:
- if opp_butler.id in opps and (opp_butler.match < butler.match or (opp_butler.match == butler.match and opp_butler.segment < butler.segment)):
+ if opp_butler.id in opps \
+ and (opp_butler.match < butler.match or \
+ (opp_butler.match == butler.match and opp_butler.segment < butler.segment)):
averages[opp_butler.id]['sum'] += opp_butler.score
averages[opp_butler.id]['count'] += opp_butler.board_count
butler.opp_score = sum(
diff --git a/ausbutler/model.py b/ausbutler/model.py
index cb17ce9..6536b6a 100644
--- a/ausbutler/model.py
+++ b/ausbutler/model.py
@@ -1,12 +1,12 @@
from cached_property import cached_property
-from sqlalchemy import Column, ForeignKey, MetaData, Table, func
+from sqlalchemy import Column, MetaData, Table, func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.types import Float, Integer
-from .db import Session
+from .db import get_session
Base = declarative_base()
-session = Session()
+session = get_session()
class AusButler(Base):
__tablename__ = 'aus_butler'
diff --git a/butler.py b/butler.py
index bb95925..6f3a2d4 100644
--- a/butler.py
+++ b/butler.py
@@ -4,10 +4,9 @@ from os import path
import __main__
from ausbutler.interface import Interface
-config = json.load(
- open(path.join(path.dirname(__main__.__file__), 'config', 'butler.json')))
-i = Interface(config)
+i = Interface(json.load(
+ open(path.join(path.dirname(__main__.__file__), 'config', 'butler.json'))))
i.init_db()
i.populate_db()
i.opp_scores()