Skip to content

Commit

Permalink
Use dataclass decorator to tidy up code
Browse files Browse the repository at this point in the history
  • Loading branch information
docNord committed Nov 2, 2024
1 parent d38e534 commit be2cce3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 86 deletions.
138 changes: 64 additions & 74 deletions notebooks/passenger_vehicle_co2_lca.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ authors = [
{ name = "IFK", email = "[email protected]" },
]
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 = [
Expand All @@ -33,6 +33,7 @@ dev = [
"pre-commit ~= 2.20",
"ipykernel ~= 6.26",
"matplotlib ~= 3.8",
"dataclasses ~= 0.6",
]

[tool.setuptools.packages.find]
Expand Down
23 changes: 12 additions & 11 deletions src/ifk_analyses/objects/personal_vehicle_lca.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit be2cce3

Please sign in to comment.