Use basedpyright refactor
This commit is contained in:
@@ -1,11 +1,28 @@
|
||||
import datetime
|
||||
import json
|
||||
from typing import TypedDict
|
||||
|
||||
from folkugat_web.model import IndexedList
|
||||
from folkugat_web.model import search as search_model
|
||||
from folkugat_web.model import temes as model
|
||||
|
||||
TemaRowTuple = tuple[int, str, str, str, str, str, str, str, str, int]
|
||||
|
||||
def tema_to_row(tema: model.Tema) -> dict:
|
||||
|
||||
class TemaRowDict(TypedDict):
|
||||
id: int | None
|
||||
title: str
|
||||
properties: str
|
||||
links: str
|
||||
lyrics: str
|
||||
alternatives: str
|
||||
ngrams: str
|
||||
modification_date: str
|
||||
creation_date: str
|
||||
hidden: int
|
||||
|
||||
|
||||
def tema_to_row(tema: model.Tema) -> TemaRowDict:
|
||||
return {
|
||||
'id': tema.id,
|
||||
'title': tema.title,
|
||||
@@ -20,11 +37,11 @@ def tema_to_row(tema: model.Tema) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def cell_to_ngrams(cell: str) -> model.NGrams:
|
||||
def cell_to_ngrams(cell: str) -> search_model.NGrams:
|
||||
return {int(n): ngrams_ for n, ngrams_ in json.loads(cell).items()}
|
||||
|
||||
|
||||
def row_to_tema(row: tuple) -> model.Tema:
|
||||
def row_to_tema(row: TemaRowTuple) -> model.Tema:
|
||||
return model.Tema(
|
||||
id=row[0],
|
||||
title=row[1],
|
||||
@@ -1,23 +1,15 @@
|
||||
from typing import Optional
|
||||
|
||||
from folkugat_web import data
|
||||
from folkugat_web.dal.sql import Connection, get_connection
|
||||
|
||||
from .write import insert_tema
|
||||
|
||||
|
||||
def create_db(con: Optional[Connection] = None):
|
||||
def create_db(con: Connection | None = None):
|
||||
with get_connection(con) as con:
|
||||
create_temes_table(con)
|
||||
|
||||
# for tema in data.TEMES:
|
||||
# insert_tema(tema, con)
|
||||
|
||||
|
||||
def drop_temes_table(con: Connection):
|
||||
query = "DROP TABLE IF EXISTS temes"
|
||||
cur = con.cursor()
|
||||
cur.execute(query)
|
||||
_ = cur.execute(query)
|
||||
|
||||
|
||||
def create_temes_table(con: Connection):
|
||||
@@ -36,4 +28,4 @@ def create_temes_table(con: Connection):
|
||||
)
|
||||
"""
|
||||
cur = con.cursor()
|
||||
cur.execute(query)
|
||||
_ = cur.execute(query)
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
from typing import Optional
|
||||
|
||||
from folkugat_web.dal.sql import Connection, get_connection
|
||||
from folkugat_web.model import search as search_model
|
||||
from folkugat_web.model import temes as model
|
||||
|
||||
from ._conversion import cell_to_ngrams, row_to_tema
|
||||
from . import conversion
|
||||
|
||||
TEMA_ID_TO_NGRAMS_CACHE = None
|
||||
_tema_id_to_ngrams_cache: dict[int, search_model.NGrams] | None = None
|
||||
|
||||
|
||||
def get_tema_by_id(tema_id: int, con: Optional[Connection] = None) -> Optional[model.Tema]:
|
||||
def get_tema_by_id(tema_id: int, con: Connection | None = None) -> model.Tema | None:
|
||||
query = """
|
||||
SELECT
|
||||
id, title, properties, links, lyrics, alternatives, ngrams,
|
||||
@@ -19,30 +18,30 @@ def get_tema_by_id(tema_id: int, con: Optional[Connection] = None) -> Optional[m
|
||||
data = dict(id=tema_id)
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query, data)
|
||||
row = cur.fetchone()
|
||||
return row_to_tema(row) if row else None
|
||||
_ = cur.execute(query, data)
|
||||
row: conversion.TemaRowTuple = cur.fetchone()
|
||||
return conversion.row_to_tema(row) if row else None
|
||||
|
||||
|
||||
def evict_tema_id_to_ngrams_cache():
|
||||
global TEMA_ID_TO_NGRAMS_CACHE
|
||||
TEMA_ID_TO_NGRAMS_CACHE = None
|
||||
global _tema_id_to_ngrams_cache
|
||||
_tema_id_to_ngrams_cache = None
|
||||
|
||||
|
||||
def get_tema_id_to_ngrams(con: Optional[Connection] = None) -> dict[int, model.NGrams]:
|
||||
global TEMA_ID_TO_NGRAMS_CACHE
|
||||
if TEMA_ID_TO_NGRAMS_CACHE is None:
|
||||
TEMA_ID_TO_NGRAMS_CACHE = _get_tema_id_to_ngrams(con)
|
||||
return TEMA_ID_TO_NGRAMS_CACHE
|
||||
def get_tema_id_to_ngrams(con: Connection | None = None) -> dict[int, search_model.NGrams]:
|
||||
global _tema_id_to_ngrams_cache
|
||||
if _tema_id_to_ngrams_cache is None:
|
||||
_tema_id_to_ngrams_cache = _get_tema_id_to_ngrams(con)
|
||||
return _tema_id_to_ngrams_cache
|
||||
|
||||
|
||||
def _get_tema_id_to_ngrams(con: Optional[Connection] = None) -> dict[int, model.NGrams]:
|
||||
def _get_tema_id_to_ngrams(con: Connection | None = None) -> dict[int, search_model.NGrams]:
|
||||
query = """
|
||||
SELECT id, ngrams
|
||||
FROM temes
|
||||
"""
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query)
|
||||
rows = cur.fetchall()
|
||||
return {id_: cell_to_ngrams(ng) for id_, ng in rows}
|
||||
_ = cur.execute(query)
|
||||
rows: list[tuple[int, str]] = cur.fetchall()
|
||||
return {id_: conversion.cell_to_ngrams(ng) for id_, ng in rows}
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
from typing import Optional
|
||||
|
||||
from folkugat_web.dal.sql import Connection, get_connection
|
||||
from folkugat_web.model import temes as model
|
||||
|
||||
from ._conversion import row_to_tema, tema_to_row
|
||||
from . import conversion
|
||||
from .query import evict_tema_id_to_ngrams_cache
|
||||
|
||||
|
||||
def insert_tema(tema: model.Tema, con: Optional[Connection] = None) -> model.Tema:
|
||||
def insert_tema(tema: model.Tema, con: Connection | None = None) -> model.Tema:
|
||||
query = """
|
||||
INSERT INTO temes
|
||||
(id, title, properties, links, lyrics, alternatives, ngrams,
|
||||
@@ -17,16 +15,16 @@ def insert_tema(tema: model.Tema, con: Optional[Connection] = None) -> model.Tem
|
||||
:creation_date, :modification_date, :hidden)
|
||||
RETURNING *
|
||||
"""
|
||||
data = tema_to_row(tema)
|
||||
data = conversion.tema_to_row(tema)
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query, data)
|
||||
row = cur.fetchone()
|
||||
_ = cur.execute(query, data)
|
||||
row: conversion.TemaRowTuple = cur.fetchone()
|
||||
evict_tema_id_to_ngrams_cache()
|
||||
return row_to_tema(row)
|
||||
return conversion.row_to_tema(row)
|
||||
|
||||
|
||||
def update_tema(tema: model.Tema, con: Optional[Connection] = None):
|
||||
def update_tema(tema: model.Tema, con: Connection | None = None):
|
||||
query = """
|
||||
UPDATE temes
|
||||
SET
|
||||
@@ -36,15 +34,15 @@ def update_tema(tema: model.Tema, con: Optional[Connection] = None):
|
||||
WHERE
|
||||
id = :id
|
||||
"""
|
||||
data = tema_to_row(tema)
|
||||
data = conversion.tema_to_row(tema)
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query, data)
|
||||
_ = cur.execute(query, data)
|
||||
evict_tema_id_to_ngrams_cache()
|
||||
return
|
||||
|
||||
|
||||
def delete_tema(tema_id: int, con: Optional[Connection] = None):
|
||||
def delete_tema(tema_id: int, con: Connection | None = None):
|
||||
query = """
|
||||
DELETE FROM temes
|
||||
WHERE id = :id
|
||||
@@ -52,6 +50,6 @@ def delete_tema(tema_id: int, con: Optional[Connection] = None):
|
||||
data = dict(id=tema_id)
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query, data)
|
||||
_ = cur.execute(query, data)
|
||||
evict_tema_id_to_ngrams_cache()
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user