A Python project using Flask for generating barcodes. This project belongs to Rocketseat's event called NLW-Experts.
⏰Day-1
- Adding Pylint to project- Adding pre-commit to project
- Adding server base params, including route and feature for generating barcode
- Adding and update the requirements
- Adding README.md and LICENSE
Pylint and naming conventions:
def my_func(): # snake_case -> Functions, Variables, Methods
print('Ola')
def myFunc(): # camelCase -> It's not the usual default.
print('Ola2')
class MyFunc: # PascalCase -> Classes
SCREAMING_SNAKE_CASE: # -> Const
Requirements:
When we want to keep a record of installed dependencies and their versions, we use this command in the terminal.
.venv\Scripts\pip3 freeze > requirements.txt
⏰Day-2
- Implementing App in Src- Adding class HttpRequest to Http_types
- Implementing View for tag creator with Http Types
- Adding class BarcodeHandler to Drivers
init.py:
This file is responsible for allowing imports inside the folders. All folders that need imports in their functions must have one of these files. Even if the folders were cascading, each folder must have a file init.py.
Code refactoring
The application's main responsibilities have been better organized and distributed. For instance, the framework's primary folder is now solely responsible for any changes to the framework, making it easier to manage and maintain. Additionally, all components related to the HTTP protocol and business rules logic have been consolidated in specific locations. These changes have been implemented to enhance the application's scalability.
Blueprints
Blueprints simplify the identification of each application route's role and contribute to better code organization and readability, making it a valuable library in the Flask framework.
Controllers folder
Our business rules are located in this place.
Drivers folder e Barcode_handler.py
'Drivers' is the place where we concentrate all external libraries. In our code, 'Barcode_handler.py' acts as a central point for accessing the external libraries. This means that if any other file needs to access an external library, it can only do so through barcode_handler.py. This is a principle of good practice.
⏰Day-3
- Implementing Error Handler - Implementing Validator - Implementing Unit TestCerberus e Validator_raw.py
To avoid creating each input validation manually, we can use the 'Validator' from the Cerberus library.
Mock_value
In order to avoid the redundant generation of barcodes for every test, a duplicate or mirrored copy was created, which fulfills the requirements of the unit test without actually creating anything. This copy returns the desired value through 'mock_value', as shown in the code snippet below.
from unittest.mock import patch
from src.drivers.barcode_handler import BarcodeHandler
from .tag_creator_controller import TagCreatorController
@patch.object(BarcodeHandler, 'create_barcode')
def test_create(mock_create_barcode):
mock_value = "image_path"
mock_create_barcode.value = mock_value
tag_creator_controller = TagCreatorController()
result = tag_creator_controller.create(mock_value)