-
Notifications
You must be signed in to change notification settings - Fork 16
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
Conversation
…PEP8, and added type hints. About 2/3rds done, will need future revisions.
added microStudio to game engine list
There was a problem hiding this 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)
dynamic_listings/python/default/developing_mechanics/match3_fallingtiles.py
Outdated
Show resolved
Hide resolved
dynamic_listings/python/default/containers/particle_emitter_complex.py
Outdated
Show resolved
Hide resolved
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!
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.
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
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.
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.
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! |
…and half of collision detection
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. |
There was a problem hiding this 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!
colliding_items: list[tuple[Circle]] = [] | ||
|
||
for A in items: | ||
for B in items: | ||
if A is not B: | ||
if A != B: |
There was a problem hiding this comment.
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).
self.center = center | ||
self.radius = radius |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing/Removed Type hints
self.center = center | ||
self.radius = radius |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
)
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) |
There was a problem hiding this comment.
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.
Hello, I wanted to ask if you are going to add further changes to this merge request. Thank you! |
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! |
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 inmatch3_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 intimer.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 useVector2
instead ofVector2D
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 thanVector2D
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:
Checklist