Python: warnings and deprecation

Aside the logging library resides the less known warning library. The former is meant to log events related to execution whereas the later is meant to warn more or less about improper module usage or deprecated functions. By default, most warnings are displayed once, meaning that they will not clutter your logs by being shown repeatedly. However, some are “ignored by default”, hence not displayed at all. This is where the important difference with logging is: the control you get over them at the command line level.

Continue reading

Python counters on unhashable types

Have you ever heard or used python counters? They are very useful to count the number of occurrences of “simple” items. Basically:

> from collections import Counter
> colors = ['red', 'blue', 'red', 'green']
> Counter(colors)
Counter({'red': 2, 'blue': 1, 'green': 1})

However, if you try to use it on non hashable types it doesn’t work.

> colors = [['red', 'warm'], ['blue', 'cold'], ['red', 'warm']]
> Counter(colors)
[...]
TypeError: unhashable type: 'list'

What do we do then?

Continue reading

Lecture: Python Cookbook

Le nom de ce livre ne pourrait être mieux choisi. On traite à 99% de python (il y a un peu de C) et l’approche utilisée est effectivement celle d’un livre de recettes. Chaque section se découpe en trois parties: un problème, une solution et des notes à propos de la solution.

Continue reading

Python Cookbook Book Cover Python Cookbook
David Beazley, Brian Jones,
Computers
O'Reilly Media, Incorporated
2013-05-15
600