7

As a general rule it is best not to nest comprehensions. It makes code hard to read. You are better off just writing a for loop and appending the results to a list or using a generator. Here are a couple rules with comprehensions that will make your code less brittle and easy for others to work with: Don't nest comprehensions. If a comprehension is too long ...


6

Readability comprehensions have advantages they may be short one liners that are more readable (in the context code) than explicit loops they may be more efficient however - when done wrong they tend to be unreadable and thus unmaintainable. Yours is near unmaintainable. It did take me a little time to identify some of your code is superfluous. Your ...


5

Ignoring the fact that your particular code doesn't actually need a nested loop, in general I would say that nested comprehensions can be fairly readable, but helps a lot to put each for on a line of its own: return [ some_function_of(item, product) for item in order['order']['items'] for product in pricing['prices'] if some_condition_on(item,...


2

There's a lot of good feedback on how to work with the data as it's structured now, but my opinion is that - as soon as humanly possible - you should deserialize it out of a collection of weakly-typed dictionaries and lists, to a set of classes. This will make a handful of things better-structured, more testable and verifiable, and more easily maintainable ...


1

As we do not know the task given we cannot fully review. While the code itself looks reasonable there is some smell. date To me it is very smelly, that a function named id() returns a date start_date = ids('DIR', '348') This is also requiring a switch inside your function, so most probably there are two functions intermingled that should not be. data ...


1

It depends on your needs, but generally I would say that the second approach would be better. Why? because it allows you to run multiple tasks and that gives flexibility to your system. Now the use of synchronized may be troublesome if there are many threads running, if you don't need to handle static or shared resources/variables it would be better to not ...


1

On PowerShell v6+, I would do the following: $String = $Fragment1, $Fragment2 | Where-Object { -not [System.String]::IsNullOrWhiteSpace($_) } | Join-String -Separator $Separator You could use [System.String]::IsNullOrEmpty() instead of [System.String]::IsNullOrWhiteSpace(), but I find [System.String]::IsNullOrWhiteSpace() to be the desired ...


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