Skip to content

Commit

Permalink
changed Row NamedTuple
Browse files Browse the repository at this point in the history
changed to a named tuple enum so the subtext does not need to be instanced all the time
  • Loading branch information
agmes4 committed Jan 2, 2024
1 parent 6a5c0e8 commit 6e656e9
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions sipa/model/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import typing as t
# noinspection PyMethodMayBeStatic
from abc import ABCMeta, abstractmethod
from collections import namedtuple
from contextlib import contextmanager
from datetime import date
from typing import TypeVar
Expand All @@ -27,7 +26,13 @@ class AuthenticatedUserMixin:
is_anonymous = False


Row = namedtuple('Row', [ 'property','description', 'subtext'])
class TableRow(t.NamedTuple):
"""Represents a Row in on the user pages Table"""

property: str
description: str
subtext: str | None = None


# for annotating the classmethods
T = TypeVar('T', bound='BaseUser')
Expand Down Expand Up @@ -150,18 +155,18 @@ def traffic_history(self) -> list[dict]:
"""
pass

def generate_rows(self, description_dict: dict) -> t.Iterator[Row]:
def generate_rows(self, description_dict: dict) -> t.Iterator[TableRow]:
for key, val in description_dict.items():
yield Row(property=getattr(self, key), **self.__text_to_dict(val))
yield TableRow(property=getattr(self, key), **self.__text_to_dict(val))

def __text_to_dict(self, val: str|list[str]) -> dict:
match val:
case [d, s]:
return {"description": d, "subtext": s}
case [d]:
return {"description": d, "subtext": None}
return {"description": d}
case _:
return {"description": "Error", "subtext": None}
return {"description": "Error"}

@property
@abstractmethod
Expand Down

0 comments on commit 6e656e9

Please sign in to comment.