From 553bc01ade915a6b6e7ed60c30103750a25c3e0d Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Sat, 4 Mar 2023 18:15:52 -0800 Subject: [PATCH] types -> typez as the name mirrors a python core library name. --- docs/pyutils.rst | 2 +- examples/wordle/wordle.py | 32 +++++++++++----------- src/pyutils/collectionz/interval_tree.py | 2 +- src/pyutils/graph.py | 2 +- src/pyutils/math_utils.py | 2 +- src/pyutils/parallelize/executors.py | 4 +-- src/pyutils/{types => typez}/__init__.py | 0 src/pyutils/{types => typez}/centcount.py | 2 +- src/pyutils/{types => typez}/histogram.py | 0 src/pyutils/{types => typez}/money.py | 2 +- src/pyutils/{types => typez}/rate.py | 0 src/pyutils/{types => typez}/simple.py | 0 src/pyutils/{types => typez}/type_utils.py | 0 tests/{types => typez}/centcount_test.py | 2 +- tests/{types => typez}/money_test.py | 2 +- tests/{types => typez}/rate_test.py | 4 +-- 16 files changed, 28 insertions(+), 28 deletions(-) rename src/pyutils/{types => typez}/__init__.py (100%) rename src/pyutils/{types => typez}/centcount.py (99%) rename src/pyutils/{types => typez}/histogram.py (100%) rename src/pyutils/{types => typez}/money.py (99%) rename src/pyutils/{types => typez}/rate.py (100%) rename src/pyutils/{types => typez}/simple.py (100%) rename src/pyutils/{types => typez}/type_utils.py (100%) rename tests/{types => typez}/centcount_test.py (98%) rename tests/{types => typez}/money_test.py (98%) rename tests/{types => typez}/rate_test.py (95%) diff --git a/docs/pyutils.rst b/docs/pyutils.rst index aff3220..888b660 100644 --- a/docs/pyutils.rst +++ b/docs/pyutils.rst @@ -55,7 +55,7 @@ Most code includes inline documentation and doctests. I've tried to organize it into logical packages based on the code's functionality. Note that when words would collide with a Python standard library or reserved keyword I've used a 'z' at the end, e.g. 'collectionz' -instead of 'collections'. +instead of 'collections' and 'typez' instead of 'types'. There's some example code that uses various features of this project checked in under `examples/ `_ that you can check out. See the `README `__ in that directory for more information diff --git a/examples/wordle/wordle.py b/examples/wordle/wordle.py index ca7930a..a3042ee 100755 --- a/examples/wordle/wordle.py +++ b/examples/wordle/wordle.py @@ -24,7 +24,7 @@ from pyutils.files import file_utils from pyutils.parallelize import executors from pyutils.parallelize import parallelize as par from pyutils.parallelize import smart_future -from pyutils.types import histogram +from pyutils.typez import histogram logger = logging.getLogger(__name__) args = config.add_commandline_args( @@ -574,7 +574,7 @@ class AutoPlayer(object): self.position_hash = {} filename = config.config['hash_file'] if filename is not None and file_utils.is_readable(filename): - logger.debug(f'Initializing position hash from {filename}...') + logger.debug('Initializing position hash from %s...', filename) with open(filename, 'r') as rf: for line in rf: line = line[:-1] @@ -589,13 +589,13 @@ class AutoPlayer(object): fprint = fprint.strip() word = word.strip() self.position_hash[(count, fprint)] = word - logger.debug(f'...hash contains {len(self.position_hash)} entries.') + logger.debug('...hash contains %s entries.', len(self.position_hash)) # All legal solutions pre-sorted by length. self.all_possible_solutions_by_length = defaultdict(list) filename = config.config['solutions_file'] if filename is not None and file_utils.is_readable(filename): - logger.debug(f'Initializing valid solution word list from {filename}...') + logger.debug('Initializing valid solution word list from %s...', filename) with open(filename) as rf: for word in rf: word = word[:-1] @@ -609,7 +609,7 @@ class AutoPlayer(object): self.all_possible_guesses_by_length = defaultdict(list) filename = config.config['guesses_file'] if filename is not None and file_utils.is_readable(filename): - logger.debug(f'Initializing legal guess word list from {filename}...') + logger.debug('Initializing legal guess word list from %s...', filename) with open(filename) as rf: for word in rf: word = word[:-1] @@ -756,7 +756,7 @@ class AutoPlayer(object): out = '' for letter, weight in sorted(letter_frequency.items(), key=lambda x: -x[1]): out += f'{letter}:{weight}, ' - if len(out): + if len(out) > 0: out = out[:-2] logger.debug(out) @@ -766,7 +766,7 @@ class AutoPlayer(object): pop = letter_position_frequency[n] for letter, weight in sorted(pop.items(), key=lambda x: -x[1]): out += f'pos{n}:{letter}@{weight:.5f}, ' - if len(out): + if len(out) > 0: out = out[:-2] logger.debug(out) @@ -780,7 +780,7 @@ class AutoPlayer(object): template = self.word_state.get_template() possible_solutions = self.get_all_possible_solutions() num_possible_solutions = len(possible_solutions) - fprint = hashlib.md5(possible_solutions.__repr__().encode('ascii')).hexdigest() + fprint = hashlib.md5(repr(possible_solutions).encode('ascii')).hexdigest() n = num_possible_solutions logger.debug( @@ -797,7 +797,7 @@ class AutoPlayer(object): ) ) logger.debug( - f'Letter count restrictions: {self.word_state.letters_in_solution}' + 'Letter count restrictions: %s', self.word_state.letters_in_solution ) if num_possible_solutions == 0: logger.error('No possible solutions?!') @@ -812,7 +812,7 @@ class AutoPlayer(object): # Check the hash table for a precomputed best guess. elif self.position_in_hash(num_possible_solutions, fprint): guess = self.position_hash[(num_possible_solutions, fprint)] - logger.debug(f'hash hit: {guess}') + logger.debug('hash hit: %s', guess) return guess # If there are just a few solutions possible, brute force the @@ -821,7 +821,7 @@ class AutoPlayer(object): # large numbers of solutions. elif num_possible_solutions < 20: logger.debug( - f'Only {num_possible_solutions} solutions; using brute force strategy.' + 'Only %d solutions; using brute force strategy.', num_possible_solutions ) return self.brute_force_internal( possible_solutions, @@ -834,7 +834,7 @@ class AutoPlayer(object): # guesses via brute force. elif num_possible_solutions < 100: logger.debug( - f'Only {num_possible_solutions} solutions; using hybrid strategy.' + 'Only %d solutions; using hybrid strategy.', num_possible_solutions ) return self.hybrid_search(possible_solutions) @@ -842,7 +842,7 @@ class AutoPlayer(object): # fast heuristics (i.e. letter frequency). else: logger.debug( - f'There are {num_possible_solutions} solutions; using fast heuristics.' + 'There are %s solutions; using fast heuristics.', num_possible_solutions ) return self.heuristics_search(possible_solutions) @@ -969,7 +969,7 @@ class AutoPlayer(object): elif guess_entropy == best_entropy and best_count < 15: logger.debug(f'{label}: #{n}: {guess} with {guess_entropy:.5f} bits') best_count += 1 - logger.debug(f'{label}: best guess is {best_guess}.') + logger.debug('%s: best guess is %s.', label, best_guess) return best_guess def hybrid_search(self, possible_solutions: List[Word]) -> Optional[Word]: @@ -1154,7 +1154,7 @@ def cheat(): if not avoid: avoid = '' avoid = avoid.lower() - letters_to_avoid = set([letter for letter in avoid]) + letters_to_avoid = set(list(avoid)) # Initialize the set of letters we know are in the solution but # not where, yet. @@ -1528,7 +1528,7 @@ def play() -> None: if guess == solution: print('Nice!') break - elif num_guesses >= 6: + if num_guesses >= 6: print('Better luck next time.') print(padding + f'{solution}') break diff --git a/src/pyutils/collectionz/interval_tree.py b/src/pyutils/collectionz/interval_tree.py index 4a8bdd8..0a88a3d 100644 --- a/src/pyutils/collectionz/interval_tree.py +++ b/src/pyutils/collectionz/interval_tree.py @@ -14,7 +14,7 @@ from typing import Any, Generator, Optional from overrides import overrides from pyutils.collectionz import bst -from pyutils.types.simple import Numeric +from pyutils.typez.simple import Numeric @total_ordering diff --git a/src/pyutils/graph.py b/src/pyutils/graph.py index 411eb13..faa0ec3 100644 --- a/src/pyutils/graph.py +++ b/src/pyutils/graph.py @@ -10,7 +10,7 @@ import math from typing import Dict, Generator, List, Optional, Set, Tuple from pyutils import list_utils -from pyutils.types.simple import Numeric +from pyutils.typez.simple import Numeric class Graph(object): diff --git a/src/pyutils/math_utils.py b/src/pyutils/math_utils.py index 9de2052..4ceee8c 100644 --- a/src/pyutils/math_utils.py +++ b/src/pyutils/math_utils.py @@ -11,7 +11,7 @@ from heapq import heappop, heappush from typing import Dict, List, Optional, Tuple from pyutils import dict_utils -from pyutils.types.simple import Numeric +from pyutils.typez.simple import Numeric class NumericPopulation(object): diff --git a/src/pyutils/parallelize/executors.py b/src/pyutils/parallelize/executors.py index e5cfcfe..fd8cc7c 100644 --- a/src/pyutils/parallelize/executors.py +++ b/src/pyutils/parallelize/executors.py @@ -57,7 +57,7 @@ from typing import Any, Callable, Dict, List, Optional, Set import cloudpickle # type: ignore from overrides import overrides -import pyutils.types.histogram as hist +import pyutils.typez.histogram as hist from pyutils import ( argparse_utils, config, @@ -70,7 +70,7 @@ from pyutils.ansi import bg, fg, reset, underline from pyutils.decorator_utils import singleton from pyutils.exec_utils import cmd_exitcode, cmd_in_background, run_silently from pyutils.parallelize.thread_utils import background_thread -from pyutils.types import type_utils +from pyutils.typez import type_utils logger = logging.getLogger(__name__) diff --git a/src/pyutils/types/__init__.py b/src/pyutils/typez/__init__.py similarity index 100% rename from src/pyutils/types/__init__.py rename to src/pyutils/typez/__init__.py diff --git a/src/pyutils/types/centcount.py b/src/pyutils/typez/centcount.py similarity index 99% rename from src/pyutils/types/centcount.py rename to src/pyutils/typez/centcount.py index 58a8274..a17060d 100644 --- a/src/pyutils/types/centcount.py +++ b/src/pyutils/typez/centcount.py @@ -41,7 +41,7 @@ numbers). of this and decide whether it's suitable for your application. -See also the :class:`pyutils.types.Money` class which uses Python +See also the :class:`pyutils.typez.Money` class which uses Python Decimals (see: https://docs.python.org/3/library/decimal.html) to represent monetary amounts. """ diff --git a/src/pyutils/types/histogram.py b/src/pyutils/typez/histogram.py similarity index 100% rename from src/pyutils/types/histogram.py rename to src/pyutils/typez/histogram.py diff --git a/src/pyutils/types/money.py b/src/pyutils/typez/money.py similarity index 99% rename from src/pyutils/types/money.py rename to src/pyutils/typez/money.py index 3a23afc..5eafd63 100644 --- a/src/pyutils/types/money.py +++ b/src/pyutils/typez/money.py @@ -11,7 +11,7 @@ another, and has a strict mode which disallows comparison or aggregation with non-:class:`Money` operands (i.e. no comparison or aggregation with literal numbers). -See also :class:`pyutils.types.centcount.CentCount` which represents +See also :class:`pyutils.typez.centcount.CentCount` which represents monetary amounts as an integral number of cents. """ diff --git a/src/pyutils/types/rate.py b/src/pyutils/typez/rate.py similarity index 100% rename from src/pyutils/types/rate.py rename to src/pyutils/typez/rate.py diff --git a/src/pyutils/types/simple.py b/src/pyutils/typez/simple.py similarity index 100% rename from src/pyutils/types/simple.py rename to src/pyutils/typez/simple.py diff --git a/src/pyutils/types/type_utils.py b/src/pyutils/typez/type_utils.py similarity index 100% rename from src/pyutils/types/type_utils.py rename to src/pyutils/typez/type_utils.py diff --git a/tests/types/centcount_test.py b/tests/typez/centcount_test.py similarity index 98% rename from tests/types/centcount_test.py rename to tests/typez/centcount_test.py index 62181f4..f5cc4ca 100755 --- a/tests/types/centcount_test.py +++ b/tests/typez/centcount_test.py @@ -7,7 +7,7 @@ import unittest from pyutils import unittest_utils -from pyutils.types.centcount import CentCount +from pyutils.typez.centcount import CentCount class TestCentCount(unittest.TestCase): diff --git a/tests/types/money_test.py b/tests/typez/money_test.py similarity index 98% rename from tests/types/money_test.py rename to tests/typez/money_test.py index 524f103..9b87eb8 100755 --- a/tests/types/money_test.py +++ b/tests/typez/money_test.py @@ -8,7 +8,7 @@ import unittest from decimal import Decimal from pyutils import unittest_utils -from pyutils.types.money import Money +from pyutils.typez.money import Money class TestMoney(unittest.TestCase): diff --git a/tests/types/rate_test.py b/tests/typez/rate_test.py similarity index 95% rename from tests/types/rate_test.py rename to tests/typez/rate_test.py index 15449eb..ff9dcdd 100755 --- a/tests/types/rate_test.py +++ b/tests/typez/rate_test.py @@ -7,8 +7,8 @@ import unittest from pyutils import unittest_utils -from pyutils.types.money import Money -from pyutils.types.rate import Rate +from pyutils.typez.money import Money +from pyutils.typez.rate import Rate class TestRate(unittest.TestCase): -- 2.47.1