Commit 2394fd9ed14c611c35260219b08df96f68bb764e

Authored by Miguel Barão
1 parent 1f734486
Exists in master and in 1 other branch dev

code cleaning

aprendizations/learnapp.py
... ... @@ -10,8 +10,7 @@ from typing import Dict
10 10  
11 11 # third party libraries
12 12 import bcrypt
13   -from sqlalchemy import create_engine, func
14   -from sqlalchemy.orm import sessionmaker
  13 +import sqlalchemy as sa
15 14 import networkx as nx
16 15  
17 16 # this project
... ... @@ -29,7 +28,7 @@ class LearnException(Exception):
29 28 pass
30 29  
31 30  
32   -class DatabaseUnusableException(LearnException):
  31 +class DatabaseUnusableError(LearnException):
33 32 pass
34 33  
35 34  
... ... @@ -48,7 +47,7 @@ class LearnApp(object):
48 47 yield session
49 48 session.commit()
50 49 except Exception:
51   - logger.error('DB rollback!!!')
  50 + logger.error('!!! Database rollback !!!')
52 51 session.rollback()
53 52 raise
54 53 finally:
... ... @@ -268,12 +267,12 @@ class LearnApp(object):
268 267 f'database')
269 268  
270 269 # ------------------------------------------------------------------------
271   - # setup and check database
  270 + # setup and check database contents
272 271 # ------------------------------------------------------------------------
273 272 def db_setup(self, db):
274 273 logger.info(f'Checking database "{db}":')
275   - engine = create_engine(f'sqlite:///{db}', echo=False)
276   - self.Session = sessionmaker(bind=engine)
  274 + engine = sa.create_engine(f'sqlite:///{db}', echo=False)
  275 + self.Session = sa.orm.sessionmaker(bind=engine)
277 276 try:
278 277 with self.db_session() as s:
279 278 n = s.query(Student).count()
... ... @@ -281,7 +280,7 @@ class LearnApp(object):
281 280 q = s.query(Answer).count()
282 281 except Exception:
283 282 logger.error(f'Database "{db}" not usable!')
284   - raise DatabaseUnusableException()
  283 + raise DatabaseUnusableError()
285 284 else:
286 285 logger.info(f'{n:6} students')
287 286 logger.info(f'{m:6} topics')
... ... @@ -311,7 +310,7 @@ class LearnApp(object):
311 310  
312 311 # iterate over topics and populate graph
313 312 topics = config.get('topics', {})
314   - g = self.deps # the dependency graph
  313 + g = self.deps # dependency graph
315 314  
316 315 g.add_nodes_from(topics.keys())
317 316 for tref, attr in topics.items():
... ... @@ -442,10 +441,10 @@ class LearnApp(object):
442 441 total_topics = s.query(Topic).count()
443 442  
444 443 # answer performance
445   - totalans = dict(s.query(Answer.student_id, func.count(Answer.ref)).
  444 + totalans = dict(s.query(Answer.student_id, sa.func.count(Answer.ref)).
446 445 group_by(Answer.student_id).
447 446 all())
448   - rightans = dict(s.query(Answer.student_id, func.count(Answer.ref)).
  447 + rightans = dict(s.query(Answer.student_id, sa.func.count(Answer.ref)).
449 448 filter(Answer.grade == 1.0).
450 449 group_by(Answer.student_id).
451 450 all())
... ...
aprendizations/main.py
... ... @@ -12,7 +12,7 @@ import sys
12 12 import tornado
13 13  
14 14 # this project
15   -from .learnapp import LearnApp, DatabaseUnusableException
  15 +from .learnapp import LearnApp, DatabaseUnusableError
16 16 from .serve import WebApplication
17 17 from .tools import load_yaml
18 18 from . import APP_NAME, APP_VERSION
... ... @@ -149,7 +149,7 @@ def main():
149 149 try:
150 150 learnapp = LearnApp(arg.conffile, prefix=arg.prefix, db=arg.db,
151 151 check=arg.check)
152   - except DatabaseUnusableException:
  152 + except DatabaseUnusableError:
153 153 logging.critical('Failed to start application.')
154 154 print('--------------------------------------------------------------')
155 155 print('Could not find a usable database. Use one of the follwing ')
... ...