codestyle.rst 3.42 KB
Newer Older
BO ZHANG's avatar
BO ZHANG committed
1
2
3
4
Code Structure
==============
An example code structure is shown below.
Files/directories with asterisks (*) marks are optional.
BO ZHANG's avatar
BO ZHANG committed
5

BO ZHANG's avatar
BO ZHANG committed
6
.. code-block::
BO ZHANG's avatar
BO ZHANG committed
7

BO ZHANG's avatar
BO ZHANG committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    csst_proto                                # the repository name
    ├── csst_proto                            # the package name
    │   ├── data                              # package associated data directory
    │   ├── __init__.py                       # necessary file for a Python package
    │   ├── flip_image.py                     # Python modules
    │   ├── scratch.py
    │   ├── some_other_modules.py
    │   └── top_level_interface.py            # the top level interface module
    ├── 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
BO ZHANG's avatar
BO ZHANG committed
37
38


BO ZHANG's avatar
BO ZHANG committed
39
40
41
Code Style
==========
Python code should follow PEP 8
BO ZHANG's avatar
tweaks    
BO ZHANG committed
42
43
44

- https://peps.python.org/pep-0008/

BO ZHANG's avatar
BO ZHANG committed
45
Whereas C/C++ code should follow PEP 7
BO ZHANG's avatar
tweaks    
BO ZHANG committed
46
47
48
49
50
51
52
53
54
55
56
57
58

- https://peps.python.org/pep-0007/

Many IDEs (Pycharm, etc) and tools can be used to validate code style

- pycodestyle:
    - https://pycodestyle.pycqa.org/en/latest/

In our case, we recommend the following method to check code style

.. code-block:: bash

    pycodestyle ./**/*.py --ignore=E121,E123,E126,E226,E24,E704,W503,W504,E501,E722
BO ZHANG's avatar
BO ZHANG committed
59

BO ZHANG's avatar
BO ZHANG committed
60
61
62
63
64
65
66

Numpy-style docstring
=====================
There are several popular docstring styles, namely Google, reStructuredText, and Numpy.
The CSST DAS adopts Numpydoc-style among the three.
We refer our developers to the Numpydoc official style guide for instructions on
how to write docstrings.
BO ZHANG's avatar
tweaks    
BO ZHANG committed
67
68
69

- https://numpydoc.readthedocs.io/en/latest/format.html

BO ZHANG's avatar
BO ZHANG committed
70
In particular, a complete example is available here.
BO ZHANG's avatar
tweaks    
BO ZHANG committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84

- https://numpydoc.readthedocs.io/en/latest/example.html

In our case, we recommend the following method to check docstring

.. code-block:: bash

    python -m numpydoc --validate {YOUR_PACKAGE}.top_level_interface.{YOUR_FUNCTION/CLASS}

For example,

.. code-block:: bash

    python -m numpydoc --validate csst_proto.top_level_interface.flip_image
BO ZHANG's avatar
BO ZHANG committed
85
86


BO ZHANG's avatar
BO ZHANG committed
87
88
Markup language
===============
BO ZHANG's avatar
BO ZHANG committed
89
90
91
Markdown and reStructrured Text are two of popular markup languages.
reStructrured Text is used in Numpydoc docstrings as well as sphinx-based documentation.
Markdown is much easier and usually used to write `README` for a package.
BO ZHANG's avatar
BO ZHANG committed
92

BO ZHANG's avatar
BO ZHANG committed
93
94
- Markdown
    - https://www.markdownguide.org/
BO ZHANG's avatar
BO ZHANG committed
95
- reStructured Text
BO ZHANG's avatar
BO ZHANG committed
96
    - https://www.writethedocs.org/guide/writing/reStructuredText/