diff --git a/ants.py b/ants.py index 981bce1..8e7fcd1 100644 --- a/ants.py +++ b/ants.py @@ -1,11 +1,13 @@ from __future__ import annotations +from typing import ClassVar -import utils +from utils import Utils from database import Database + class Ant: def __init__(self) -> None: - now = utils.now() + now = Utils.now() self.id: int self.created = now self.updated = now @@ -14,26 +16,27 @@ class Ant: self.hits = 0 self.triumph = 0 - def get_name(self): + def get_name(self) -> str: return self.name or "Nameless" - def get_age(self): - now = utils.now() - return utils.time_ago(self.created, now) + def get_age(self) -> str: + now = Utils.now() + return Utils.time_ago(self.created, now) + + def describe(self) -> None: + Utils.print(f"Name is {self.get_name()}") + Utils.print(f"It hatched {self.get_age()}") - def describe(self): - print(f"Name is {self.get_name()}") - print(f"It hatched {self.get_age()}") class Ants: - ants: list[Ant] = [] + ants: ClassVar[list[Ant]] = [] @staticmethod def get_all() -> None: - Database.cursor.execute("SELECT id, created, updated, name, status, hits, triumph FROM ants") - + Database.cursor.execute( + "SELECT id, created, updated, name, status, hits, triumph FROM ants" + ) rows = Database.cursor.fetchall() - ants = [] for row in rows: ant = Ant() @@ -44,14 +47,11 @@ class Ants: ant.status = row[4] ant.hits = row[5] ant.triumph = row[6] - ants.append(ant) - - Database.connection.commit() - return ants + Ants.ants.append(ant) @staticmethod def hatch() -> None: - now = utils.now() + now = Utils.now() Database.cursor.execute( "INSERT INTO ants (created, updated) VALUES (?, ?)", @@ -65,4 +65,8 @@ class Ants: ant = Ant() ant.id = row[0] - print(f"Ant hatched: {ant.id}") \ No newline at end of file + Utils.print(f"Ant hatched: {ant.id}") + + @staticmethod + def terminate() -> None: + pass diff --git a/config.py b/config.py new file mode 100644 index 0000000..07e5312 --- /dev/null +++ b/config.py @@ -0,0 +1,4 @@ +class Config: + title = "Cromulant" + width = 800 + height = 600 diff --git a/database.py b/database.py index f951c45..7cf689f 100644 --- a/database.py +++ b/database.py @@ -3,6 +3,7 @@ from __future__ import annotations import sqlite3 from pathlib import Path + class Database: connection: sqlite3.Connection cursor: sqlite3.Cursor @@ -18,4 +19,4 @@ class Database: schema = file.read() Database.cursor.executescript(schema) - Database.connection.commit() \ No newline at end of file + Database.connection.commit() diff --git a/main.py b/main.py index 4b3ac21..5fd10bd 100644 --- a/main.py +++ b/main.py @@ -21,4 +21,4 @@ def main() -> None: if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/ruff.toml b/ruff.toml new file mode 100644 index 0000000..b2ec399 --- /dev/null +++ b/ruff.toml @@ -0,0 +1,41 @@ +[lint] + +select = [ + "T", + "Q", + "W", + "B", + "N", + "F", + "FA", + "RET", + "PTH", + "ERA", + "PLW", + "PERF", + "RUF", + "FLY", + "PT", + "PYI", + "PIE", + "ICN", + "UP", + "TRY", + "C4", + "E401", + "E713", + "E721", + "S101", + "S113", + "SIM103", + "SIM114", + "SIM118", + "SIM210", + "PLR5501", + "PLR1711", +] + +exclude = [ + "pyperclip.py", + "tests.py", +] \ No newline at end of file diff --git a/utils.py b/utils.py index 1b9f643..f91540b 100644 --- a/utils.py +++ b/utils.py @@ -1,49 +1,55 @@ import time -def now() -> float: - return int(time.time()) +class Utils: + @staticmethod + def now() -> float: + return int(time.time()) + @staticmethod + def singular_or_plural(num: float, singular: str, plural: str) -> str: + if num == 1: + return singular -def singular_or_plural(num: float, singular: str, plural: str) -> str: - if num == 1: - return singular + return plural - return plural + @staticmethod + def time_ago(start_time: float, end_time: float) -> str: + diff = end_time - start_time + seconds = int(diff) + if seconds < 60: + word = Utils.singular_or_plural(seconds, "second", "seconds") + return f"{seconds} {word} ago" -def time_ago(start_time: float, end_time: float) -> str: - diff = end_time - start_time - seconds = int(diff) + minutes = seconds // 60 - if seconds < 60: - word = singular_or_plural(seconds, "second", "seconds") - return f"{seconds} {word} ago" + if minutes < 60: + word = Utils.singular_or_plural(minutes, "minute", "minutes") + return f"{minutes} {word} ago" - minutes = seconds // 60 + hours = minutes / 60 - if minutes < 60: - word = singular_or_plural(minutes, "minute", "minutes") - return f"{minutes} {word} ago" + if hours < 24: + word = Utils.singular_or_plural(hours, "hour", "hours") + return f"{hours:.1f} {word} ago" - hours = minutes / 60 + days = hours / 24 - if hours < 24: - word = singular_or_plural(hours, "hour", "hours") - return f"{hours:.1f} {word} ago" + if days < 30: + word = Utils.singular_or_plural(days, "day", "days") + return f"{days:.1f} {word} ago" - days = hours / 24 + months = days / 30 - if days < 30: - word = singular_or_plural(days, "day", "days") - return f"{days:.1f} {word} ago" + if months < 12: + word = Utils.singular_or_plural(months, "month", "months") + return f"{months:.1f} {word} ago" - months = days / 30 + years = months / 12 + word = Utils.singular_or_plural(years, "year", "years") + return f"{years:.1f} {word} ago" - if months < 12: - word = singular_or_plural(months, "month", "months") - return f"{months:.1f} {word} ago" - - years = months / 12 - word = singular_or_plural(years, "year", "years") - return f"{years:.1f} {word} ago" \ No newline at end of file + @staticmethod + def print(text: str) -> None: + print(text) # noqa: T201 diff --git a/window.py b/window.py index a0803c5..363dd87 100644 --- a/window.py +++ b/window.py @@ -1,25 +1,30 @@ from __future__ import annotations -from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QHBoxLayout, QWidget -from PySide6.QtWidgets import QGraphicsView, QGraphicsScene, QVBoxLayout +from PySide6.QtWidgets import QApplication # type: ignore +from PySide6.QtWidgets import QMainWindow +from PySide6.QtWidgets import QWidget +from PySide6.QtWidgets import QGraphicsView +from PySide6.QtWidgets import QGraphicsScene +from PySide6.QtWidgets import QVBoxLayout +from PySide6.QtWidgets import QPushButton +from PySide6.QtWidgets import QHBoxLayout +from config import Config from ants import Ants class Window: - title = "Cromulant" - width = 800 - height = 600 app: QApplication window: QMainWindow root: QWidget + view: QGraphicsView @staticmethod def make() -> None: Window.app = QApplication([]) Window.window = QMainWindow() - Window.window.setWindowTitle(Window.title) - Window.window.resize(Window.width, Window.height) + Window.window.setWindowTitle(Config.title) + Window.window.resize(Config.width, Config.height) Window.root = QWidget() Window.window.setCentralWidget(Window.root) @@ -40,9 +45,8 @@ class Window: @staticmethod def add_view() -> None: Window.view = QGraphicsView() - Window.scene = QGraphicsScene() - Window.view.setScene(Window.scene) - + scene = QGraphicsScene() + Window.view.setScene(scene) layout = QVBoxLayout(Window.root) layout.addWidget(Window.view)