OpenAPI is a documentation format created by Swagger to document how a web service works. It defines the paths, the methods and parameters. Let’s know how to quickly create web services using OpenAPI
Why I use it?
Usually documentation is one of the tasks that developers don’t like because it can be boring, hard, and take time. In this case it is really quick and it can be a way to increase your productivity, because this documentation can be used to generate.
Starting with OpenAPI
The OpenAPI format can be generated and viewed with a lot of tools, but my favorite is Apicurio, a free tool from Redhat. A great feature of it is that it can be collaborative
The Apicurio screen is splited on 4 parts
Paths: In this part you can see the paths the application uses, you can also click on it to view more details.
Data Types: The data types that the application will use and the relation with other data types.
Responses : The diferent types of responses of the application
Design: The content of this part can change to edit the details of the previous parts
This is a short list of less known tricks that I use in Python
Delete repeated elements of a list
An easy way to delete repeated elements from a list is to convert it to a set and then convert to a list. This works because a set can’t have repeated elements.
a = [1,1,2,3]
[1, 2, 3]
Serve static files with HTTP
The python interpreter has the SimpleHttp module which is very useful. If you run this command on a directory you will be able to access to the files thought http.
python3 -m http.server 8080
Join a list of strings with a string separator
A useful not so known str function it’s the join method. You can use this to join a list with a string
With this you don’t have to control if it’s the last element of the list to add the separator
Use a dictionary to pass parameters to a function
Sometimes you need to send data to functions and you need to adapt the same data to the function parameters, you can do this with the operator ** . This operator passes the values of the dictionary to a function using the key as a parameter name:
Sentry is another service you don’t know that you need it until you try it.Basically Sentry is a service that catches the exceptions in your code and sends it to a web service to be analyzed, also sends you an email to notify you.
Sentry server is an opensource server that can be installed on your own server, also sentry can work as a SaS and you can use it at https://sentry.io/welcome/
Sentry can work with multiple languages from Python, to JS or Go, you can check all the supported languages here https://sentry.io/platforms/
Configuring sentry with python
First of all you need to create a project like this:
Once you created the project you will get an screen like this
After this you will need to install the sentry package with this command:
pip install --upgrade sentry-sdk
You will need to add this on the package, if you use a setyp.py add it on the install_requires key or if you use it on a requirements.txt file you need to add it.
Now we need to initialize the sentry code. We do it adding this code on the entry point of the code.
"!Place here your project DSN"
With this we have our script configured to catch all the exceptions with Sentry.
We can make our code notify an exception manually with sentry with this code:
from sentry_sdk import capture_exception
catch Exception as e:
Also can notify suspicious data without sending an exception with this code:
from sentry_sdk import capture_message
Usually when you develop an application it will not run on the same computer where you are developing. This can caused unexpected behaviors when you deploy it,usually cause by permissions, software/package versions,configuration,ect.. or just that we write an error on our code that we didn’t notice. In those cases, it’s difficult to know what is failing but we can use remote debug to check the application state when it fails.
If you are trying to debug a python package and the debugger doesn’t stop, try to install it with the -e flag