Skip to content

Commit 105bb4f

Browse files
committed
Update docs.
1 parent a8573d4 commit 105bb4f

19 files changed

+822
-115
lines changed

README.rst

+73-91
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
******************************************************************************************************************************************
2-
python-dev-tips, `Slides <https://docs.google.com/presentation/d/1BnezhwUy22DiF72wss8GU_YIMfhjortz-uILdIFGuoM/edit?usp=sharing>`__
2+
pydevtips, `Slides <https://docs.google.com/presentation/d/1BnezhwUy22DiF72wss8GU_YIMfhjortz-uILdIFGuoM/edit?usp=sharing>`__
33
******************************************************************************************************************************************
44

55
.. image:: https://readthedocs.org/projects/pydevtips/badge/?version=latest
@@ -11,139 +11,121 @@ python-dev-tips, `Slides <https://docs.google.com/presentation/d/1BnezhwUy22DiF7
1111
:target: https://github.com/ebezzam/python-dev-tips/blob/main/.github/workflows/python.yml
1212
:alt: Unit tests and formatting
1313

14-
Tutorial first given at ENS Ulm (January 2023) on how to develop a Python package. There is a slight focus on scientific computing, but the general principles apply to any Python project.
1514

16-
.. contents:: **Table of Contents**
15+
.. |ss| raw:: html
1716

18-
Creating virtual environment and local install
19-
==============================================
17+
<strike>
2018

21-
With `Anaconda <https://www.anaconda.com/>`__ (recommended).
22-
After installing Anaconda or `Miniconda <https://docs.conda.io/en/latest/miniconda.html>`__ (light version), create a new environment:
19+
.. |se| raw:: html
2320

24-
.. code:: bash
21+
</strike>
2522

26-
# create new environment, press enter to accept
27-
conda create -n project_env python=3.9
2823

29-
# view available environments
30-
conda info --envs
24+
Reproducibility is important for software: *if it's not reproducible,
25+
it's not useful*. Even if you don't plan on sharing your code, imagine
26+
coming back to a project after a few weeks, or having
27+
to install it on a new machine. You'll be all the more thankful to your
28+
past self if you have a clear way to install and run your code.
3129

32-
# activate environment
33-
conda activate project_env
30+
This repository is a collection of tips and tricks for developing stable
31+
and reproducible Python code. There is a slight focus on scientific
32+
computing, but the general principles can apply to most Python projects.
33+
If you're reading this from GitHub, please check out the
34+
`documentation <https://pydevtips.readthedocs.io/en/latest/>`_ for a
35+
more in-depth explanation of the topics covered.
3436

35-
# install package locally
36-
(project_env) pip install -e .
37+
The intended audience is myself (as I often find myself going to past
38+
projects to find how I did something!), but also for students and
39+
anyone who is interested in learning some new tricks or even
40+
sharing their own! I try to follow the principles laid out here on
41+
development and reproducibility, so feel free to point out any lapses
42+
or suggest improvements, either by opening an issue or pull request.
3743

38-
# deactivate environment
39-
(project_env) conda deactivate
44+
As is typical in open source, there are many ways to do the same thing.
45+
But hopefully this gives you a starting point. Feel free to pick and
46+
choose the features that you like. This flexibility is one of the best
47+
(and worst parts) of open source. Some of the things we cover:
4048

49+
* Virtual environments.
50+
* Version control.
51+
* Reproducible examples.
52+
* Documentation.
53+
* Code formatting.
54+
* Unit tests and continuous integration.
55+
* Packaging and distribution.
56+
* Remove development.
4157

42-
For machines really light on memory (e.g. Raspberry Pi) use
43-
`Virtualenv <https://virtualenv.pypa.io/en/latest/>`__:
58+
The accompanying
59+
`slides <https://docs.google.com/presentation/d/1BnezhwUy22DiF72wss8GU_YIMfhjortz-uILdIFGuoM/edit?usp=sharing>`__
60+
are from a tutorial given at ENS Ulm (January 2023).
61+
Feel free to modify and use it for your own purposes.
4462

45-
.. code:: bash
46-
47-
# install library if not already
48-
pip install virtualenv
49-
50-
# create virtual environment (creates folder called project_env)
51-
python3 -m venv project_env
52-
53-
# activate virtual environment
54-
source project_env/bin/activate
55-
56-
# install package locally
57-
(project_env) pip install -e .
58-
59-
# deactivate virtual environment
60-
(project_env) deactivate
63+
.. note::
6164

65+
A good amount of this documentation and code is written with `GitHub
66+
Copilot <https://github.com/features/copilot>`_, which I highly recommend for development. If you don't like
67+
writing documentation, it is a great way to get started as it is able to
68+
understand the functionality of your code and produce meaningful text to describe it.
69+
It should be used be used with caution, |ss| *but it can be a great tool for getting started* |se|
70+
and you often you need to make a few tweaks (*like the previous repetition*).
71+
But it's a huge time-saver!
6272

