Add rest of project files
This commit is contained in:
3
model/__init__.py
Normal file
3
model/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from model import db_service
|
||||
|
||||
from model import text_db
|
||||
15
model/db_service.py
Normal file
15
model/db_service.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from abc import abstractmethod
|
||||
|
||||
|
||||
class DbService:
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
|
||||
@abstractmethod
|
||||
def get_fact(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_multiple_facts(self, count=5):
|
||||
pass
|
||||
26
model/text_db.py
Normal file
26
model/text_db.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import random
|
||||
|
||||
from model.db_service import DbService
|
||||
from util import common
|
||||
|
||||
|
||||
class TextDb(DbService):
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.FACTS_LIST = None
|
||||
|
||||
def load_facts_from_file(self):
|
||||
facts_file = open(f"{common.get_project_root()}/resources/facts.txt", mode="r+")
|
||||
self.FACTS_LIST = facts_file.readlines()
|
||||
assert self.FACTS_LIST
|
||||
|
||||
def get_fact(self):
|
||||
if self.FACTS_LIST is None:
|
||||
self.load_facts_from_file()
|
||||
return random.choice(self.FACTS_LIST)
|
||||
|
||||
def get_multiple_facts(self, count=5):
|
||||
if self.FACTS_LIST is None:
|
||||
self.load_facts_from_file()
|
||||
return random.sample(self.FACTS_LIST, k=count)
|
||||
Reference in New Issue
Block a user