From 6a2e4a223314b799ef9502b79a714e645172722b Mon Sep 17 00:00:00 2001 From: Adam Walkiewicz Date: Fri, 25 Nov 2022 23:39:14 +0100 Subject: [PATCH] Add utf-8 encoding when open files, #14 This should fix the issue with broken tests on Windows. --- cochar/__init__.py | 8 ++++++-- cochar/cochar.py | 2 +- setup.py | 2 +- webapp/webapp.py | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/cochar/__init__.py b/cochar/__init__.py index 8ded632..74944e3 100755 --- a/cochar/__init__.py +++ b/cochar/__init__.py @@ -58,7 +58,9 @@ def set_logging_level(logging_level=logging_level): "damage_bonus": ["-2", "-1", "0", "+1K4", "+1K6", "+2K6", "+3K6", "+4K6", "+5K6"], } -with open(os.path.join(_THIS_FOLDER, "data", "occupations.json"), "r") as json_file: +with open( + os.path.join(_THIS_FOLDER, "data", "occupations.json"), "r", encoding="utf-8" +) as json_file: # Full data of occupations OCCUPATIONS_DATA: Dict[str, dict] = dict(json.load(json_file)) # Just occupations names in the list @@ -72,7 +74,9 @@ def set_logging_level(logging_level=logging_level): [key for key, value in OCCUPATIONS_DATA.items() if "edustr" in value["groups"]], ] -with open(os.path.join(_THIS_FOLDER, "data", "settings.json"), "r") as json_file: +with open( + os.path.join(_THIS_FOLDER, "data", "settings.json"), "r", encoding="utf-8" +) as json_file: settings: dict = json.load(json_file) MIN_AGE: int = settings["min_age"] MAX_AGE: int = settings["max_age"] diff --git a/cochar/cochar.py b/cochar/cochar.py index d3dec08..b56384e 100644 --- a/cochar/cochar.py +++ b/cochar/cochar.py @@ -168,7 +168,7 @@ def correct_bisect_left(data, year): file_name = f"pop{corrected_year}" - with open(cochar.POP_PYRAMID_PATH) as json_file: + with open(cochar.POP_PYRAMID_PATH, "r", encoding="utf-8") as json_file: age_population = cochar.utils.AGE_RANGE age_weights = json.load(json_file)[file_name][sex][3:-1] age_range = random.choices(age_population, weights=age_weights)[0] diff --git a/setup.py b/setup.py index 8113ef3..1ce0ea5 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) -with open(f"{THIS_FOLDER}/docs/readme.md", "r") as fh: +with open(f"{THIS_FOLDER}/docs/readme.md", "r", encoding="utf-8") as fh: long_description = fh.read() setup( diff --git a/webapp/webapp.py b/webapp/webapp.py index 128c701..da083ca 100644 --- a/webapp/webapp.py +++ b/webapp/webapp.py @@ -8,7 +8,7 @@ _THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) _README = os.path.abspath(os.path.join(_THIS_FOLDER, "static", "README.md")) -with open(_README, "r") as readme: +with open(_README, "r", encoding="utf-8") as readme: text = readme.read() html = markdown.markdown(text)