style: Refactoring imports, adapting MongoConnector to different connection_strings

This commit is contained in:
TrisNol 2023-06-30 20:36:03 +02:00
parent 3cd8860312
commit e44385ce3a
6 changed files with 145 additions and 8 deletions

View File

@ -1,6 +1,6 @@
from abc import ABC
from models.News import News
from News.models.News import News
class NewsServiceInterface(ABC):

View File

@ -1,6 +1,6 @@
import pymongo
from models.News import News
from utils.NewsServiceInterface import NewsServiceInterface
from News.models.News import News
from News.utils.NewsServiceInterface import NewsServiceInterface
class MongoConnector:
@ -8,7 +8,7 @@ class MongoConnector:
self,
hostname,
database: str,
port: int = 27017,
port: int | None,
username: str | None = None,
password: str | None = None,
):
@ -20,10 +20,13 @@ class MongoConnector:
def connect(self, hostname, port, username, password) -> pymongo.MongoClient:
if username is not None and password is not None:
connection_string = f"mongodb://{username}:{password}@{hostname}:{port}"
connection_string = f"mongodb+srv://{username}:{password}@{hostname}"
else:
connection_string = f"mongodb://{hostname}:{port}"
connection_string = f"mongodb+srv://{hostname}"
if port is not None:
connection_string += f":{port}"
connection_string = connection_string.replace("mongodb+srv", "mongodb")
print(connection_string)
return pymongo.MongoClient(connection_string)

View File

@ -1,5 +1,5 @@
from abc import ABC
from dataclasses import dataclass
from dataclasses import asdict, dataclass
from enum import Enum
@ -35,3 +35,6 @@ class Company:
name: str
last_update: str
relationships: list[CompanyRelationship]
def dict(self):
return asdict(self)

View File

@ -4199,6 +4199,102 @@
" ) as export_file:\n",
" json.dump(dataclasses.asdict(company), export_file, ensure_ascii=False)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"\n",
"module_path = os.path.abspath(os.path.join(\"..\"))\n",
"if module_path not in sys.path:\n",
" sys.path.append(module_path)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"mongodb://root:pR0R0v2e2@trisnol.tech:27017\n"
]
}
],
"source": [
"from News.utils.mongodb.mongo import MongoConnector\n",
"from Unternehmensregister.utils.CompanyMongoService import CompanyMongoService\n",
"\n",
"connector = MongoConnector(\n",
" hostname=\"trisnol.tech\",\n",
" database=\"transparenzregister\",\n",
" username=\"root\",\n",
" password=\"pR0R0v2e2\",\n",
" port=27017,\n",
")\n",
"\n",
"service = CompanyMongoService(connector)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 0%| | 0/3147 [00:00<?, ?it/s]"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 3147/3147 [00:30<00:00, 102.30it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inserted documents: 0\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"from tqdm import tqdm\n",
"import glob\n",
"import json\n",
"from Unternehmensregister.models.Company import Company\n",
"\n",
"num_inserted = 0\n",
"for file in tqdm(glob.glob1(\"./data/Unternehmensregister/transformed\", \"*.json\")):\n",
" path = os.path.join(\"./data/Unternehmensregister/transformed\", file)\n",
" with open(path, \"r\", encoding=\"utf-8\") as file_object:\n",
" data = json.loads(file_object.read())\n",
" company: Company = Company(**data)\n",
"\n",
" company_db = service.get_by_id(company.id)\n",
" if company_db is None:\n",
" service.insert(company)\n",
" num_inserted += 1\n",
"print(f\"Inserted documents: {num_inserted}\")"
]
}
],
"metadata": {

View File

@ -0,0 +1,21 @@
from News.utils.mongodb.mongo import MongoConnector
from Unternehmensregister.models.Company import Company
from Unternehmensregister.utils.CompanyServiceInterface import CompanyServiceInterface
class CompanyMongoService(CompanyServiceInterface):
def __init__(self, connector: MongoConnector):
self.collection = connector.database["companies"]
def get_all(self) -> list[Company]:
result = self.collection.find()
return list(result)
def get_by_id(self, id: str) -> Company | None:
result = list(self.collection.find({"id": id}))
if len(result) == 1:
return result[0]
return None
def insert(self, company: Company):
return self.collection.insert_one(company.dict())

View File

@ -0,0 +1,14 @@
from abc import ABC
from models import Company
class CompanyServiceInterface(ABC):
def get_all(self) -> list[Company.Company]:
raise NotImplementedError()
def get_by_id(self, id: Company.CompayID) -> Company.Company | None:
raise NotImplementedError()
def insert(self, company: Company.Company):
raise NotImplementedError()