While writing a chess program in Python as a learning exercise, I had two separate functions for converting a position from an algebraic notation string, 'a1', to a tuple, (97, 1), and vice versa. I later merged the two functions into a static method for the Board class. The code works. I should have left it at that and move on.
I found any interesting situation when refactoring the code to work with @functools.singledispatch and @staticmethod in a class. The code worked but I was using the wrong single dispatch decorator. When is a bug not a bug?
Read my essayand follow me on Medium.Your support is greatly appreciated.
I’m writing a chess program in Python as a learning exercising. Not surprisingly, mypy, the static type checker, complained about my code with yet another incomprehensible error message. Here’s a quick-and-dirty fix for the incompatible type assignment error.
Read my essay and follow me on Medium. Your support is greatly appreciated.
While cleaning up code in my chess program that I’m writing in Python as a learning exercise, I came across yet another problem with the mypy static type checker. This time with the Enum.name attribute.
color = Color(self._color).name.capitalize()
The statement worked but mypy reported an error message.
error: Item “None” of “Optional[str]” has no attribute “capitalize”
I came up with a solution for that problem.
Read my essay and follow me on Medium.Your support is greatly appreciated.
While cleaning up my code in a chess program that I’m writing in Python as a learning exercise, I created an Enum class to represent the starting ranks for certain pieces on the board. Since I wanted the Enum to also behave like a list, I added that to the class definition. The mypy static type checker reported an incompatible __hash__ dunder method error between list and Enum. I came up with a solution for that problem.
Read my essay and follow meon Medium. Your support is greatly appreciated.
Johnny B. Getgoode tweeted a screenshot from his developer Discourse server on February 13, 2022. Experienced programmers cringed at the repeating lines of if else to determine if a number is even or odd. That prompted a lively discussion on how to “un-cringe” this function.
Here are four ways to refactor the function for readability and speed in Python: match and case, recursive inner function, modulo operator, and bitwise operator.
Read the rest of the essay on Medium.If you’re interested in supporting my writing on Medium, please follow me and become a paid member (I’ll get a referral bonus).