Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Jun 16, 2024
1 parent 53cb46e commit 9e8247b
Show file tree
Hide file tree
Showing 64 changed files with 584 additions and 459 deletions.
10 changes: 7 additions & 3 deletions bw2io/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ def backup_data_directory(
fp = dir_backup / backup_filename

# Create the backup archive
print("Creating backup archive of data directory - this could take a few minutes...")
print(
"Creating backup archive of data directory - this could take a few minutes..."
)
with tarfile.open(fp, "w:gz") as tar:
data_directory = Path(projects._base_data_dir)
tar.add(data_directory, arcname=data_directory.name)

print(f"Saved to: {fp}")

return fp


Expand Down Expand Up @@ -120,7 +122,9 @@ def backup_project_directory(
if not os.access(dir_backup, os.W_OK):
raise PermissionError(f"The directory {dir_backup} is not writable.")

timestamp_str = datetime.datetime.now().strftime("%d-%B-%Y-%I-%M%p") if timestamp else ""
timestamp_str = (
datetime.datetime.now().strftime("%d-%B-%Y-%I-%M%p") if timestamp else ""
)
backup_filename = f"brightway2-project-{project}-backup{timestamp_str}.tar.gz"
fp = dir_backup / backup_filename

Expand Down
1 change: 1 addition & 0 deletions bw2io/chemidplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Multiple(Exception):
exception : Exception
Exception to raise.
"""

pass


Expand Down
19 changes: 11 additions & 8 deletions bw2io/data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import copy
import gzip
import json
from functools import partial
from numbers import Number
from pathlib import Path
from typing import Union
import copy
import gzip
import json

from bw2data import Database, Method, config, databases, methods, parameters
from bw2data.parameters import Group
Expand Down Expand Up @@ -105,14 +105,15 @@ def get_biosphere_2_3_name_migration_data():
Note that the input data excel sheet is **modified** from the raw data provided by ecoinvent - some biosphere flows which had no equivalent in ecospold2 were mapped using my best judgment. Name changes from 3.1 were also included. Modified cells are marked in **dark orange**.
Note that not all rows have names in ecoinvent 3. There are a few energy resources that we don't update. For water flows, the categories are updated by a different strategy, and the names don't change, so we just ignore them for now."""
Note that not all rows have names in ecoinvent 3. There are a few energy resources that we don't update. For water flows, the categories are updated by a different strategy, and the names don't change, so we just ignore them for now.
"""

ws = get_sheet(
dirpath / "lci" / "ecoinvent elementary flows 2-3.xlsx", "ElementaryExchanges"
)

def to_exchange(obj):
obj[0][3] = u"biosphere"
obj[0][3] = "biosphere"
return obj

def strip_unspecified(one, two):
Expand All @@ -131,7 +132,7 @@ def strip_unspecified(one, two):
ws.cell(row=row + 1, column=11).value,
),
normalize_units(ws.cell(row=row + 1, column=7).value),
u"emission", # Unit
"emission", # Unit
],
{"name": ws.cell(row=row + 1, column=9).value},
)
Expand Down Expand Up @@ -207,7 +208,8 @@ def get_exiobase_biosphere_migration_data():
def convert_simapro_ecoinvent_elementary_flows():
"""Write a correspondence list from SimaPro elementary flow names to ecoinvent 3 flow names to a JSON file.
Uses custom SimaPro specific data. Ecoinvent 2 -> 3 conversion is in a separate JSON file."""
Uses custom SimaPro specific data. Ecoinvent 2 -> 3 conversion is in a separate JSON file.
"""
ws = get_sheet(dirpath / "lci" / "SimaPro - ecoinvent - biosphere.xlsx", "ee")
data = [
[ws.cell(row=row + 1, column=col + 1).value for col in range(3)]
Expand Down Expand Up @@ -255,7 +257,8 @@ def get_simapro_ecoinvent_3_migration_data(version):
Note that even the official matching data from Pré is incorrect, but works if we cast all strings to lower case.
SimaPro type is either ``System terminated`` or ``Unit process``. We always match to unit processes regardless of SimaPro type."""
SimaPro type is either ``System terminated`` or ``Unit process``. We always match to unit processes regardless of SimaPro type.
"""
fp = dirpath / "lci" / ("Simapro - ecoinvent {} mapping.gzip".format(version))
with gzip.GzipFile(fp, "r") as fout:
data = json.loads(fout.read().decode("utf-8"))
Expand Down
15 changes: 8 additions & 7 deletions bw2io/download_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from tqdm import tqdm
import requests
import re
from pathlib import Path

import requests
from tqdm import tqdm


def get_filename(response):
"""
Expand All @@ -18,7 +19,9 @@ def get_filename(response):
Filename
"""
if "Content-Disposition" in response.headers.keys():
filename = re.findall("filename=(.+)", response.headers["Content-Disposition"])[0]
filename = re.findall("filename=(.+)", response.headers["Content-Disposition"])[
0
]
else:
filename = url.split("/")[-1]
if filename[0] in "'\"":
Expand Down Expand Up @@ -52,14 +55,12 @@ def download_with_progressbar(url, filename=None, dirpath=None, chunk_size=4096
"""
response = requests.get(url, stream=True)
if response.status_code != 200:
raise ValueError(
f"URL {url} returns status code {response.status_code}"
)
raise ValueError(f"URL {url} returns status code {response.status_code}")

if not filename:
filename = get_filename(response)

total_length = response.headers.get('content-length')
total_length = response.headers.get("content-length")

filepath = (Path(dirpath) / filename) if dirpath else (Path.cwd() / filename)

Expand Down
15 changes: 8 additions & 7 deletions bw2io/ecoinvent.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
from ecoinvent_interface.string_distance import damerau_levenshtein

from .extractors import ExcelExtractor
from .importers import (
Ecospold2BiosphereImporter,
SingleOutputEcospold2Importer,
)
from .importers import Ecospold2BiosphereImporter, SingleOutputEcospold2Importer


def get_excel_sheet_names(file_path: Path) -> list[str]:
Expand Down Expand Up @@ -225,11 +222,15 @@ def import_ecoinvent_release(
existing = {flow["code"] for flow in bd.Database(biosphere_name)}
new = [flow for flow in eb.data if flow["code"] not in existing]
if new:
new_list = "\n\t".join(["{}: {}".format(o['name'], o['categories']) for o in new])
print(f"Adding {len(new)} biosphere flows to {biosphere_name}:\n\t{new_list}")
new_list = "\n\t".join(
["{}: {}".format(o["name"], o["categories"]) for o in new]
)
print(
f"Adding {len(new)} biosphere flows to {biosphere_name}:\n\t{new_list}"
)
for flow in new:
if "database" in flow:
del flow['database']
del flow["database"]
bd.Database(biosphere_name).new_activity(**flow).save()

bd.preferences["biosphere_database"] = biosphere_name
Expand Down
7 changes: 4 additions & 3 deletions bw2io/export/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ def get_activity_metadata(self, act):
[
(k, reformat(v))
for k, v in act.items()
if k != 'id' and k not in excluded and not isinstance(v, (dict, list))
if k != "id"
and k not in excluded
and not isinstance(v, (dict, list))
]
),
"parameters": self.get_activity_parameters(act),
Expand Down Expand Up @@ -160,7 +162,7 @@ def get_unformatted_data(self):
-------
dict
A nested python dictionary with the following structure:
{
'database': {
'name': name,
Expand Down Expand Up @@ -189,7 +191,6 @@ def get_unformatted_data(self):
}]
}
"""


return {
"database": self.get_database_metadata(),
Expand Down
8 changes: 5 additions & 3 deletions bw2io/export/ecospold1.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,11 @@ def add_dataset(self, node: dict) -> None:
"number": str(source.get("identifier", index + 1)),
"sourceType": SOURCE_MAP.get(source.get("type"), "0"),
"firstAuthor": source.get("authors", [""])[0],
"additionalAuthors": source["authors"][1]
if len(source.get("authors", [])) > 1
else "",
"additionalAuthors": (
source["authors"][1]
if len(source.get("authors", [])) > 1
else ""
),
}
| {
k: str(source.get(v))
Expand Down
54 changes: 25 additions & 29 deletions bw2io/export/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,14 @@ def lci_matrices_to_excel(database_name, include_descendants=True):
print("Sorting objects")

sorted_activity_keys = sorted(
[
(Database.get(key).get("name") or u"Unknown", key)
for key in lca.activity_dict
]
[(Database.get(key).get("name") or "Unknown", key) for key in lca.activity_dict]
)
sorted_product_keys = sorted(
[(Database.get(key).get("name") or u"Unknown", key) for key in lca.product_dict]
[(Database.get(key).get("name") or "Unknown", key) for key in lca.product_dict]
)
sorted_bio_keys = sorted(
[
(Database.get(key).get("name") or u"Unknown", key)
(Database.get(key).get("name") or "Unknown", key)
for key in lca.biosphere_dict
]
)
Expand Down Expand Up @@ -154,12 +151,12 @@ def lci_matrices_to_excel(database_name, include_descendants=True):
bm_sheet.write_number(bio_lookup[row] + 1, act_lookup[col] + 1, value)

COLUMNS = (
u"Index",
u"Name",
u"Reference product",
u"Unit",
u"Categories",
u"Location",
"Index",
"Name",
"Reference product",
"Unit",
"Categories",
"Location",
)

tech_sheet = workbook.add_worksheet("technosphere-labels")
Expand All @@ -183,17 +180,17 @@ def lci_matrices_to_excel(database_name, include_descendants=True):
obj = Database.get(data[1])

tech_sheet.write_number(index + 1, 0, index + 1)
tech_sheet.write_string(index + 1, 1, obj.get(u"name") or u"Unknown")
tech_sheet.write_string(index + 1, 2, obj.get(u"reference product") or u"")
tech_sheet.write_string(index + 1, 3, obj.get(u"unit") or u"Unknown")
tech_sheet.write_string(index + 1, 4, u" - ".join(obj.get(u"categories") or []))
tech_sheet.write_string(index + 1, 5, obj.get(u"location") or u"Unknown")
tech_sheet.write_string(index + 1, 1, obj.get("name") or "Unknown")
tech_sheet.write_string(index + 1, 2, obj.get("reference product") or "")
tech_sheet.write_string(index + 1, 3, obj.get("unit") or "Unknown")
tech_sheet.write_string(index + 1, 4, " - ".join(obj.get("categories") or []))
tech_sheet.write_string(index + 1, 5, obj.get("location") or "Unknown")

COLUMNS = (
u"Index",
u"Name",
u"Unit",
u"Categories",
"Index",
"Name",
"Unit",
"Categories",
)

bio_sheet = workbook.add_worksheet("biosphere-labels")
Expand All @@ -209,9 +206,9 @@ def lci_matrices_to_excel(database_name, include_descendants=True):
obj = Database.get(data[1])

bio_sheet.write_number(index + 1, 0, index + 1)
bio_sheet.write_string(index + 1, 1, obj.get(u"name") or u"Unknown")
bio_sheet.write_string(index + 1, 2, obj.get(u"unit") or u"Unknown")
bio_sheet.write_string(index + 1, 3, u" - ".join(obj.get(u"categories") or []))
bio_sheet.write_string(index + 1, 1, obj.get("name") or "Unknown")
bio_sheet.write_string(index + 1, 2, obj.get("unit") or "Unknown")
bio_sheet.write_string(index + 1, 3, " - ".join(obj.get("categories") or []))

workbook.close()
return filepath
Expand All @@ -225,7 +222,7 @@ def write_lci_excel(database_name, objs=None, sections=None, dirpath=None):
-----
Not all data can be exported. The following constraints apply:
* Nested data, e.g. `{'foo': {'bar': 'baz'}}` are excluded.
* Nested data, e.g. `{'foo': {'bar': 'baz'}}` are excluded.
* Spreadsheets are not a great format for nested data. However, *tuples* are exported, and the characters `::` are used to join elements of the tuple.
* The only well-supported data types are strings, numbers, and booleans.
Expand All @@ -247,7 +244,7 @@ def write_lci_excel(database_name, objs=None, sections=None, dirpath=None):
str
Filepath of the exported file.
"""

safe_name = safe_filename(database_name, False)
if dirpath is None:
dirpath = projects.output_dir
Expand Down Expand Up @@ -343,7 +340,7 @@ def write_row(sheet, row, data, exc=True):
sheet.write_string(row, 3, data.get("input", [""])[0], style)
sheet.write_string(row, 4, data.get("unit", "(unknown)"), style)
sheet.write_string(
row, 5, u":".join(data.get("categories", ["(unknown)"])), style
row, 5, ":".join(data.get("categories", ["(unknown)"])), style
)
sheet.write_string(row, 6, data.get("location", "(unknown)"), style)
if exc:
Expand Down Expand Up @@ -435,7 +432,6 @@ def write_lcia_matching(db, name):
Filepath of the exported file.
"""


def write_headers(sheet, row):
columns = ("Name", "Amount", "Unit", "Categories", "Matched")
for index, col in enumerate(columns):
Expand All @@ -445,7 +441,7 @@ def write_row(sheet, row, data):
sheet.write_string(row, 0, data.get("name", "(unknown)"))
sheet.write_number(row, 1, data.get("amount", -1))
sheet.write_string(row, 2, data.get("unit", "(unknown)"))
sheet.write_string(row, 3, u":".join(data.get("categories", ["(unknown)"])))
sheet.write_string(row, 3, ":".join(data.get("categories", ["(unknown)"])))
sheet.write_boolean(row, 4, "input" in data)

safe_name = safe_filename(name, False)
Expand Down
2 changes: 1 addition & 1 deletion bw2io/export/gexf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DatabaseToGEXF(object):
Database name.
include_descendants : bool, optional
Include databases which are linked from ``database``. (default False)
Warnings
--------
``include_descendants`` is not yet implemented.
Expand Down
2 changes: 1 addition & 1 deletion bw2io/extractors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
from .excel import ExcelExtractor
from .exiobase import Exiobase3MonetaryDataExtractor
from .simapro_csv import SimaProCSVExtractor
from .simapro_lcia_csv import SimaProLCIACSVExtractor
from .simapro_lcia_95project_csv import SimaProLCIA95ProjectCSVExtractor
from .simapro_lcia_csv import SimaProLCIACSVExtractor
Loading

0 comments on commit 9e8247b

Please sign in to comment.