Autoformat your Python code with Ruff

March 11, 2024 Leave a comment

Problem

You use VS Code. When you save a Python file, you want the source code to be autoformatted.

Solution

Use Ruff for that. Follow these steps:

  1. Install the Ruff VS Code extension from Astral Software (extension ID: charliermarsh.ruff).
  2. Add the following lines to your settings.json :
"[python]": {
    "editor.tabSize": 4,
    "editor.insertSpaces": true,
    "editor.defaultFormatter": "charliermarsh.ruff",
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
      "source.organizeImports": "explicit"
    },
    "editor.formatOnType": true
  },

3. Optionally, you can create a config file for Ruff. Under Linux, its location is ~/.config/ruff/ruff.toml . Here is a basic config file:

# on Linux:     ~/.config/ruff/ruff.toml
# on Windows:   C:\Users\Alice\AppData\Roaming\ruff\ruff.toml

line-length = 100

[lint]
# rule IDs: https://docs.astral.sh/ruff/rules/

# ignore = ["E203","F401","F841","E712","E722"]
ignore = ["E722"]

Now, if you save a Python file in VS Code, you’ll get the following goodies:

  1. The code will be formatted automatically.
  2. The import lines will be sorted.
  3. A linter will analyze your code and indicate problems.

Links

  • My complete config files are here
Categories: python Tags: , , , , ,

Play a sound file from memory

February 6, 2024 Leave a comment

Problem

I wanted to play short sound files a lot of times. Calling an external program each time to play a file would have been too slow. How to read sound files in the memory and play them from there?

Solution

First I tried the package simpleaudio. It worked well but it turned out that it couldn’t play every .wav file. VLC could play my file correctly, but simpleaudio just produced some noise. So I needed another solution.

And that’s how I found soundfile and sounddevice. Their usage is very simple:

import soundfile as sf
import sounddevice as sd

samples, samplerate = sf.read('file.wav')
sd.play(samples, samplerate)
sd.wait()

To use them, you also need to install numpy.

More info here: https://stackoverflow.com/a/42387773/232485

Update:

I had some .wav files that were not played correctly with soundfile+sounddevice. So I ended by using soundfile+sounddevice for certain files and by using simpleaudio for some other files.

Links

Categories: python Tags: , , ,

Handle extended file attributes

February 6, 2024 Leave a comment

Problem

You want to handle extended file attributes from Python.

Solution

If you are not familiar with extended file attributes, you can read more about it here. I also have a blog post (see here), in which I show how to set/remove extended file attributes using the attr Linux command.

To handle the extended file attributes from Python, you can use the pyxattr package: https://github.com/iustin/pyxattr

Its usage is very simple:

>>> import xattr
>>> xattr.listxattr("main.py")
[b'user.com.dropbox.attrs']
>>> xattr.getxattr("main.py", "user.com.dropbox.attrs")
b'\n\x12...'
>>> xattr.setxattr("main.py", "user.com.dropbox.ignored", "1")
>>> xattr.listxattr("main.py")
[b'user.com.dropbox.attrs', b'user.com.dropbox.ignored']
>>> xattr.getxattr("main.py", "user.com.dropbox.ignored")
b'1'
>>> xattr.removexattr("main.py", "user.com.dropbox.ignored")
>>> xattr.listxattr("main.py")
[b'user.com.dropbox.attrs']

A typical use case: you want Dropbox to ignore your venv/ folder inside your project.

Links

keysound

February 5, 2024 Leave a comment

Problem

I have a non-mechanical keyboard. However, when I press keys, I also want to hear some insane clickytty clicks. What to do?

Solution

If you don’t want to buy a new keyboard, you can try my keysound project. When you press a key, a short sound file is played.

At the bottom of the README of this project’s GitHub page, I also collected some related work, thus there are several alternatives available.

Categories: python Tags: , , ,

Sum the digits of a number until you get just one digit

Problem

Take a positive integer and sum its digits. Repeat this process until you get just one digit.

