~sporiff/soap-calculator

fb6a387e0f27d73fb98dc12f91137b1612be7a90 — CiarĂ¡n Ainsworth 4 months ago 764a231
Split calculation out of main function for neatness
2 files changed, 30 insertions(+), 32 deletions(-)

M src/__main__.py
A src/calc.py
M src/__main__.py => src/__main__.py +2 -32
@@ 1,15 1,9 @@
import gi
from dict import saponification_values

from calc import calculation

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

def get_saponification(val):
    for key, value in saponification_values.items():
        if val == key:
            return float(value)

class Handler:

    # Allow the x button to destroy the window


@@ 31,31 25,7 @@ class Handler:
            builder.get_object("reductionAmount").set_value(0)

    def calculate(self, *args):
        calculation()

# Define the calculation to be performed

def calculation():
    # Define Variables

    oil1Lye = get_saponification(builder.get_object("oil1Picker").get_active_text())
    oil2Lye = get_saponification(builder.get_object("oil2Picker").get_active_text())

    oil1Amount = float(builder.get_object("oil1").get_value())
    oil2Amount = float(builder.get_object("oil2").get_value())
    reductionAmount = float(builder.get_object("reductionAmount").get_value())

    use_reduction = builder.get_object("reduction").get_active()
    if use_reduction:
        oil1Lye = oil1Lye - ((reductionAmount/100) * oil1Lye)
        oil2Lye = oil2Lye - ((reductionAmount/100) * oil2Lye)

    oil1Lye_needed = oil1Amount * oil1Lye
    oil2Lye_needed = oil2Amount * oil2Lye
    water_needed = round(((oil1Amount + oil2Amount) / 3),3)
    calculatedValue = round(oil1Lye_needed + oil2Lye_needed, 3)
    builder.get_object("waterResults").set_text(f"{water_needed}ml")
    builder.get_object("lyeResults").set_text(f"{calculatedValue}g")
        calculation(builder)

if __name__ == "__main__":


A src/calc.py => src/calc.py +28 -0
@@ 0,0 1,28 @@
from dict import saponification_values

def get_saponification(val):
    for key, value in saponification_values.items():
        if val == key:
            return float(value)

def calculation(builder):
    # Define Variables

    oil1Lye = get_saponification(builder.get_object("oil1Picker").get_active_text())
    oil2Lye = get_saponification(builder.get_object("oil2Picker").get_active_text())

    oil1Amount = float(builder.get_object("oil1").get_value())
    oil2Amount = float(builder.get_object("oil2").get_value())
    reductionAmount = float(builder.get_object("reductionAmount").get_value())

    use_reduction = builder.get_object("reduction").get_active()
    if use_reduction:
        oil1Lye = oil1Lye - ((reductionAmount/100) * oil1Lye)
        oil2Lye = oil2Lye - ((reductionAmount/100) * oil2Lye)

    oil1Lye_needed = oil1Amount * oil1Lye
    oil2Lye_needed = oil2Amount * oil2Lye
    water_needed = round(((oil1Amount + oil2Amount) / 3),3)
    calculatedValue = round(oil1Lye_needed + oil2Lye_needed, 3)
    builder.get_object("waterResults").set_text(f"{water_needed}ml")
    builder.get_object("lyeResults").set_text(f"{calculatedValue}g")
\ No newline at end of file