Python Adventure

I did not find Python by myself. I was aware of existence but ignored it. I knew Google used it, I knew two geeks at a place I worked in a past, used it and spoke positively about it. But actually it is my wife, who dragged the thing to our home. She attended a training and told me a couple of warm words about it. So I put a little more attention to Python than I did before. And it did not took much to fall in love with Python - it became my favourite language. I like do things with Python, I like the beauty of code imposed by the language structure, I like its design. I like very much to contemplate beautiful code in Python, I really enjoy it.

Being not a professional Python programmer, I write in it for fun. No pressure, slow pace, more satisfaction. So even simple discoveries make me wonder.

I thought I learned Python after I read Dive into Python and skimmed over its Python 3 version. I got familiar with Python tutorial. Then I wrote some scripts, created some classes, started my adventure with Django.

I started to feel confident with my skills. The first doubt came when I found Django documentation not being good enough and I had to dig over its source code. Some parts were clearly a challenge to me.

I have recently bought two books:

  1. “Fluent Python” by Luciano Ramalho
  2. and “Test Driven Development with Python” by Harry J.W. Percival

So I started to read “Fluent Python” just to immediately find out I have no idea what I am reading about. With the first chapter I had no idea about Python’s collections and itertools.

So I started to study Python’s Standard Library.

Itertools

Itertools are utilities to improve Python’s loops. There are really simple, infinite iterators like count(), repeat(), cycle().

>>> from itertools import count, repeat, cycle
>>> [i for i in zip('ABCDEF', count())]
[('A', 0), ('B', 1), ('C', 2), ('D', 3), ('E', 4), ('F', 5)]
>>> [pow(i, j) for i,j in zip(range(10), repeat(2))]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> "+".join([i for i,_ in zip(cycle('ABC'), range(12))])
'A+B+C+A+B+C+A+B+C+A+B+C'

Grouper

Stupid Python Ideas - How The Grouper Works