Python Nuggets for Testers I: Basic Comprehensions
The Ground Rules
This is not an introductory programming series teaching the syntax of the very basics of the language like variables, operators, how to run a script etc. There are many good tutorials available on the same. So, in case you are completely new to Python, do this basic exploration for a couple of days. Then, this series will make much more sense and you'll be able to make better use of it. Maybe at a later stage I'll write a jumpstart article, but at the moment I am not motivated to write the same.
In this series, I will write about specific Python features that I use in my work for automation in testing - ranging from raw throw-away scripts to frameworks that I've created and maintained for years.
The articles most likely will not be in any logical sequence of learning the concepts step by step, although I'll take care of depended-on concepts covered in a logical sequence within the universe of this series.
Comprehensions
Comprehensions are a very powerful feature in Python. They result in compact code which is also performant. List Comprehensions are very often used and discussed by testers. However, we can also use the concept for Sets and Dictionaries, which are the lesser discussed counterparts but equally useful.
List Comprehension
For simplicity sake I am using the word input sequence here. Technically, it can be any object that has iteration protocol implemented in it. More on this, in a later article.
A basic use case:
For example, let's say you want to take a sequence of numbers as an input and then calculate cubes for each number and populate an output sequence.
Following Python code shows a non-comprehension approach to achieve this:
In contrast, following is the solution based on list comprehension:
In the above snippet, the following code is the list comprehension:
[number**3 for number in seq]
You can also have a predicate part. Let's say, you want to filter only the odd numbers and then calculate the cube:
领英推荐
As you can see the update list comprehension looks as:
[number**3 for number in seq if number % 2 != 0]
Here
if number % 2 != 0
is the predicate part which acts as the odd number filter.
Set Comprehension
Sets have different use cases as compared to lists. By their nature they are unordered and can not contain duplicates.
It's pretty easy to create a Set comprehension. Just change [ ] to { }.
Here's an example with deliberate duplicates to demonstrate it:
Dictionary Comprehension
Dictionaries are the built-in mapping types in Python. They are unordered data types just like sets.
Instead of using a single object like sequences and sets, they contain key-value pairs.
To create a dictionary comprehension, one uses the { } just like sets, as that's also the shared syntax while writing a set/dict in Python. The difference is that you indicate the key-value pair as "key : value".
Let's change the example code to create a dictionary where the keys are the original numbers and the values are the corresponding cubes.
These are the absolute basics of this wonderful feature. In the subsequent articles, I'll continue delving deeper into it.
That's all for now.
Software tester || Risk investigator || Writer || Public Speaker
1 年Thanks for sharing this Rahul Verma. I have started something like this only when I have started exploring Python. You have provided some great ideas for me to try it out. Thanks for sharing once again. I am looking forward for upcoming articles on this series