RSSAmplifier

Blog

A blog about Python, testing, and best practices

Helping developers learn more about Python, testing, and best practices. I share everything I know through tutorials, and code examples.

miguendes.meRSS feed ↗20 posts

Latest posts

The Best Ways to Compare Two Lists in Python

A while ago I wrote a guide on how to compare two dictionaries in Python 3, and how this task is not as simple as it might sound. It turns out comparing two lists in Python is just so tricky as comparing dicts. The way we've been taught to compare tw...

How to Compare Two Strings in Python (in 8 Easy Ways)

Comparing strings is a fundamental task common to any programming language. When it comes to Python, there are several ways of doing it. The best one will always depend on the use case, but we can narrow them down to a few that best fit this goal. In...

Pylint: How to fix "c0209: formatting a regular string which could be a f-string (consider-using-f-string)"

Some weeks I ago I faced this problem in one of my projects after upgrading pylint to 2.11. The error was: script.py:7:8: C0209: Formatting a regular string which could be a f-string (consider-using-f-string) At first I found it very confusing; my c...

Python pathlib Cookbook: 57+ Examples to Master It (2022)

When I started learning Python, there was one thing I always had trouble with: dealing with directories and file paths! I remember the struggle to manipulate paths as strings using the os module. I was constantly looking up error messages related to ...

How to Disable Autouse Fixtures in pytest

pytest is a very robust framework that comes with lots of features. One such feature is the autouse fixtures, a.k.a xUnit setup on steroids. They are a special type of fixture that gets invoked automatically, and its main use case is to act as a set...

15 Easy Ways to Trim a String in Python

I'm not gonna lie. There are multiple ways you can trim a string in Python. But... the truth is, you don't need to know every one of them. In this article, you'll see only the most important techniques, such as stripping leading and trailing spaces (...

How to Choose Between isdigit(), isdecimal() and isnumeric() in Python

In this post, you'll learn the subtle difference between str.isdigit, str.isdecimal, and str.isnumeric in Python 3 and how to choose the best one for the job. When processing strings, usually by reading them from some source, you might want to check ...

One Year of Blogging

On the 31st of Aug 2020, I published my first ever blog post. It’s been a wild journey, full of ups and downs, but a very rewarding one. In this post, I will detail everything I’ve learned, show some numbers and plans for the next year to come. The M...

How I Patched Python to Include This Ruby Feature

In this post, I'll present how I changed Python’s source code and compiled from scratch to accept "else-less" if expressions, similar to Ruby's "inline if", also known as conditional modifier 👇 Why? The idea of having an else-less if expression in ...

11 Useful Resources To Learn Python's Internals From Scratch

"How does Python work internally?" I have been asking myself that question for the past few months and now it seems that I'm starting to grasp, slowly... During this time, I have grown a strong interest in learning more about the internal working of...

How to Implement a Random String Generator With Python

In this post, you'll learn how to create a random string in Python using different methods; but, beware! Some of them only work with Python 3.6+. By the end of this article, you should be able to: use the choice function to generate a random string ...

How to Sort a Dict in Descending Order by Value With Python

In this post, you will learn how to sort a Python dictionary by value descending i.e. in reverse order. Say that you have the following dictionary containing your grades associated with a subject. You want to sort the values, in this case the grades,...

How to Check If a String Is a Valid URL in Python

How to check if a URL is valid in python? You'll be surprise how easy it is to check that. In this article you'll learn how to determine if a string is a valid web address or not. The good new is, you don't need to write your own URL validator. We'll...

How to Find the Current Working Directory in Python

Python provides two different ways to get the current working directory. The first method uses the os module and the second uses the newer pathlib. Using the os Module to Get the Current Directory First thing you need to do is to import the module. >...

7 Different Ways to Flatten a List of Lists in Python

Ever wondered how can you flatten, or unnest, a 2D list of lists in Python? In another words, how to turn 2-D lists into 1D: [[1, 2], [3, 4], [5, 6, 7], [8]] -> [1, 2, 3, 4, 5, 6, 7, 8]? [[1, 2], [4, 5], [[[7]]], [[[[8]]]]] -> [1, 2, 3, 4, 5, 6, 7,...

Design Patterns That Make Sense in Python: Simple Factory

In the first post of this series, I'll talk about Design Patterns that make sense in Python. We'll see how to implement them and how they are used in the standard library and other third-party packages. We'll see what is and how we can use the Simple...

How to Pass Multiple Arguments to a map Function in Python

Introduction The map() function is everywhere in Python. It's a built in, it's part of the concurrent.futures.Executor, and also multiprocessing.Pool; but... it's limited! It's limited because you cannot pass multiple arguments to it. However, what i...

How to Use Fixtures as Arguments in pytest.mark.parametrize

TL;DR Time is a precious resource so I won't waste yours. In this post, you'll learn how to use a pytest fixture in parametrize using a library or getfixturevalue. Introduction In this post, we'll see how we can use pytest.mark.parametrize with fixtu...

How to Use datetime.timedelta in Python With Examples

In this tutorial, you'll learn how to use datetime.timedelta to perform date arithmetic. With timedelta you can add days, minutes, seconds, hours, weeks and more to a datetime.date, or a datetime.datetime object. You'll also learn how to: convert a...

Python F-String: 73 Examples to Help You Master It

Python f-strings are impressive! Did you know you can use f-strings to string format almost anything in Python? You can use them to format floats, multiline strings, decimal places, objects and even use if-else conditionals within them. In this pos...

A blog about Python, testing, and best practices · RSS Amplifier