You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/python/general.md
+8-7Lines changed: 8 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ sidebar_label: General Coding Guidelines
7
7
#### These are the general guidelines to be followed:
8
8
9
9
* 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.
11
11
* Indentation should always be **space** and width should always be **4**.
12
12
* File size and functionality:
13
13
- break files into modules if you feel they have multiple functionalities.
@@ -17,10 +17,9 @@ sidebar_label: General Coding Guidelines
17
17
- comprehensions over `map` and loop
18
18
- minimal use of `lambda`, use `operator` module with keyfunctions in `sorted`, `groupby` etc.
19
19
- 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
21
20
* Imports:
22
21
- 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`
24
23
- Never use `import *` i.e. `from MODULE import *`
25
24
- use namespace whenever possible. Use `as SOMEOTHERNAMESPACE` for collision of namespace
26
25
* 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
29
28
if condition:
30
29
break
31
30
else: #nobreak
32
-
ELSELOGIC
31
+
WHENbreakDOESNOTHAPPEN
33
32
34
33
whileTrue:
35
34
if condition:
36
35
break
37
36
else: #nobreak
38
-
ELSELOGIC
37
+
WHENbreakDOESNOTHAPPEN
39
38
```
40
39
* Use `pathlib`for path related use case rather than `os.path`
41
40
* Use `mypy`andtype annotation when possible fortype safe code.
42
41
*`Docker` can be used for deployment. Use `python` images for [`docker`](https://hub.docker.com/_/python).
43
42
* Use `generators`and`yield` instead of data structures for high streams of data.
44
43
* Use `itertools`, `functools`for utilities and`collections`for data structures.
45
-
* Use `dataclasses`if available. Go for`attrs` library if`dataclass`isnot present.
46
44
* Use `isnot`and`is`for`None`, `True`and`False` specific check only. If most cases truthy and falsy can be checked with`ifVARNAME:`.
47
45
* Strings:
48
46
- 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
57
55
- Use [`abc`](https://docs.python.org/3/library/abc.html) if you need abstraction. Mixins are more famous in python due to multiple inheritance.
58
56
- Use `property` setter getter only when you need readonly attributes. `__` variables can be used for some privacy.
59
57
- Use `super`for overrides and parent calls.
58
+
- Use inbuilt `dataclasses`if available. Go for`attrs` library if`dataclasses`isnot present or you require a much richer library.
59
+
- Use `classmethod` decorator for multiple initialization of classes as well as`staticmethod` where needed.
60
60
* Use `pdb`as debugger whenever required.
61
61
* Multi-threading can be especially used when we have io bound and network bound multiple operation. Multiprocessing can be used to use multiple cores.
62
62
- Recommended module is`concurrent.futures`in most cases. If lower level APIis needed there is always `threading`and`multiprocessing` module.
63
63
- Be very carefult on threads and locks, so always discuss what you are doing as it may not always be optimized.
64
64
- Use `asyncio`forIO 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.
65
66
* Recommended third party modules:
66
67
- For Relational Database:
67
68
+ Use `sqlalchemy` [core](https://docs.sqlalchemy.org/en/13/core/) forDB abstraction. This is particularly helpful when doing testing in`sqlite`and some other database for production. Also, for query and parameters consistency.
68
69
+ Use `sqlalchemy` [ORM](https://docs.sqlalchemy.org/en/13/orm/) or framework supported **ORM** when using specific framework.
69
70
+ 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.**
70
71
-`requests`for http request stuff.
71
72
+`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`.
0 commit comments