This is part of the REST API Testing in Python series.
This post will show you how to build a dynamic REST API mock service with magic using Flask before_request decorator.
The traditional approach of mock service is to build a mock service for each endpoint, and for the same endpoint you need to modify the mock service when you want to test a different scenario, e.g. return an error instead of a successful response, not to mention different mocks for different request methods, i.e. GET, POST, PUT, etc. …
During the covid-19 pandemic, most people work from home and VPN is a common approach to access insecure private network servers like an HTTP server without access authentication. Most modem servers have security control and can be moved to the internet for direct access by assigning a public IP address and a domain name, such as JIRA and Confluence servers. So there should be a very limited number of legacy servers that are kept in the private network and are protected by VPN, i.e. only accessible from the private network. …
To compare the computation performance between Python and C languages, let’s do a loop for sum in one second. The code itself is pretty much self-explanatory.
time python python_loop.py 10000000
It is 10 millions loops in a second for Python. It sounds not bad.
Compile in normal mode:
gcc c_loop.c -o c_loop
time ./c_loop 450000000
It is 450 millions loops in a second, which is 45 times faster than Python.
Furthermore, C can be compiled in optimized mode for…
Sat Jul 4 13:45:52 2020 debugging ...
logging.basicConfig(filename = 'a.log')
log = logging.getLogger()
import logging# DEBUG, INFO, WARNING, ERROR, CRITICAL
log_level = logging.INFO
logging.basicConfig(filename = 'b.log',
filemode='w', # or 'a'
format='%(asctime)s %(levelname)s: %(message)s',
)log = logging.getLogger()
log.info('Some info log')
log.debug("Won't print at INFO level")
2020-07-04 17:50:42,953 INFO: Some info log
Use different logger names for different log files.
import logginglog_level = logging.INFO
def create_logger(filename, logname=''):
handler = logging.FileHandler(filename)
formatter = logging.Formatter(…
Let’s start with a perfect concurrent example #1.
Async function say_after is an example from Python official documentation. It prints something ‘what’ after sleeping ‘delay’ seconds.
In the main function, we create two tasks of say_after, one says ‘hello’ after 1 second and the other say ‘world’ after 2 seconds. Run it and we see it takes 2 seconds in total because both tasks run concurrently. Perfect!
This post will explain how to set up an app password in Google account and use Python to send emails in a few lines of code for automatic reporting, test automation or CI/CD failure notification etc.
To use a Gmail account to send emails with a third party app, e.g. Python script, in this case, we need to set up an app password. For security reasons, the normal Gmail password is restricted to web login only. …
There are many open-source face recognition packages like face_recognition which you can easily install on Linux servers. But it is very difficult or impossible to deploy them on mobile and IoT devices. One option is to use machine learning mobile frameworks such as TensorFlow Lite to call pre-trained models.
But are there easier options? Yes! With 5G coming, it will take only 0.01 second to upload a 100KB image at a speed of about 100Mbps, so we can deploy almost everything including face recognition as a service on server-side and a light app on client-side. …
Have you ever used bank/payment app face login and been asked to open mouth, nod or turn head? Such methods have been very popular, especially in China, to prevent deceiving/hacking using static face images or 3D prints. I spent about two days and finally figured out a quite simple way to detect mouth open utilizing the feature outputs from the face_recognition project.
Here is a quick look at the effect when applying the algorithm to real-time webcam video.
The world’s simplest facial…
Glances + Influxdb + Grafana are a good combination to monitor Linux server performance stats. I will show how to set it up on Ubuntu as an example.
These three components work together as below.
I was reading a machine learning book and learned that edges are important feature inputs for machines to learn if there is an object in the picture, a face in this case. Look at the figure with only edges on the left, you can easily tell it is a face by human eyes, isn’t it? That helps machines the same way.
Originally I thought finding edges itself requires some ‘Artificial Intelligence’. But I remember Python PIL library has find_edges filter, which definitely is not a machine learning function. …