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:

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

C

Code Credits: One second code

Compile in normal mode:

Run:

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

Furthermore, C can be compiled in optimized mode for a better performance. …


A collection of my favorite Python debugging options.

Image for post
Image for post
Photo by Danial RiCaRoS on Unsplash

Print

Output:

Logging

Basic

cat a.log:

More Config

cat b.log:

Multiple Log Files

Use different logger names (logname) for different log files.


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

Image for post
Image for post

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

Image for post
Image for post

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.

Image for post
Image for post

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 recognition api for Python and the command…


Image for post
Image for post

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.


Image for post
Image for post

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


Image for post
Image for post

There are three methods to format a string in Python:

  • %-format - Python 2+
  • {} str.format() - Python 3.1+
  • f-string - Python 3.6+

Note: From version 3.1, the positional argument specifiers can be omitted for str.format(), so is equivalent to though the method was firstly introduced in version 2.6.

I was confused when I started learning Python. If you have the same feeling, I recommend you have a read of Roberto Preste’s post first. He explains quite well what they are. Basically, they are equally effective and efficient. You can just pick one you like. …


Imagine you have written REST API function tests in Python, how to scale them up to do performance tests?

Image for post
Image for post

I have previously written an article about how to create REST API function tests using Python. In this article, I will continue to explain how you can use existing function tests and scale them up to do performance tests, using Python modules requests, threading, and queue.

Let’s use the same flask mock service endpoint I used for the functional tests, but just add a to simulate network delay 0.2 seconds.

Save the code as a file, e.g. flask_mock_simple_service.py, …

About

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