This commit is contained in:
Auric Vente 2024-07-18 01:20:36 -06:00
parent 69acfd0389
commit a14e36e932
7 changed files with 123 additions and 63 deletions

42
ants.py
View File

@ -1,11 +1,13 @@
from __future__ import annotations from __future__ import annotations
from typing import ClassVar
import utils from utils import Utils
from database import Database from database import Database
class Ant: class Ant:
def __init__(self) -> None: def __init__(self) -> None:
now = utils.now() now = Utils.now()
self.id: int self.id: int
self.created = now self.created = now
self.updated = now self.updated = now
@ -14,26 +16,27 @@ class Ant:
self.hits = 0 self.hits = 0
self.triumph = 0 self.triumph = 0
def get_name(self): def get_name(self) -> str:
return self.name or "Nameless" return self.name or "Nameless"
def get_age(self): def get_age(self) -> str:
now = utils.now() now = Utils.now()
return utils.time_ago(self.created, 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: class Ants:
ants: list[Ant] = [] ants: ClassVar[list[Ant]] = []
@staticmethod @staticmethod
def get_all() -> None: 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() rows = Database.cursor.fetchall()
ants = []
for row in rows: for row in rows:
ant = Ant() ant = Ant()
@ -44,14 +47,11 @@ class Ants:
ant.status = row[4] ant.status = row[4]
ant.hits = row[5] ant.hits = row[5]
ant.triumph = row[6] ant.triumph = row[6]
ants.append(ant) Ants.ants.append(ant)
Database.connection.commit()
return ants
@staticmethod @staticmethod
def hatch() -> None: def hatch() -> None:
now = utils.now() now = Utils.now()
Database.cursor.execute( Database.cursor.execute(
"INSERT INTO ants (created, updated) VALUES (?, ?)", "INSERT INTO ants (created, updated) VALUES (?, ?)",
@ -65,4 +65,8 @@ class Ants:
ant = Ant() ant = Ant()
ant.id = row[0] ant.id = row[0]
print(f"Ant hatched: {ant.id}") Utils.print(f"Ant hatched: {ant.id}")
@staticmethod
def terminate() -> None:
pass

4
config.py Normal file
View File

@ -0,0 +1,4 @@
class Config:
title = "Cromulant"
width = 800
height = 600

View File

@ -3,6 +3,7 @@ from __future__ import annotations
import sqlite3 import sqlite3
from pathlib import Path from pathlib import Path
class Database: class Database:
connection: sqlite3.Connection connection: sqlite3.Connection
cursor: sqlite3.Cursor cursor: sqlite3.Cursor

41
ruff.toml Normal file
View File

@ -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",
]

View File

@ -1,49 +1,55 @@
import time import time
def now() -> float: class Utils:
@staticmethod
def now() -> float:
return int(time.time()) return int(time.time())
@staticmethod
def singular_or_plural(num: float, singular: str, plural: str) -> str: def singular_or_plural(num: float, singular: str, plural: str) -> str:
if num == 1: if num == 1:
return singular return singular
return plural return plural
@staticmethod
def time_ago(start_time: float, end_time: float) -> str: def time_ago(start_time: float, end_time: float) -> str:
diff = end_time - start_time diff = end_time - start_time
seconds = int(diff) seconds = int(diff)
if seconds < 60: if seconds < 60:
word = singular_or_plural(seconds, "second", "seconds") word = Utils.singular_or_plural(seconds, "second", "seconds")
return f"{seconds} {word} ago" return f"{seconds} {word} ago"
minutes = seconds // 60 minutes = seconds // 60
if minutes < 60: if minutes < 60:
word = singular_or_plural(minutes, "minute", "minutes") word = Utils.singular_or_plural(minutes, "minute", "minutes")
return f"{minutes} {word} ago" return f"{minutes} {word} ago"
hours = minutes / 60 hours = minutes / 60
if hours < 24: if hours < 24:
word = singular_or_plural(hours, "hour", "hours") word = Utils.singular_or_plural(hours, "hour", "hours")
return f"{hours:.1f} {word} ago" return f"{hours:.1f} {word} ago"
days = hours / 24 days = hours / 24
if days < 30: if days < 30:
word = singular_or_plural(days, "day", "days") word = Utils.singular_or_plural(days, "day", "days")
return f"{days:.1f} {word} ago" return f"{days:.1f} {word} ago"
months = days / 30 months = days / 30
if months < 12: if months < 12:
word = singular_or_plural(months, "month", "months") word = Utils.singular_or_plural(months, "month", "months")
return f"{months:.1f} {word} ago" return f"{months:.1f} {word} ago"
years = months / 12 years = months / 12
word = singular_or_plural(years, "year", "years") word = Utils.singular_or_plural(years, "year", "years")
return f"{years:.1f} {word} ago" return f"{years:.1f} {word} ago"
@staticmethod
def print(text: str) -> None:
print(text) # noqa: T201

View File

@ -1,25 +1,30 @@
from __future__ import annotations from __future__ import annotations
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QHBoxLayout, QWidget from PySide6.QtWidgets import QApplication # type: ignore
from PySide6.QtWidgets import QGraphicsView, QGraphicsScene, QVBoxLayout 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 from ants import Ants
class Window: class Window:
title = "Cromulant"
width = 800
height = 600
app: QApplication app: QApplication
window: QMainWindow window: QMainWindow
root: QWidget root: QWidget
view: QGraphicsView
@staticmethod @staticmethod
def make() -> None: def make() -> None:
Window.app = QApplication([]) Window.app = QApplication([])
Window.window = QMainWindow() Window.window = QMainWindow()
Window.window.setWindowTitle(Window.title) Window.window.setWindowTitle(Config.title)
Window.window.resize(Window.width, Window.height) Window.window.resize(Config.width, Config.height)
Window.root = QWidget() Window.root = QWidget()
Window.window.setCentralWidget(Window.root) Window.window.setCentralWidget(Window.root)
@ -40,9 +45,8 @@ class Window:
@staticmethod @staticmethod
def add_view() -> None: def add_view() -> None:
Window.view = QGraphicsView() Window.view = QGraphicsView()
Window.scene = QGraphicsScene() scene = QGraphicsScene()
Window.view.setScene(Window.scene) Window.view.setScene(scene)
layout = QVBoxLayout(Window.root) layout = QVBoxLayout(Window.root)
layout.addWidget(Window.view) layout.addWidget(Window.view)