Example: 1472 🠖 1+4+7+2 = 14 🠖 1+4 = 5. The answer is 5.

Solution

# Python code
n = 1472
result = ((n-1) % 9) + 1

Credits

Thanks to my friend Mocsa who told me this math trick.

Categories: python Tags: , ,

Merge Overlapping Intervals

December 17, 2022 Leave a comment

Problem

You have some overlapping closed intervals and you want to merge them. What is the algorithm for this?

Solution

Here are some examples with input and output:

Input1: Intervals = [[1, 3], [2, 4], [6, 8], [9, 10]]

Output1: [[1, 4], [6, 8], [9, 10]]


Input2: Intervals = [[6, 8], [1, 9], [2, 4], [4, 7]]

Output2: [[1, 9]]

And the algorithm:

def merge_intervals(intervals: list[list[int]]) -> list[list[int]]:
    # Sort the array on the basis of start values of intervals.
    intervals.sort()
    stack = []
    # insert first interval into stack
    stack.append(intervals[0])
    for curr in intervals[1:]:
        # Check for overlapping interval,
        # if interval overlap
        if stack[-1][0] <= curr[0] <= stack[-1][-1]:
            stack[-1][-1] = max(stack[-1][-1], curr[-1])
        else:
            stack.append(curr)
        #
    #
    return stack

Links

Categories: python Tags: , ,

Compile Python module to C extension

I found an interesting blog post: You Should Compile Your Python And Here’s Why. In short: if you have a type-annotated Python code (mypy), then with the command mypyc you can compile it to a C extension. Under Linux, the result will be an .so file, which is a binary format.

Here you can find a concrete example: https://github.com/jabbalaci/SpeedTests/tree/master/python3

Steps:

  • We need a type-annotated source code (example). It’s enough to add type hints to functions. It’s not necessary to annotate every single variable.
  • Compile it (mypyc main.py). The result is an .so file.
  • You can import the .so file as if it were a normal Python module.
  • If your project consists of just one file (main.py) that you compiled, here is a simple launcher for it:
#!/usr/bin/env bash

python3 -c "import main; main.main()"

If you do a lot of computation in a function, then with this trick you can make it 4-5 times faster.

I don’t think I would use it very often, but it’s good to know that this thing exists. And all you have to do is to add type hints to your code.

Links

Categories: python Tags: , , ,

Simulate keypresses

March 3, 2022 Leave a comment

Problem

You want to simulate keypresses from a Python program.

Solution

Use PyAutoGUI. Easy to install, easy to use. The keyboard control functions are here.

Categories: python Tags: , ,

upgrade all your poetry packages

February 22, 2022 Leave a comment

Problem

You have a project and you use poetry for managing it. You want to upgrade all the dependencies to the latest versions and you also want to update the dependency versions in the file pyproject.toml .

Solution

Use poetryup. See here too: https://github.com/python-poetry/poetry/issues/461#issuecomment-921244993 .

[Windows] clear screen in the Python shell

February 22, 2022 Leave a comment

Problem

Under Linux, when using the Python shell, it’s very easy to clear the screen. Just press Ctrl+L. But what about Windows? Ctrl+L doesn’t work there :( Is there a painless way to clear the screen under Windows?

Solution

We’ll create a function called cls() that will clear the screen. Usage example:

C:\> python
>>> a = 5
>>> cls()

For this, create a file called .pystartup (the name doesn’t matter). Add the following function to it:

def cls():
    """
    clear screen for Windows
    """
    import os
    cmd = 'cls' if os.name == 'nt' else 'clear'
    os.system(cmd)

Create a new environment variable called PYTHONSTARTUP and assign the path of .pystartup to it. Example:

PYTHONSTARTUP=C:\opt\.pystartup

Open a new terminal. Now, if you start the Python shell, it’ll read the content of the file above. Thus, the function cls() will be available in all your Python shell sessions.

It also works under Linux. You can add more utility functions to .pystartup (however, I think it’s a good idea to keep it minimal).

Categories: python Tags: , , , , ,