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...
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...
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...
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 ...
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...
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 (...
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 ...
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...
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 ...
"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...
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 ...
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 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...
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. >...
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,...
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...
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...
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...
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-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