From 156eddc6ef878faae84ceba7b0997a269e64d197 Mon Sep 17 00:00:00 2001 From: Michael Osthege Date: Thu, 3 Oct 2024 11:05:44 +0200 Subject: [PATCH] Manual edits to remove remaining code style errors --- Dockerfile | 2 +- README-DE.md | 5 +++-- README.md | 6 ++++-- config.example.py => config.py | 2 +- flask_server.py | 32 +++++++++++++++----------------- modules/class_load_corrector.py | 9 --------- modules/class_optimize.py | 27 +++++++++++---------------- modules/class_pv_forecast.py | 5 ++++- modules/class_soc_calc.py | 1 + modules/visualize.py | 2 +- pyproject.toml | 5 +++++ test.py | 1 - 12 files changed, 46 insertions(+), 51 deletions(-) rename config.example.py => config.py (97%) diff --git a/Dockerfile b/Dockerfile index d66fe0c..c37b83e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,7 +19,7 @@ COPY requirements.txt requirements.txt RUN pip install --no-cache-dir -r requirements.txt COPY . . -COPY config.example.py config.py +COPY config.py config.py ENTRYPOINT [] diff --git a/README-DE.md b/README-DE.md index 482c7fb..670a8d8 100644 --- a/README-DE.md +++ b/README-DE.md @@ -27,7 +27,8 @@ Unter Macos (benötigt [Homebrew](https://brew.sh)): brew install make ``` -Nun `config.example.py` anpassen und dann in `config.py` umbennenen. Anschließend kann der Server über `make run` gestartet werden. +Nun `config.py` anpassen. +Anschließend kann der Server über `make run` gestartet werden. Eine vollständige Übersicht über die wichtigsten Kurzbefehle gibt `make help`. ### Ausführliche Anleitung @@ -76,7 +77,7 @@ gefolgt von einem erneuten `pip install -r requirements.txt`. ## Nutzung -`config.example.py` anpassen und dann in config.py umbennenen +Einstellungen in `config.py` anpassen. Um das System zu nutzen, führen Sie `flask_server.py` aus, damit wird der Server gestartet diff --git a/README.md b/README.md index 76cbd53..8d86e25 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,8 @@ On MacOS (requires [Homebrew](https://brew.sh)): brew install make ``` -Next, adjust `config.example.py` and rename it to `config.py`. The server can then be started with `make run`. A full overview of the main shortcuts is given by `make help`. +Next, adjust `config.py`. +The server can then be started with `make run`. A full overview of the main shortcuts is given by `make help`. ### Detailed Instructions @@ -71,7 +72,8 @@ Followed by a renewed `pip install -r requirements.txt`. ## Usage -Adjust `config.example.py` and rename it to `config.py`. To use the system, run `flask_server.py`, which starts the server: +Adjust `config.py`. +To use the system, run `flask_server.py`, which starts the server: ```bash ./flask_server.py diff --git a/config.example.py b/config.py similarity index 97% rename from config.example.py rename to config.py index c102016..b59ca63 100644 --- a/config.example.py +++ b/config.py @@ -27,7 +27,7 @@ def get_start_enddate(prediction_hours=48, startdate=None): ############ # Parameter ############ - if startdate == None: + if startdate is None: date = (datetime.now().date() + timedelta(hours=prediction_hours)).strftime( "%Y-%m-%d" ) diff --git a/flask_server.py b/flask_server.py index 69f23e3..edba766 100755 --- a/flask_server.py +++ b/flask_server.py @@ -1,30 +1,28 @@ #!/usr/bin/env python3 import os +import sys +from datetime import datetime import matplotlib -matplotlib.use( - "Agg" -) # Sets the Matplotlib backend to 'Agg' for rendering plots in environments without a display -from datetime import datetime, timedelta +# Sets the Matplotlib backend to 'Agg' for rendering plots in environments without a display +matplotlib.use("Agg") +from datetime import timedelta import pandas as pd -from config import * from flask import Flask, jsonify, redirect, request, send_from_directory, url_for -from modules.class_akku import * -from modules.class_ems import * -from modules.class_heatpump import * -from modules.class_load import * -from modules.class_load_container import * -from modules.class_load_corrector import * -from modules.class_optimize import * -from modules.class_pv_forecast import * -from modules.class_soc_calc import * -from modules.class_sommerzeit import * -from modules.class_strompreis import * -from modules.visualize import * +from modules.class_load import LoadForecast +from modules.class_load_container import Gesamtlast +from modules.class_load_corrector import LoadPredictionAdjuster +from modules.class_optimize import isfloat, optimization_problem +from modules.class_pv_forecast import PVForecast +from modules.class_soc_calc import BatteryDataProcessor +from modules.class_strompreis import HourlyElectricityPriceForecast + +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from config import db_config, get_start_enddate, optimization_hours, prediction_hours app = Flask(__name__) diff --git a/modules/class_load_corrector.py b/modules/class_load_corrector.py index b599140..5ea1f24 100644 --- a/modules/class_load_corrector.py +++ b/modules/class_load_corrector.py @@ -1,6 +1,3 @@ -import os -import sys - import matplotlib.pyplot as plt import numpy as np import pandas as pd @@ -16,12 +13,6 @@ # from sklearn.preprocessing import MinMaxScaler # from sqlalchemy import create_engine -# Add the parent directory to sys.path -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from config import * - -from modules.class_load import * - class LoadPredictionAdjuster: def __init__(self, measured_data, predicted_data, load_forecast): diff --git a/modules/class_optimize.py b/modules/class_optimize.py index 9e433fa..975cc28 100644 --- a/modules/class_optimize.py +++ b/modules/class_optimize.py @@ -1,34 +1,29 @@ import os +import sys import matplotlib import numpy as np -from modules.class_akku import * -from modules.class_ems import * -from modules.class_haushaltsgeraet import * -from modules.class_heatpump import * -from modules.class_inverter import * -from modules.class_load import * -from modules.class_load_container import * -from modules.class_pv_forecast import * -from modules.class_sommerzeit import * -from modules.visualize import * +from modules.class_akku import PVAkku +from modules.class_ems import EnergieManagementSystem, Wechselrichter +from modules.class_haushaltsgeraet import Haushaltsgeraet +from modules.visualize import visualisiere_ergebnisse matplotlib.use("Agg") # Setzt das Backend auf Agg import random -from datetime import datetime +from datetime import datetime, timedelta from deap import algorithms, base, creator, tools sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from config import * +from config import moegliche_ladestroeme_in_prozent def isfloat(num): try: float(num) return True - except: + except ValueError: return False @@ -213,7 +208,7 @@ def evaluate_inner(self, individual, ems, start_hour): def evaluate(self, individual, ems, parameter, start_hour, worst_case): try: o = self.evaluate_inner(individual, ems, start_hour) - except: + except Exception: return (100000.0,) gesamtbilanz = o["Gesamtbilanz_Euro"] @@ -339,7 +334,7 @@ def optimierung_ems( ############ # Parameter ############ - if startdate == None: + if startdate is None: date = ( datetime.now().date() + timedelta(hours=self.prediction_hours) ).strftime("%Y-%m-%d") @@ -437,7 +432,7 @@ def optimierung_ems( ############## opti_param = {} opti_param["haushaltsgeraete"] = 0 - if spuelmaschine != None: + if spuelmaschine is not None: opti_param["haushaltsgeraete"] = 1 self.setup_deap_environment(opti_param, start_hour) diff --git a/modules/class_pv_forecast.py b/modules/class_pv_forecast.py index ca9b86e..5caae99 100644 --- a/modules/class_pv_forecast.py +++ b/modules/class_pv_forecast.py @@ -69,7 +69,9 @@ def __init__(self, filepath=None, url=None, cache_dir="cache", prediction_hours= f"Die Vorhersage muss mindestens {self.prediction_hours} Stunden umfassen, aber es wurden nur {len(self.forecast_data)} Stunden vorhergesagt." ) - def update_ac_power_measurement(self, date_time=None, ac_power_measurement=None): + def update_ac_power_measurement( + self, date_time=None, ac_power_measurement=None + ) -> bool: found = False input_date_hour = date_time.replace(minute=0, second=0, microsecond=0) @@ -81,6 +83,7 @@ def update_ac_power_measurement(self, date_time=None, ac_power_measurement=None) forecast.ac_power_measurement = ac_power_measurement found = True break + return found def process_data(self, data): self.meta = data.get("meta", {}) diff --git a/modules/class_soc_calc.py b/modules/class_soc_calc.py index f88b7eb..eda4890 100644 --- a/modules/class_soc_calc.py +++ b/modules/class_soc_calc.py @@ -295,6 +295,7 @@ def plot_data(self, last_points_100_df, last_points_0_df, soc_df): if __name__ == "__main__": # MariaDB Verbindungsdetails + config = {} # Parameter festlegen voltage_high_threshold = 55.4 # 100% SoC diff --git a/modules/visualize.py b/modules/visualize.py index 6e19614..b752432 100644 --- a/modules/visualize.py +++ b/modules/visualize.py @@ -6,7 +6,7 @@ import numpy as np from matplotlib.backends.backend_pdf import PdfPages -from modules.class_sommerzeit import * # Ensure this matches the actual import path +from modules.class_sommerzeit import ist_dst_wechsel matplotlib.use("Agg") diff --git a/pyproject.toml b/pyproject.toml index 5d7bf33..5432643 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,2 +1,7 @@ [tool.isort] profile = "black" + +[tool.ruff.lint] +ignore = [ + "F841", # don't complain about unused variables +] diff --git a/test.py b/test.py index f62d901..eda46fc 100644 --- a/test.py +++ b/test.py @@ -4,7 +4,6 @@ # Import necessary modules from the project from modules.class_optimize import optimization_problem -from modules.visualize import * start_hour = 10