New job: unit conversion. Refactoring to modules
This commit is contained in:
parent
a87770a274
commit
0e16ea75fb
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
__pycache__
|
||||||
1
__init__.py
Normal file
1
__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
# Package marker
|
||||||
24
jobs/base.py
Normal file
24
jobs/base.py
Normal 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
jobs/math_homework.py
Normal file
14
jobs/math_homework.py
Normal 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
jobs/unit_conversion.py
Normal file
39
jobs/unit_conversion.py
Normal 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")
|
||||||
@ -1,7 +1,7 @@
|
|||||||
import random
|
|
||||||
from datetime import datetime
|
|
||||||
from escpos.printer import Usb, Dummy
|
from escpos.printer import Usb, Dummy
|
||||||
from escpos.exceptions import USBNotFoundError
|
from escpos.exceptions import USBNotFoundError
|
||||||
|
from jobs.math_homework import MathHomeworkJob
|
||||||
|
from jobs.unit_conversion import UnitConversionJob
|
||||||
|
|
||||||
# ==========================================
|
# ==========================================
|
||||||
# CONFIGURATION
|
# CONFIGURATION
|
||||||
@ -36,40 +36,10 @@ def get_printer():
|
|||||||
print(f"Error initializing printer: {e}")
|
print(f"Error initializing printer: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def generate_homework_job(p):
|
JOBS = [
|
||||||
"""
|
MathHomeworkJob(),
|
||||||
Sends ESC/POS commands to the printer to print the homework.
|
UnitConversionJob()
|
||||||
"""
|
]
|
||||||
# 1. Header (Center aligned, Double Height/Width)
|
|
||||||
p.set(align='center', height=2, width=2, bold=True)
|
|
||||||
p.text("MALA NASOBILKA\n")
|
|
||||||
p.text("-------------\n")
|
|
||||||
|
|
||||||
# Reset text formatting for body
|
|
||||||
p.set(align='left', height=1, width=1, bold=False)
|
|
||||||
today = datetime.now().strftime("%d.%m.%Y")
|
|
||||||
p.text(f"Datum: {today}\n\n")
|
|
||||||
|
|
||||||
# 2. Generate and Print Equations
|
|
||||||
p.text("Vypocitej:\n\n")
|
|
||||||
|
|
||||||
for i in range(1, 11):
|
|
||||||
num1 = random.randint(2, 12)
|
|
||||||
num2 = random.randint(2, 12)
|
|
||||||
|
|
||||||
# Format: "1) 5 x 4 = ____"
|
|
||||||
line = f"{i}) {num1} * {num2} = ____\n"
|
|
||||||
p.text(line)
|
|
||||||
p.text("\n") # Extra spacing for writing
|
|
||||||
|
|
||||||
p.text("Hodne stesti!\n")
|
|
||||||
|
|
||||||
# 4. Cut Paper
|
|
||||||
p.cut()
|
|
||||||
|
|
||||||
# If using Dummy, print the output to console to verify
|
|
||||||
if isinstance(p, Dummy):
|
|
||||||
print(p.output.decode('utf-8', errors='ignore'))
|
|
||||||
|
|
||||||
def run_tui():
|
def run_tui():
|
||||||
print("==========================================")
|
print("==========================================")
|
||||||
@ -78,28 +48,44 @@ def run_tui():
|
|||||||
|
|
||||||
while True:
|
while True:
|
||||||
print("\nAvailable Jobs:")
|
print("\nAvailable Jobs:")
|
||||||
print(" [1] Print Math Homework (Multiplication)")
|
for i, job in enumerate(JOBS):
|
||||||
|
print(f" [{i + 1}] {job.get_name()}")
|
||||||
print(" [q] Quit")
|
print(" [q] Quit")
|
||||||
|
|
||||||
choice = input("\nSelect an option: ").strip().lower()
|
choice = input("\nSelect an option: ").strip().lower()
|
||||||
|
|
||||||
if choice == '1':
|
if choice == 'q':
|
||||||
|
print("Exiting...")
|
||||||
|
break
|
||||||
|
|
||||||
|
try:
|
||||||
|
idx = int(choice) - 1
|
||||||
|
if 0 <= idx < len(JOBS):
|
||||||
|
job = JOBS[idx]
|
||||||
|
else:
|
||||||
|
print("Invalid selection.")
|
||||||
|
continue
|
||||||
|
except ValueError:
|
||||||
|
print("Invalid selection.")
|
||||||
|
continue
|
||||||
|
|
||||||
|
if job:
|
||||||
p = get_printer()
|
p = get_printer()
|
||||||
if p:
|
if p:
|
||||||
print("Printing...")
|
print(f"Printing {job.get_name()}...")
|
||||||
try:
|
try:
|
||||||
generate_homework_job(p)
|
job.run(p)
|
||||||
|
|
||||||
|
# If using Dummy, print the output to console to verify
|
||||||
|
if isinstance(p, Dummy):
|
||||||
|
print(p.output.decode('utf-8', errors='ignore'))
|
||||||
|
|
||||||
print("Success! Job sent to printer.")
|
print("Success! Job sent to printer.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Print Error: {e}")
|
print(f"Print Error: {e}")
|
||||||
finally:
|
finally:
|
||||||
if not isinstance(p, Dummy):
|
if not isinstance(p, Dummy):
|
||||||
p.close()
|
p.close()
|
||||||
elif choice == 'q':
|
|
||||||
print("Exiting...")
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
print("Invalid selection. Please try again.")
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run_tui()
|
run_tui()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user