Useful Python tips and tricks— #1

Johni Douglas Marangon
3 min readJun 15, 2023

--

I’m improving my Python coding skills and I often learn some cool stuff that I use to improve my coding. Then I decided to write about that.

So, let’s dive right in 🚀🚀🚀.

01 — Ellipsis

The ellipsis (three dots) is a build-in constant used to represent an infinitive or something unspecified.

print(...) # Ellipsis literal

print(Ellipsis) # Ellipsis object

You can replace pass keyword with ellipses.

class MyClass: ...

def my_method():
...

02 — Merge dictionaries

There are some interesting ways to merge dictionaries. Here, I will show two of them:

Using double star **. This is a trick because all items in will be unpacking.

d1 = {"v1": 22, "v2": 33}
d2 = {"v2": 44, "v3": 55}

d3 = {**d1, **d2}
print(d3)

From Python 3.9 the | merge operator was introduced:

d1 = {"v1": 22, "v2": 33}
d2 = {"v2": 44, "v3": 55}

print(d1 | d2)

d1 |= d2
print(d1)

03 — Decorators with classes

Decorator is a design pattern that lets you add functionality to a function without modifying its source code. Decorator is a shorthand way of calling higher order functions.

class Auth():
def __init__(self, level="user"):
self.level = level

def __call__(self, function):
def wrapper(*args):
if self.level == "admin":
return function(*args)
else:
raise Exception("Not allwed!")

return wrapper
@Auth("admin")
def greet(name):
print("Wellcome!")

greet("John")

You can use functions to create decorators. The end result is the same as using classes.

But why should you use a function instead of a class to create a decorator? Nothing special, but with classes you can explore OOP concepts.

04 — Save memory with generators

The generator is a pattern that allows you to get the values lazily (on demand) saving memory usage. If you have a large list, consider using generators to save memory.

Here’s an example of how to use generator expression (also called generator comprehension):

lst1 = [i for i in range(10000)] # list comprehension
lst2 = (i for i in range(10000)) # generator comprehension

print(sum(lst1))
print(sum(lst2))

import sys

print(sys.getsizeof(lst1), "bytes") # 85176 bytes
print(sys.getsizeof(lst2), "bytes") # 104 bytes

Generator functions are a statement that returns a lazy iterator to iterate over the values on demand. Very useful when you have a large sequence of values and you don’t need to store all these items in memory. Let’s take a look:

def read_line_by_line(filename):
for row in open(filename, "r"):
yield row # access the lines of file on demand

for line in read_line_by_line("products.txt"): # products.txt has a ton of lines
print(line) # do something here

05 — Sort dictionary by values

It is common to need to sort dictionaries by values when you are solving problems. A Pythonic way to access the value of dictionaries is to use the itemgetter method. See how to do that:

from operator import itemgetter


d = {"v3": 44,, "v2": 33, "v4": 55, "v1": 22,}

sorted_d = dict(sorted(d.items(), key=itemgetter(1)))

06 — Use stopwords from spaCy

Stopwords are a list of words that has no meaning or relevance in a text. When you are working with text pre-processing, a common task is to remove stopwords to clean up the text, so you need to make a list of stopwords to perform this task.

Each language has its own set of stopwords. spaCy has predefined stopwords per language. Here’s how you can access this list:

import spacy


nlp = spacy.blank("pt") # there are many languages available
stopwords = nlp.Defaults.stop_words

print(stopwords)

08 — Convert a list to a namedtuple

The namedtuple is a way to create an immutable tuple with named fields and allow access to their values in dot notation.

The _make method is an iterable that creates a namedtuple from a list.

from collections import namedtuple


Student = namedtuple("Student", 'name, age, grade')

lines = [
["Albie Bailey", 21, 3],
["Brooklyn Price", 23, 5],
["Noah Hawkins", 19, 1],
["Marcel Hall", 25, 6],
["Dominik Davis", 20, 1],
["Clayton Elliott", 21, 3],
]

students = list(map(Student._make, lines))
students

09 — Parsing URL

The method urlparse extract the components of a URL string such as the domains, user, password, etc.

from urllib.parse import urlparse


uri = urlparse('https://john:qwer1234@127.0.0.1:5672/convert-pdf-to-txt?admin=true')

print(uri)

print(uri.scheme)
print(uri.username)
print(uri.password)
print(uri.hostname)
print(uri.port)

10 — Match

Since Python 3.10, we can use match to execute the switch case statement, as these statements are known in other languages. We can say goodbye to long if-elif chains.

option = input()

match option:
case "M":
print("Menu List")
case "1" | "2":
print("Menu Option")
case _:
print("Exit")

The _ (underscore) is a wildcard pattern that matches any value.

Conclusion

Hope you learned interesting things here. I will keep writing this series of posts. There is a lot of cool stuff in Python.

--

--