Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed typos and made adjustments for PEP8 in Python listings #119

Merged
merged 13 commits into from
Jul 29, 2024

Conversation

Doublestuf
Copy link
Contributor

@Doublestuf Doublestuf commented Jul 14, 2024

Description

I fixed some typos, changed variable and function names to match PEP8, and added type hints in Python. Got through about 9.5 out of 14 folders in the Python listing, so about 2/3rds done. Didn't realize until after starting that I cloned the master branch and some type hints were being added in develop, but I didn't reach computer science, collision detection, algorithms, or AI so I don't think we changed any of the same files. More changes could be made for code cleanliness in function and variable names, but I decided not to focus on that for now, can always go through the files again later to do another sweep. I did do some slight variable name changes for code cleanliness/readability (i.e player.on_ground -> player.is_on_ground), but mostly making Python-specific adjustments.

Speaking of which, I noticed that the abc module could be used for implementing abstract classes in quite a few design patterns. I didn't add that because it could raise concerns about the code being understandable for beginners, but it may be worth considering since perhaps the language-specific versions should assume intermediate-or-so level knowledge in that language, not sure. A change I made in match3_fallingtiles.py may not be functionally correct, the function changed would probably benefit from a rewrite for Python due to the differences from how for loops work in C++, although maybe I should be using the Psuedocode version as a reference instead. Related, made a minor change in timer.cpp for more concise code since I was using it as a reference for type hinting, not focusing on C++ as of now though. When adding type hints for vectors, I chose to use Vector2 instead of Vector2D like in the C++ listings because that seems more reasonable based on its usage, and the Python snippets are based on Pygame so it may help for familiarity. From what I can tell, Vector2 is more common in game engines than Vector2D anyways.

I planned on going through the rest of the folders and doing a few more goes through the Python snippets to ideally make them fairly idiomatic Python code before noticing you got started on them, decided to submit what I've got now and see until further notice. My changes could benefit from future revisions, but it's a start I suppose.

Context and motivation

I saw theopen issue for Python type hints (#69) and recognized that I could help (5+ years experience in Python). When completed and revised, my changes will hopefully make the book more helpful for Python users, considering they'd be closer to functioning, usable Python code. Additionally, some changes I made help follow the code cleanliness rules for PEP8, which can help setup good habits for those learning and require less refactoring time for experienced developers.

Type of changes:

  • Added new chapters
  • Fixed mistakes
  • Filled placeholders

Checklist

  • My contribution follows the contribution guidelines
  • I added myself in the contributors section (or I am already present)
  • I consent that my contribution will be distributed under the CC BY-NC-SA License

Copy link
Member

@Penaz91 Penaz91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution, hope you don't mind me asking a few more things to fix! (I'll add more context to this review in the next comment)

@Penaz91
Copy link
Member

Penaz91 commented Jul 14, 2024

Hello! First of all, thanks for taking the time to help this project. I was thinking about making the Python edition more "Pythonic" (by applying PEP8 more strictly and adding Type hints) so you are saving me a ton of work!

I fixed some typos, changed variable and function names to match PEP8, and added type hints in Python. Got through about 9.5 out of 14 folders in the Python listing, so about 2/3rds done. Didn't realize until after starting that I cloned the master branch and some type hints were being added in develop, but I didn't reach computer science, collision detection, algorithms, or AI so I don't think we changed any of the same files. More changes could be made for code cleanliness in function and variable names, but I decided not to focus on that for now, can always go through the files again later to do another sweep. I did do some slight variable name changes for code cleanliness/readability (i.e player.on_ground -> player.is_on_ground), but mostly making Python-specific adjustments.

That is great! Feel free to pull from the develop branch so that you can get my edits too, saving you a bit of effort when using type hints.

Speaking of which, I noticed that the abc module could be used for implementing abstract classes in quite a few design patterns. I didn't add that because it could raise concerns about the code being understandable for beginners, but it may be worth considering since perhaps the language-specific versions should assume intermediate-or-so level knowledge in that language, not sure.

ABCs are a bit of a problematic area: they are more of a way to register a certain class as one that abides by a certain "informal protocol" (like "Hashable", which means it contains the __hash__ method, or "Sized", which means it contains the __len__ method.) so it may not really be the correct way to declare an "Abstract Class" in the style of C++.

