I was surprised this morning when I realized that the call to sys.exit is not really exiting. It is instead raising exception SystemExit which, if left unhandled, will exit with the proper exit code.
Continue reading “Python sys.exit: a suggestion”
Tag: Python
bookmark_borderQuery string white space vs plus
Trivia: What is the difference between the encoded query string parameter “a+b” and “a%20b” ?
Answer: Nothing! They are both encoded representations for “a b”.
Isn’t a “+” supposed to remain a “+”? Well, the URL and the query strings are not encoded following the same rules. In the URL, the “+” remains a “+” indeed, but in the query string it’s actually encoded and becomes a “%2B”. This can be misleading.
Continue reading “Query string white space vs plus”
bookmark_borderPython: 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.
bookmark_borderPython 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?