35 lines
976 B
Python
35 lines
976 B
Python
import random
|
|
|
|
import boto3
|
|
|
|
import constants
|
|
from db_service import DbService
|
|
import common
|
|
|
|
|
|
class DynamoDb(DbService):
|
|
|
|
def __init__(self) -> None:
|
|
super().__init__()
|
|
self.FACTS_LIST = []
|
|
self.dynamodb = boto3.resource('dynamodb')
|
|
self.facts_table = self.dynamodb.Table(constants.DB_TABLE_NAME)
|
|
print(self.facts_table.creation_date_time)
|
|
|
|
def load_facts_from_db(self):
|
|
# global facts_table
|
|
self.FACTS_LIST.clear()
|
|
resp = self.facts_table.scan()
|
|
self.FACTS_LIST = [i["fact"] for i in resp["Items"]]
|
|
assert self.FACTS_LIST
|
|
|
|
def get_fact(self):
|
|
if self.FACTS_LIST is None or not self.FACTS_LIST:
|
|
self.load_facts_from_db()
|
|
return random.choice(self.FACTS_LIST)
|
|
|
|
def get_multiple_facts(self, count=2):
|
|
if self.FACTS_LIST is None or not self.FACTS_LIST:
|
|
self.load_facts_from_db()
|
|
return random.sample(self.FACTS_LIST, k=count)
|