A change I made in match3_fallingtiles.py may not be functionally correct, the function changed would probably benefit from a rewrite for Python due to the differences from how for loops work in C++, although maybe I should be using the Psuedocode version as a reference instead

You are probably right, the entire function may benefit from a full rewrite. I'll be cross-checking the editions to see what I can do about it.

I chose to use Vector2 instead of Vector2D like in the C++ listings because that seems more reasonable based on its usage, and the Python snippets are based on Pygame so it may help for familiarity. From what I can tell, Vector2 is more common in game engines than Vector2D anyways.

Absolutely fair, I took inspiration from PyGame myself and maybe, in a far away future, there might be a PyGame edition too (although It's not really a priority, since the "Pygame 4000 book" exists and its cost finances part of the PyGame development itself). I say it's a good idea to switch to Vector2.

I planned on going through the rest of the folders and doing a few more goes through the Python snippets to ideally make them fairly idiomatic Python code before noticing you got started on them, decided to submit what I've got now and see until further notice. My changes could benefit from future revisions, but it's a start I suppose.

This is more than just "a start", if you're okay with working more on this pull request, I'll do my best to assist you every step of the way, until this is merged.

Thank you again for taking the time to contribute!

@Doublestuf
Copy link
Contributor Author

Working on it 👍. The last few folders have a lot more files, took notes as I made changes and ended up with 4 pages for today, will do a write-up soon enough. Glad to have this as my first open-source contribution.

Copy link
Member

@Penaz91 Penaz91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job so far! (Don't mind my nitpicking)

To me it seems that you're doing far more than just "Improving Python listings" since you've been giving a glance-over to other languages too. Feel free to write "Improved code listings" inside CONTRIBUTORS.md.

Again, thank you for your contribution!

dynamic_listings/C++/default/containers/fsm_usage.cpp Outdated Show resolved Hide resolved
dynamic_listings/python/default/collisiondetection/AABB.py Outdated Show resolved Hide resolved
colliding_items: list[tuple[Circle]] = []

for A in items:
for B in items:
if A is not B:
if A != B:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure about this edit. If the class Circle had an __eq__ operator that uses center and radius to compare two circles, two different circles with the same radius and center would show as "not colliding"; while the is operator will skip the check if the two pointers (A and B) point to the same object (thus two identical circles would collide, as long as they are two different objects).

Comment on lines +16 to +17
self.center = center
self.radius = radius
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing/Removed Type hints

Comment on lines +16 to +17
self.center = center
self.radius = radius
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing/Removed Type Hints

nodeptr = self.nodeList
while nodeptr:
counter: int = 0
node = self.node_list[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing type hint

class FSM:
"""
This class defines a Finite State Machine
The currently active state is represented by a function
pointer
"""

current_state = None
current_state: Callable = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incomplete type hint, from the top of my brain it should be something akin to Callable[[float], None], but maybe it's still valid.


def setState(self, f):
def set_state(self, f: Callable) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incomplete type hint, probably Callable[[float], None]

active = False
time: float = 0
set_time: float = 0
function_to_execute: Callable = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly incomplete type hint (Probably Callable[[], None)

Comment on lines +41 to +44
is_colliding = (A.corner.x < B.corner.x + B.width) and\
(A.corner.x + A.width > B.corner.x) and\
(A.corner.y < B.corner.y + B.height) and\
(A.corner.y + A.height > A.corner.y):
return True
return False
(A.corner.y + A.height > A.corner.y)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can omit backslashes in parentheses, it should look cleaner. Good call on stuffing the complex statement in a variable.

@Penaz91
Copy link
Member

Penaz91 commented Jul 28, 2024

Hello, I wanted to ask if you are going to add further changes to this merge request. Thank you!

@Doublestuf
Copy link
Contributor Author

I think that'll be all of my changes for now. There are probably a few more type hints that could be added across all of the Python listings, but most of them should be there now.

@Penaz91
Copy link
Member

Penaz91 commented Jul 29, 2024

I think that'll be all of my changes for now. There are probably a few more type hints that could be added across all of the Python listings, but most of them should be there now.

Thank you, I'll merge this probably this evening or tomorrow evening and iron out some things here and there. Your contribution has shown me that there are a lot of things that need to be double-checked in the project.

Thank you again for your contribution!

@Penaz91 Penaz91 merged commit c018579 into 2DGD-F0TH:develop Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants