From 69f75d03b550f9e59f5ca5e2b73e02e87fae09ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Thu, 4 Feb 2021 18:40:39 +0100 Subject: [PATCH] Use pathlib.Path --- setup.py | 10 ++++----- test/test_basic.py | 50 +++++++++++++++++++------------------------ test/test_combine.py | 23 +++++++++++--------- test/test_itertext.py | 30 ++++++++++++-------------- 4 files changed, 53 insertions(+), 60 deletions(-) diff --git a/setup.py b/setup.py index 0b90c35..36a9742 100644 --- a/setup.py +++ b/setup.py @@ -1,15 +1,13 @@ +from pathlib import Path from setuptools import setup, find_packages -import os NAME = "markovify" -HERE = os.path.abspath(os.path.dirname(__file__)) +HERE = Path(__file__).resolve().parent version_ns = {} -with open(os.path.join(HERE, NAME, '__version__.py'), encoding='utf-8') as f: - exec(f.read(), {}, version_ns) +exec((HERE / NAME / '__version__.py').read_text(), {}, version_ns) -with open(os.path.join(HERE, 'README.md'), encoding='utf-8') as f: - long_description = f.read() +long_description = (HERE / 'README.md').read_text() setup( name="markovify", diff --git a/test/test_basic.py b/test/test_basic.py index b005b1b..ebed37e 100644 --- a/test/test_basic.py +++ b/test/test_basic.py @@ -1,7 +1,11 @@ +import operator import unittest +from pathlib import Path + import markovify -import os -import operator + + +TEXTS = Path(__file__).resolve().parent / 'texts' def get_sorted(chain_json): @@ -149,9 +153,7 @@ def test_min_words(self): @staticmethod def test_newline_text(): - with open(os.path.join(os.path.dirname(__file__), - "texts/senate-bills.txt")) as f: - model = markovify.NewlineText(f.read()) + model = markovify.NewlineText((TEXTS / 'senate-bills.txt').read_text()) model.make_sentence() def test_bad_corpus(self): @@ -178,25 +180,19 @@ def test_custom_regex(self): class MarkovifyTest(MarkovifyTestBase): __test__ = True - with open(os.path.join(os.path.dirname(__file__), - "texts/sherlock.txt")) as f: - sherlock_text = f.read() - sherlock_model = markovify.Text(sherlock_text) - sherlock_model_ss2 = markovify.Text(sherlock_text, state_size=2) - sherlock_model_ss3 = markovify.Text(sherlock_text, state_size=3) + sherlock_text = (TEXTS / 'sherlock.txt').read_text() + sherlock_model = markovify.Text(sherlock_text) + sherlock_model_ss2 = markovify.Text(sherlock_text, state_size=2) + sherlock_model_ss3 = markovify.Text(sherlock_text, state_size=3) class MarkovifyTestCompiled(MarkovifyTestBase): __test__ = True - with open(os.path.join(os.path.dirname(__file__), - "texts/sherlock.txt")) as f: - sherlock_text = f.read() - sherlock_model = (markovify.Text(sherlock_text)).compile() - sherlock_model_ss2 = ( - markovify.Text(sherlock_text, state_size=2)).compile() - sherlock_model_ss3 = ( - markovify.Text(sherlock_text, state_size=3)).compile() + sherlock_text = (TEXTS / 'sherlock.txt').read_text() + sherlock_model = markovify.Text(sherlock_text).compile() + sherlock_model_ss2 = markovify.Text(sherlock_text, state_size=2).compile() + sherlock_model_ss3 = markovify.Text(sherlock_text, state_size=3).compile() def test_recompiling(self): model_recompile = self.sherlock_model.compile() @@ -211,15 +207,13 @@ def test_recompiling(self): class MarkovifyTestCompiledInPlace(MarkovifyTestBase): __test__ = True - with open(os.path.join(os.path.dirname(__file__), - "texts/sherlock.txt")) as f: - sherlock_text = f.read() - sherlock_model = markovify.Text(sherlock_text) - sherlock_model_ss2 = markovify.Text(sherlock_text, state_size=2) - sherlock_model_ss3 = markovify.Text(sherlock_text, state_size=3) - sherlock_model.compile(inplace=True) - sherlock_model_ss2.compile(inplace=True) - sherlock_model_ss3.compile(inplace=True) + sherlock_text = (TEXTS / 'sherlock.txt').read_text() + sherlock_model = markovify.Text(sherlock_text) + sherlock_model_ss2 = markovify.Text(sherlock_text, state_size=2) + sherlock_model_ss3 = markovify.Text(sherlock_text, state_size=3) + sherlock_model.compile(inplace=True) + sherlock_model_ss2.compile(inplace=True) + sherlock_model_ss3.compile(inplace=True) if __name__ == '__main__': diff --git a/test/test_combine.py b/test/test_combine.py index 5418815..73805e2 100644 --- a/test/test_combine.py +++ b/test/test_combine.py @@ -1,18 +1,21 @@ +import operator import unittest +from pathlib import Path + import markovify -import os -import operator + + +TEXTS = Path(__file__).resolve().parent / 'texts' def get_sorted(chain_json): return sorted(chain_json, key=operator.itemgetter(0)) -with open(os.path.join(os.path.dirname(__file__), "texts/sherlock.txt")) as f: - sherlock = f.read() - sherlock_model = markovify.Text(sherlock) - sherlock_model_no_retain = markovify.Text(sherlock, retain_original=False) - sherlock_model_compiled = sherlock_model.compile() +sherlock_text = (TEXTS / 'sherlock.txt').read_text() +sherlock_model = markovify.Text(sherlock_text) +sherlock_model_no_retain = markovify.Text(sherlock_text, retain_original=False) +sherlock_model_compiled = sherlock_model.compile() class MarkovifyTest(unittest.TestCase): @@ -55,14 +58,14 @@ def test_bad_weights(self): def test_mismatched_state_sizes(self): with self.assertRaises(Exception): - text_model_a = markovify.Text(sherlock, state_size=2) - text_model_b = markovify.Text(sherlock, state_size=3) + text_model_a = markovify.Text(sherlock_text, state_size=2) + text_model_b = markovify.Text(sherlock_text, state_size=3) markovify.combine([text_model_a, text_model_b]) def test_mismatched_model_types(self): with self.assertRaises(Exception): text_model_a = sherlock_model - text_model_b = markovify.NewlineText(sherlock) + text_model_b = markovify.NewlineText(sherlock_text) markovify.combine([text_model_a, text_model_b]) def test_compiled_model_fail(self): diff --git a/test/test_itertext.py b/test/test_itertext.py index 8119044..b86f4a1 100644 --- a/test/test_itertext.py +++ b/test/test_itertext.py @@ -1,34 +1,34 @@ import unittest +from pathlib import Path + import markovify -import os + + +TEXTS = Path(__file__).resolve().parent / 'texts' class MarkovifyTest(unittest.TestCase): @staticmethod def test_simple(): - with open(os.path.join(os.path.dirname(__file__), - "texts/sherlock.txt")) as f: - sherlock_model = markovify.Text(f) + sherlock_model = markovify.Text((TEXTS / 'sherlock.txt').read_text()) sent = sherlock_model.make_sentence() assert sent is not None assert len(sent) != 0 @staticmethod def test_without_retaining(): - with open(os.path.join(os.path.dirname(__file__), - "texts/senate-bills.txt")) as f: - senate_model = markovify.Text(f, retain_original=False) + senate_model = markovify.Text((TEXTS / 'senate-bills.txt').read_text(), + retain_original=False) sent = senate_model.make_sentence() assert sent is not None assert len(sent) != 0 @staticmethod def test_from_json_without_retaining(): - with open(os.path.join(os.path.dirname(__file__), - "texts/senate-bills.txt")) as f: - original_model = markovify.Text(f, retain_original=False) - d = original_model.to_json() + senate_model = markovify.Text((TEXTS / 'senate-bills.txt').read_text(), + retain_original=False) + d = senate_model.to_json() new_model = markovify.Text.from_json(d) sent = new_model.make_sentence() assert sent is not None @@ -37,11 +37,9 @@ def test_from_json_without_retaining(): @staticmethod def test_from_mult_files_without_retaining(): models = [] - for (dirpath, _, filenames) in os.walk( - os.path.join(os.path.dirname(__file__), "texts")): - for filename in filenames: - with open(os.path.join(dirpath, filename)) as f: - models.append(markovify.Text(f, retain_original=False)) + for path in TEXTS.glob('*.txt'): + models.append(markovify.Text(path.read_text(), + retain_original=False)) combined_model = markovify.combine(models) sent = combined_model.make_sentence() assert sent is not None