Links to books, repositories, blogs and videos about useful Python resources
-
Single list from a list of lists; example and expected output:
nested_list = [['some'], ['items']] flat_list = ['some', 'items']
- List comprehension
flat_list = [item for sublst in nested_list for item in sublst]
- Chaining
import itertools flat_list = list(itertools.chain.from_iterable(nested_list)) # equivalently: flat_list = list(itertools.chain(*nested_list))
- Monoid
flat_list = sum(nested_list, [])
- List comprehension
-
-
As of Python 3.8, positional-only arguments are supported. These arguments cannot by passed as keywords. The forward slash, /, indicates to Python that all arguments before the forward-slash are positional-only arguments. Anything following the forward slash are positional or keyword arguments up to the *. The asterisk indicates that everything following it are keyword-only arguments. For instance, calling the function
def positional(name, age, /, a, b, *, key): pass
Here are some invalid calls to this function:
>>> positional(name='Mike') TypeError: positional() got some positional-only arguments passed as keyword arguments: 'name' >>> positional('Mike', 17, 2, b=3) TypeError: positional() missing 1 required keyword-only argument: 'key' >>> positional('Mike', 17, 2, 3, 'test') TypeError: positional() takes 4 positional arguments but 5 were given >>> positional('Mike', 17, 2, b=3, keyword='test') TypeError: positional() got an unexpected keyword argument 'keyword'
Some valid function calls are
positional('Mike', 17, 2, 3, key='test') positional('Mike', 17, 2, b=3, key='test')
A major design priciple behind Python was that code should be easily read. Conventions greatly improve the readability of your code, making it ''Pythonic''.
The directory structure of a Python project should be created so that it is easily testable, deployable and put under continuous integration. Some links I found useful:
- https://github.com/yngvem/python-project-structure
- https://www.kennethreitz.org/essays/repository-structure-and-python
- https://hynek.me/articles/testing-packaging/
- NumPy version
or from the command line:
import numpy print numpy.__version__
python -c 'import numpy; print(numpy.__version__)'
- Print information about the CPU
import numpy.distutils.cpuinfo.cpu cpu.info()
- Figure out which BLAS and LAPACK implementations are used by your NumPy (and therefore SciPy, etc.)
Here is the list of all options for
import numpy.distutils.system_info get_info('blas_opt') get_info('lapack_opt')
get_info
.
Browse the extensive gallery for examples, read the User's Guide, or check the tutorials. Two types of plotting:
- pyplot: high-level MATLAB-like plotting for simple interactive plotting
- object-oriented API
Links to APIs I often use:
If you install Pylint, it comes with Pyreverse. Type
pyreverse -o <output_format> <module_name>
to the terminal to save the class hierarchy fetched from the module <module_name>
in <output_format>
(png
, svg
, eps
, pdf
, dot
, etc.). Note that module_name
must be importable. If module_name
is a package that contains other packages or modules, all the contents of the package will be parsed. To select a subpackage or a module, use the slash; e.g. to process only f2py from numpy, use
pyreverse -o png numpy/f2py
It is often useful for the end user to ignore the tests. You can ignore a directory by giving the --ignore
flag. In the example above:
pyreverse -o png --ignore tests numpy/f2py
For further options, check the documentation:
pyreverse --help
-
view pandas DataFrame objects
PandasDataFrameGUI supports useful data analysis (filtering, sorting, plotting, etc.) and has few dependencies. A common mistake is that your code already imported matplotlib, which selected a backend other than
'WXAgg'
. The solution is to load matplotlib before you use your own code, withimport matplotlib matplotlib.use('WXAgg')
as shown here.
- Generate custom badges: the link you need to insert comes from MyBinder.