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