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.

Photo by Artur Tumasjan on Unsplash

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. …


Photo by Aaron Burden on Unsplash

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. …


45,000 times slower!

OMG
OMG
Photo by Ben White on Unsplash

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.

Python

Python code

Run:

time python python_loop.py 10000000
real 0m1.044s
user 0m1.004s
sys 0m0.004s

It is 10 millions loops in a second for Python. It sounds not bad.

C

Code Credits: One second code

Compile in normal mode:

gcc c_loop.c -o c_loop

Run:

time ./c_loop 450000000
s: 450000000
real 0m1.032s
user 0m1.008s
sys 0m0.008s

It is 450 million loops in a second, which is 45 times faster than Python.

Furthermore, C can be compiled in optimized…


CODEX

A collection of my favorite Python debugging options

Photo by Danial RiCaRoS on Unsplash

Print

import time
print(time.asctime(),'debugging ...')

Output:

Sat Jul  4 13:45:52 2020 debugging ...

Logging

Basic

import logging
logging.basicConfig(filename = 'a.log')
log = logging.getLogger()
log.error('Some error')

cat a.log:

ERROR:root:Some error

More Config

import logging# DEBUG, INFO, WARNING, ERROR, CRITICAL
log_level = logging.INFO
logging.basicConfig(filename = 'b.log',
level=log_level,
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")

cat b.log:

2020-07-04 17:50:42,953 INFO: Some info log

Multiple Log Files

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(…

You may have heard that Python asyncio is concurrent but not parallel programming. But how? I will explain it with simple examples.

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!


Send emails in a few lines of code

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.

Google Account Setup

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.

Face_recognition Project

face_recognition is an awesome open source project for face recognition based on dlib, just as described by itself:

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. …

Peter Xie

Code Simple | Python and AI Enthusiast @ Melbourne | peter.jp.xie@gmail.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store