40th Week of 2021
Coding⚑
Python⚑
Click⚑
-
New: Invoke other commands from a command.
This is a pattern that is generally discouraged with Click, but possible nonetheless. For this, you can use the
Context.invoke()
orContext.forward()
methods.
Optimization⚑
-
New: Add tips on how to optimize your python command line tools.
-
Minimize the relative import statements on command line tools:
When developing a library, it's common to expose the main objects into the package
__init__.py
under the variable__all__
. The problem with command line programs is that each time you run the command it will load those objects, which can mean an increase of 0.5s or even a second for each command, which is unacceptable. * Don't dynamically install the package:If you install the package with
pip install -e .
you will see an increase on the load time of ~0.2s. It is useful to develop the package, but when you use it, do so from a virtualenv that installs it directly without the-e
flag.
-
Python Snippets⚑
-
New: Check if a dictionary is a subset of another.
If you have two dictionaries
big = {'a': 1, 'b': 2, 'c':3}
andsmall = {'c': 3, 'a': 1}
, and want to check whethersmall
is a subset ofbig
, use the next snippet:>>> small.items() <= big.items() True
-
Correction: Group or sort a list of dictionaries or objects by a specific key.
Improve previous method with the concepts learned from the official docs
Particularly improve the sorting by multiple keys with the next function:
>>> def multisort(xs, specs): for key, reverse in reversed(specs): xs.sort(key=attrgetter(key), reverse=reverse) return xs >>> multisort(list(student_objects), (('grade', True), ('age', False))) [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
Pydantic⚑
-
New: Copy produces copy that modifies the original.
When copying a model, changing the value of an attribute on the copy updates the value of the attribute on the original. This only happens if
deep != True
. To fix it use:model.copy(deep=True)
.
DevOps⚑
Continuous Integration⚑
Flakehell⚑
-
New: Troubleshoot the 'Namespace' object has no attribute 'extended_default_ignore' error.
Add to your
pyproject.toml
:[tool.flakeheaven] extended_default_ignore=[]
Dependency managers⚑
-
New: Sync the virtualenv libraries with the requirements files.
python -m piptools sync requirements.txt requirements-dev.txt
-
Correction: Use
-c
instead of-r
in the nested requirement files.To avoid duplication of version pins.
Operative Systems⚑
Linux⚑
Kitty⚑
-
New: Introduce kitty the terminal emulator.
kitty is a fast, feature-rich, GPU based terminal emulator written in C and Python with nice features for the keyboard driven humans like me.