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

Categories: python

[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: , , , , ,

convert your book to an audiobook

October 26, 2020 Leave a comment

Problem

You need the audiobook version of your favourite book. However, you are on a strict budget and you don’t want to buy the audiobook. You want to make your own audiobook, for free, of course.

Solution

I found this project idea on YouTube, see here. The first 2 minutes were more than enough for me :), but I found the project idea interesting. The source code of the guy is here.

I tried it under Linux and it sounded like a robot. Can we do it better? Of course! I won’t give a complete solution but you can put together your own solution using my previous 2 posts:

Categories: python Tags: ,

text to speech

October 26, 2020 Leave a comment

Problem #1

You have a text and you want to convert it to audio (possibly to .mp3).

Solution #1

The gTTS module can do this for you.

gTTS (Google Text-to-Speech), a Python library and CLI tool to interface with Google Translate’s text-to-speech API. Writes spoken mp3 data to a file, a file-like object (bytestring) for further audio manipulation, or stdout. It features flexible pre-processing and tokenizing, as well as automatic retrieval of supported languages.” (source)

Example:

from gtts import gTTS

TEXT = """
Python is an interpreted, high-level and general-purpose programming
language. Created by Guido van Rossum and first released in 1991, Python's
design philosophy emphasizes code readability with its notable use of
significant whitespace. Its language constructs and object-oriented
approach aim to help programmers write clear, logical code for small
and large-scale projects.
""".replace("\n", " ")

def main():
tts = gTTS(text=TEXT, lang='en')
tts.save("audio.mp3")
print("done")

The text is read by a pleasant female voice in good quality.

The reader takes a little pause at every end of line, that’s why newline characters are replaced by a space. This way the reading is fluid.

Problem #2

There is one little problem. I find the reading speed in audio.mp3 a bit slow. When you watch a YouTube video, there you have the possibility to speed up the audio by 25%, 50%, etc. How to play back audio.mp3 a bit faster?

Solution #2

\$ play audio.mp3 tempo 1.15

or

\$ mplayer audio.mp3 -speed 1.15 -af scaletempo

It means 15% faster playback.

gTTS from the command-line

If you install the package gtts, you also get a command-line program called gtts-cli. Some examples:

\$ gtts-cli --all

^^^ List all languages. At the time of writing, the list contains 78 entries. (Hungarian is also there).

\$ gtts-cli 'hello' | play -t mp3 -

^^^ Play the sound directly.

\$ gtts-cli "bonjour tout le monde" --lang fr | play -t mp3 -

^^^ Specify the language of the text.

\$ gtts-cli "c'est la vie" --lang fr --output cestlavie.mp3

^^^ Save the sound in an .mp3 file.

Links

Notes

I also tried the pyttsx library under Linux but the quality was terrible. It calls the command espeak and it reads the text like a f* robot. No, thanks. Maybe it sounds better under Windows (under Windows there is a different text to speech engine); I didn’t try that.

Categories: python Tags: , , ,

extract text from a PDF file

October 26, 2020 Leave a comment

Problem

You have a PDF file and you want to extract text from it.

Solution

You can use the PyPDF2 module for this purpose.

import PyPDF2

def main():
book = open('book.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(book)
pages = pdfReader.numPages
page = pdfReader.getPage(0)    # 1st page
text = page.extractText()
print(text)

Note that indexing starts at 0. So if you open your PDF with Adobe Reader for instance and you locate page 20, in the source code you must use getPage(19).

Links

Exercise

Write a program that extracts all pages of a PDF and saves the content of the pages to separate files, e.g. page0.txt, page1.txt, etc.

Categories: python Tags: , , ,

Python Developer’s Guide

September 15, 2020 Leave a comment

If you want to contribute to the Python language, start here: https://devguide.python.org .

Categories: python

Python 3.9 is coming

September 15, 2020 Leave a comment

See this post to have an idea of the new features of Python 3.9: https://ayushi7rawat.hashnode.dev/python-39-all-you-need-to-know .

More details here: https://docs.python.org/3.9/whatsnew/3.9.html .

Categories: python Tags: