My current code uses tenary assignments One line if-condition-assignment, but with more verbose identifyers it is easily passing line length limits.
Since I am not yet into pythonic coding, I would be glad to see some refactoring suggestions.
for label in range(num_labels):
d_tresh = drop_treshold[label] if type(drop_treshold) == numpy.ndarray) else drop_treshold
r_tresh1 = relabel_treshold1[label] if type(relabel_treshold1) == numpy.ndarray else relabel_treshold1
r_tresh2 = relabel_treshold2[label] if type(relabel_treshold2) == numpy.ndarray else relabel_treshold2
Using locally short variable names seems part of solution, but I like to have more explanatory function arguments. Hm. So dramatically shortening function argument names results in (for me) unreadable code.
for l in range(n_labels):
t0 = d_t[l] if type(d_t) == numpy.ndarray) else d_t
t1 = r_t1[l] if type(r_t1) == numpy.ndarray else r_t1
t2 = r_t2[l] if type(r_t2) == numpy.ndarray else r_t1
So shall I resort to multi line if - else assignments? It will stretch and bloat the simple logic dramatically.
for label in range(num_labels):
if type(drop_treshold) == numpy.ndarray):
d_tresh = drop_treshold[label]
else
drop_treshold
if type(relabel_treshold1) == numpy.ndarray:
r_tresh1 = relabel_treshold1[label]
else
relabel_treshold1
if type(relabel_treshold2) == numpy.ndarray:
r_tresh2 = relabel_treshold2[label]
else
relabel_treshold2
(Surely I (sh/)could also refactor the whole code around the shown example... This example snippet comes from function with arguments, which could be scalar float/int or 1D numpy.array. If it is an array it will apply each item to each label, just being plain scalar it will apply it globally to all labels) But here again how is the pythonic way? When to start to refactor more exhaustively and when to stay put - because it works?
Aucun commentaire:
Enregistrer un commentaire