install PyQt5

The following is based on this YouTube video.

$ sudo apt install python3-pyqt5
$ python3
>>> import PyQt5
$ sudo apt install python3-pyqt5.qtsql
$ python3
>>> from PyQt5 import QtSql
$ sudo apt install qttools5-dev-tools
$ ls -al /usr/lib/x86_64-linux-gnu/qt5/bin/designer
lrwxrwxrwx 1 root root 25 ápr   14 09:38 /usr/lib/x86_64-linux-gnu/qt5/bin/designer -> ../../../qt5/bin/designer

I put a symbolic link on designer to launch it easily.

Categories: PyQt5, python

word cloud generator

March 12, 2018 Leave a comment


Categories: python Tags:

unzip: perform the opposite of zip

March 8, 2018 Leave a comment


>>> a = [1, 2, 3]
>>> b = ["one", "two", "three"]
>>> zip(a, b)
<zip object at 0x7fd30310b508>
>>> list(zip(a, b))
[(1, 'one'), (2, 'two'), (3, 'three')]

How to perform the opposite of zip? That is, we have [(1, 'one'), (2, 'two'), (3, 'three')], and we want to get back [1, 2, 3] and ["one", "two", "three"].

>>> li
[(1, 'one'), (2, 'two'), (3, 'three')]
>>> a, b = zip(*li)
>>> a
(1, 2, 3)
>>> b
('one', 'two', 'three')

Notice that the results are tuples.

More info here.

Categories: python Tags: ,

sanitizing tweets

February 12, 2018 Leave a comment

You have the text of a tweet and you want to get rid of the bullshit (smileys, emojis, etc.)

See It’s customizable, you can select what to remove, e.g. URLs, smileys, etc.

What are the built-in functions?

January 19, 2018 Leave a comment

How to figure out the built-in functions in Python? Of course, you can look up the documentation, but now the exercise is to list them in the Python shell.


In [1]: import builtins

In [2]: dir(builtins)
Categories: python Tags:

BASE64 as URL parameter

January 1, 2018 Leave a comment

In a REST API, I wanted to pass a URL as a BASE64-encoded string, e.g. “http://host/api/v2/url/aHR0cHM6...“. It worked well for a while but I got an error for a URL. As it turned out, a BASE64 string can contain the “/” sign, and it caused the problem.

Replace the “+” and “/” signs with “-” and “_“, respectively. Fortunately, Python has functions for that (see here).

Here are my modified, URL-safe functions:

def base64_to_str(b64):
    return base64.urlsafe_b64decode(b64.encode()).decode()

def str_to_base64(s):
    data = base64.urlsafe_b64encode(s.encode())
    return data.decode()

You can also quote and unquote a URL instead of using BASE64:

>>> url = ""
>>> import urllib.parse
>>> new = urllib.parse.quote(url)
>>> new
>>> 'https%3A//'    # notice the "/" signs!
>>> urllib.parse.quote(url, safe='')
>>> ''    # no "/" signs!
>>> new = urllib.parse.quote(url, safe='')
>>> urllib.parse.unquote(new)
>>> ''
Categories: python Tags: , , ,

convert a file to an UTF-8-encoded text

December 16, 2017 Leave a comment

I wrote a simple script that takes an input file, changes its character encoding to UTF-8, and prints the result to the screen.

It’s actually a wrapper around the Unix commands “file” and “iconv“. The goal was to make its usage as simple as possible. The script is here:


$ input.txt

The program tries to detect the encoding of the input file.


Categories: bash, python Tags: ,