How to strip all whitespace from string

Taking advantage of str.split’s behavior with no sep parameter: If you just want to remove spaces instead of all whitespace: Premature optimization Even though efficiency isn’t the primary goal—writing clear code is—here are some initial timings: Note the regex is cached, so it’s not as slow as you’d imagine. Compiling it beforehand helps some, but … Read more

“python” not recognized as a command

You need to add the python executable path to your Window’s PATH variable. From the desktop, right-click My Computer and click Properties. In the System Properties window, click on the Advanced tab. In the Advanced section, click the Environment Variables button. Highlight the Path variable in the Systems Variable section and click the Edit button. … Read more

Is there a way to specify which pytest tests to run from a file?

You can use -k option to run test cases with different patterns: This will run test cases with name test_001 and test_some_other_test deselecting the rest of the test cases. Note: This will select any test case starting with test_001 or test_some_other_test. For example, if you have test case test_0012 it will also be selected.

What does the “at” (@) symbol do in Python?

An @ symbol at the beginning of a line is used for class, function and method decorators. Read more here: PEP 318: Decorators Python Decorators The most common Python decorators you’ll run into are: @property @classmethod @staticmethod If you see an @ in the middle of a line, that’s a different thing, matrix multiplication. See this answer showing the use of @ as … Read more

How to have a set of sets in Python?

Use frozenset, To represent a set of sets, the inner sets must be frozenset objects for the reason that the elements of a set must be hashable (all of Python’s immutable built-in objects are hashable). frozenset is immutable and set is mutable.