The operator Module and Functional Programming

Functional programming is a paradigm that consists of building a program from small functions that perform specific tasks and that can be passed as arguments to other functions to express complex operations. The standard operator module makes it easy to write functional code (along with the functools and itertools modules) by exposing Python operators (for example, the addition operator + or the item access operator []) as functions. This is useful because functions, unlike operators, can be stored in variables and passed as arguments.

Read more…

How to pip install From a Git Repository

You can install a Python package from a Git repository by running the pip install git+ command followed by the URL of the repository. For example, to install the Requests module from its GitHub repository:

pip install git+

This requires Git installed, since the git command is executed within a subprocess.

Read more…

Simple Notepad With Tk (tkinter)


Here's a simple text file editor written in Python with the tkinter standard module. The program is made up of a simple window, a menu bar menu bar and a tk.Text widget to display the content of the files. To allow the user to browse files , the tkinter.filedialog standard module is used. Text files are written to and read via the standard pathlib.Path class. The code includes type annotations, so it requires Python 3.9 or higher. The rest is enough commented below.

Read more…

Automate WordPress Post Share on Twitter

The following Python script will pick up a random post from your WordPress MySQL database and share it on Twitter. Since even simple shared hosting providers support Python, you can set up this script as a cron jon to share posts periodically. Shared posts are temporary saved in the database in order to not repeat them until a certain amount of shared posts has been reached (customizable via the --repeat-limit option). PyMySQL and Tweepy are required, so make sure to install them before running the script:

pip install pymysql tweepy

When invoking the script you will need to pass your MySQL, Twitter API and SMTP credentials. SMTP is used to send an email with the proper traceback when the execution fails (but feel free to comment out the send_error_email() definition and call if you don't need that feature.)

Read more…

Browse File or Folder in Tk (tkinter)


Tk provides functions to display dialogs for browsing files or folders on Windows, Linux (or any other Unix-based system) and macOS. In Python those functions are accessible through the tkinter.filedialog standard module. The most important ones are:

  • askopenfilename() / askopenfilenames()

  • askdirectory()

  • asksaveasfilename()

Read more…

Duplicate File Finder Script


The following cross-platform Python script lets you find duplicate files within a directory tree. Files are considered duplicates when they have the same name and content. First, the script walks through the directory tree by using the os.walk() standard function and stores the MD5 digest of every file found in the process. The threading module is used to spawn multiple workers (as much as available cores) in order to speed up this process. Second, file names and MD5 digests are compared to find duplicate files and write the results into duplicate_files.txt.

The code is simple, type-annotated (checked against mypy) and much commented so it can be easily customized. Python 3.9 or greater is required.

Read more…

Find Biggest File in Directory Tree


The following cross-platform Python script allows you to search for the biggest file within a directory and all its subfolders (the search is recursive). The directory path must be passed as the first argument when invoking the script. Additionally, a second argument indicating a file extension can optionally be passed to limit the search.

Read more…

Display Image in Tk (tkinter)

An image can be loaded from disk and displayed within a label (tk.Label or ttk.Label) by using the tk.PhotoImage class:

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Display Image in Tk")
# Load image from disk.
image = tk.PhotoImage(file="image.png")
# Display it within a label.
label = ttk.Label(image=image)

Read more…