Skip to main content
Share Your Experience: Take the 2024 Developer Survey

New answers tagged

0 votes

Simple size difference measuring tool

Instead of manually writing a CLI with one mandatory argument and one optional argument, it's pretty easy to require one reference mandatory argument and zero or more arguments to compare. Don't use <...
Reinderien's user avatar
  • 62.2k
2 votes

Benchmark for loop and deque/map/methodcaller

With regards to your benchmark that uses two different methods to invoke a method on each element of an iterable of objects, then note that: In with_for_loop you ...
Booboo's user avatar
  • 1,656
5 votes

Simple size difference measuring tool

modern annotation We used to have to do this: from typing import List But in a modern interpreter like 3.11 we prefer to just say ...
J_H's user avatar
  • 28.1k
1 vote

Array Math to Calculate Ice Thickness -- Need Help Simplifying

Thank you for citing your reference. That \$hs\$ snow depth figure seems like quite the wildcard, since snow can be wet or it can be fluffy. I wonder if some folks normalize for that by taking a core ...
J_H's user avatar
  • 28.1k
1 vote

A game where the player guesses the computer's number

Style typing_speed is a constant (that is, a variable that is set and you never plan to change the value of while running the code), so most style guides recommend ...
Arcanist Lupus's user avatar
3 votes

Python secrets command-line tool

print(secrets.token_bytes(int(args[i+1]))) doesn't particularly work, or at least not in a form I'd expect. It's possible for a script to want to call this and ...
Reinderien's user avatar
  • 62.2k
3 votes

Python secrets command-line tool

Unreachable code I think the following condition can never be true: if len(sys.argv) == 0: argv will always have a length ...
toolic's user avatar
  • 3,556
10 votes
Accepted

Python secrets command-line tool

UX for the CLI some commands you can run with this tool: sct -tokenhex 8, ... That's nice. But wouldn't you like to also expose ...
J_H's user avatar
  • 28.1k
1 vote

Mapping pandas' Series to dataclasses

This looks great, exactly what I need, I'm stealing it. :) I'm using this in a base class PandasClass, with the functions that create ...
Pedro Feijão's user avatar
6 votes

A simple Python class designed to facilitate investment portfolio analysis

You've mixed degrees of freedom by computing some of the stats with Pandas (\$N-1\$) and others with NumPy (\$N\$): pandas.Series.std (default ...
tdy's user avatar
  • 1,420
9 votes

A simple Python class designed to facilitate investment portfolio analysis

