83 lines
2.4 KiB
Python
83 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Optional, Self, Any
|
|
from Abstracts.TableAbstract import TableAbstract
|
|
|
|
class TableModel(TableAbstract):
|
|
|
|
def __init__(self:Self,
|
|
data:tuple[tuple[Any|None, ...], ...],
|
|
map:Optional[tuple[str, ...]] = None
|
|
) -> None:
|
|
self.data:tuple[tuple[Any|None, ...], ...] = data
|
|
self.map:tuple[str, ...]|None = map
|
|
|
|
def get(self:Self) -> tuple[tuple[str]|None, tuple[tuple[Any|None, ...], ...]]:
|
|
return self.map, self.data
|
|
|
|
def get_tuples(self:Self) -> tuple[tuple[Any|None, ...], ...]:
|
|
return self.data
|
|
|
|
def get_dictionaries(self:Self) -> list[dict[str, Any|None]]:
|
|
|
|
result:list[dict[str, Any|None]] = []
|
|
|
|
if len(self.data):
|
|
|
|
names:tuple[str, ...] = self.map or tuple()
|
|
l:int = len(self.data[0])
|
|
data:tuple[Any|None, ...]
|
|
|
|
while len(names) < l:
|
|
names += (f"column_{len(names)}",)
|
|
|
|
for data in self.data:
|
|
|
|
i:int
|
|
key:str
|
|
row:dict[str, Any|None] = {}
|
|
|
|
for i, key in enumerate(names):
|
|
row[key] = data[i] if i < len(data) else None
|
|
|
|
result.append(row)
|
|
|
|
return result
|
|
|
|
def get(self:Self, column:int|str, tuple:int = 0) -> Any|None:
|
|
|
|
if isinstance(column, str):
|
|
|
|
if self.map is not None and column in self.map:
|
|
column = self.map.index(column)
|
|
else:
|
|
return None
|
|
|
|
if 0 <= tuple < len(self.data) and 0 <= column < len(self.data[tuple]):
|
|
return self.data[tuple][column]
|
|
|
|
return None
|
|
|
|
class ResultsModel:
|
|
|
|
def __init__(self:Self) -> None:
|
|
self.tables:list[TableModel] = []
|
|
self.variables:dict[str, Any|None] = {}
|
|
self.ids:list[int] = []
|
|
|
|
def set(self:Self,
|
|
data:tuple[tuple[Any|None, ...], ...],
|
|
variables:dict[str, Any|None]|None = None,
|
|
Type:Optional[type[TableAbstract]|tuple[str, ...]] = None
|
|
) -> None:
|
|
self.tables.append(
|
|
TableModel(data, Type) if Type is None or isinstance(Type, tuple) else
|
|
Type(data))
|
|
if variables is not None:
|
|
self.variables.update(variables)
|
|
|
|
def get(self:Self, column:int|str, tuple:int = 0, table:int = 0) -> Any|None:
|
|
return (
|
|
self.tables[table].get(column, tuple) if 0 <= table < len(self.tables) else
|
|
None) |