Writing /var/lib/dokuwiki/data/meta/tutorials/python_development_standards.meta failed
tutorials:python_development_standards
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorials:python_development_standards [2018/05/01 13:52] – [Python development standards] dgarcia | tutorials:python_development_standards [2022/09/20 00:08] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 19: | Line 19: | ||
To install: | To install: | ||
- | < | + | < |
apt-get install flake8 | apt-get install flake8 | ||
</ | </ | ||
Usage: | Usage: | ||
- | < | + | < |
flake8 path/ | flake8 path/ | ||
</ | </ | ||
Line 32: | Line 32: | ||
To install: | To install: | ||
- | < | + | < |
apt-get install yapf3 | apt-get install yapf3 | ||
</ | </ | ||
Usage: | Usage: | ||
- | < | + | < |
yapf3 -ir path/ | yapf3 -ir path/ | ||
</ | </ | ||
or | or | ||
- | < | + | < |
yapf3 -i path/ | yapf3 -i path/ | ||
</ | </ | ||
+ | ==== Logging ==== | ||
+ | |||
+ | Logging is a very important part of every software project, because it is the best way to deliver valuable information to our users. Logs are used to report errors, warnings, or just general information of how the program is executing. Good logs help us to discover why the program fails some times, they help us diagnose problems with our programs and help us plan strategies for improvement. It is crucial to maintain good and healthy logging practices so that you can increase the maintainability of your code. | ||
+ | |||
+ | Python developers, knowing that logs are so important, have developed a module just for this: the [[https:// | ||
+ | |||
+ | === Logging levels === | ||
+ | |||
+ | '' | ||
+ | |||
+ | '' | ||
+ | |||
+ | The '' | ||
+ | |||
+ | '' | ||
+ | |||
+ | Finally '' | ||
+ | |||
+ | When you define a logging level, you are telling Python which messages you want to receive. If you define a level //A//, you will receive all //A// messages and all messages with more priority. If you say that you want a '' | ||
+ | |||
+ | === Loggers === | ||
+ | |||
+ | In Python, the [[https:// | ||
+ | |||
+ | The following is an example, | ||
+ | |||
+ | <code python> | ||
+ | # myapp.py | ||
+ | import logging | ||
+ | import math | ||
+ | |||
+ | def main(): | ||
+ | # Instead of printing the message send it to a file named ' | ||
+ | # Set the INFO level | ||
+ | logging.basicConfig(filename=" | ||
+ | | ||
+ | # log some info | ||
+ | logging.info(" | ||
+ | total = 0 | ||
+ | for number in range(-2, 100): | ||
+ | logging.debug(number) | ||
+ | try: | ||
+ | math.sqrt(number) | ||
+ | except ValueError: | ||
+ | # report that there was an error, but we handled it. | ||
+ | logging.warning(" | ||
+ | number = 0 | ||
+ | |||
+ | total += number | ||
+ | |||
+ | if total < 0: | ||
+ | # If after adding possive numbers you got a negative, then there is something really wrong | ||
+ | logging.error(" | ||
+ | raise ValueError | ||
+ | | ||
+ | # If everything went OK, report it | ||
+ | logging.info(' | ||
+ | |||
+ | if __name__ == ' | ||
+ | main() | ||
+ | </ | ||
+ | |||
+ | |||
+ | === Colors === | ||
+ | |||
+ | It is also possible to add colors to your logs. For that we need to deal with a [[https:// | ||
+ | |||
+ | If you want to add colors to your logs you will need a package named `colorlog`. You can install it from PyPi with: | ||
+ | |||
+ | <code bash> | ||
+ | pip install colorlog | ||
+ | </ | ||
+ | |||
+ | If you do not want to use '' | ||
+ | |||
+ | <code bash> | ||
+ | sudo apt install python-colorlog python3-colorlog | ||
+ | </ | ||
+ | |||
+ | Here you have an example of how to use `colorlog`: | ||
+ | |||
+ | <code python> | ||
+ | import colorlog | ||
+ | |||
+ | # create the format object with colors. | ||
+ | formatter = colorlog.ColoredFormatter( | ||
+ | " | ||
+ | datefmt=None, | ||
+ | reset=True, | ||
+ | log_colors={ | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | }, | ||
+ | secondary_log_colors={}, | ||
+ | style=' | ||
+ | ) | ||
+ | |||
+ | # Create the handler | ||
+ | handler = colorlog.StreamHandler() | ||
+ | # Add the formatter we created earlier | ||
+ | handler.setFormatter(formatter) | ||
+ | |||
+ | # Get a logger called " | ||
+ | logger = colorlog.getLogger(" | ||
+ | # Add the colored formatter to the logger we just created | ||
+ | logger.addHandler(handler) | ||
+ | |||
+ | logger.error(" | ||
+ | logger.warning(" | ||
+ | logger.info(" | ||
+ | logger.debug(" | ||
+ | </ | ||
===== Project Layout ===== | ===== Project Layout ===== | ||
The recommended project layout for a python projected named < | The recommended project layout for a python projected named < | ||
Line 111: | Line 226: | ||
==== Issues ==== | ==== Issues ==== | ||
- | Any problems encountered due to migration should be reported with a github | + | Any problems encountered due to migration should be reported with a GitLab |
==== Progress tracking ==== | ==== Progress tracking ==== | ||
tutorials/python_development_standards.1525182737.txt.gz · Last modified: 2022/09/20 00:08 (external edit)