time representation class Portfolio: ... start_date: str end_date: str I'm sad those aren't datetimes, or at least dates. Then we get validation (no ...
J_H's user avatar
  • 28.1k
1 vote

Performance Tuning to enable answer for Project Euler #566 "Cake Icing Puzzle"

your code for definition needs a lot of cycles (this makes it slow); to make it faster you need to use more efficient formulas or a different language like c or c++. If you want to use python, I ...
AsrtoMichi's user avatar
2 votes
Accepted

Clique Connect: minimum spanning tree

Your code with the example dataset has only 645 calls (that's very fast). Anyway, I improved it and added some comment. I don't think any particular gimmick are needed, such as using JIT. There are ...
AsrtoMichi's user avatar
3 votes

Pytorch code running slow for Deep Q learning (Reinforcement Learning)

What I can do for you and give you some general suggestions: Use library like Nuba or similar; try Pypy is a JIT compiler; if is possible use C or C++ modules. and here the code with some ...
AsrtoMichi's user avatar
3 votes

Connecting MongoDB using Motor in FastAPI

I am quite unfamiliar with Mongodb and best practices so I won't delve too much in the specifics. To enhance what @J_H has written: A close method is typically run ...
Kate's user avatar
  • 6,838
4 votes

Connecting MongoDB using Motor in FastAPI

@classmethod In connect() and close(), it's weird to see you assigning these: Database._client Database._db Prefer the ...
J_H's user avatar
  • 28.1k
1 vote
Accepted

A game where the player guesses the computer's number

Possible bug When I run the code, and it executes this line: slow_print('You\'ll have ', str(guesses - 1), ' guesses to get it.') the code exits with an error. My ...
toolic's user avatar
  • 3,556
4 votes

API Generator for Python

working on my titulation project Mi lengua materna es el ingles, but I'm afraid I don't know what the verb "to titulate" is all about. Nonetheless, I welcome you with open arms to our ...
J_H's user avatar
  • 28.1k
3 votes

Python - streaming file

A small optimization you could do is to use session and you can expect a performance boost from that. You can also shorten the code a little bit by setting auth at session level: ...
Kate's user avatar
  • 6,838
3 votes

Python - streaming file

This isn't "streaming". Each gigabyte input file will fill up a gigabyte of RAM before even a single byte of it is sent to the destination server. With type annotation, the signature would ...
J_H's user avatar
  • 28.1k
3 votes

Python - Text-based game about an escape room

The answer provided by @toolic is already very good. I would add two comments. Cyclomatic code complexity In your main while loop you have a lot of nested ...
Attack68's user avatar
  • 351
3 votes

Can I make my percolation in python function more efficient?

One idea is to split the square into a grid[][] of smaller 2x2 squares. Then, for each of these squares store all circles whose center is in that square. This means that when you insert a new circle ...
Dimitris's user avatar
  • 131
5 votes
Accepted

Can I make my percolation in python function more efficient?

Analytical problem This is a an analytical problem and it may be best to solve it first before coding to have an idea how best to structure your code. If I understand you correctly: each disc has ...
Attack68's user avatar
  • 351
2 votes

Python - Text-based game about an escape room

File name This line implies that you named a file classes.py: from classes import * The file name is too generic. Give it a ...
toolic's user avatar
  • 3,556
4 votes

Bug Byte puzzle from Jane Street

I solved this problem here, using python-constraint. I modelled it as a set of linear equations with inequality constraints. The code is ChatGPT generated and I adapted the constraints. ...
Himmelspiegel's user avatar
1 vote
Accepted

FastAPI not full utilization

"todo with undo" Ok, I just kind of love that! modern type annotations ...
J_H's user avatar
  • 28.1k
7 votes

How do I speed up this simulation program

This was very difficult for a quantitative analyst to examine because the separation between variables and constants was not distinct. It has already been observed in another answer that your naming ...
Attack68's user avatar
  • 351
4 votes

How do I speed up this simulation program

I'm not an expert but let me raise very common tips that make a calculation faster and that you might be already aware of. Decreasing the number of function calls You call ...
atommy's user avatar
  • 41
3 votes

List all classes in a package directory

The limitations of using the __subclasses()__ approach are: You need to load the classes (i.e. have them imported), otherwise it will not work. If you have ...
theQuestionMan's user avatar
3 votes

How do I speed up this simulation program

...
greybeard's user avatar
  • 6,456
2 votes

How do I speed up this simulation program

I am not very qualified to comment on your algorithm, so this is naive review from the POV of developer, but here is some generic advice that can be used in any situation where you want to optimize ...
Kate's user avatar
  • 6,838
3 votes

How do I speed up this simulation program

physical constants Thank you for using MKS, it's all very clear. self.e = 1.6e-19 self.me = 9.1e-31 Number of coulombs is correct to three decimal ...
J_H's user avatar
  • 28.1k
4 votes

Bug Byte puzzle from Jane Street

This problem can be solved optimally in pseudo-polynomial time. This can be done by using k-sum. For every edge that has two connected M nodes we can compute k-sum on both vertices then take the ...
ToeStone's user avatar
2 votes

Multithreaded sparse linear solver in Python

What I can do for you and give you suggestions Use library like Nuba or similar; try to use superlu_dist, uses optimize algorithms (install it using conda); try Pypy is a JIT compiler; prefer if is ...
AsrtoMichi's user avatar
3 votes
Accepted

Validating a web crawlers page visits with a decorator

assert There is misuse of assert. As eloquently said elsewhere: Asserts should be used to test conditions that should never happen. The purpose is to crash early ...
Kate's user avatar
  • 6,838
3 votes

Validating a web crawlers page visits with a decorator

name mangling def login(self): splash_page = self.__visit_splash_page() Not sure why you requested _ ...
J_H's user avatar
  • 28.1k
4 votes

Semi-generic data loading utility class

single responsibility The review context only says "file 1", but hopefully we're using an instructive name like data_loader.py. I do thank you for the ...
J_H's user avatar
  • 28.1k
8 votes
Accepted

Bug Byte puzzle from Jane Street

This submission would benefit from the addition of profiling measurements. mutable default This signature has well-defined semantics. But please don't do it. Human maintenance engineers in future will ...
J_H's user avatar
  • 28.1k
2 votes

samba automounting script written in Python

One additional thing that hasn't been mentioned: error handling. First, it is not a good practise to catch generic Exception. There are use cases for this (for ...
MrBean Bremen's user avatar
2 votes

samba automounting script written in Python

Superfluous comments: create_credentials_folder() is a good function name. Preceding it with this: ...
Harith's user avatar
  • 7,334
5 votes

code format and steps web scraping using beautiful soup

I would like to see a few things generally to make this code easier to work with in future. Rather than have main do all the work I would have ...
El Ronnoco's user avatar
6 votes

Guess the number game with an easy and hard mode

meaningful identifiers You wrote: def is_number(technical_number): ... def is_number_repetition(): ... def more(): ... def more_user(): ... You elected not to ...
J_H's user avatar
  • 28.1k
5 votes

code format and steps web scraping using beautiful soup

Since your df is just using a default range index, I suggest ignoring the index column with index=False when saving: ...
tdy's user avatar
  • 1,420
7 votes

code format and steps web scraping using beautiful soup

I agree here is little to review. Two tips though: use requests.Session() and always check the status code. If it's not 200, the request has not succeeded. You should not proceed with parsing. In the ...
Kate's user avatar
  • 6,838
1 vote

Using odd prime powers, my heuristic tries to solve instances of Exact 3 Cover in polytime, what can be done to optimize it?

Using sub-lists isn't as efficient as using subsets. So this optimized part of the code will use subsets to achieve the goal of ensuring input is valid, and that it follows the combinatorial rules I ...
The T's user avatar
  • 451
2 votes

Using odd prime powers, my heuristic tries to solve instances of Exact 3 Cover in polytime, what can be done to optimize it?

This submission suffers from a lack of clarity. It definitely contains code, but the motivation for running it and the manner in which it would be called is unclear. top level vs. function This code ...
J_H's user avatar
  • 28.1k
0 votes

Pairs with Specific Difference python

I would try to optimize by iterating through the list while adding to the set at the same time rather than building a set separately. See my answer below: ...
Aaron Nickovich's user avatar
4 votes
Accepted

Collatz conjecture plots (python)

Since there is only 1 argument ... what I cannot understand is why it doesn't put collatz_values on the x axis. That's just how matplotlib implemented it. If you ...
tdy's user avatar
  • 1,420
6 votes
Accepted

Hackerrank "New Year chaos" solution - permute sequence by swapping adjacent terms

Algorithm aside, it's easier to reason through the code if you: Use enumerate(q, 1) since the stickers are 1-indexed (to avoid the +1's) Use semantic variables <...
tdy's user avatar
  • 1,420
0 votes

Proper way to pass an infromation/variable across QTabWidgets?

Building on the answer by S.Nick, I would suggest something along the lines of: ...
Antony Hatchkins's user avatar

Top 50 recent answers are included