New answers tagged python
1
vote
Python pyautogui used for a 2D game
Layout
There should be blank lines between functions.
The black program can be used to automatically
reformat the code for you.
Comments
Remove all commented-out code to reduce clutter:
...
3
votes
Parse path string
Movement conflates at least three different things: tokenising/scanning (part of lexing), evaluating, and application of the path to start coordinates. In other ...
2
votes
Parse path string
Portability
I realize this question was posted many years ago when Python version 2.x
was prevalent, but now that it is deprecated, consider porting to 3.x.
To do so, it is necessary to add ...
4
votes
Find the nearest school to you
CLI
user_input = input("Enter number and name of street/road ")
The input() function has its place.
But consider ...
3
votes
Find the nearest school to you
Layout
Move the function to the top after the import lines.
Having it in the middle of the code interrupts the natural
flow of the code (from a human readability ...
3
votes
Hackerearth: Counting numbers in an array that are divisible by any 2 numbers
Always assume that coding challenge websites will give you worst case scenarios to test your code. They won't test your code with small arrays of small numbers. They'll give you large arrays with huge ...
1
vote
Hackerearth: Counting numbers in an array that are divisible by any 2 numbers
Portability
I realize this question was posted many years ago when Python version 2.x
was prevalent, but now that it is deprecated, consider porting to 3.x.
To do so, it is necessary to add ...
2
votes
Beginner python program to get data from string
My review will focus on the parsing logic.
months_of_year and days_of_week should be deleted. ...
2
votes
4
votes
Transform dataset coordinates from Lambert Conformal Conic projection to WGS84
size of significand
has high memory demand
I'm only seeing 1.5 MiB for each of the pair of grids;
sys.getsizeof(lon_grid) reports 1_473_416 bytes.
You don't have ...
4
votes
Transform dataset coordinates from Lambert Conformal Conic projection to WGS84
Here are some general coding style suggestions.
Naming
The PEP 8 style guide recommends
using all capital letters for constants. For example:
dx, dy = 0.1, 0.1
...
3
votes
Guess a Number Game
Observations:
PEP 8 suggests using snake case for function names. A name like GuessNum is appropriate for a class, but a function should be something like ...
4
votes
News API in Python - Take II
Session
This may not a big deal since you are using requests at only one place, but using a session has some benefits. You can set all your headers at session level ...
4
votes
Accepted
News API in Python - Take II
Reinventing the wheel
The following loop simply reinvents next.
...
5
votes
News API in Python - Take II
Type hints
I like the usage of type hints for your functions. However, the return hint
looks wrong for:
def clean_query(text: str) -> str:
The function always ...
4
votes
News API in Python
Just a high-level comment: A large part of the code consists of rule-based parsing of text (all the string-replace calls). This is extremely brittle (there are lots of cases that you won't cover) and ...
13
votes
Accepted
5
votes
News API in Python
single responsibility
The SRP
says that a function should do one thing well.
The expression that replaces "concerning" and other stop words
should be in a helper function.
For one thing, ...
5
votes
News API in Python
String Replacements and Matching
You should use a case-insensitive regular expression using the re.I flag to do your matching. For example:
...
10
votes
News API in Python
Naming
NewsConcerning is styled in the way PEP 8 would tell us to style a class name. As function it should be in snake case: ...
3
votes
Accepted
3
votes
Create Frame Widget with Pygame
I have just read the code and made no attempt to run it, so this is a high level overview but some things are visually obvious.
DRY (do not repeat yourself)
In function ...
7
votes
Substitute patterns with values from lists
I would prefer to take Iterable[tuple[str, Iterable[str]]] rather than dict[str, list[str]] for two reasons:
...
3
votes
Substitute patterns with values from lists
I'm not comfortable with that mutating pop, even if it is on a copy. Also, why not make the substitution state reusable? In the following style, subsequent calls on the same object will resume ...
4
votes
Substitute patterns with values from lists
Given that the problem states enough replacement values must be provided to the function, it would seem reasonable to catch that IndexError and raise a ...
6
votes
Substitute patterns with values from lists
Tests
It's nice to see that you have included various tests to verify that replace_in_string works as expected.
Docstring
Its great that you have provided a ...
7
votes
ASCII-based Lightweight Browser with Clippy-Style Helper in Python (Updated)
I will try not to repeat what has already been said:
Syntax Error
...
4
votes
ASCII-based Lightweight Browser with Clippy-Style Helper in Python (Updated)
Comments
This comment is cryptic:
# gethtmlwithjs
Comments should be used to explain the code, not make it harder to understand.
It should be deleted or replaced ...
7
votes
Accepted
ASCII-based Lightweight Browser with Clippy-Style Helper in Python (Updated)
Here's a few of the things I noticed while reading through
DRY (Don't Repeat Yourself)
The following repeats itself multiple times, and like toolic stated parts of it repeat as well
...
3
votes
ASCII-based Lightweight Browser with Clippy-Style Helper in Python
Layout
There are many really long lines. The black program can be used to automatically
reformat the code with more reasonable line lengths.
Tools
You could run code development
tools
to ...
2
votes
ASCII-based Lightweight Browser with Clippy-Style Helper in Python
At first glance:
I see a lot of global variables.
main is too long and complex.
Statements like the following can just be expressions.
...
4
votes
Optimizing real-time ASCII playback in Python when each frame is fetched as a PNG via GET
Portability
The clear_screen function is terrific. You partitioned the code
into a function which also supports multiple operating systems.
And you clearly ...
3
votes
Optimizing real-time ASCII playback in Python when each frame is fetched as a PNG via GET
Actually, let me put this as an answer. The fastest code is code you don't write at all. So why don't convert the PNG's once and host them as text on your server? Then the client is much simpler and ...
8
votes
Accepted
Optimizing real-time ASCII playback in Python when each frame is fetched as a PNG via GET
I first tried to fetch an image via my browser just to see what I would be displaying, but I am currently getting redirected. I will certainly try again later and I may have more to say when I am able ...
4
votes
Accepted
Unicode-based platformer game in Python (Updated)
Layout
There is never a reason to have a long comment line:
...
5
votes
Unicode-based platformer game in Python (Updated)
I'm going to look at some of the points I made in my previous review and let's see how you've done.
Avoid global variables. You use them extensively, but it makes your logic much harder to follow. ...
5
votes
Unicode-based platformer game in Python
Initial Impression
The first thing I did was to try running your program to get a feel for what it is doing. But I was frustrated because, although you posted instructions of a sort in your post, I ...
4
votes
Unicode-based platformer game in Python
Layout
Move the functions to the top after the import lines.
Having them in the middle of the code interrupts the natural
flow of the code (from a human readability ...
6
votes
Accepted
Unicode-based platformer game in Python
Notes
Avoid global variables. You use them extensively, but it makes your logic much harder to follow. Information should be passed via function arguments, back out via return values, or can be ...
5
votes
List folders with size
PEP8 and coding style
Since you are asking about bad practices, you can make code more PEP8-compliant, for example:
if file_name.endswith(".txt") :
...
4
votes
List folders with size
write_into_file doesn't tell me a whole lot about what the function does. I have to read about two thirds into the method to figure out that it compiles some kind ...
4
votes
List folders with size
Let me add some additional suggestions:
Allow Arguments to write_into_file Be Path or str
...
3
votes
List folders with size
Here are my tips
The encoding line is unnecessary as default is utf8
Similarly when opening a file, utf8 is assumed by default.
Instead of printing error messages, raise exceptions. Leave it to the ...
6
votes
List folders with size
Notes:
The run function is entirely superfluous. Get rid of it.
Comments like # # # # # Functions # # # # # are also extraneous....
4
votes
CLI reading logger in Python
There is a lot of code here and I'm not going to look at all parts equally. I'll write down a few things that stood out to me.
I like your idea of a settings file, although the result looks like a ...
Mast♦
- 13.9k
2
votes
Scraping Forum Tables w/ Links Using Beautiful Soup
When I run the code, I get an error:
table = soup.find_all("table")[0]
~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range
I ...
4
votes
Shortest Cell Path In a given grid
Style
Let's look at this conditional in shortestCellPathHelper.
...
6
votes
LeetCode 3542: Minimum Operations to Convert All Elements to Zero
missing docstring
def split_non_zeros(self, nums):
This is terrible.
The identifier is not bad, as far as it goes.
But we need to know what the function ...
6
votes
Accepted
LeetCode 3542: Minimum Operations to Convert All Elements to Zero
Style
First off, let's address a couple of style issues.
Looping over indices of a list is better achieved with enumerate than with ...
3
votes
LeetCode 3542: Minimum Operations to Convert All Elements to Zero
Simpler
Here are some general coding style suggestions.
The following:
counter = counter + 1
can be simplified using the special assignment operator:
...
Top 50 recent answers are included
Related Tags
python × 15592python-3.x × 5187
performance × 2368
beginner × 2007
python-2.x × 1235
algorithm × 1210
programming-challenge × 1047
numpy × 755
object-oriented × 728
pandas × 599
game × 552
strings × 490
web-scraping × 429
time-limit-exceeded × 398
tkinter × 357
parsing × 324
csv × 304
django × 302
hash-map × 295
random × 253
pygame × 248
recursion × 245
file-system × 238
regex × 234
reinventing-the-wheel × 225