-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the ruff linter to the pre-commit check #214
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ docs/api/ | |
*.log | ||
/profile.* | ||
xl2times/.cache/ | ||
*.log.zip |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,16 @@ | |
|
||
import pandas as pd | ||
|
||
from xl2times import transforms, utils, datatypes | ||
from xl2times import datatypes, transforms, utils | ||
from xl2times.transforms import ( | ||
_process_comm_groups_vectorised, | ||
_count_comm_group_vectorised, | ||
_match_wildcards, | ||
_process_comm_groups_vectorised, | ||
commodity_map, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ^ This was the original motivation - standard import sorting/formatting |
||
expand_rows, | ||
get_matching_commodities, | ||
get_matching_processes, | ||
_match_wildcards, | ||
process_map, | ||
commodity_map, | ||
) | ||
|
||
logger = utils.get_logger() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
import argparse | ||
import sys | ||
from collections import defaultdict | ||
import json | ||
import os | ||
import sys | ||
from collections import defaultdict | ||
from pathlib import Path | ||
from typing import Dict, List, Tuple, Union | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
@@ -13,7 +12,7 @@ | |
|
||
def parse_parameter_values_from_file( | ||
path: Path, | ||
) -> Tuple[Dict[str, List], Dict[str, set]]: | ||
) -> tuple[dict[str, list], dict[str, set]]: | ||
SamRWest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Parse *.dd to turn it into CSV format | ||
There are parameters and sets, and each has a slightly different format | ||
|
@@ -35,11 +34,11 @@ def parse_parameter_values_from_file( | |
|
||
""" | ||
|
||
data = list(open(path, "r")) | ||
data = list(open(path)) | ||
data = [line.rstrip() for line in data] | ||
|
||
param_value_dict: Dict[str, List] = dict() | ||
set_data_dict: Dict[str, set] = dict() | ||
param_value_dict: dict[str, list] = dict() | ||
set_data_dict: dict[str, set] = dict() | ||
index = 0 | ||
while index < len(data): | ||
if data[index].startswith("PARAMETER"): | ||
|
@@ -124,8 +123,8 @@ def parse_parameter_values_from_file( | |
|
||
|
||
def save_data_with_headers( | ||
param_data_dict: Dict[str, Union[pd.DataFrame, List[str]]], | ||
headers_data: Dict[str, List[str]], | ||
param_data_dict: dict[str, pd.DataFrame | list[str]], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What python versions are you planning to support? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We haven't had that discussion actually... How about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Works for me, type hints are a lot cleaner in 3.12 for instance. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we use the match-case statement somewhere in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd say it is easy! :-) But I am also fine with sticking to 3.11. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I use pyenv on windows also, works very well. |
||
headers_data: dict[str, list[str]], | ||
save_dir: str, | ||
) -> None: | ||
""" | ||
|
@@ -157,7 +156,7 @@ def save_data_with_headers( | |
return | ||
|
||
|
||
def generate_headers_by_attr() -> Dict[str, List[str]]: | ||
def generate_headers_by_attr() -> dict[str, list[str]]: | ||
with open("xl2times/config/times-info.json") as f: | ||
attributes = json.load(f) | ||
|
||
|
@@ -173,7 +172,7 @@ def generate_headers_by_attr() -> Dict[str, List[str]]: | |
|
||
|
||
def convert_dd_to_tabular( | ||
basedir: str, output_dir: str, headers_by_attr: Dict[str, List[str]] | ||
basedir: str, output_dir: str, headers_by_attr: dict[str, list[str]] | ||
) -> None: | ||
dd_files = [p for p in Path(basedir).rglob("*.dd")] | ||
|
||
|
@@ -201,15 +200,15 @@ def convert_dd_to_tabular( | |
os.makedirs(set_path, exist_ok=True) | ||
|
||
# Extract headers with key=param_name and value=List[attributes] | ||
lines = list(open("xl2times/config/times_mapping.txt", "r")) | ||
lines = list(open("xl2times/config/times_mapping.txt")) | ||
headers_data = headers_by_attr | ||
# The following will overwrite data obtained from headers_by_attr | ||
# TODO: Remove once migration is done? | ||
for line in lines: | ||
line = line.strip() | ||
if line != "": | ||
param_name = line.split("[")[0] | ||
attributes = line.split("[")[1].split("]")[0].split(",") | ||
ln = line.strip() | ||
if ln != "": | ||
param_name = ln.split("[")[0] | ||
attributes = ln.split("[")[1].split("]")[0].split(",") | ||
headers_data[param_name] = [*attributes] | ||
|
||
save_data_with_headers(all_parameters, headers_data, param_path) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,22 +9,22 @@ | |
from concurrent.futures import ProcessPoolExecutor | ||
from functools import partial | ||
from os import path, symlink | ||
from typing import Any, Tuple | ||
from typing import Any | ||
|
||
import git | ||
import pandas as pd | ||
import yaml | ||
from dd_to_csv import main | ||
from tabulate import tabulate | ||
|
||
from dd_to_csv import main | ||
from xl2times import utils | ||
from xl2times.__main__ import parse_args, run | ||
from xl2times.utils import max_workers | ||
|
||
logger = utils.get_logger() | ||
|
||
|
||
def parse_result(output: str) -> Tuple[float, int, int]: | ||
def parse_result(output: str) -> tuple[float, int, int]: | ||
# find pattern in multiline string | ||
m = re.findall( | ||
r"(\d+\.\d)% of ground truth rows present in output \((\d+)/(\d+)\), (\d+) additional rows", | ||
|
@@ -65,6 +65,7 @@ def run_gams_gdxdiff( | |
stdout=subprocess.PIPE, | ||
stderr=subprocess.STDOUT, | ||
text=True, | ||
check=False, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
) | ||
if res.returncode != 0: | ||
logger.info(res.stdout) | ||
|
@@ -96,6 +97,7 @@ def run_gams_gdxdiff( | |
stdout=subprocess.PIPE, | ||
stderr=subprocess.STDOUT, | ||
text=True, | ||
check=False, | ||
) | ||
if res.returncode != 0: | ||
logger.info(res.stdout) | ||
|
@@ -119,6 +121,7 @@ def run_gams_gdxdiff( | |
stdout=subprocess.PIPE, | ||
stderr=subprocess.STDOUT, | ||
text=True, | ||
check=False, | ||
) | ||
if verbose: | ||
logger.info(res.stdout) | ||
|
@@ -138,7 +141,7 @@ def run_benchmark( | |
out_folder: str = "out", | ||
verbose: bool = False, | ||
debug: bool = False, | ||
) -> Tuple[str, float, str, float, int, int]: | ||
) -> tuple[str, float, str, float, int, int]: | ||
xl_folder = path.join(benchmarks_folder, "xlsx", benchmark["input_folder"]) | ||
dd_folder = path.join(benchmarks_folder, "dd", benchmark["dd_folder"]) | ||
csv_folder = path.join(benchmarks_folder, "csv", benchmark["name"]) | ||
|
@@ -160,6 +163,7 @@ def run_benchmark( | |
stderr=subprocess.STDOUT, | ||
text=True, | ||
shell=True if os.name == "nt" else False, | ||
check=False, | ||
) | ||
if res.returncode != 0: | ||
# Remove partial outputs | ||
|
@@ -191,7 +195,7 @@ def run_benchmark( | |
if "regions" in benchmark: | ||
args.extend(["--regions", benchmark["regions"]]) | ||
if "inputs" in benchmark: | ||
args.extend((path.join(xl_folder, b) for b in benchmark["inputs"])) | ||
args.extend(path.join(xl_folder, b) for b in benchmark["inputs"]) | ||
else: | ||
args.append(xl_folder) | ||
start = time.time() | ||
|
@@ -203,6 +207,7 @@ def run_benchmark( | |
stdout=subprocess.PIPE, | ||
stderr=subprocess.STDOUT, | ||
text=True, | ||
check=False, | ||
) | ||
else: | ||
# If debug option is set, run as a function call to allow stepping with a debugger. | ||
|
@@ -295,7 +300,6 @@ def run_all_benchmarks( | |
for benchmark in benchmarks: | ||
with open( | ||
path.join(benchmarks_folder, "out-main", benchmark["name"], "stdout"), | ||
"r", | ||
) as f: | ||
result = parse_result(f.readlines()[-1]) | ||
# Use a fake runtime and GAMS result | ||
|
@@ -330,7 +334,8 @@ def run_all_benchmarks( | |
results_main = list(executor.map(run_a_benchmark, benchmarks)) | ||
|
||
# Print table with combined results to make comparison easier | ||
trunc = lambda s: s[:10] + "\u2026" if len(s) > 10 else s | ||
trunc = lambda s: s[:10] + "\u2026" if len(s) > 10 else s # noqa | ||
|
||
combined_results = [ | ||
( | ||
f"{b:<20}", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these last two will require a reasonable number of (simple) changes, so I've left them off for now so this diff isn't too polluted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice idea, thanks. Looking forward to the next linting PR. :)