New job: unit conversion. Refactoring to modules

This commit is contained in:
Dejvino
2025-12-23 00:10:50 +01:00
parent a87770a274
commit 0e16ea75fb
6 changed files with 110 additions and 45 deletions
+24
View File
@@ -0,0 +1,24 @@
from datetime import datetime
class Job:
def get_name(self):
raise NotImplementedError
def print_body(self, p):
raise NotImplementedError
def run(self, p):
# Shared Header
p.set(align='center', height=2, width=2, bold=True)
p.text(f"{self.get_name()}\n")
p.text("-------------\n")
p.set(align='left', height=1, width=1, bold=False)
now = datetime.now().strftime("%d.%m.%Y %H:%M")
p.text(f"Datum: {now}\n\n")
# Job specific body
self.print_body(p)
# Cut
p.cut()
+14
View File
@@ -0,0 +1,14 @@
import random
from .base import Job
class MathHomeworkJob(Job):
def get_name(self):
return "MALA NASOBILKA"
def print_body(self, p):
p.text("Vypocitej:\n\n")
for i in range(1, 11):
num1 = random.randint(2, 12)
num2 = random.randint(2, 12)
p.text(f"{i}) {num1} * {num2} = ____\n\n")
p.text("Hodne stesti!\n")
+39
View File
@@ -0,0 +1,39 @@
import random
from .base import Job
class UnitConversionJob(Job):
def get_name(self):
return "PREVODY JEDNOTEK"
def print_body(self, p):
p.text("Preved:\n\n")
# (from_unit, to_unit, factor)
conversions = [
('kg', 'g', 1000),
('km', 'm', 1000),
('m', 'cm', 100),
('cm', 'mm', 10),
('h', 'min', 60),
('min', 's', 60)
]
for i in range(1, 11):
u_from, u_to, factor = random.choice(conversions)
# Randomly choose direction (multiply or divide)
if random.choice([True, False]):
# Big to Small (Multiply)
val = random.randint(1, 20)
question = f"{val} {u_from}"
target_unit = u_to
else:
# Small to Big (Divide) - ensure clean integer
target = random.randint(1, 20)
val = target * factor
question = f"{val} {u_to}"
target_unit = u_from
p.text(f"{i}) {question} = ____ {target_unit}\n\n")
p.text("Hodne stesti!\n")