Skip to content

English

Python function decorator

python function decorator post logo

This guide will show you how to create a Python function decorator with a few easy steps, to help you separate different concerns in code. This guide does not aim to provide reference material regarding decorators, but rather explain the basics and provide a starting point for implementing your own decorator.

What is a decorator?

A Python decorator is similar to a Java annotation if you are familiar with the JVM language, where you decorate or “annotate” a function prefixing the decorator’s name with @. For example, the decorator below publishes the function hello() on the url / on a Flask application:

@app.route('/')
def hello() -> str:
    return 'Hello world from Flask!'

More broadly speaking, a decorator is a structural design pattern that lets you add behaviour to an object, by wrapping its decorated function in a new function that has the same signature.

Read More »Python function decorator

Got permission denied while trying to connect to the Docker daemon socket

got permission denied while trying to connect to the docker daemon socket post logo

This guide will show you how to fix the error Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock when trying to run docker commands.

The Docker daemon binds to a Unix socket instead of a TCP port, but only root users can access this socket by default, hence, when you run docker commands without sudo, you will get the error below:

~ $ docker run hello-world

docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/create": dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.
Read More »Got permission denied while trying to connect to the Docker daemon socket

Python: Relocation R_X86_64_PC32 against symbol can not be used when making a shared object Error

This guide shows a possible solution for Python error Relocation R_X86_64_PC32 against symbol can not be used when making a shared object.

When running some Python libs such as matplotlib or pymc, you might face and error similar to below:

libpython3.9.a(bytearrayobject.o): relocation R_X86_64_PC32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
---
E           /usr/bin/ld: /home/bgasparotto/.pyenv/versions/3.9.7/lib/libpython3.9.a(longobject.o): relocation R_X86_64_PC32 against symbol `PyExc_OverflowError' can not be used when making a shared object; recompile with -fPIC
E           /usr/bin/ld: final link failed: bad value
E           collect2: error: ld returned 1 exit status

In a nutshell, this error tells us that a Python shared lib expected to be available in your Python was not found during execution.

Read More »Python: Relocation R_X86_64_PC32 against symbol can not be used when making a shared object Error

Create aliases for commands on Unix

linux shell

This tutorial will show how to create aliases for commands on Unix based systems, so you can run any set of commands with a simple shortcut.

An alias is a shortcut for a different and sometimes more complicated command, which is integrated to your bash so you can run them from any path with the help from auto-completion. for example, on Linux Mint, we have the following aliases defined by default:

alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

This means that running la in your shell will be the same as running ls -A

Read More »Create aliases for commands on Unix

Add SBT wrapper to a project

This tutorial will show how to add SBT wrapper to a project, so we don’t have to install either Sbt or Scala in order to build it.

Similarly to Gradle Wrapper, SBT users also have the ability to add a wrapper to their projects, thanks to the excellent sbt-extras project maintained by a group of more than 50 contributors up to this date. This project provides a sbt bash script that works similarly to gradlew for Gradle projects.

According to the documentation, the script will automatically detect and download the required Scala and Sbt versions on runtime. This means you can work on multiple projects with different version combinations without having to update your environment settings.

Read More »Add SBT wrapper to a project

Scala: scala.util.Try

The type scala.util.Try is an operation that may result in either an exception or a valid output. Let’s explore a few of its usages, including single and chained calls to scala.util.Try.

The Try operation will result in either a Success or Failure. To simplify, think about it an Either for successes and failures instead of Left and Right, but proper for encapsulating exceptions instead of other arbitrary values.

Read More »Scala: scala.util.Try