Exception Handling

One of the most important features of Python is its ability to handle exceptions gracefully. Exception handling is the process of detecting and dealing with errors that occur during the execution of a program. In Python, exception handling is implemented using a combination of try, except, else, and finally blocks. Exception Handling The try block The try block is the section of code where you put the code that may raise an exception. It is essentially a “sandbox” where you can test your code for potential errors. If an exception occurs in the try block, the code execution jumps to the corresponding except block. ...

April 21, 2023 Â· 4 min Â· 656 words

Finding element with partial id in Selenium

I am currently automating some tasks with selenium and I faced a challenge where an element would render each time with a slightly different ID - the first part would be the same, but the ending would be different, such as id=navbarTop_13123. Part navbarTop_ would always be the same, but the ending different. Partial ID Selectors Partial ID selectors are a powerful feature of CSS selectors that allow you to select elements based on a partial match of their id attribute. This can be useful when the id attribute of an element contains a dynamic or changing value, and you need to select the element based on only part of the id attribute. ...

April 4, 2023 Â· 2 min Â· 237 words

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