ch03_packaging.rst 6.66 KB
Newer Older
BO ZHANG's avatar
BO ZHANG committed
1
2
3
4
5
6
7
8
9
Python Packaging
================
Python projects should be packaged in the standard way as shown in the
official `Python Packaging User Guide`_, and more specifically `here`_.

.. _here: https://packaging.python.org/en/latest/tutorials/packaging-projects/
.. _Python Packaging User Guide: https://packaging.python.org/en/latest/


BO ZHANG's avatar
tweaks    
BO ZHANG committed
10
Project structure
BO ZHANG's avatar
BO ZHANG committed
11
12
13
14
15
16
17
18
19
20
21
22
23
-----------------
An example code structure is shown below.
Files/directories with asterisks (``*``) marks are optional.

.. code-block::

    csst_proto                                # the repository name
    ├── csst_proto                            # the package name
    │   ├── data                              # package associated data directory
    │   ├── __init__.py                       # necessary file for a Python package
    │   ├── demo.py                           # Python modules
    │   ├── flip_image.py
    │   ├── scratch.py
BO ZHANG's avatar
tweaks    
BO ZHANG committed
24
    │   └── api.py                            # the top level interface module
BO ZHANG's avatar
BO ZHANG committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    ├── doc                                   # *sphinx-based documentation directory
    │   ├── build
    │   ├── source
    │   ├── Makefile
    │   ├── apidoc.sh
    │   ├── contents.md
    │   ├── make.bat
    │   └── preview.sh
    ├── examples                              # *example scripts
    │   ├── how_this_code_will_be_used.py
    │   └── how_to_write_docstring.py
    ├── tests                                 # unit tests
    │   ├── test_flip_image.py
    │   └── test_other_functions.py
    ├── LICENSE                               # license file
    ├── README.md                             # README file (Markdown recommended)
    ├── install.sh                            # a single-line installation script
    ├── install_local.sh                      # local installation script
    ├── readthedocs.yml                       # *configuration file for readthedocs
    ├── requirements.txt                      # package dependencies
    └── setup.py                              # setup file

.. note::
    The ``__init__.py`` files is important, without which
    the directory will not be seen as a ``Python package``.

Python version
--------------

Developers should use ``Python 3.9.X`` to implement algorithms.
The base docker image will be ``continuumio/anaconda3``, which uses
``Python 3.9.12``.

Relative import
---------------
When import a class / function within the same package, a ``relative import``
should be used instead of ``absolute import``.
An example is below (``top_level_interface.py``):

.. literalinclude:: ../../csst_proto/top_level_interface.py
   :linenos:
   :language: python

.. note::
    Note the ``.`` means it's a relative import.

README.md
---------

A Markdown format text file to describe your package.

.. literalinclude:: ../../README.md

LICENSE
-------

Among the many choices, we encourage our developers to use ``MIT`` LICENSE.
An example is below.

.. literalinclude:: ../../LICENSE


``requirements.txt``
--------------------
``requirements.txt`` is a text file, within which a line represents a package and its version.

.. literalinclude:: ../../requirements.txt
   :linenos:

.. note::
    For each package, developers must specify a version.
    DO NOT use ``>``, ``>=``, ``<``, ``<=`` or ``~=``, use ``==`` ONLY!


``setup.py``
------------
We use a classic format of ``setup.py`` file as shown below.
We refer readers to https://setuptools.pypa.io/en/latest/ for more information
and usages about the package `setuptools`_.

.. _setuptools: https://setuptools.pypa.io/en/latest/

.. literalinclude:: ../../setup.py
   :linenos:
   :language: python

.. note::

   The ``install_requires`` keyword argument is commented because it takes no effect
   when installed with ``pip install <your_package> --no-deps``.
   Please DO NOT use it.

Include data
------------

Data files can be packaged together with code.
To include ``csst_proto/data/test_image.txt`` and ``csst_proto/data/table_data.vsb``,
set the keyword ``package_data`` in ``setup.py`` as below:

.. code-block:: python

    ...
    package_data={"": ["LICENSE", "README.md"],
                  "csst_proto": ["data/test_image.txt",
                                 "data/table_data.csv"
                                 ]},
    ...

.. note::
    The ``""`` key represents the data files located directly in the project directory.
    For files associated with package ``csst_proto``, link the data files to ``csst_proto`` key.

.. note::
    DO NOT include any test data (e.g., CSST images) in the package.
    We only encourage developers to pacakge configuration files inside.
    Please keep your package *clean* and *tight*.

To access the data, developers should use ``relative path``.
In the example below, we show the function ``read_test_image``,
an example of how to access data under ``csst_proto/data/``.
In ``__init__.py``:

.. literalinclude:: ../../csst_proto/__init__.py
    :linenos:
    :language: python

and in ``flip_image.py``:

.. literalinclude:: ../../csst_proto/flip_image.py
    :linenos:
    :language: python


158
159
160
161
162
163
164
165
Including C code
----------------

To include C code, developers should follow the guide provided by ``setuptools``:
https://setuptools.pypa.io/en/latest/userguide/ext_modules.html.
And also consider using https://docs.astropy.org/en/latest/development/ccython.html#building-c-or-cython-extensions.


BO ZHANG's avatar
BO ZHANG committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
``top_level_interface`` module
------------------------------
This is a special requirement for all of CSST DAS packages.
This module is used to store the interfaces (classes / functions) that
will be called by users.

.. tip::
    Remember to set the ``__all__`` variable.
    This should be a list containing all interfaces.

.. note::
    All the interfaces in this module should use a complete ``Numpy``-style docstring
    -- They will go through docstring format validations in Jenkins!


How to install package
----------------------
We recommend the following way to install your package.

.. code-block:: bash

    # install requirements
    pip install -r requirements
    # build extensions in place
    python setup.py build_ext --inplace
    # build source code
    python setup.py sdist
    # install from the source code
    pip install dist/*.tar.gz --force-reinstall --no-deps


Sphinx-based documentation
--------------------------
This is currently regarded as an optional section.
But we still recommend developers to read a bit of the ``sphinx`` documentation.

The sphinx homepage:

- https://www.sphinx-doc.org/en/master/index.html

A tutorial on how to write restructured text (.rst) files:

- https://docutils.sourceforge.io/rst.html