Posts

Showing posts with the label coding-style

Python loops vs comprehension lists vs map for side effects (i.e. not using return values)

Python loops vs comprehension lists vs map for side effects (i.e. not using return values) TL;DR Which is the best? 1.- [r.update(r.pop('some_key')) for r in res if r.get('some_key')] 2.- map(lambda r: r.update(r.pop('some_key') if r.get('some_key') else ), res) 3.- map(lambda r: r.update(r.pop('some_key')), filter(lambda r: r.get('some_key'), res)) 4.- for r in res: if r.get('some_key'): for element in r['some_key']: r[element] = r['some_key'][element] del r['some_key'] 5.- Insert your own approach here Note : This is not production code. It is code that is run in a test suite, so I am more concerned about legibility/maintainability than performance. Nevertheless I would also like to know if the decision regarding which is better (accounting the tradeoff performance/legibility) would change if this was production code. The number of elements 'some_key...

How to improve code and reduce number of code?

How to improve code and reduce number of code? That code is exactly the same, so I want to refactor it in a simple way to reduce the number of lines. One thing which is different is min/max function execution. Is it available pointer to function in python to call min/max as a pointer to function like in C? def calculate_min(a, b, c, d, e, f): try: v = a[e][f] b[d] = v if np.isnan(b[d]) else min(b[d], v) #min() except KeyError as exc: logger.error("keyerror") def calculate_max(a, b, c, d, e, f): try: v = a[e][f] b[d] = v if np.isnan(b[d]) else max(b[d], v) #max() except KeyError as exc: logger.error("keyerror") Totally unrelated but you should use logger.exception() instead of logger.error() => this will add the full error message and traceback to the log, which is very useful for debugging. – bruno desthuilliers Jun 29 at 9:06 ...