Skip to content

Learning Pieces

Writing technique

Prefer to use parallel structure in your writing.

Parallel:

Mary likes hiking, swimming, and bicycling

Non-parallel

Mary likes hiking, swimming and to ride a bike.

Python buildin http server

It is possible to serve static pages by simply running a command similar to this one:

py -m http.server -d docs/_build/ 9876

Creating a fixture with secret key in pytest

In the decelopment of collins api dictionary package, I wanted to create some tests to test the client api. In order to test properly, I need to pass the entrypoint and also the secret key to make a request. These parameters should not be hardcoded and should be passed during the test incarnation.

I came up with a solution that is pretty neat with pytest. First you are going to instruct pytest to accept some extra options during its incarnation. You do that in a file called conftest.py. pytest recognizes this file as having configurations for pytest.

def pytest_addoption(parser)
    parser.addoption("secret_key", required=True, action="store")

Now we can call pytest like this

pytest --secret-key <MY_SECRET_KEY>

Or with a proper configuration in tox.ini:

tox -- --secret-key <MY_SECRET_KEY>

To recover the options in your test, you can create specialized fixtures for them:

@pytest.fixture(scope="session")
def secret_key(pytestconfig):
    return pytestconfig.getoption("secret_key")

I had to remove stuff from ~/.cache in order to update a package that I was installing with a URI to the sdist. (collins_dictionary_api_client). When running tox, I was systematically getting the old version. After remove the entries in the .cache folder I succeeded.

All problems were caused by __init_.py

I was facing several issues with the instructions provided by the book.

  • Data files were not being exported to the wheel
  • The import directives to my package were messed up. Sometimes needed the 'src' other times not.
  • The console script entry point was not finding my function.

I realized that I mispelled the __init__.py file. It was missing an underscore. After fixing the mispeeling, everything start worked.

About the building process

Here is the description of the build backend when the command python -m build is typed

A simple, correct PEP 517 build frontend.

By default, a source distribution (sdist) is built from {srcdir}
and a binary distribution (wheel) is built from the sdist.
This is recommended as it will ensure the sdist can be used
to build wheels.

Pass -s/--sdist and/or -w/--wheel to build a specific distribution.
If you do this, the default behavior will be disabled, and all
artifacts will be built from {srcdir} (even if you combine
-w/--wheel with -s/--sdist, the wheel will be built from {srcdir}).

For example, I need to export the .pyx file to the sdist in order to compile them and have it exported to he bdist.

Cool explanations can also be found in the PEP 517 as well.

What happens during a python package installation

All the contents of my package (corresponding to the configuration of MANIFEST.in and setup.cfg) goes to lib/pythonX.YZ/site-packages/<my-package> folder.