From 36464a78de8fd9628f81c8936854592429f2731a Mon Sep 17 00:00:00 2001 From: Marc Date: Thu, 7 Mar 2024 18:36:36 -0800 Subject: [PATCH] Update load_data_.py for deprecation of pkg_resources --- axelrod/load_data_.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/axelrod/load_data_.py b/axelrod/load_data_.py index 30e407566..eff61a564 100644 --- a/axelrod/load_data_.py +++ b/axelrod/load_data_.py @@ -1,8 +1,12 @@ import pathlib from typing import Dict, List, Text, Tuple -import pkg_resources - +try: + # For Python >= 3.9 + from importlib import resources +except ImportError: + # Try backported to Python < 3.7 `importlib_resources`. + import importlib_resources as resources def axl_filename(path: pathlib.Path) -> pathlib.Path: """Given a path under Axelrod/, return absolute filepath. @@ -24,9 +28,15 @@ def axl_filename(path: pathlib.Path) -> pathlib.Path: def load_file(filename: str, directory: str) -> List[List[str]]: """Loads a data file stored in the Axelrod library's data subdirectory, likely for parameters for a strategy.""" - path = "/".join((directory, filename)) - data_bytes = pkg_resources.resource_string(__name__, path) - data = data_bytes.decode("UTF-8", "replace") + + try: + path = (resources.files(directory) / filename) + with path.open("r") as f: + data = f.read() + except AttributeError: + # Python < 3.9, fall back to method deprecated in 3.11. + data = resources.read_text(directory, filename) + rows = [] for line in data.split("\n"): if line.startswith("#") or len(line) == 0: