gui-db/db/__init__.py

51 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from typing import List
from pydantic import BaseModel, Field, TypeAdapter
from uuid import UUID, uuid4
import json
class Record(BaseModel):
id: UUID = Field(default_factory=uuid4)
name: str | None = ''
description:str | None = ''
img_path: str | None = ''
class DB:
records: List[Record] = []
path = ''
def __init__(self, path=''):
if path != '':
with open(path, 'r') as file:
js_data = json.load(file)
tmp = []
for item in js_data:
tmp.append(json.loads(item))
js_data = tmp
self.records = [Record(**model_data) for model_data in js_data]
self.path = path
def add_record(self, name='', description='', img_path=''):
new_rec = Record(name=name, description=description, img_path=img_path)
self.records.append(new_rec)
with open(self.path, 'w') as file:
model_list_json = [record.model_dump_json() for record in self.records]
json.dump(model_list_json, file)
def edit_record(self, id):
pass
def delete_record(self, id):
for i in range(len(self.records)):
if self.records[i].id == id:
self.records.pop(i)
# db_t = DB('db.json')
# db_t.add_record(name='Азовское море',description='полузамкнутое море Атлантического океана на востоке Европы, омывающее побережье России и Украины. Самое мелкое море в мире: глубина не превышает 13,5 метров[2], средняя глубина около 7,4 м (по разным оценкам — от 6,8 до 8 м)[3]. \n Соединяется с Атлантическим океаном длинной цепочкой проливов и морей: Керченский пролив — Чёрное море — пролив Босфор — Мраморное море — пролив Дарданеллы — Эгейское море — Средиземное море — Гибралтарский пролив. По отдалённости от океана Азовское море является самым континентальным морем планеты. Объём воды — 290 км³[4]. \n В древности Азовского моря не существовало и Дон впадал в Чёрное море в районе современного Керченского пролива. Предполагается, что заполнение акватории Азовского моря произошло около 5600 года до н. э.', img_path='img/azovskoe.jpg')