Skip to content

Commit 3652a58

Browse files
committed
update main index.rst
1 parent 0fdcbbf commit 3652a58

File tree

6 files changed

+200
-36
lines changed

6 files changed

+200
-36
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ docs: build-api-docs build-docs ## Build and server the docs locally
3030
cd docs && python -m http.server 8083 --directory _build/html
3131

3232
build-api-docs: ## Build api docs
33-
sphinx-apidoc --output-dir docs src src/examples/*.py --separate
33+
sphinx-apidoc --output-dir docs src src/examples/*.py --separate
3434

3535
build-docs: ## Build docs
3636
sphinx-build docs docs/_build/html

docs/index.rst

+156-4
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,170 @@
33
You can adapt this file completely to your liking, but it should at least
44
contain the root `toctree` directive.
55
6-
Welcome to pycompile's documentation!
6+
Welcome to pycompile's docs!
77
=====================================
88

99
.. toctree::
1010
:maxdepth: 2
1111
:caption: Contents:
1212

13+
CLI
1314

14-
15-
Indices and tables
15+
Contents
1616
==================
1717

1818
* :ref:`genindex`
1919
* :ref:`modindex`
20-
* :ref:`search`
20+
21+
22+
23+
CLI
24+
==================
25+
1. `compile <#compile>`__
26+
2. `benchmark <#benchmark>`__
27+
3. `dry run <#dry-run>`__
28+
29+
30+
=================
31+
compile
32+
=================
33+
34+
+-------------------+--------------------------------------------------+
35+
| Syntax | Description |
36+
+===================+==================================================+
37+
| ``-- | by default it excludes any ``test`` and |
38+
| input-path PATH`` | ``__init__.py`` files |
39+
+-------------------+--------------------------------------------------+
40+
| ` | Deletes the sources files. |
41+
| `--clean-source`` | |
42+
+-------------------+--------------------------------------------------+
43+
| ``--keep-builds`` | Keeps the temp build files. |
44+
+-------------------+--------------------------------------------------+
45+
| ``--cl | Deletes the shared objects (``.so``) files. |
46+
| ean-executables`` | |
47+
+-------------------+--------------------------------------------------+
48+
| ``--engine`` | Can be ``cython`` or ``nuitka``. |
49+
+-------------------+--------------------------------------------------+
50+
| ``--exc | Glob file patterns for excluding specific files. |
51+
| lude-glob-paths`` | |
52+
+-------------------+--------------------------------------------------+
53+
| ``--verbose`` | Increase log messages. |
54+
+-------------------+--------------------------------------------------+
55+
56+
.. code:: bash
57+
58+
pycompile -i your_python_files --clean-source --engine nuitka
59+
60+
By default, the `Cython <https://cython.org/>`__ is being used as the
61+
default compiler.
62+
63+
For compiling the ``examples`` use the following command:
64+
65+
.. code:: bash
66+
67+
pycompile -i input_path --engine cython
68+
69+
which by default, deletes any temp build files and keeps the source
70+
files.
71+
72+
.. code:: bash
73+
74+
pycompile -i input_path --engine nuitka
75+
76+
77+
78+
79+
After the compilation the ``input`` dir will have the following
80+
structure.
81+
82+
.. code:: text
83+
84+
examples
85+
├── fib.py.py
86+
├── fib.cpython-310-darwin.so
87+
├── test_fib.py
88+
89+
90+
91+
Benchmark
92+
~~~~~~~~~
93+
94+
+-----------------------+----------------------------------------------+
95+
| Syntax | Description |
96+
+=======================+==============================================+
97+
| ``--input-path PATH`` | by default it excludes any ``test`` and |
98+
| | ``__init__.py`` files |
99+
+-----------------------+----------------------------------------------+
100+
| ``--engine`` | Can be ``cython``, ``nuitka``, ``all`` or |
101+
| | ``none``. |
102+
+-----------------------+----------------------------------------------+
103+
| ``--type`` | Can be ``memory`` , ``cpy``, or ``both`` |
104+
+-----------------------+----------------------------------------------+
105+
| ``--verbose`` | Increase log messages. |
106+
+-----------------------+----------------------------------------------+
107+
| ``--profil | function name pattern for profiling defaults |
108+
| e_func_pattern TEXT`` | to ``benchmark`` |
109+
+-----------------------+----------------------------------------------+
110+
111+
For running a benchmark on the ``input-path`` use the following command:
112+
113+
.. code:: bash
114+
115+
pycompile benchmark -i src/examples -vvv
116+
117+
which by default will start a ``memory`` and a ``cpu`` benchmark,
118+
starting with ``python`` and then with ``cython`` and ``nuitka``
119+
The python package must have a ``test_module.py`` because both benchmark
120+
types are invoked with ``pytest`` runs
121+
122+
- For **memory profiling** the script will decorate all the functions
123+
in ``benchmark.py`` with the ``profile`` decorator from
124+
``memory-profiler``. This is not optimal memory profiling, because we
125+
don’t actually ``profile`` the function itself, instead we profile
126+
the ``caller`` but it’s necessary if we want to ``profile`` also the
127+
compiled code. Use the ``profile_func_pattern`` to specify the
128+
function to be profiled in different module for example if ``main``
129+
is the entrypoint under ``main.py`` use
130+
``--profile_func_pattern main``.
131+
132+
Hence, the following structure are required for the ``benchmark``
133+
subcommand.
134+
135+
- For **cpu profiling** the same approached is being used, but instead
136+
of decorating the ``calling functions`` it ``decorates`` the test
137+
cases with the ``benchmark`` from ``pytest-benchmark``.
138+
139+
.. code:: text
140+
141+
module
142+
├── sample_funcs.py # implementation
143+
├── main.py # entrypoint with a `main` function
144+
├── test_sample_funcs.py # test cases
145+
146+
147+
148+
149+
Dry run
150+
~~~~~~~
151+
152+
+-------------------+--------------------------------------------------+
153+
| Syntax | Description |
154+
+===================+==================================================+
155+
| ``-- | by default it excludes any ``test`` and |
156+
| input-path PATH`` | ``__init__.py`` files |
157+
+-------------------+--------------------------------------------------+
158+
| ``--exc | Glob file patterns for excluding specific files. |
159+
| lude-glob-paths`` | |
160+
+-------------------+--------------------------------------------------+
161+
| ``--verbose`` | Increase log messages. |
162+
+-------------------+--------------------------------------------------+
163+
164+
.. code:: bash
165+
166+
pycompile dry_run -i ./src
167+
168+
169+
170+
171+
172+

src/benchmark.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
logger = logging.getLogger(__name__)
1515

1616

17-
class Benchmark: # pylint: disable=invalid-name
17+
class Benchmark:
1818
"""
1919
Runs a benchmarks (memory, cpu) with (``python``, ``Cython``, ``Nuitka``)
2020
on ``input_path`` files using the:
@@ -98,8 +98,10 @@ def _compile(
9898
@staticmethod
9999
def mem_bench(input_path: Path, prof_func_name: str, engine: str) -> None:
100100
"""
101-
decorate all the function from the `examples path` marked as `benchmark`
102-
with `@profile` decorator from `memory_profiler`
101+
decorate all the function(s) from the ``input_path`` where their name match
102+
the ``prof_func_name`` pattern,
103+
with the ``@profile`` decorator from ``memory_profiler``.
104+
103105
Finally, invoke pytest within.
104106
"""
105107
logger.info(
@@ -124,8 +126,9 @@ def mem_bench(input_path: Path, prof_func_name: str, engine: str) -> None:
124126
@staticmethod
125127
def cpu_bench(input_path: Path, engine: str) -> None:
126128
"""
127-
decorate all the test functions from the `examples path`
128-
with the `@benchmark_wrapper` decorator
129+
decorate all the test functions from the ``input_path``
130+
with the ``@benchmark_wrapper`` decorator.
131+
129132
Finally, invoke pytest within.
130133
"""
131134
logger.info(

src/compiler_handler.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414

1515
class CompilerHandler:
1616
"""
17-
CompilerHandler is responsible for compiling all the `.py` files using
18-
`Cython` or `Nuitka`
17+
CompilerHandler is responsible for compiling all the ``.py`` files using
18+
``Cython`` or ``Nuitka``
19+
example usage::
20+
compiler_handler = CompilerHandler(files=dir_files,compiler=compiler,
21+
clean_source=True,keep_builds=True)
22+
compiler_handler.start()
1923
"""
2024

2125
def __init__(
@@ -65,8 +69,8 @@ def clean_executables(self) -> None:
6569

6670
def start(self) -> None:
6771
"""
68-
For each `.py` file runs the compiler command
69-
to build the final executable `.so`
72+
For each ``.py`` file runs the compiler command
73+
to build the final executable ``.so``
7074
"""
7175
total_iterations = sum(len(files) for files in self.files.values())
7276
for directory, dir_files in tqdm(

src/file_handler.py

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""
22
FileHandler implementation
3-
Note:
4-
`glob patterns`: `**`: Recursively matches zero or more directories
5-
that fall under the current directory.
6-
`*` : On Unix, will match everything except slashes.
7-
On Windows, it will avoid matching backslashes as well as slashes.
3+
glob patterns ::
4+
**: Recursively matches zero or more directories
5+
that fall under the current directory.
6+
* : On Unix, will match everything except slashes.
7+
On Windows, it will avoid matching backslashes as well as slashes.
88
"""
99
import logging
1010
from collections import defaultdict
@@ -18,9 +18,10 @@
1818

1919
class FileHandler:
2020
"""
21-
FileHandler is responsible for finding all the `.py` files given
22-
any `input_path`.
23-
example usage: FileHandler("./my_module).start()
21+
FileHandler is responsible for finding all the ``.py`` files within
22+
the ``input_path``.
23+
example usage::
24+
dir_files = FileHandler("./my_module").start()
2425
"""
2526

2627
def __init__(
@@ -29,9 +30,10 @@ def __init__(
2930
additional_exclude_patterns: Optional[list[str]] = None,
3031
):
3132
"""
32-
By default, all the `test` files and any `__init__` files are excluded
33-
:param `input_path`: Should be a valid file/directory path.
34-
:param `additional_exclude_patterns`: Any optional additional glob patterns.
33+
By default, all the ``test`` files and any ``__init__`` files are excluded.
34+
35+
:param input_path: Should be a valid file/directory path.
36+
:param additional_exclude_patterns: Any optional additional glob patterns.
3537
"""
3638
self.input_path = input_path
3739
self.exclude_patterns = additional_exclude_patterns or []
@@ -71,8 +73,9 @@ def exclude_patterns(
7173

7274
def start(self) -> dict[str, list[Path]]:
7375
"""
74-
For the given `input path` collect all valid `.py` files based on
75-
the `exclude_patterns`
76+
For the given ``input path`` collect all valid ``.py`` files based on
77+
the ``exclude_patterns``
78+
7679
:return: a dictionary for valid files within each directory.
7780
"""
7881

@@ -125,10 +128,13 @@ def _collect_files(self) -> Generator[Path, None, None]:
125128

126129
def collect_with_pattern(self, pattern: str) -> Generator[Path, None, None]:
127130
"""
128-
Collects all python files from the `input_path` where they mach
129-
the `pattern`
131+
Collects all python files within the ``input_path`` where they mach
132+
the ``pattern``
130133
134+
:param pattern: str containing glob patters
135+
:return: A Generator of file paths.
131136
"""
137+
132138
if not self.input_path.is_dir():
133139
yield self.input_path
134140
else:

src/helpers.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
"""
2-
implementations for
3-
`change_dir` context manager,
4-
`copy_files` context manager,
5-
`decorate_functions`,
6-
`run_pytest`,
7-
and `run_sub_process` helper function.
2+
pycompile helper functions.
83
"""
94
import ast
105
import os
@@ -21,7 +16,11 @@
2116

2217

2318
@dataclass(frozen=True)
24-
class Colors: # pylint: disable=missing-class-docstring
19+
class Colors:
20+
"""
21+
pycompile colors
22+
"""
23+
2524
HEADER = "\033[95m"
2625
BLUE = "\033[94m"
2726
CYAN = "\033[96m"

0 commit comments

Comments
 (0)