python-chess icon indicating copy to clipboard operation
python-chess copied to clipboard

Code refactor, meta stuff

Open johnslavik opened this issue 2 years ago • 3 comments

  • [ ] Fix inconsistent string formatting (both .format() and f-string methods are used)
  • [ ] Refactor & optimize algorithms (syzygy, gaviota)
  • [ ] Shorten maximum line length to 99 (thank later)
  • [ ] Migrate to PEP 604 X | Y type syntax due to #564
  • [ ] Add a requirements.txt file (or even better - migrate from setuptools to poetry)
  • [ ] Fix 517 issues reported by Ruff, possibly add a Ruff spec file or add a proper section in pyproject.toml if we're into poetry
  • [ ] Bump pylint rating, my initial goal: 9.00/10, current rating: 7.64/10

Some algorithms do need logic refactoring, for instance

https://github.com/niklasf/python-chess/blob/f76c387ef9c68555a08b1629b56655d85f86d949/chess/syzygy.py#L323-L352

which could simply be truncated into

for i in range(5):
    j = 0
    a = PFACTOR[i]
    p = PAWNIDX[i]

    for k in range(4):
        s = 0
        t = 6 * (k+1)
        while j < t:
            p[j] = s
            s += 1 if i == 0 else binom(PTWIST[INVFLAP[j]], i)
            j += 1
        a[k] = s

I'll work on those enhancements soon. Please let me know what you think about migrating to poetry and maybe possibly migrating tests to pytest to make them easier to read and work with. @niklasf

johnslavik avatar Jul 24 '23 00:07 johnslavik

Hi, some thoughts about these:

  • (1), (2), (3): Sure, if the new code would be more readable. For (2), the code was ported line by line from a C original. It has since diverged, so I'd be fine with giving up the 1:1 correspondence and make it look more like Python code.
  • (4) will only be merged after Python 3.9 is past its end of life.
  • (5): There are no dependencies :)
  • (6), (7): Sure, if the findings make sense.

For pytest, at a glance I don't see how it would improve dx for this project. Do you have any particular features in mind?

niklasf avatar Jul 25 '23 18:07 niklasf

About (4), I think as long as you import from __future__ importannotations, you should be able to use the new syntax just fine, we use it on berserk and support 3.8

kraktus avatar Aug 05 '23 20:08 kraktus

It can actually work even for type hints evaluated at runtime since https://github.com/alexmojaki/eval_type_backport.

johnslavik avatar Apr 10 '24 16:04 johnslavik