63-
Code formatting
64-
===============
73+
Installation
74+
============
6575

66-
Through pre-commit hooks:
76+
This "dummy" package can be installed with pip:
6777

6878
.. code:: bash
6979
70-
# inside virtual environment
71-
(project_env) pip install pre-commit
72-
(project_env) pip install black
80+
pip install pydevtips
7381
74-
# Install git hooks
75-
(project_env) pre-commit install
76-
# pre-commit installed at .git/hooks/pre-commit
82+
Or from source, e.g. with Anaconda / Miniconda:
7783

84+
.. code:: bash
7885
79-
Testing
80-
=======
81-
82-
Write tests in the `tests` folder as function that begin with `test_`.
86+
# create new environment, press enter to accept
87+
conda create -n project_env python=3.9
8388
84-
To run tests (install `pytest <https://docs.pytest.org/en/stable/>`__ first if not already done):
89+
# view available environments
90+
conda info --envs
8591
86-
.. code:: bash
92+
# activate environment
93+
conda activate project_env
8794
88-
# inside virtual environment
89-
(project_env) pip install pytest
95+
# install package locally
96+
(project_env) pip install -e .
9097
9198
# run tests
9299
(project_env) pytest
93100
94-
To run a specific test:
95-
96-
.. code:: bash
97-
98-
# inside virtual environment
99-
(project_env) pytest tests/test_fftconvolve.py::test_fft
100-
101-
102-
Releasing new version and deploying to PyPi
103-
===========================================
104-
105-
Uploading to PyPi is done via `twine <https://pypi.org/project/twine/>`__.
101+
# deactivate environment
102+
(project_env) conda deactivate
106103
107-
In the steps below and **after merging to** ``main``, replace "X.X.X" with the appropriate version number.
104+
Examples
105+
========
108106

109-
See `Semantic Versioning <https://semver.org/>`__ for recommendations on picking version numbers.
107+
Examples can be found in the ``examples`` and ``notebooks`` folders.
108+
Scripts from the ``examples`` folder should be run from the root of the
109+
repository, e.g.:
110110

111111
.. code:: bash
112112
113-
# inside virtual environment
114-
(project_env) pip install twine
115-
116-
# edit version in setup
117-
# build package
118-
(project_env) python setup.py sdist bdist_wheel
119-
# -- creates zip in dist folder
120-
121-
# upload to pypi
122-
(project_env) python -m twine upload dist/pydevtips-X.X.X.tar.gz
123-
# -- X.X.X is the version number in setup.py
124-
# -- enter username and password
125-
# -- check https://pypi.org/project/pydevtips/X.X.X/
126-
127-
# new tag on GitHub
128-
git tag -a X.X.X -m "version X.X.X"
129-
git push origin X.X.X
130-
131-
On `GitHub <https://github.com/ebezzam/python-dev-tips/tags>`__ create a new release by:
113+
python examples/real_convolve.py
132114
133-
#. Clicking (the rightmost) "..." dropdown menu.
134-
#. Selecting "Create release".
135-
#. At the bottom pressing "Publish release".
115+
Parameter setting is done with `hydra <https://hydra.cc/>`_. More on that
116+
in the :ref:`Reproducible examples<Reproducible examples>` section of the
117+
documentation.
136118

137119

138120
TODO
139121
====
140122

123+
- writing: packaging
141124
- change documentation links to main branch
142125
- joblib example in profile
143126
- github page
144127
- point out features in scripts: object-oriented, asserts, tqdm, type hints
145128
- matplotlib, pytest, black in dev install
146-
- example file with hydra
147129
- manifest file to not include file in package
148130
- GitHub actions for releasing to PyPi when changes to version
149-
- adding badges to README
131+
- cupy / pytorch compatible

