Use basedpyright refactor
This commit is contained in:
@@ -7,13 +7,13 @@ from folkugat_web.config import auth as config
|
||||
from folkugat_web.log import logger
|
||||
|
||||
|
||||
def login(value):
|
||||
def login(value: str | None) -> bool:
|
||||
if value and value.lower() == config.ADMIN_PASSWORD:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def logged_in(nota_folkugat: Annotated[Optional[str], Cookie()] = None) -> bool:
|
||||
def logged_in(nota_folkugat: Annotated[str | None, Cookie()] = None) -> bool:
|
||||
if not nota_folkugat:
|
||||
return False
|
||||
try:
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
import operator
|
||||
|
||||
from folkugat_web.config import search as config
|
||||
from folkugat_web.model.search import NGrams
|
||||
from folkugat_web.utils import groupby
|
||||
|
||||
|
||||
def get_all_ngrams(text):
|
||||
def get_all_ngrams(text: str) -> list[tuple[int, str]]:
|
||||
return [(m, text[i:i+m])
|
||||
for m in range(config.MIN_NGRAM_LENGTH, len(text) + 1)
|
||||
for i in range(len(text) - m + 1)
|
||||
if m > 0]
|
||||
|
||||
def get_text_ngrams(*texts):
|
||||
texts = [word.lower() for text in texts for word in text.split()]
|
||||
word_ngrams = [ngram for ngrams in map(get_all_ngrams, texts) for ngram in ngrams]
|
||||
result = dict(groupby(word_ngrams,
|
||||
key_fn=operator.itemgetter(0),
|
||||
group_fn=lambda gr: list(set(map(operator.itemgetter(1), gr)))))
|
||||
|
||||
def get_text_ngrams(*texts: str) -> NGrams:
|
||||
lower_texts = [word.lower() for text in texts for word in text.split()]
|
||||
word_ngrams = [ngram for ngrams in map(get_all_ngrams, lower_texts) for ngram in ngrams]
|
||||
result = dict(groupby(
|
||||
word_ngrams,
|
||||
key_fn=lambda x: x[0],
|
||||
group_fn=lambda gr: list(set(map(lambda x: x[1], gr))),
|
||||
))
|
||||
return result
|
||||
|
||||
@@ -25,14 +25,14 @@ def add_tema_to_tema_in_set(tema_in_set: playlists.TemaInSet) -> playlists.TemaI
|
||||
return tema_in_set
|
||||
|
||||
|
||||
def get_playlist(session_id: int, con: Optional[Connection] = None) -> playlists.Playlist:
|
||||
def get_playlist(session_id: int, con: Connection | None = None) -> playlists.Playlist:
|
||||
return playlists.Playlist.from_playlist_entries(
|
||||
session_id=session_id,
|
||||
entries=list(query.get_playlist_entries(session_id=session_id, con=con))
|
||||
)
|
||||
|
||||
|
||||
def add_set(session_id: int, con: Optional[Connection] = None) -> playlists.Set:
|
||||
def add_set(session_id: int, con: Connection | None = None) -> playlists.Set:
|
||||
with get_connection(con) as con:
|
||||
curr_playlist = get_playlist(session_id=session_id, con=con)
|
||||
new_set_id = max([set_entry.id for set_entry in curr_playlist.sets], default=0) + 1
|
||||
@@ -41,7 +41,7 @@ def add_set(session_id: int, con: Optional[Connection] = None) -> playlists.Set:
|
||||
return playlists.Set.from_playlist_entries(set_id=inserted_entry.set_id, entries=[inserted_entry])
|
||||
|
||||
|
||||
def get_set(session_id: int, set_id: int, con: Optional[Connection] = None) -> Optional[playlists.Set]:
|
||||
def get_set(session_id: int, set_id: int, con: Connection | None = None) -> playlists.Set | None:
|
||||
entries = list(query.get_playlist_entries(session_id=session_id, set_id=set_id, con=con))
|
||||
if entries:
|
||||
return playlists.Set.from_playlist_entries(set_id=set_id, entries=entries)
|
||||
@@ -49,30 +49,30 @@ def get_set(session_id: int, set_id: int, con: Optional[Connection] = None) -> O
|
||||
return None
|
||||
|
||||
|
||||
def delete_set(session_id: int, set_id: int, con: Optional[Connection] = None):
|
||||
def delete_set(session_id: int, set_id: int, con: Connection | None = None):
|
||||
write.delete_playlist_set(session_id=session_id, set_id=set_id, con=con)
|
||||
|
||||
|
||||
def add_tema(session_id: int, set_id: int, con: Optional[Connection] = None) -> playlists.TemaInSet:
|
||||
def add_tema(session_id: int, set_id: int, con: Connection | None = None) -> playlists.TemaInSet:
|
||||
with get_connection(con) as con:
|
||||
new_entry = playlists.PlaylistEntry(id=None, session_id=session_id, set_id=set_id, tema_id=None)
|
||||
inserted_entry = write.insert_playlist_entry(new_entry)
|
||||
return playlists.TemaInSet.from_playlist_entry(inserted_entry)
|
||||
|
||||
|
||||
def get_tema(entry_id: int, con: Optional[Connection] = None) -> playlists.TemaInSet:
|
||||
def get_tema(entry_id: int, con: Connection | None = None) -> playlists.TemaInSet:
|
||||
with get_connection(con) as con:
|
||||
entry = next(query.get_playlist_entries(entry_id=entry_id))
|
||||
return playlists.TemaInSet.from_playlist_entry(entry)
|
||||
|
||||
|
||||
def delete_tema(entry_id: int, con: Optional[Connection] = None):
|
||||
def delete_tema(entry_id: int, con: Connection | None = None):
|
||||
with get_connection(con) as con:
|
||||
write.delete_playlist_entry(entry_id=entry_id, con=con)
|
||||
|
||||
|
||||
def set_tema(session_id: int, set_id: int, entry_id: int, tema_id: Optional[int],
|
||||
con: Optional[Connection] = None):
|
||||
def set_tema(session_id: int, set_id: int, entry_id: int, tema_id: int | None,
|
||||
con: Connection | None = None):
|
||||
with get_connection(con) as con:
|
||||
new_entry = playlists.PlaylistEntry(id=entry_id, session_id=session_id, set_id=set_id, tema_id=tema_id)
|
||||
write.update_playlist_entry(entry=new_entry, con=con)
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import datetime
|
||||
from datetime import date as Date
|
||||
from typing import Optional
|
||||
|
||||
from folkugat_web.config import date as config
|
||||
from folkugat_web.dal.sql import sessions as dal
|
||||
from folkugat_web.dal.sql.sessions import query, write
|
||||
from folkugat_web.model import sessions as model
|
||||
from folkugat_web.model.sql import Order, OrderCol, Range
|
||||
|
||||
@@ -27,11 +26,11 @@ def new_session() -> model.Session:
|
||||
|
||||
|
||||
def get_sessions() -> list[model.Session]:
|
||||
return dal.get_sessions(order_by=OrderCol(model.SessionCols.DATE, Order.ASCENDING))
|
||||
return query.get_sessions(order_by=OrderCol(model.SessionCols.DATE, Order.ASCENDING))
|
||||
|
||||
|
||||
def get_next_sessions(limit: Optional[int] = None, offset: Optional[int] = None) -> list[model.Session]:
|
||||
return dal.get_sessions(
|
||||
def get_next_sessions(limit: int | None = None, offset: int | None = None) -> list[model.Session]:
|
||||
return query.get_sessions(
|
||||
date_range=Range(gte=datetime.date.today()),
|
||||
order_by=OrderCol(model.SessionCols.DATE, Order.ASCENDING),
|
||||
limit=limit,
|
||||
@@ -39,8 +38,8 @@ def get_next_sessions(limit: Optional[int] = None, offset: Optional[int] = None)
|
||||
)
|
||||
|
||||
|
||||
def get_sessions_history(limit: Optional[int] = None, offset: Optional[int] = None) -> list[model.Session]:
|
||||
return dal.get_sessions(
|
||||
def get_sessions_history(limit: int | None = None, offset: int | None = None) -> list[model.Session]:
|
||||
return query.get_sessions(
|
||||
date_range=Range(lt=datetime.date.today()),
|
||||
order_by=OrderCol(model.SessionCols.DATE, Order.DESCENDING),
|
||||
limit=limit,
|
||||
@@ -48,33 +47,33 @@ def get_sessions_history(limit: Optional[int] = None, offset: Optional[int] = No
|
||||
)
|
||||
|
||||
|
||||
def get_next_session() -> Optional[model.Session]:
|
||||
def get_next_session() -> model.Session | None:
|
||||
return next(iter(get_next_sessions(limit=1,)), None)
|
||||
|
||||
|
||||
def get_live_session() -> Optional[model.Session]:
|
||||
return next(iter(dal.get_sessions(is_live=True)), None)
|
||||
def get_live_session() -> model.Session | None:
|
||||
return next(iter(query.get_sessions(is_live=True)), None)
|
||||
|
||||
|
||||
def get_session(session_id: int) -> Optional[model.Session]:
|
||||
return next(iter(dal.get_sessions(session_id=session_id)), None)
|
||||
def get_session(session_id: int) -> model.Session | None:
|
||||
return next(iter(query.get_sessions(session_id=session_id)), None)
|
||||
|
||||
|
||||
def insert_session(session: model.Session):
|
||||
return dal.insert_session(session)
|
||||
return write.insert_session(session)
|
||||
|
||||
|
||||
def set_session(session: model.Session):
|
||||
dal.update_session(session)
|
||||
write.update_session(session)
|
||||
|
||||
|
||||
def delete_session(session_id: int):
|
||||
dal.delete_session_by_id(session_id)
|
||||
write.delete_session_by_id(session_id)
|
||||
|
||||
|
||||
def stop_live_sessions():
|
||||
dal.stop_live_sessions()
|
||||
write.stop_live_sessions()
|
||||
|
||||
|
||||
def set_live_session(session_id: int):
|
||||
dal.set_live_session(session_id=session_id)
|
||||
write.set_live_session(session_id=session_id)
|
||||
|
||||
@@ -24,7 +24,7 @@ LINK_RES = {
|
||||
}
|
||||
|
||||
|
||||
def guess_link_type(url: str) -> Optional[model.LinkType]:
|
||||
def guess_link_type(url: str) -> model.LinkType | None:
|
||||
for link_type, regexes in LINK_RES.items():
|
||||
for regex in regexes:
|
||||
if regex.match(url):
|
||||
|
||||
@@ -4,5 +4,5 @@ from folkugat_web.dal.sql.temes import query as temes_q
|
||||
from folkugat_web.model import temes as model
|
||||
|
||||
|
||||
def get_tema_by_id(tema_id: int) -> Optional[model.Tema]:
|
||||
def get_tema_by_id(tema_id: int) -> model.Tema | None:
|
||||
return temes_q.get_tema_by_id(tema_id)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import functools
|
||||
import time
|
||||
import typing
|
||||
from collections.abc import Callable, Iterable, Iterator
|
||||
from collections.abc import Callable, Iterable
|
||||
|
||||
import Levenshtein
|
||||
from folkugat_web.config import search as config
|
||||
@@ -12,7 +12,7 @@ from folkugat_web.model import search as search_model
|
||||
from folkugat_web.model import temes as model
|
||||
|
||||
|
||||
def get_query_word_similarity(query_word: str, text_ngrams: model.NGrams) -> search_model.SearchMatch:
|
||||
def get_query_word_similarity(query_word: str, text_ngrams: search_model.NGrams) -> search_model.SearchMatch:
|
||||
n = len(query_word)
|
||||
if n < config.MIN_NGRAM_LENGTH:
|
||||
return search_model.SearchMatch(distance=0.0, ngram='')
|
||||
@@ -27,13 +27,13 @@ def get_query_word_similarity(query_word: str, text_ngrams: model.NGrams) -> sea
|
||||
default=search_model.SearchMatch(distance=float("inf"), ngram=""))
|
||||
|
||||
|
||||
def get_query_similarity(query: str, ngrams: model.NGrams) -> search_model.SearchMatch:
|
||||
def get_query_similarity(query: str, ngrams: search_model.NGrams) -> search_model.SearchMatch:
|
||||
query_words = query.lower().split()
|
||||
word_matches = map(lambda query_word: get_query_word_similarity(query_word, ngrams), query_words)
|
||||
return search_model.SearchMatch.combine_matches(word_matches)
|
||||
|
||||
|
||||
def build_result(query: str, entry: tuple[int, model.NGrams]) -> search_model.QueryResult:
|
||||
def build_result(query: str, entry: tuple[int, search_model.NGrams]) -> search_model.QueryResult:
|
||||
if len(query) == 0:
|
||||
return search_model.QueryResult(
|
||||
id=entry[0],
|
||||
@@ -51,6 +51,7 @@ def build_result(query: str, entry: tuple[int, model.NGrams]) -> search_model.Qu
|
||||
T = typing.TypeVar("T")
|
||||
|
||||
|
||||
@typing.no_type_check
|
||||
def _thread(it: Iterable[T], *funcs: Callable[[Iterable], Iterable]) -> Iterable:
|
||||
return functools.reduce(lambda i, fn: fn(i), funcs, it)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user