Skip to content

Commit

Permalink
Refactored for CRUD operations on data
Browse files Browse the repository at this point in the history
  • Loading branch information
ajkolenc committed Dec 17, 2024
1 parent 164cd67 commit 2bbc3ed
Show file tree
Hide file tree
Showing 29 changed files with 1,723 additions and 517 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ profile = "black"

[tool.pylint]
max-line-length=240
disable = "C0103, C0114, C0115, C0116, C0302, E0401, W0212, W0511, R0801, R0903, R0917"
disable = "C0103, C0114, C0115, C0116, C0302, E0401, W0212, W0511, R0801, R0902, R0903, R0913, R0914, R0904, R0917"
extension-pkg-allow-list = [
"matplotlib",
"PySide6",
Expand Down
2 changes: 1 addition & 1 deletion source/package/adaptation_pathways/alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
Actions = list[Action | ActionCombination]
Sequence = tuple[Action, Action]
Sequences = list[Sequence]
TippingPoint = int
TippingPoint = float
TippingPointByAction = dict[Action, TippingPoint]
13 changes: 7 additions & 6 deletions source/package/adaptation_pathways/app/model/action.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dataclasses

from .comparisons import SequenceComparison
from .metric import Metric, MetricValue
from .metric import MetricEffect


@dataclasses.dataclass
Expand All @@ -10,13 +10,14 @@ class Action:
name: str
color: str
icon: str
metric_data: dict[Metric, MetricValue | None]
metric_data: dict[str, MetricEffect]

def get_data(self, metric) -> MetricValue | None:
if metric not in self.metric_data:
return None
def apply_effect(self, metric_id: str, value: float) -> float:
if metric_id not in self.metric_data:
return value

return self.metric_data[metric]
effect = self.metric_data[metric_id]
return effect.apply_to(value)


@dataclasses.dataclass
Expand Down
81 changes: 46 additions & 35 deletions source/package/adaptation_pathways/app/model/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,6 @@
from enum import Enum


class MetricEstimate(Enum):
MANUAL = 1
SUM = 2
AVERAGE = 3
MINIMUM = 4
MAXIMUM = 5
LAST = 6

def __repr__(self) -> str:
match self:
case MetricEstimate.MANUAL:
return "Manual"
case MetricEstimate.SUM:
return "Sum"
case MetricEstimate.AVERAGE:
return "Average"
case MetricEstimate.MINIMUM:
return "Minimum"
case MetricEstimate.MAXIMUM:
return "Max"
case MetricEstimate.LAST:
return "Last"
case _:
return "Unknown"


@dataclasses.dataclass
class MetricUnit:
name: str
Expand All @@ -43,7 +17,7 @@ def display_name(self):
if self.short_name is not None:
return self.short_name

return self.symbol
return self.name

def get_symbol(self, value: float):
return (
Expand All @@ -66,7 +40,6 @@ class Metric:
id: str
name: str
current_value: float
estimate: MetricEstimate
unit_or_default: MetricUnit | str

@property
Expand All @@ -90,6 +63,44 @@ class MetricValue:
is_estimate: bool = False


class MetricOperation(Enum):
NONE = "None"
ADD = "Add"
MULTIPLY = "Multiply"
MINIMUM = "Min"
MAXIMUM = "Max"
REPLACE = "Replace"


class MetricEffect:
value: float
operation: MetricOperation

def __init__(self, value: float, operation: MetricOperation = MetricOperation.ADD):
self.value = value
self.operation = operation

def __repr__(self):
return f"MetricEffect({self.operation} {self.value})"

def apply_to(self, value: float) -> float:
match self.operation:
case MetricOperation.NONE:
return value
case MetricOperation.ADD:
return value + self.value
case MetricOperation.MULTIPLY:
return value * self.value
case MetricOperation.MINIMUM:
return min(value, self.value)
case MetricOperation.MAXIMUM:
return max(value, self.value)
case MetricOperation.REPLACE:
return self.value
case _:
return value


class DefaultUnits:
FORMAT_SLIDER = "n"

Expand All @@ -111,12 +122,12 @@ class Length:

class Area:
si = [
MetricUnit(name="Square Meter", symbol="m^2"),
MetricUnit(name="Square Kilometer", symbol="km^2"),
MetricUnit(name="Square Meter", symbol="m²"),
MetricUnit(name="Square Kilometer", symbol="km²"),
MetricUnit(name="Hectare", symbol="ha"),
]
imperial = [
MetricUnit(name="Square Feet", symbol="ft^2"),
MetricUnit(name="Square Feet", symbol="ft²"),
MetricUnit(name="Acre", symbol="acre", symbol_plural="acres"),
MetricUnit(name="Square Mile", symbol="sq mi"),
]
Expand Down Expand Up @@ -154,21 +165,21 @@ class Velocity:
si = [
MetricUnit(name="Meters/Second", symbol="m/s"),
MetricUnit(name="Kilometers/Hour", symbol="km/h"),
MetricUnit(name="Meters/Second^2", symbol="m/s^2"),
MetricUnit(name="Meters/Second²", symbol="m/s²"),
]
imperial = [
MetricUnit(name="Miles/Hour", symbol="mph"),
MetricUnit(name="Feet/Second^2", symbol="ft/s^2"),
MetricUnit(name="Feet/Second²", symbol="ft/s²"),
]

velocity = Velocity()

class Acceleration:
si = [
MetricUnit(name="Meters/Second^2", symbol="m/s^2"),
MetricUnit(name="Meters/Second²", symbol="m/s²"),
]
imperial = [
MetricUnit(name="Feet/Second^2", symbol="ft/s^2"),
MetricUnit(name="Feet/Second²", symbol="ft/s²"),
]

acceleration = Acceleration()
Expand Down
71 changes: 10 additions & 61 deletions source/package/adaptation_pathways/app/model/pathway.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,23 @@
import dataclasses
from typing import Iterator

from .action import Action
from .metric import Metric, MetricValue


@dataclasses.dataclass
class Pathway:
id: str
parent_id: str | None = None
last_action: Action
metric_data: dict[Metric, MetricValue | None] | None = None
children: list["Pathway"] | None = None

@classmethod
def get_id(cls, ancestors: list["Pathway"], new_action: Action):
ids = [pathway.last_action.id for pathway in ancestors]
ids.append(new_action.id)
return "->".join(ids)

def __hash__(self):
return self.id.__hash__()

def all_children(self):
if self.children is None:
return

for child in self.children:
yield child
yield from child.all_children()

def self_and_all_children(self):
yield self
yield from self.all_children()

def all_paths(
self, base_path: list["Pathway"] | None = None
) -> Iterator[tuple["Pathway", list["Pathway"]]]:

path = [self] if base_path is None else base_path + [self]

yield (self, path)
metric_data: dict[str, MetricValue]

if self.children is None:
return

for child in self.children:
yield from child.all_paths(path)

def all_child_paths(
self, base_path: list["Pathway"] | None = None
) -> Iterator[tuple["Pathway", list["Pathway"]]]:
path = [self] if base_path is None else base_path + [self]

if self.children is None:
return

for child in self.children:
yield from child.all_paths(path)

def get_value(self, metric: Metric) -> MetricValue | None:
if self.metric_data is None:
return None

return self.metric_data.get(metric)
def __init__(
self, pathway_id: str, last_action: Action, parent_id: str | None = None
):
self.id = pathway_id
self.parent_id = parent_id
self.last_action = last_action
self.metric_data = {}

def get_formatted_value(self, metric: Metric) -> str:
if self.metric_data is None:
return ""

data = self.metric_data.get(metric)
data = self.metric_data.get(metric.id, None)
if data is None:
return ""

Expand Down
Loading

0 comments on commit 2bbc3ed

Please sign in to comment.