renamed module
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
"""Database classes module content."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from .dataset import Dataset
|
||||
@@ -1,18 +1,18 @@
|
||||
"""Definition of database Dataset class."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import random
|
||||
from datetime import datetime
|
||||
from datetime import UTC
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from pydantic import BaseModel
|
||||
from pydantic import Field
|
||||
from pydantic import BaseModel, Field
|
||||
from pymongo.collection import Collection
|
||||
|
||||
|
||||
class Dataset(BaseModel):
|
||||
"""Database Dataset model."""
|
||||
|
||||
create_time: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
||||
train_names: list[str]
|
||||
test_names: list[str]
|
||||
@@ -44,13 +44,9 @@ class Dataset(BaseModel):
|
||||
num_test = round(num_total * fraction_map['test'])
|
||||
# split data
|
||||
validation_name_list = random.choices(name_list, k=num_validation)
|
||||
name_list = [
|
||||
name for name in name_list if name not in validation_name_list
|
||||
]
|
||||
name_list = [name for name in name_list if name not in validation_name_list]
|
||||
test_name_list = random.choices(name_list, k=num_test)
|
||||
train_name_list = [
|
||||
name for name in name_list if name not in test_name_list
|
||||
]
|
||||
train_name_list = [name for name in name_list if name not in test_name_list]
|
||||
# instantiate object
|
||||
dataset = Dataset(
|
||||
train_names=train_name_list,
|
||||
@@ -1,4 +1,5 @@
|
||||
"""Definition of database exception."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""Definition of function to count documents in database."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pymongo.collection import Collection
|
||||
@@ -8,10 +9,8 @@ def count_documents(
|
||||
collection: Collection,
|
||||
only_with_annotation: bool = False,
|
||||
) -> int:
|
||||
"""
|
||||
Get the total number of documents
|
||||
in database that matches the filters.
|
||||
"""
|
||||
"""Get the total number of documents in database that matches the
|
||||
filters."""
|
||||
assert isinstance(collection, Collection)
|
||||
assert isinstance(only_with_annotation, bool)
|
||||
# build query
|
||||
+5
-5
@@ -1,12 +1,12 @@
|
||||
"""Definition of function to get visual communication from database."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from pymongo.collection import Collection
|
||||
|
||||
from shared.database import NoDocumentFoundException
|
||||
from shared.database import VisualCommunication
|
||||
from shared.mongodb import NoDocumentFoundException, VisualCommunication
|
||||
|
||||
|
||||
def get_visual_communication(
|
||||
@@ -22,16 +22,16 @@ def get_visual_communication(
|
||||
data = collection.aggregate(
|
||||
pipeline=[
|
||||
{
|
||||
'$match': query, # find using filters
|
||||
'$match': query, # find using filters
|
||||
},
|
||||
{
|
||||
'$sample': {
|
||||
'size': 1, # get one random
|
||||
'size': 1, # get one random
|
||||
},
|
||||
},
|
||||
],
|
||||
)
|
||||
data_list = list(data) # read data from cursor object
|
||||
data_list = list(data) # read data from cursor object
|
||||
if len(data_list) == 0:
|
||||
raise NoDocumentFoundException()
|
||||
vis_com = VisualCommunication.model_validate(data_list[0])
|
||||
@@ -1,7 +1,6 @@
|
||||
"""
|
||||
Definition of function to list names
|
||||
of all visual communication documents in database.
|
||||
"""
|
||||
"""Definition of function to list names of all visual communication documents
|
||||
in database."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pymongo.collection import Collection
|
||||
@@ -4,7 +4,7 @@ import logging
|
||||
|
||||
from pymongo.collection import Collection
|
||||
|
||||
from shared.database import Dataset
|
||||
from shared.mongodb import Dataset
|
||||
|
||||
|
||||
def save_dataset(
|
||||
@@ -22,7 +22,7 @@ if __name__ == '__main__':
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv('local.env')
|
||||
from shared.database import connect_mongodb, list_names
|
||||
from shared.mongodb import connect_mongodb, list_names
|
||||
|
||||
# connect to database
|
||||
collection, db, client = connect_mongodb()
|
||||
+4
-8
@@ -2,22 +2,18 @@ from __future__ import annotations
|
||||
|
||||
from pymongo.collection import Collection
|
||||
|
||||
from shared.database import VisualCommunication
|
||||
from shared.mongodb import VisualCommunication
|
||||
|
||||
|
||||
def upsert_visual_communication(
|
||||
collection: Collection,
|
||||
visual_communication_list: list[VisualCommunication],
|
||||
) -> bool:
|
||||
"""
|
||||
Upsert VisualCommunication object in the database.
|
||||
"""Upsert VisualCommunication object in the database.
|
||||
|
||||
Returns bool stating success.
|
||||
"""
|
||||
response = collection.insert_many(
|
||||
[
|
||||
vis_com.model_dump()
|
||||
for vis_com
|
||||
in visual_communication_list
|
||||
],
|
||||
[vis_com.model_dump() for vis_com in visual_communication_list],
|
||||
)
|
||||
return response.acknowledged
|
||||
Reference in New Issue
Block a user