docs/source/code_formatting.rst

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Code formatting
2+
===============
3+
4+
Code formatting is important for readability. It allows you to write code that
5+
is easy to follow (for your future self as much as others), and (least importantly)
6+
adheres to the `PEP8 <https://www.python.org/dev/peps/pep-0008/>`_ standard.
7+
8+
However, remembering all the rules and manually formatting your code is not
9+
how we want to spend our time as developers. To this end, there are several
10+
tools that can help us with this task. The ones we use are:
11+
12+
* `Black <https://github.com/psf/black>`_ which will reformat your code
13+
in-place to conform to the PEP8 standard.
14+
* `Flake8 <https://flake8.pycqa.org/en/latest/>`__ which is a *linter* that
15+
will check your code for errors and style violations, but not reformat it. For
16+
example, for me it has identified code where I have unused variables or
17+
scripts / functions that are too long.
18+
19+
While you can use these tools manually, it is much more convenient to use them
20+
as pre-commit hooks. This means that before you commit your code, these tools
21+
will be run automatically. If they find any errors, the commit will be aborted
22+
and you will have to fix the errors before you can commit again. Pre-commit
23+
helps you to automate this process and avoid commits that do not conform to
24+
the PEP8 standard (and commits that are just for formatting).
25+
26+
A few files are needed to setup pre-commit hooks:
27+
28+
* `.pre-commit-config.yaml <https://github.com/ebezzam/python-dev-tips/blob/main/.pre-commit-config.yaml>`_: This file contains the configuration for the
29+
pre-commit hooks. It specifies which tools to use, and how to use them.
30+
* `.flake8 <https://github.com/ebezzam/python-dev-tips/blob/main/.flake8>`_: This file contains the configuration for Flake8. It specifies
31+
e.g. which errors to ignore, and which line length to use.
32+
* `pyproject.toml <https://github.com/ebezzam/python-dev-tips/blob/main/pyproject.toml>`_: This file contains the configuration for Black. It
33+
specifies e.g. which line length to use.
34+
35+
You can then install the pre-commit hooks for your project by running the
36+
following commands:
37+
38+
.. code:: bash
39+
40+
# inside virtual environment
41+
(project_env) pip install pre-commit
42+
(project_env) pip install black
43+
44+
# Install git hooks
45+
(project_env) pre-commit install
46+
# pre-commit installed at .git/hooks/pre-commit

docs/source/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"sphinx.ext.autosummary",
4545
"sphinx.ext.autodoc",
4646
"sphinx.ext.napoleon",
47+
"sphinx.ext.autosectionlabel",
4748
]
4849

4950
autodoc_default_options = {

docs/source/documentation.rst

+34-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
Documentation
22
=============
33

4-
We will be using `Sphinx <https://www.sphinx-doc.org/en/master/>`__ to generate documentation from
5-
docstrings that are written within code. Docstrings are long-form
4+
*"Documentation is a love letter that you write to your future self."*
5+
6+
*-- Damian Conway*
7+
8+
Documentation is a critical part of any software project. It is the first
9+
point of contact for new users, and it is the first place to look for
10+
developers when they need to understand how a piece of code works. It is
11+
also a great way to share your knowledge with the community.
12+
13+
*As a minimum*, it's good to have a README file that describes:
14+
15+
* What the project is about.
16+
* How to install the project.
17+
* How to use the project.
18+
19+
Inside your code, comments can be very useful to describe what a function or
20+
script does. Even better, is to add `docstrings <https://peps.python.org/pep-0257/#what-is-a-docstring>`_
21+
to your functions and classes. Docstrings are long-form
622
comments beneath class and functions declaration that typically describe:
723

8-
- What the function or class does.
9-
- Its inputs and their data types.
10-
- Its outputs and their data types.
24+
* What the function or class does.
25+
* Its inputs and their data types.
26+
* Its outputs and their data types.
1127

1228
For example:
1329

@@ -35,7 +51,17 @@ For example:
3551
assert isinstance(b, int)
3652
return a + b
3753
38-
Which will be rendered as :func:`pydevtips.utils.add`.
54+
A documentation generator, such as `Sphinx <https://www.sphinx-doc.org/en/master/>`__,
55+
can then be used to render your docstrings into a static website, as shown at
56+
:func:`pydevtips.utils.add` for the above example.
57+
58+
59+
This website can be hosted *for free* on `ReadTheDocs <https://readthedocs.org/>`__ (like this!)
60+
or on GitHub pages. It is also possible to host the documentation on your own
61+
server.
62+
63+
Below we describe how to setup your project to generate documentation with Sphinx,
64+
and how to host it on ReadTheDocs.
3965

4066
Initial setup
4167
-------------
@@ -101,6 +127,7 @@ You can do a clean build of your documentation with the following commands:
101127

102128
.. code:: bash
103129
130+
# inside `docs` folder
104131
make clean
105132
make html
106133
@@ -133,7 +160,7 @@ Pro-tips
133160
* `Add the path <https://github.com/ebezzam/python-dev-tips/blob/e51dd62a2dd156fdd3e559be3930f87f2a4e6405/docs/source/conf.py#L22>`__
134161
to your package, so that it doesn't have to be installed (again keeping your documentation environment light!).
135162
* `Automate year <https://github.com/ebezzam/python-dev-tips/blob/e51dd62a2dd156fdd3e559be3930f87f2a4e6405/docs/source/conf.py#L32>`__.
136-
163+
* You can reference other sections in your documentation by their title, e.g. :ref:`like this <Code formatting>` with ``:ref:`like this <Code formatting>``.
137164

138165
Publishing
139166
----------

docs/source/index.rst

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ Contents
1515

1616
.. toctree::
1717

18-
remote_development
18+
virtual_env
19+
version_control
20+
reproducible
1921
documentation
22+
code_formatting
23+
testing
24+
packaging
25+
remote_development
2026
badges
2127

2228
.. toctree::

0 commit comments

Comments
 (0)