Lambda functions

Python’s lambda function is a powerful tool for creating small, anonymous functions on the fly. Lambda functions are known as anonymous functions, because: they don’t have a name, are created and used at the same time. I have used the lambda function numerous times, especially when I needed to sort something on a specific key. Some benefits of using lambda functions: Concise code - no need to create new named functions if we are using it only once, Cleaner code - lambda functions are easy to read (with good naming practices), Flexibility - can be used in various situations - as an argument to another function, in a list comprehension or as the key function for sorting, No side effects - lambda functions are pure. This is very important when working with data that needs to be kept in its original state. Lambda functions, as mentioned above, are very handy when sorting more complex iterables. ...

March 28, 2023 · 2 min · 237 words

Pattern Matching

What is Pattern Matching Pattern matching is a newer feature that came with Python 3.10. Using pattern matching simplifies the code and improves readability. The other option is to write multiple if-else statements, but this can quickly become very unreadable. Example Consider these 2 examples. The first one using multiple if-else statements, the second one pattern matching - match/case. Which one is more readable? 1 2 3 4 5 6 7 8 9 10 # if-else statements if x == 'red': print("The color is red") elif x == 'blue': print("The color is blue") elif x == 'green': print("The color is green") else: print("Unknown color") 1 2 3 4 5 6 7 8 9 10 11 # Pattern matching match x: case 'red': print("The color is red") case 'blue': print("The color is blue") case 'green': print("The color is green") case _: print("Unknown color") This is a very simple example, and pattern matching can be also used in more complex situations where more conditions need to be met. ...

March 12, 2023 · 2 min · 249 words

Useful One-Liners

There are a lot of tricks one can use in Python. So-called Python one-liners are one of them. It’s a nice way of shortening the length of the code. However, it’s good to know that sometimes, using the longer version might be the better choice when it comes to readability. Swapping two variables 1 2 3 4 a = 10 b = 20 a, b = b, a List comprehension 1 squares = [i*i for i in range(20)] Ternary operator 1 my_value = 30 if 5>3 else 50 Print only elements (unpacking) 1 2 numbers = [1, 2, 3, 4, 5] print(*numbers) Days remaining in a year 1 import datetime;print((datetime.date(2023,12,31)-datetime.date.today()).days) Reversing a list/string 1 2 letters = ['a', 'b', 'c', 'd', 'e'] print(letter[::-1]) Multiple variable assignments 1 age, city, position = 25, 'London', 'Python developer' Convert number strings into ints 1 2 strs = '1 2 3 4 5 6' ints = list(map(int, strs.split())) Read all lines of a file to a list 1 values = [line.strip() for line in open('document.txt', 'r')] Start an HTTP server in a terminal python -m http.server ...

February 18, 2023 · 1 min · 182 words

Small details that matter a lot

Each programming language has its own peculiarities - knowing these will make your code more reliable, and you will avoid unexpected behaviors and/or bugs. In Python, one such peculiarity, that I learned recently, is that you should not use mutable objects as default arguments. Let’s see why. Example You want a function that appends a number to a list. If the list is not passed, it appends data to a newly created list. ...

February 12, 2023 · 2 min · 255 words

The help() function

I am currently exploring the topic of objects a little more. Once you go a bit deeper into the topic, it can get quite complex and confusing (at least at first). What is pretty cool though is realizing that in Python, everything is an object. Basically, every value or data type is an instance of a class. For example, creating a new string like my_name = 'Peter' actually creates an object of the class str. That’s why we can also use methods such as upper() (and many others) as these come with the class. ...

January 23, 2023 · 2 min · 415 words

Asynchronous tasks with Celery & RabbitMQ

The Challenge I’m currently working on an application in Django which I hope to deploy in the upcoming weeks, it’s just that I fix one bug and another one appears, then tests pass, I add something new, suddenly nothing is working so I have to redesign the whole thing again. This has also prompted me to explore the topic of debugging a bit more recently. I guess that’s how we learn. ...

January 18, 2023 · 3 min · 525 words

Write better For-Loops

Looping through an iterable is the most basic and fundamental ability of any programming language. Python’s for loop is at a first glance very straightforward, but just like with any other tool it can be used in multiple ways - these become handy when you start taking performance into account. Here are 6 things to consider when you need to loop over something. 1. Maybe I don’t actually need a for-loop For loops can be slow. There are many built-in methods optimized for specific tasks. Perhaps a better approach would be to simply use a built-in function. Let’s say you need to sum up a list of numbers. ...

January 15, 2023 · 4 min · 746 words

Better Debugging

“Debugging is just problem-solving - attack it as such. Always try to discover the root cause of a problem, not just this particular appearance of it.” - Authors of Pragmatic Programmer Debugging is a very important skill for any programmer. It is an essential part of the development process as it ensures that the software you are writing is reliable, efficient, and free of errors. On the higher level it forces you to think about the code you are writing - seeing where errors occur, you gain a deeper understanding of the overall design and functionality of the code leading to better and more reliable design in the long run. ...

January 10, 2023 · 3 min · 633 words

Python's Argparse module

As your scripts start getting a bit more complex, it might be a challenge setting them up in a way that is consistent and predictable by others. Working with a script that requires different arguments each time you use it wouldn’t be very consistent and predictable. Luckily, Python’s Standard Library comes with a module named Argparse that has a set of tools for parsing command-line arguments in your Python scripts. ...

December 29, 2022 · 2 min · 287 words

Django & Docker

I have been reading about Docker for quite some time now. This term would pop up very often whenever I was checking something regarding Django. It wasn’t until recently that I actually made time to read up about it and understand what Docker is. Needless to say, I was missing out. I have now started using Docker with every project, and it makes the development process a lot more manageable. Obviously, I probably understand about 10% of all the features, but I am learning and already these 10% are of tremendous help. ...

December 28, 2022 · 2 min · 292 words