readability

How can I clean up these print statements?

How can I clean up these print statements? Question: So, I’m playing with .csv files learning how to read and present the info. I’m printing the results to the terminal, but as I print more content, I have a wall of print statments that just keeps getting longer and longer. Is there any way to …

Total answers: 3

Scipy Sparse Row/Column Dot Products

Scipy Sparse Row/Column Dot Products Question: What is the readable and efficient way to compute the dot product between two columns or rows of a sparse matrix using scipy? Let’s say that we want to take the dot product of two vectors x and y, two columns of sparse matrix A, then I’m currently doing: …

Total answers: 2

Python: if not val, vs if val is None

Python: if not val, vs if val is None Question: I’ve always coded in the style of if not value, however, a few guides have brought to my attention that while this style works, it seems to have 2 potential problems: It’s not completely readable; if value is None is surely more understandable. This can …

Total answers: 5

`if key in dict` vs. `try/except` – which is more readable idiom?

`if key in dict` vs. `try/except` – which is more readable idiom? Question: I have a question about idioms and readability, and there seems to be a clash of Python philosophies for this particular case: I want to build dictionary A from dictionary B. If a specific key does not exist in B, then do …

Total answers: 11

Generic Exception Handling in Python the "Right Way"

Generic Exception Handling in Python the "Right Way" Question: Sometimes I find myself in the situation where I want to execute several sequential commands like such: try: foo(a, b) except Exception, e: baz(e) try: bar(c, d) except Exception, e: baz(e) … This same pattern occurs when exceptions simply need to be ignored. This feels redundant …

Total answers: 6