From be2cce3c92da93fd0adde3ac13258940526f22e8 Mon Sep 17 00:00:00 2001 From: Anders Nord Date: Sat, 2 Nov 2024 09:15:39 +0100 Subject: [PATCH] Use dataclass decorator to tidy up code --- notebooks/passenger_vehicle_co2_lca.py | 138 ++++++++---------- pyproject.toml | 3 +- .../objects/personal_vehicle_lca.py | 23 +-- 3 files changed, 78 insertions(+), 86 deletions(-) diff --git a/notebooks/passenger_vehicle_co2_lca.py b/notebooks/passenger_vehicle_co2_lca.py index 8a8c8a1..0939c52 100644 --- a/notebooks/passenger_vehicle_co2_lca.py +++ b/notebooks/passenger_vehicle_co2_lca.py @@ -66,64 +66,54 @@ vehicles = { pvl.Vehicle( - [ - "Liten bensinbil", - 1500, - 0, - 0.05, - vehicle_manufacturing_cost, - battery_manufacturing_cost, - kgCO2_per_litre, - vehicle_life, - ] + name="Liten bensinbil", + weight=1500, + battery_capacity=0, + consumption_per_km=0.05, + co2_build_cost_per_kg=vehicle_manufacturing_cost, + co2_battery_build_cost_per_kWh=battery_manufacturing_cost, + co2_cost_per_consumption=kgCO2_per_litre, + vehicle_life_km=vehicle_life, ), pvl.Vehicle( - [ - "Stor elbil", - 2200, - 125, - 0.25, - vehicle_manufacturing_cost, - battery_manufacturing_cost, - kgCO2_per_kWh, - vehicle_life, - ] + name="Stor elbil", + weight=2200, + battery_capacity=125, + consumption_per_km=0.25, + co2_build_cost_per_kg=vehicle_manufacturing_cost, + co2_battery_build_cost_per_kWh=battery_manufacturing_cost, + co2_cost_per_consumption=kgCO2_per_kWh, + vehicle_life_km=vehicle_life, ), pvl.Vehicle( - [ - "Liten elbil", - 1500, - 50, - 0.15, - vehicle_manufacturing_cost, - battery_manufacturing_cost, - kgCO2_per_kWh, - vehicle_life, - ] + name="Liten elbil", + weight=1500, + battery_capacity=50, + consumption_per_km=0.15, + co2_build_cost_per_kg=vehicle_manufacturing_cost, + co2_battery_build_cost_per_kWh=battery_manufacturing_cost, + co2_cost_per_consumption=kgCO2_per_kWh, + vehicle_life_km=vehicle_life, ), pvl.Vehicle( - [ - "Cykel", - 10, - 0, - 0, - vehicle_manufacturing_cost, - battery_manufacturing_cost, - kgCO2_per_kWh, - vehicle_life, - ] + name="Cykel", + weight=10, + battery_capacity=0, + consumption_per_km=0, + co2_build_cost_per_kg=vehicle_manufacturing_cost, + co2_battery_build_cost_per_kWh=battery_manufacturing_cost, + co2_cost_per_consumption=kgCO2_per_kWh, + vehicle_life_km=vehicle_life, ), pvl.Vehicle( - [ - "Elcykel", - 15, - 0.5, - 0.005, - vehicle_manufacturing_cost, - battery_manufacturing_cost, - kgCO2_per_kWh, - vehicle_life, - ] + name="Elcykel", + weight=15, + battery_capacity=0.5, + consumption_per_km=0.005, + co2_build_cost_per_kg=vehicle_manufacturing_cost, + co2_battery_build_cost_per_kWh=battery_manufacturing_cost, + co2_cost_per_consumption=kgCO2_per_kWh, + vehicle_life_km=vehicle_life, ), } @@ -139,8 +129,12 @@ plt.show() # ## Fordonspark -# Många av oss äger ju flera fordon. Hur mycket förbättrar vi situationen om vi cyklar 10km för varje 100km vi kör i vår stora bil? Här jämför vi -# alternativ för omläggning av persontransporterna för någon som tidigare kört en liten bensinbil men som nu vill lägga om sitt liv för att minska klimatpåverkan. Efter ett antal mil med bilpendling bestämmer sig vår protagonist för att lägga om sitt liv för att minska sitt klimatavtryck, genom att: +# Många av oss äger ju flera fordon. Hur mycket förbättrar vi situationen om +# vi cyklar 10km för varje 100km vi kör i vår stora bil? Här jämför vi +# alternativ för omläggning av persontransporterna för någon som tidigare kört +# en liten bensinbil men som nu vill lägga om sitt liv för att minska klimatpåverkan. +# Efter ett antal mil med bilpendling bestämmer sig vår protagonist för att lägga om +# sitt liv för att minska sitt klimatavtryck, genom att: # # 1. Fortsätta köra samma bensinbil # 2. Köpa en stor elbil @@ -176,30 +170,26 @@ # And define additional vehicle combinations large_electric_and_bike = pvl.Vehicle( - [ - "Stor elbil och cykel", - large_electric_car.weight + bike.weight, - large_electric_car.battery_capacity + bike.battery_capacity, - large_electric_car.consumption_per_km * (1 - fraction_bike_rides) - + bike.consumption_per_km * fraction_bike_rides, - vehicle_manufacturing_cost, - battery_manufacturing_cost, - kgCO2_per_kWh, - vehicle_life, - ] + name="Stor elbil och cykel", + weight=large_electric_car.weight + bike.weight, + battery_capacity=large_electric_car.battery_capacity + bike.battery_capacity, + consumption_per_km=large_electric_car.consumption_per_km * (1 - fraction_bike_rides) + + bike.consumption_per_km * fraction_bike_rides, + co2_build_cost_per_kg=vehicle_manufacturing_cost, + co2_battery_build_cost_per_kWh=battery_manufacturing_cost, + co2_cost_per_consumption=kgCO2_per_kWh, + vehicle_life_km=vehicle_life, ) small_electric_and_bike = pvl.Vehicle( - [ - "Liten elbil och cykel", - small_electric_car.weight + bike.weight, - small_electric_car.battery_capacity + bike.battery_capacity, - small_electric_car.consumption_per_km * (1 - fraction_bike_rides) - + bike.consumption_per_km * fraction_bike_rides, - vehicle_manufacturing_cost, - battery_manufacturing_cost, - kgCO2_per_kWh, - vehicle_life, - ] + name="Liten elbil och cykel", + weight=small_electric_car.weight + bike.weight, + battery_capacity=small_electric_car.battery_capacity + bike.battery_capacity, + consumption_per_km=small_electric_car.consumption_per_km * (1 - fraction_bike_rides) + + bike.consumption_per_km * fraction_bike_rides, + co2_build_cost_per_kg=vehicle_manufacturing_cost, + co2_battery_build_cost_per_kWh=battery_manufacturing_cost, + co2_cost_per_consumption=kgCO2_per_kWh, + vehicle_life_km=vehicle_life, ) # Define the scenarios as described above diff --git a/pyproject.toml b/pyproject.toml index 511cbae..923bb10 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,7 @@ authors = [ { name = "IFK", email = "info@ingenjorsarbeteforklimatet.se" }, ] requires-python = ">=3.9" -dependencies = ["pyscbwrapper", "requests", "numpy", "pandas ~= 2.2"] +dependencies = ["pyscbwrapper", "requests", "numpy", "pandas ~= 2.2", "dataclasses ~= 0.6"] [project.optional-dependencies] lint = [ @@ -33,6 +33,7 @@ dev = [ "pre-commit ~= 2.20", "ipykernel ~= 6.26", "matplotlib ~= 3.8", + "dataclasses ~= 0.6", ] [tool.setuptools.packages.find] diff --git a/src/ifk_analyses/objects/personal_vehicle_lca.py b/src/ifk_analyses/objects/personal_vehicle_lca.py index ccd0087..568c7c9 100644 --- a/src/ifk_analyses/objects/personal_vehicle_lca.py +++ b/src/ifk_analyses/objects/personal_vehicle_lca.py @@ -1,19 +1,20 @@ -"""Backend data class for vehicle.""" +"""Backend data class and some methods for vehicle.""" +from dataclasses import dataclass + +@dataclass(frozen=True) class Vehicle: """Class for vehicles.""" - def __init__(self, data) -> None: - """Initialization.""" - self.name = data[0] - self.weight = data[1] - self.battery_capacity = data[2] - self.consumption_per_km = data[3] - self.co2_build_cost_per_kg = data[4] - self.co2_battery_build_cost_per_kWh = data[5] - self.co2_cost_per_consumption = data[6] - self.vehicle_life_km = data[7] + name: str + weight: int + battery_capacity: int + consumption_per_km: int + co2_build_cost_per_kg: int + co2_battery_build_cost_per_kWh: int + co2_cost_per_consumption: int + vehicle_life_km: int def co2analysis(car: Vehicle, driven_distance, co2debt):