Seven lesser known tips for Idiomatic Python

If you have been programming in python, you must have often heard phrases like “code like a pythonista” or “idiomatic python”. These phrases basically try to emphasize on “readability counts” philosophy in the Zen of Python. There have been several articles on idiomatic python, but below are seven lesser known tips on writing idiomatic python:

1. Use else to execute code after a for loop ends

>>> for zipcode in zipcodes:
...    if len(zipcode) > 5:
...        print "Invalid length"
... else:
...    print "Valid length"
...
Valid length

2. Use comprehensions

List comprehensions are very popular, but few people know about dict comprehensions and set comprehensions.

List Comprehension
>>> name_dict = {name.fname : name.lname for name in names if name.lname}

Set Comprehension
>>> name_set = {name.fname for name in names if name.lname}

3. Define __str__ in a class to show a human readable representation of the class

>>> class Name:
...     def __init__(self, name):
...         self.name = name
...     def __str__(self):
...         return "This class represents a Name"
...
>>> name = Name('john')
>>> print name
This class represents a Name

4. Use a generator to lazily load infinite sequences

5. Use _ as a place holder for data in a tuple that should be ignored

(instance_name, instance_size, _) = get_instance_info(instance_uuid)

6. Use partial to fixate function variables

>>> def long_name(first_name, middle_name, last_name):
...     print first_name, middle_name, last_name
...
>>> short_name = functools.partial(long_name, 'john')
>>> short_name('jim', 'jr')
john
jim
jr

7. Use zip and izip to help loop over two collections

>>> number = [1, 2, 3]
>>> number_name = ['one', 'two', 'three']
>>> for num, num_name in zip(number, number_name):
...     print num, num_name
...
1 one
2 two
3 three

References:

[1] Writing Idiomatic Python
[2] Advice by Raymond Hettinger

Migration in devstack

I have been working on image sharing in glance. As a part of it, the code requires I write a new migration adding in a column in an existing table. Below are the steps on how to run test if your migration works using devstack:

1. sudo python setup.py install
This is important because Step 3, runs on packages built by this command.

2.sudo glance-manage version_control 0

3.sudo glance-manage db_sync

4.sudo restart glance-api

5.sudo restart glance-registry

6. Test your migration using various glance commands!

Contributing to OpenStack

My colleague, Alex, and I recently conducted a workshop on Contributing to OpenStack. In order to create a presentation deck for the workshop, I searched through several of the existing presentations, though they were very resourceful in their own ways, I could not find a one stop place for all the information I needed. I have embedded below the presentation I created for the workshop, hope you find it useful too.

 

OpenStack 101 Resources

In past few months, many people have asked me how to get started on contributing to OpenStack or about getting started with OpenStack guides. So instead of making this post about how to contribute to OpenStack, below is a culmination of various resources many people have found useful. If you have any other useful resources, feel free to leave links to them in the comments.

Other Misc resources:

FOSS Outreach Program for Women

Free and Open Source Software (FOSS) Outreach Program for Women(OPW) has been one of my main sources of inspiration to start blogging. Though the main objective of the program is to help get women introduced to the world of contributing to open source, I feel it is also a big learning experience for the mentors of the program.

When Anne Gentle asked me if I would like to be a mentor on the OpenStack project for the FOSS OPW, the teacher in me got all charged up and enthusiastic. It has been only a few weeks since the application phase of the program has begun, but I have already got to meet several brilliant women from all walks of life. The rush the applicants get from submitting their first patch to OpenStack, does not die subsequently, but in fact they go on to inspire other applicants and guide them through the process of submitting their first patch as well. Anita and Shruthi, applicants for the OPW program penned down their experiences with OpenStack and it has inspired several others, including me, to give back in as many different ways as possible.