Developer Blog

by Roman Soldatow


  • Home

  • Tags

  • Archives

  • Projects

  • Imprint

Python Logging Benchmark

Posted on 2019-09-08

If you have a heavy logging application, you might want to take into consideration to use the %-format strings for logging.
The Python logging module recommends to use the %-format.
But I guess most of the developers will prefer the f-string format, which came with the Python 3.6 release, because it simply easier to read.

My question was: Which impact does it have on performance? In case, we use logging with string concatenation.

So, I wrote a short script to “benchmark” it (and yes, I know it is not a proper benchmark).

Compared was: logging and concatenation with ‘+’ vs formatting with % vs f-strings, over multiple runs in a different order of the benchmark functions. So that, we can at least exclude the cold start issue.

Benchmark Diagram

If we take an average of all measuring points and compare them with each other, we would come to the result:

formatting with %concatenation with +f-strings
7.087545455 sec7.588454545 sec7.707727273 sec
100%107.06%108.75%

That means that, formatting with % is 8.75% faster than with f-string or 7.06% faster than with concatenation. At least in the combination with “deactivated” logging.

The reason for that is probably, because the string will be concatenated anyway, even if we don’t log anything. But in case of formatting with %, the string will be not concatenated.

Read more »

Python Common Style Guidelines

Posted on 2019-08-31

One of the best practices is, to define a common style guideline. So that it would be much easier to understand a codebase when all the code in it, is in a consistent style. Specially if you are working in a team. PEP (Python Enhancement Proposals) provides a good starting point to define those style guide.

Here are some highlights which are mostly from PEP 8, but also from other best practices:

Naming Conventions

Avoid one letter variables like x and y (Except in very short code blocks). Give your variables and functions a meaningful name and use follow naming convention:

  • Variables, functions, packages and modules names should be:

    1
    lower_case_with_underscores
  • Classes and Exceptions

    1
    CamelCase
  • Protected methods

    1
    _single_leading_underscore
  • Private methods

    1
    __double_leading_underscore
  • Constants

    1
    ALL_CAPS_WITH_UNDERSCORES
Read more »

My favorite Visual Studio (VS Code) Extensions

Posted on 2019-08-15

There are a lot of well known extensions like GitLens, Prettier or ESLint, but I would like to show you less known but very helpful extensions.

Version Lens

Shows package version information for npm, jspm, dub and dotnet core

"Version Lens"

Shell launcher

Easily launch multiple shell configurations in the terminal.

"Shell launcher"

Read more »

ESP8266 Weather Station / Temperature Sensor

Posted on 2019-06-18

Introduction

ESP’s are great to give your sensors the possibility to extract the sensor data, because of the on-board WIFI chip, which can connect to your network.
Imagine you have multiple sensors, and you would like to access the measured data and show it in Home Assistant. You can either spin up a web server on your ESP and pull the data or send data from your ESP to your Home Assistant.

The advantage of the push strategy is power saving through deep sleep mode of ESP, which will be explained later.

Components

  • The KY-013 Analog Temperature Sensor
  • ESP8266 from mine previous post

In my case the + and - pin label was incorrect and I had to swap them. So that the orange wire is attached to + (and not -).

Read more »

Home Assistant - Smart Life components

Posted on 2019-02-18

Home Assistant unfortunately doesn’t support Smart Life components (yet) (e.g. Switches from Gosund, COOSA or Teckin), but fortunately IFTTT works great with Smart Life components, which can be connected to Home Assistant.

In my example, I will show you how to turn on/off lights by using Teckin Switches with help of IFTTT. The result in Home Assistant will like that:

Read more »
1234…6

Roman Soldatow

Developer Blog over coding and embedded systems (DIY) projects, and also some tips and tricks around the IT world.
27 posts
13 tags
RSS
GitHub StackOverflow
© 2020 Roman Soldatow