Skip to content

Commit

Permalink
Update load_data_.py for deprecation of pkg_resources
Browse files Browse the repository at this point in the history
  • Loading branch information
marcharper authored Mar 8, 2024
1 parent c2da143 commit 36464a7
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions axelrod/load_data_.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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:
Expand Down

0 comments on commit 36464a7

Please sign in to comment.