Skip to content

Commit 7d6ad53

Browse files
committed
some additional tip addition
1 parent 6194de2 commit 7d6ad53

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

docs/python/general.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sidebar_label: General Coding Guidelines
77
#### These are the general guidelines to be followed:
88

99
* See [tools](tools.md) that can be used in development environment setup to ease your coding process.
10-
* Always use `python3` and try to stay above version `3.5`. **Latest stable** is recommended.
10+
* Always use `python3` and try to stay above version `3.5`. **Latest stable** is always recommended.
1111
* Indentation should always be **space** and width should always be **4**.
1212
* File size and functionality:
1313
- break files into modules if you feel they have multiple functionalities.
@@ -17,10 +17,9 @@ sidebar_label: General Coding Guidelines
1717
- comprehensions over `map` and loop
1818
- minimal use of `lambda`, use `operator` module with keyfunctions in `sorted`, `groupby` etc.
1919
- ternary with `if else` in same line. Donot use `and or` clause. i.e. `value and req_value or default`.
20-
- `classmethod` for multiple way of class initialization
2120
* Imports:
2221
- Always `import` specific namespace.
23-
- Try to use parent namespace for actions. i.e. `import sys: sys.path` rather that `from sys import path`
22+
- Try to use parent namespace for actions. i.e. `import sys: sys.path` or `import sys.path` rather that `from sys import path`
2423
- Never use `import *` i.e. `from MODULE import *`
2524
- use namespace whenever possible. Use `as SOMEOTHERNAMESPACE` for collision of namespace
2625
* If you are using `else` with loops that has `break`. Just comment `#nobreak` for reference as it is for that usage. See [this](http://python-notes.curiousefficiency.org/en/latest/python_concepts/break_else.html) for some clarity.
@@ -29,20 +28,19 @@ sidebar_label: General Coding Guidelines
2928
if condition:
3029
break
3130
else: #nobreak
32-
ELSE LOGIC
31+
WHEN break DOESNOT HAPPEN
3332

3433
while True:
3534
if condition:
3635
break
3736
else: #nobreak
38-
ELSE LOGIC
37+
WHEN break DOESNOT HAPPEN
3938
```
4039
* Use `pathlib` for path related use case rather than `os.path`
4140
* Use `mypy` and type annotation when possible for type safe code.
4241
* `Docker` can be used for deployment. Use `python` images for [`docker`](https://hub.docker.com/_/python).
4342
* Use `generators` and `yield` instead of data structures for high streams of data.
4443
* Use `itertools`, `functools` for utilities and `collections` for data structures.
45-
* Use `dataclasses` if available. Go for `attrs` library if `dataclass` is not present.
4644
* Use `is not` and `is` for `None`, `True` and `False` specific check only. If most cases truthy and falsy can be checked with `if VARNAME:`.
4745
* Strings:
4846
- Adhere to one quote practice. Double quote is recommended. Python doesnot differentiate between **'** or **"**. This may be controlled by `formatter` used as well.
@@ -57,17 +55,20 @@ sidebar_label: General Coding Guidelines
5755
- Use [`abc`](https://docs.python.org/3/library/abc.html) if you need abstraction. Mixins are more famous in python due to multiple inheritance.
5856
- Use `property` setter getter only when you need readonly attributes. `__` variables can be used for some privacy.
5957
- Use `super` for overrides and parent calls.
58+
- Use inbuilt `dataclasses` if available. Go for `attrs` library if `dataclasses` is not present or you require a much richer library.
59+
- Use `classmethod` decorator for multiple initialization of classes as well as `staticmethod` where needed.
6060
* Use `pdb` as debugger whenever required.
6161
* Multi-threading can be especially used when we have io bound and network bound multiple operation. Multiprocessing can be used to use multiple cores.
6262
- Recommended module is `concurrent.futures` in most cases. If lower level API is needed there is always `threading` and `multiprocessing` module.
6363
- Be very carefult on threads and locks, so always discuss what you are doing as it may not always be optimized.
6464
- Use `asyncio` for IO bound async flow. This is something new and constantly changing in `python`.
65+
* Try to use configurations outside python files. Usually they are not git tracked so should be editable by others. Try `settings.py` or `config.py` if you **must**. This cannot be the case for frameworks.
6566
* Recommended third party modules:
6667
- For Relational Database:
6768
+ Use `sqlalchemy` [core](https://docs.sqlalchemy.org/en/13/core/) for DB abstraction. This is particularly helpful when doing testing in `sqlite` and some other database for production. Also, for query and parameters consistency.
6869
+ Use `sqlalchemy` [ORM](https://docs.sqlalchemy.org/en/13/orm/) or framework supported **ORM** when using specific framework.
6970
+ Use DBAPI drivers such as `pyodbc`, `sqlite`, `mysqlclient` etc only when you donot want `sqlalchemy` dependency or when you are very performance conscious. While the API will be mostly compatible for this as python has DBAPI specification. Parameters binding and some methods may be incompatible or unavailable. **sqlalchemy core is recommended.**
7071
- `requests` for http request stuff.
7172
+ `aiohttp` or `httpx` are also good.
72-
- `attrs` for data oriented objects and classes design.
73+
- `attrs` for data oriented objects and classes design. If you don't want to use `dataclasses`.
7374
- `pytest` for tests.

docs/python/project_structure.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@ sidebar_label: Project Structure
1111
- :memo: pyproject.toml (can be requirements.txt and setup.py)
1212
- :file_folder: docs
1313
* Your documentation
14-
- :file_folder: data
14+
- :file_folder: data (**OPTIONAL**)
1515
* Data for project.
16-
- :file_folder: {PROJECT_NAME}
16+
17+
:::tip
18+
* This folder structure can be used as well. It will have **src** folder for more descriptive stature.
19+
+ :file_folder: src
20+
- :file_folder: {PROJECT_NAME}
21+
22+
:::
23+
*
24+
- :file_folder: {PROJECT_NAME}
1725
+ :file_folder: utils
1826
+ :file_folder: service
1927
+ :file_folder: config
@@ -22,7 +30,7 @@ sidebar_label: Project Structure
2230
+ :memo: conftest.py
2331
- :memo: LICENSE (**OPTIONAL**)
2432
- :memo: README (can be `md` or `rst`)
25-
- Other configurable from third parties (**OPTIONAL**) such as tox.ini
33+
- :file_folder: :memo: Other configurable from third parties (**OPTIONAL**) such as tox.ini
2634

2735
:::note
2836
+ **There can be cases where MVC folder structure as well as framework related folder structure can be used.**

0 commit comments

Comments
 (0)