Skip to content

Commit cc24ca6

Browse files
author
Murilo Marinho
committed
[python practices] Adjusting tree.
1 parent e05c64d commit cc24ca6

1 file changed

Lines changed: 70 additions & 32 deletions

File tree

docs/source/preamble/python/python_best_practices.rst

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,22 @@ Minimalist package: something to start with
5252
.. admonition:: In this step, we'll work on these.
5353

5454
.. code-block:: console
55-
:emphasize-lines: 2,3
55+
:emphasize-lines: 1-4
5656
57-
python/minimalist_package/
58-
└── minimalist_package/
59-
└── __init__.py
57+
python/
58+
`-- minimalist_package
59+
|-- minimalist_package
60+
| |-- __init__.py
61+
| |-- _minimalist_class.py
62+
| |-- minimalist_async
63+
| | |-- __init__.py
64+
| | |-- _unlikely_to_return.py
65+
| | |-- async_await_example.py
66+
| | `-- async_callback_example.py
67+
| `-- minimalist_script.py
68+
|-- setup.py
69+
`-- test
70+
`-- test_minimalist_class.py
6071
6172
6273
First, let's make a folder for our project
@@ -94,19 +105,30 @@ The (empty) package is done!
94105
Minimalist script
95106
-----------------
96107

97-
.. admonition:: In this step, we'll work on this.
108+
.. admonition:: In this step, we'll work on these.
98109

99110
.. code-block:: console
100-
:emphasize-lines: 4
101-
102-
python/minimalist_package/
103-
└── minimalist_package/
104-
└── __init__.py
105-
└── minimalist_script.py
111+
:emphasize-lines: 11
112+
113+
python/
114+
`-- minimalist_package
115+
|-- minimalist_package
116+
| |-- __init__.py
117+
| |-- _minimalist_class.py
118+
| |-- minimalist_async
119+
| | |-- __init__.py
120+
| | |-- _unlikely_to_return.py
121+
| | |-- async_await_example.py
122+
| | `-- async_callback_example.py
123+
| `-- minimalist_script.py
124+
|-- setup.py
125+
`-- test
126+
`-- test_minimalist_class.py
106127
107128
Let's start with a minimalist script that prints a string periodically,
108-
as follows. Create a file in :file:`~/ros2_tutorials_preamble/python/minimalist_package/minimalist_package` called :file:`minimalist_script.py` with the following
109-
contents.
129+
as follows.
130+
131+
In the directory :file:`~/ros2_tutorials_preamble/python/minimalist_package/minimalist_package`, create the following file.
110132

111133
:download:`minimalist_script.py <../../../../preamble/python/minimalist_package/minimalist_package/minimalist_script.py>`
112134

@@ -240,20 +262,29 @@ Minimalist class: Use classes profusely
240262
.. admonition:: In this step, we'll work on these.
241263

242264
.. code-block:: console
243-
:emphasize-lines: 3,5
244-
245-
python/minimalist_package/
246-
└── minimalist_package/
247-
└── __init__.py
248-
└── minimalist_script.py
249-
└── _minimalist_class.py
265+
:emphasize-lines: 4,5
266+
267+
python/
268+
`-- minimalist_package
269+
|-- minimalist_package
270+
| |-- __init__.py
271+
| |-- _minimalist_class.py
272+
| |-- minimalist_async
273+
| | |-- __init__.py
274+
| | |-- _unlikely_to_return.py
275+
| | |-- async_await_example.py
276+
| | `-- async_callback_example.py
277+
| `-- minimalist_script.py
278+
|-- setup.py
279+
`-- test
280+
`-- test_minimalist_class.py
281+
250282
251283
As you are familiar with object-oriented programming, you know that classes are central to this paradigm.
252284
As a memory refresher, let's make a class that honestly does nothing really useful but illustrates all
253285
the basic points in a Python class.
254286

255-
Create a file in :file:`~/ros2_tutorials_preamble/python/minimalist_package/minimalist_package` called :file:`_minimalist_class.py` with the following
256-
contents.
287+
Create the file below in the directory :file:`~/ros2_tutorials_preamble/python/minimalist_package/minimalist_package`.
257288

258289
:download:`_minimalist_class.py <../../../../preamble/python/minimalist_package/minimalist_package/_minimalist_class.py>`
259290

@@ -292,7 +323,7 @@ It might be parsing through jibber-jabber code in l__tcode lessons with weird C-
292323

293324
So, always pay attention to the naming of classes (`PascalCase <https://en.wiktionary.org/wiki/Pascal_case>`_), files and functions (`snake_case <https://en.wikipedia.org/wiki/Snake_case>`_), etc.
294325

295-
Thankfully, Python has a bunch of style rules builtin the language and :abbr:`PEP (Python Enhancement Proposal)`, such as `PEP8 <https://peps.python.org/pep-0008/>`_. Take this time to read it and get inspired by `The Zen of Python <https://peps.python.org/pep-0020/>`_
326+
Thankfully, Python has a bunch of style rules builtin the language and :abbr:`PEP (Python Enhancement Proposal)`, such as `PEP8 <https://peps.python.org/pep-0008/>`_. Take this time to read it and get inspired by `The Zen of Python <https://peps.python.org/pep-0020/>`_.
296327

297328
.. _Type hints:
298329

@@ -375,15 +406,22 @@ Unit tests: always test your code
375406
.. admonition:: In this step, we'll work on these.
376407

377408
.. code-block:: console
378-
:emphasize-lines: 6,7
379-
380-
python/minimalist_package/
381-
└── minimalist_package/
382-
└── __init__.py
383-
└── minimalist_script.py
384-
└── _minimalist_class.py
385-
└── test/
386-
└── test_minimalist_class.py
409+
:emphasize-lines: 13,14
410+
411+
python/
412+
`-- minimalist_package
413+
|-- minimalist_package
414+
| |-- __init__.py
415+
| |-- _minimalist_class.py
416+
| |-- minimalist_async
417+
| | |-- __init__.py
418+
| | |-- _unlikely_to_return.py
419+
| | |-- async_await_example.py
420+
| | `-- async_callback_example.py
421+
| `-- minimalist_script.py
422+
|-- setup.py
423+
`-- test
424+
`-- test_minimalist_class.py
387425
388426
`Unit testing <https://en.wikipedia.org/wiki/Unit_testing>`_ is a flag that has been waved by programming enthusiasts
389427
and is often a good measurement of code maturity.

0 commit comments

Comments
 (0)