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
#### The following convention should be followed for `Exception` handling:
8
+
9
+
*`Exception` handling is a must and should be mitigated.
10
+
* Do not use bare `except` or `except Exception` which catches all the exception.
11
+
- Always be specific on exception. E.g. catch only `FileNotFoundError` if you are say moving a file.
12
+
* User Defined Exceptions:
13
+
- Write your custom error only when your error is not described or fulfilled by [internal exceptions](https://docs.python.org/3/library/exceptions.html).
14
+
- Create custom `Exception` class primarily suffixing it with `Error` such as `MyCustomError(Exception)` and use it.
15
+
- Always use `Exception` as your parent class for user defined exceptions. Donot use `BaseException`.
16
+
* Add traceback to you mitigation. i.e. either `logging` or mails. Donot `pass`.
17
+
* The `try` block should be specific to desired exception. Donot use huge code chunk in `try`. Use `else` if needed.
Copy file name to clipboardExpand all lines: docs/python/general.md
+7-14Lines changed: 7 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,12 @@ sidebar_label: General Coding Guidelines
17
17
- minimal use of `lambda`, use `operator` module with keyfunctions in `sorted`, `groupby` etc.
18
18
- ternary with `if else` in same line. Donot use `and or` clause. i.e. `value and req_value or default`.
19
19
-`classmethod` for multiple way of class initialization
20
-
* use namespace whenever possible. Use `as SOMEOTHERNAMESPACE` for collision of namespace
20
+
* Imports:
21
+
- Always `import` specific namespace.
22
+
- Try to use parent namespace for actions. i.e. `import os: os.path` rather that `from os import path`
23
+
- Never use `import *` i.e. `from MODULE import *`
24
+
- use namespace whenever possible. Use `as SOMEOTHERNAMESPACE` for collision of namespace
25
+
* Use `else: #nobreak` if you are using `else` with loops that has `break`.
21
26
* use `mypy` and type annotation when possible for type safe code.
22
27
*`Docker` can be used for deployment. Use `python` images for [docker](https://hub.docker.com/_/python){:target="_blank"}.
23
28
* Use `generators` and `yield` instead of data structures for high streams of data.
@@ -28,18 +33,6 @@ sidebar_label: General Coding Guidelines
28
33
- Adhere to one quote practice. Double quote is recommended. Python doesnot differentiate between **'** or **"**.
29
34
- Should be interpolated with either [fstring](https://www.python.org/dev/peps/pep-0498/){:target="_blank"} or `.format` methods. Try to avoid `%`.
30
35
-`+` can be used for direct string concatenation. Use `join` method for concatenation instead of `+=` when iterating.
31
-
*`logging` is always a must. Use the following levels as required:
32
-
-**DEBUG**: log parameters and arguments. Information needed when we need to debug or develop. Should be avoided in production.
33
-
-**INFO**: log basic information such as function entry, file being processed et al
34
-
-**WARN**: log user security and other warnings that are not critical
35
-
-**ERROR**: error related logs. Use exception method to log tracebacks in case of exceptions.
36
-
-**CRITICAL**: blocking issues or immediate attention issues.
37
-
-**ERROR and CRITICAL** levels should be mitigated and informed.
38
-
-`logger` is used for naming logger.
39
-
- It is singleton and single threaded by default for `a name` of the logger. Can be non-blocking if required.
40
-
*`Exception` handling is a must along with logging.
41
-
- Do not use bare `except` or `except Exception` which catches all the exception. Be specific on exception. E.g. catch only `FileNotFoundError` if you are say moving a file.
42
-
- For modules specific error, if something internal is not fulfilling then try to create custom `Exception` class primarily naming it Error such as `MyCustomError(Exception)` and use it.
43
36
* Use `context` whenever supported especially for io related closing actions.
44
37
- i.e. `with` statement when supported.
45
38
- Always remember to close on exit. i.e. if you open the file `close` on `finally` or better use `with` or `contextlib.closing`.
@@ -51,7 +44,7 @@ sidebar_label: General Coding Guidelines
51
44
- Use `asyncio` for IO bound async flow. This is something new and constantly changing in `python`.
52
45
* Recommended third party modules:
53
46
- For Relational Database:
54
-
+ Use `sqlalchemy`[core](https://docs.sqlalchemy.org/en/13/core/){:target="_blank"} for DB abstraction. This is particularly helpful when doing testing in `sqlite` and some other database for production.
47
+
+ Use `sqlalchemy`[core](https://docs.sqlalchemy.org/en/13/core/){:target="_blank"} for DB abstraction. This is particularly helpful when doing testing in `sqlite` and some other database for production. Also, for query consistency.
55
48
+ Use `sqlalchemy`[ORM](https://docs.sqlalchemy.org/en/13/orm/){:target="_blank"} or framework supported ORM when using specific framework.
56
49
+ 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.
#### The following convention should be followed for using `logging` in python:
8
+
9
+
* Inbuilt `logging` module is used in most cases. You can look into [structlog](https://www.structlog.org/en/stable/) for more granular logging.
10
+
*`logging` is always a must. Use the following levels as required:
11
+
-**DEBUG**: log parameters and arguments. Information needed when we need to debug or develop. Should be avoided in production.
12
+
-**INFO**: log basic information such as function entry, file being processed et al
13
+
-**WARN**: log user security and other warnings that are not critical
14
+
-**ERROR**: error related logs. Use exception method to log tracebacks in case of exceptions.
15
+
-**CRITICAL**: blocking issues or immediate attention issues.
16
+
***ERROR and CRITICAL** levels should be mitigated and informed.
17
+
*`logger` is used for naming single logger. Use `NAME_logger` name for more than one logger when required.
18
+
* It is singleton and single threaded by default for given name of the logger. Can be [non-blocking](https://docs.python.org/3/howto/logging-cookbook.html#dealing-with-handlers-that-block) if required.
19
+
*[Logging Cookbook](https://docs.python.org/3/howto/logging-cookbook.html) for reference.
20
+
* Always use `exception` method rather than `error` method of `logger` object to log traceback when catching exceptions.
0 commit comments