The Best Ways to Compare Two Lists in Python
Miguendes's Blog
by Miguel Brito
1M ago
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 two objects in Python is a bit misleading. Most books and tutorials teach object comparison by using either the == or the is operator. In reality, these two operators cover just a small fraction of the most frequent use cases. For example: what if we want to compare a list of floating-point numbers considering a certain tolerance? what if we wish to ..read more
Visit website
Python pathlib Cookbook: 57+ Examples to Master It (2022)
Miguendes's Blog
by Miguel Brito
1M ago
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 improper path manipulation. The os module never felt intuitive and ergonomic to me, but my luck changed when pathlib landed in Python 3.4. It was a breath of fresh air, much easier to use, and felt more Pythonic to me. The only problem was: finding examples on how to use it was hard; the documentation only covered a few use cases. And yes, Python's ..read more
Visit website
How to Disable Autouse Fixtures in pytest
Miguendes's Blog
by Miguel Brito
1M ago
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 setup/teardown function. Another use case is to perform some task, like mocking an external dependency, that must happen before every test. For example, suppose you have a set of functions that execute HTTP calls. For each one, you provide a test. To ensure your test doesn't call the real API, we can mock the call using a library such responses. Howeve ..read more
Visit website
15 Easy Ways to Trim a String in Python
Miguendes's Blog
by Miguel Brito
1M ago
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 (as well as the ones inside the string). You'll also learn how to remove tabs, newlines, carriage return (CRLF), and other characters. And we'll be using nothing more than native methods and regexno external libraries required! By the end of this article, you'll have mastered: How to trim a string by stripping leading whitespace from the beginni ..read more
Visit website
How to Choose Between isdigit(), isdecimal() and isnumeric() in Python
Miguendes's Blog
by Miguel Brito
1M ago
In this post, you'll learn the subtle difference between str.isdigit, str.isdecimal, andstr.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 if the given string is a number. The string class (str) comes with 3 different methods that you can use for that purpose. Each of them has pros and cons, and distinguishing the difference between them will save you tons of development and debugging time. In this article, you will: learn what str.isdigit(), str.isdecimal(), and str.isnumeric() do, t ..read more
Visit website
One Year of Blogging
Miguendes's Blog
by Miguel Brito
1M ago
On the 31st of Aug 2020, I published my first ever blog post. Its been a wild journey, full of ups and downs, but a very rewarding one. In this post, I will detail everything Ive learned, show some numbers and plans for the next year to come. The Motivation I started blogging with two goals in mind: to share things I find interesting to improve my writing skills in English Sharing Interesting Stuff The definition of interesting is in the eye of beholder. What I find cool might not catch your attention, and some things will be considered boring. If its boring, why sharing? Had I held this ..read more
Visit website
How I Patched Python to Include This Ruby Feature
Miguendes's Blog
by Miguel Brito
1M ago
In this post, I'll present how I changed Pythons 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 Python came to my mind when I had to work with a Ruby service at my past job. Ruby, contrary to Python, makes lots of things implicit [Citation needed], and this kind of if expression is one of them. I say it's implicit because it returns nil if the expression evaluates to false. This is also called conditional modifier. $ irbirb(main):001:0> RUB ..read more
Visit website
11 Useful Resources To Learn Python's Internals From Scratch
Miguendes's Blog
by Miguel Brito
1M ago
"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 python. I find the CPython implementation so fascinating that I even started contributing to the language. The CPython runtime is the most popular one but there are a few others like pypy. Unlike pypy, the core language is written in C whereas the standard library is a blend of Python and C. For newcomers, navigating through the code can be a daunt ..read more
Visit website
How to Implement a Random String Generator With Python
Miguendes's Blog
by Miguel Brito
1M ago
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 from string.ascii_letters, string.digits + string.punctuation characters in Python 3 generate a secure random string, useful to create random passwords Generating a Random String With Upper Case, Lower Case, Digits and Punctuation The string module comes with a nice set of constants that we can combine with the random module to create our random s ..read more
Visit website
How to Check If a String Is a Valid URL in Python
Miguendes's Blog
by Miguel Brito
1M ago
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 see how we can leverage a third-party URL validator to do the job for us. Using the validators package The validators package is a tool that comes with a wide range of validation utilities. You can validate all sorts of inputs such as emails, IP addresses, bitcoin addresses and, of course, URLs. The URL validation function is available in the root ..read more
Visit website

Follow Miguendes's Blog on FeedSpot

Continue with Google
Continue with Apple
OR