From 33e8aa8a8af6a81dc0ad9543bcb57f7208b8d8ea Mon Sep 17 00:00:00 2001 From: Ben Young Date: Wed, 6 Nov 2024 10:54:37 -0500 Subject: [PATCH 1/2] :heavy_check_mark: implement test to check method urls --- lciafmt/data/methods.json | 2 +- lciafmt/iw.py | 14 +++++++++----- lciafmt/recipe.py | 16 +++++++++++----- lciafmt/traci.py | 11 +++++++---- tests/test_generate_methods.py | 7 +++++++ 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/lciafmt/data/methods.json b/lciafmt/data/methods.json index d971d5f..4b42e0c 100644 --- a/lciafmt/data/methods.json +++ b/lciafmt/data/methods.json @@ -49,7 +49,7 @@ "ImpactWorld+ - Endpoint":"This method includes all global endpoint flows and their attributes."}, "path": "impactworld", "case_insensitivity": "False", - "url": "http://www.impactworldplus.org/en/writeToFile.php", + "url": "https://www.dropbox.com/sh/2sdgbqf08yn91bc/AABIGLlb_OwfNy6oMMDZNrm0a/IWplus_public_v1.3.accdb?dl=1", "citation": "Bulle, Cecile, Manuele Margni, Laure Patouillard, Anne-Marie Boulay, Guillaume Bourgault, Vincent De Bruille, Viet Cao, et al. IMPACT World+: A Globally Regionalized Life Cycle Impact Assessment Method. The International Journal of Life Cycle Assessment 24, no. 9 (September 2019), 1653–74. https://doi.org/10.1007/s11367-019-01583-0", "source_type": "Access file" }, diff --git a/lciafmt/iw.py b/lciafmt/iw.py index de66529..fdf9f1c 100644 --- a/lciafmt/iw.py +++ b/lciafmt/iw.py @@ -6,6 +6,7 @@ """ import pandas as pd +import lciafmt import lciafmt.cache as cache import lciafmt.df as dfutil from lciafmt.util import log, format_cas @@ -38,13 +39,10 @@ def get(file=None, url=None, region=None) -> pd.DataFrame: "Please install drivers to remotely connect to Access Database. " "Drivers only available on windows platform. For instructions visit: " "https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-Microsoft-Access") - + method_meta = lciafmt.Method.ImpactWorld.get_metadata() f = file if f is None: - fname = "Impact_World.accdb" - if url is None: - url = "https://www.dropbox.com/sh/2sdgbqf08yn91bc/AABIGLlb_OwfNy6oMMDZNrm0a/IWplus_public_v1.3.accdb?dl=1" - f = cache.get_or_download(fname, url) + f = _get_file(method_meta, url) df = _read(f, region) # Identify midpoint and endpoint records and differentiate in data frame. @@ -58,6 +56,12 @@ def get(file=None, url=None, region=None) -> pd.DataFrame: return df +def _get_file(method_meta, url=None): + fname = "Impact_World.accdb" + if url is None: + url = method_meta['url'] + f = cache.get_or_download(fname, url) + return f def _read(access_file: str, region) -> pd.DataFrame: """Read the Access database at passed access_file into DataFrame.""" diff --git a/lciafmt/recipe.py b/lciafmt/recipe.py index 3ddcfa5..1c95a8a 100644 --- a/lciafmt/recipe.py +++ b/lciafmt/recipe.py @@ -9,6 +9,7 @@ import pandas as pd import openpyxl +import lciafmt import lciafmt.cache as cache import lciafmt.df as dfutil import lciafmt.xls as xls @@ -52,13 +53,10 @@ def get(add_factors_for_missing_contexts=True, endpoint=True, :return: DataFrame of method in standard format """ log.info("getting method ReCiPe 2016") + method_meta = lciafmt.Method.RECIPE_2016.get_metadata() f = file if f is None: - fname = "recipe_2016.xlsx" - if url is None: - url = ("http://www.rivm.nl/sites/default/files/2018-11/" + - "ReCiPe2016_CFs_v1.1_20180117.xlsx") - f = cache.get_or_download(fname, url) + f = _get_file(method_meta, url) df = _read(f) if add_factors_for_missing_contexts: log.info("adding average factors for primary contexts") @@ -135,6 +133,14 @@ def get(add_factors_for_missing_contexts=True, endpoint=True, return df +def _get_file(method_meta, url=None): + fname = "recipe_2016.xlsx" + if url is None: + url = method_meta['url'] + f = cache.get_or_download(fname, url) + return f + + def _read(file: str) -> pd.DataFrame: log.info(f"read ReCiPe 2016 from file {file}") wb = openpyxl.load_workbook(file, read_only=True, data_only=True) diff --git a/lciafmt/traci.py b/lciafmt/traci.py index 97d6265..d061633 100644 --- a/lciafmt/traci.py +++ b/lciafmt/traci.py @@ -38,10 +38,7 @@ def get(add_factors_for_missing_contexts=True, file=None, method_meta = lciafmt.Method.TRACI.get_metadata() f = file if f is None: - fname = "traci_2.1.xlsx" - if url is None: - url = method_meta['url'] - f = cache.get_or_download(fname, url) + f = _get_file(method_meta, url) df = _read(f) if add_factors_for_missing_contexts: log.info("adding average factors for primary contexts") @@ -72,6 +69,12 @@ def get(add_factors_for_missing_contexts=True, file=None, return df +def _get_file(method_meta, url=None): + fname = "traci_2.1.xlsx" + if url is None: + url = method_meta['url'] + f = cache.get_or_download(fname, url) + return f def _read(xls_file: str) -> pd.DataFrame: """Read the data from Excel with given path into a DataFrame.""" diff --git a/tests/test_generate_methods.py b/tests/test_generate_methods.py index 4d516e5..67a48fd 100644 --- a/tests/test_generate_methods.py +++ b/tests/test_generate_methods.py @@ -23,6 +23,12 @@ def test_generate_methods(): assert not error_list +def test_url_access(): + import lciafmt.iw as impactworld + f = lciafmt.recipe._get_file(lciafmt.Method.RECIPE_2016.get_metadata()) + f = lciafmt.traci._get_file(lciafmt.Method.TRACI.get_metadata()) + f = impactworld._get_file(lciafmt.Method.ImpactWorld.get_metadata()) + def test_endpoint_method(): method = lciafmt.generate_endpoints('Weidema_valuation', name='Weidema Valuation', @@ -63,4 +69,5 @@ def test_compilation_method(): if __name__ == "__main__": # test_generate_methods() # test_method_write_json() + # test_url_access() test_compilation_method() From a2bff150c954d1680963703a18a6f5d77685c851 Mon Sep 17 00:00:00 2001 From: Ben Young Date: Wed, 6 Nov 2024 10:55:20 -0500 Subject: [PATCH 2/2] update recipe url resolves #121 --- lciafmt/data/methods.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lciafmt/data/methods.json b/lciafmt/data/methods.json index 4b42e0c..8a39c39 100644 --- a/lciafmt/data/methods.json +++ b/lciafmt/data/methods.json @@ -25,7 +25,7 @@ "ReCiPe 2016 - Midpoint/E":"ReCiPe 2016 v1.1 midpoint method, Egalitarian version. The typical ReCiPe midpoint method used is the Hierarchist version."}, "path": "recipe", "case_insensitivity": "True", - "url": "http://www.rivm.nl/sites/default/files/2018-11/ReCiPe2016_CFs_v1.1_20180117.xlsx", + "url": "https://www.rivm.nl/sites/default/files/2024-10/ReCiPe2016_CFs_v1.1_20180117.xlsx", "bib_id": "huijbregts_recipe_2017", "citation": "Huijbregts 2017", "source_type": "Excel file"