Archive

Archive for the ‘python’ Category

Get the value of PI without a constant

December 24, 2025 Leave a comment

Problem

You are using a new programming language and you need the value of PI. However, there’s no such constant in the language. What do you do?

Solution

You can copy the value of PI from somewhere and create your own constant. Or, you can use a math function for that.

>>> import math
>>> from math import *
>>> 
>>> cos(math.pi)
-1.0
>>> acos(-1)
3.141592653589793
>>> acos(-1) == math.pi
True

I must have learnt it in high school, but it was long forgotten. It was nice to re-discover it :)

Fortran

At the end of December 2025, I started to learn Fortran (just for fun). Actually, Modern Fortran is pretty good! I like it. However, the constant PI is missing. So I came up with this:

use iso_fortran_env, only: real64
real(real64), parameter :: PI = acos(-1.0_real64)

Categories: fortran, python Tags: , ,

How to solve a two-variable equation with Sympy

December 13, 2024 Leave a comment

Problem

You have a two-variable equation and you want to solve it with Python / Sympy.

Solution

Let’s see this example:

7x + 9y = 8
9x - 8y = 69

What is the value of ‘x’ and ‘y’?

We could solve it manually too, of course, but let’s say we have hundreds of these. So let’s solve it with Python using the Sympy library.

>>> import sympy as sp
>>>
>>> x = sp.symbols('x')
>>> y = sp.symbols('y')
>>>
>>> eq1 = sp.Eq(7 * x + 9 * y, 8)
>>> eq2 = sp.Eq(9 * x - 8 * y, 69)
>>>
>>> system = [eq1, eq2]
>>>
>>> sol_set = sp.linsolve(system, x, y)
>>> sol_set
{(5, -3)}
>>>
>>> 7 * 5 + 9 * (-3)
8
>>> 9 * 5 - 8 * (-3)
69
Categories: python Tags: , , , ,

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: , ,
Design a site like this with WordPress.com
Get started