Mods
This commit is contained in:
parent
69acfd0389
commit
a14e36e932
42
ants.py
42
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}")
|
||||
Utils.print(f"Ant hatched: {ant.id}")
|
||||
|
||||
@staticmethod
|
||||
def terminate() -> None:
|
||||
pass
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
class Config:
|
||||
title = "Cromulant"
|
||||
width = 800
|
||||
height = 600
|
|
@ -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()
|
||||
Database.connection.commit()
|
||||
|
|
|
@ -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",
|
||||
]
|
70
utils.py
70
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"
|
||||
@staticmethod
|
||||
def print(text: str) -> None:
|
||||
print(text) # noqa: T201
|
||||
|
|
24
window.py
24
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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue