3 ms·
I love your enthusiasm. I agree with the points about functional programming in Python though. Help me fix this line: https://github.com/kurocha/tagged-format/
by ioquatix 9y ago
I love your enthusiasm. I agree with the points about functional programming in Python though.
Help me fix this line: https://github.com/kurocha/tagged-format/blob/283d9bb1c917684724ad5d7e92f3609e33c155bc/exporters/Blender/io_tagged_format.py#L152 https://github.com/kurocha/tagged-format/blob/283d9bb1c91768...
In Ruby it would be
output.puts "\t\t#{vertex.join(' ')}"
- qznc 9y agoWhat to fix about it? Looks good to me. You could use f-strings (introduced in Python 3.6) to get rid of the `.format(`. https://www.python.org/dev/peps/pep-0498/ https://www.python.org/dev/peps/pep-0498/ The `[str(value) for value in flatten(vertex)]` part is missing in your Ruby. I assume that happens in some method of the vertex? You could make your vertex iterable as well. Python philosophy prevents you from getting rid of the `str(x) for x in v` because "explicit is better than implicit", so join will not auto-convert stuff into strings.
- throwaway287391 9y agoSince you like map, you could shorten it a bit by doing map(str, flatten(vertex)) instead of the list comprehension.
- cup-of-tea 9y agoOnly suggestion I have is not to build a list explicitly: .join(str(value) for ...)
- chronial 9y agoMy first suggestion would be to add a line break after format( , remove the [ ] and rename the loop variable to a single letter. output.write("\t\t{0}\n".format( " ".join(str(x) for x in flatten(vertex)))) But it looks like your core problem is that try you force another language's idioms onto python. Your flatten function could be a lot simpler (and without quadratic runtime): import itertools def flat(l): return itertools.chain.from_iterable( flat(e) if isinstance(e, (list, tuple)) else [e] for e in l ) But that is still not what you actually want – you only wrote this function because you have it in ruby. What you actually want is: def format_vertex(lst): return " ".join( format_vertex(e) if isinstance(e, (list, tuple)) else str(e) for e in lst ) output.write("\t\t{}\n".format(format_vertex(vertex))