5

Your data structures are a jumble, especially the inner lists. Before worrying about your code, I would encourage you to rethink the data structures. At the outermost layer, you have a dict mapping dates to lists -- so far, that's reasonable. Those outer lists contain inner lists, which consist of a string (eg "Mesure 1") followed by two dicts ...


5

Simplifications re.compile("(nonprod|non-prod)") is just a complicated way to write a badly performing regex for re.compile("non-?prod"), which does the same, but better. extract re.compile("all") into a variable like you did with non_prod_regex, or better yet, do a string comparison: if environment == "all"... ...


5

Don't use bare except statements. Under nearly all conditions, you should specify some kind of Exception; otherwise, even low-level exceptions (like SystemExit or KeyboardInterrupt) will be caught. try: ... elif players_turn == 'q': sys.exit() except Exception: # Just catch garden-variety exceptions, not low-level. ... Global ...


4

Use a collection rather than spawning lots of similarly named variables. Anytime you find yourself creating a bunch of numbered/lettered variables, stop and figure out a way to put that information in a collection. If your data is smart, your code can often be simple. Applying limits when collecting data by type. You appear to want to control the number of ...


4

A data-oriented function should return data or raise an exception, not print. If there are no matching stacks, just return an empty list or raise. Establish a habit of defining regular expressions with raw strings. If you always define regex patterns with raw strings, you never have to waste time thinking about whether your pattern contains things like \n, \...


3

Solid work. One thing you might consider doing is taking all of your program and wrapping it into a main() function. Then, at the bottom of the script, you can call the main() function in order to simplify the look of your code. def main(): pass def turn_generation(): pass main() Note how this simplifies the overall readability and makes it more ...


3

Your code is reasonable. The primary advice I have focuses not on anything particularly wrong with your current implementation but on simpler ways to complete the task using the standard library. The first part of your function assembles the data into triples, and you're on the right track in the sense that you are sorting the data by user ID. But you can ...


3

As other answers already pointed out, you should not compile call re.compile several times, and there is no need to try to filter according to all filters at the same time. I would go even further and suggest to use iterators, which are great tools in Python. It would allow you to write a single function per filter, and apply a bunch of function to each ...


3

According to your code the first optimization that came to my mind is to group your if statements by the variable accounts_ids like the next example if accounts_ids: if non_prod_regex.match(environment): do.... elif re.compile("all").match(environment): do.... else: do... else: ...


2

a down and dirty solution. Itterate the dict and use the key to index the dict. pass that into a function -> compute the return a more complete solution using list compression rather than using append you can nest multiple loops into a single line, index the object and pass that into a function to perform some logic operation. def compute(cval): ...


2

PEP 8 The Style Guide for Python Code recommends snake_case for function names and variable names. findEarliestMonth violates this, but it is a requirement of the assignment, so it gets a free pass. stockPrices however should be stock_prices. You already changed the name from stockPrice, making it plural, demonstrating that while it was a name provided by ...


2

Add a main guard This is a standard idiom: if __name__ == '__main__': n = int(input("Enter size of the square matrix: ")) matrix = [[int(input()) for j in range(n)] for i in range(n)] print("original matrix --> ", matrix) print("rotated matrix --> ", get_rotated_matrix(matrix)) This allows your definitions ...


2

FMc put it well in his commendable job: Your code is reasonable. My main gripe: The code does not document what it is to accomplish. The Style Guide for Python Code cautions Comments that contradict the code are worse than no comments. - to which I'd add where the code is right. (One might with no less justification state code that contradicts correct ...


2

I noted the use of the := operator aka walrus. That means your code requires Python >= 3.8 so consider adding a guard to make sure this requirement is satisfied. Since you are aiming for recent versions of Python you could as well "upgrade" to a data class that will make your code more concise. Thus you could declare your class EMail like this: ...


1

When possible, iterate directly over collections. Most of the time, you don't need indexes when iterating over Python collections. As an example, here's an easier way to initialize an independent copy of the matrix. (Or just use deepcopy). rotated_matrix = [list(row) for row in matrix] Put all code inside of functions or methods. Some will say it's alright ...


1

Here, I shall work with the assumption that the "playing sound" part works fine. Firstly, congratulations! The concept you came up with to resume the execution of the recin function is known as a continuation, and it is available in Python natively through generators and the yield keyword. Rewriting your code to use generators and still polling the ...


1

Your current implementation is reasonable and understandable. There are probably "clever" ways to solve it differently or with fewer lines of code -- I even wasted some time on one of them -- but I'm not sure they are worth it, other than for fun. Some notes on the your implementation. (1) Python has a built-in sum() function, so you don't need to ...


1

I'm not sure I understood the problem you're trying to solve, but instead of writing: class FooJSONMixin: @classmethod def from_json(...) -> 'Foo': ... I wanted to point out that you can do: from __future__ import annotations from typing import TypeVar, Type class BaseModel: ... ModelT = TypeVar("ModelT", bound=BaseModel) ...


Only top voted, non community-wiki answers of a minimum length are eligible