Tuesday, December 6, 2016

Did you know about `raise .. from ...` in Python 3?


Python 3.5.2 (default, Oct 11 2016, 04:59:56)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin

>>> try:
...     1/0
... except Exception as exc:
...     raise RuntimeError('Something bad happened')
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
RuntimeError: Something bad happened
>>> try:
...     1/0
... except Exception as exc:
...     raise RuntimeError('Something bad happened') from exc
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
RuntimeError: Something bad happened
>>> try:
...     1/0
... except Exception as exc:
...     raise RuntimeError('Something bad happened') from None
...
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
RuntimeError: Something bad happened
>>>

Related info:

Wednesday, November 9, 2016

Goodies in Python 3.6!

Python 3.6 is around the corner!

Reading the new features I am excited to see these:

Formatted string literals.

>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'
>>>

The dict type now uses a “compact” representation pioneered by PyPy. The memory usage of the new dict() is between 20% and 25% smaller compared to Python 3.5. Dictionaries are now ordered!

Python 3.6.0b4 (v3.6.0b4:18496abdb3d5, Nov 21 2016, 20:44:47)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def f(**kwargs):
...     print(kwargs)
...
>>> f(a=1, b=2, c=3, d=4)
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> {'a': 1, 'b': 2, 'c': 3, 'd': 4}
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> d.keys()
dict_keys(['a', 'b', 'c', 'd'])
>>> d.values()
dict_values([1, 2, 3, 4])
>>> d.items()
dict_items([('a', 1), ('b', 2), ('c', 3), ('d', 4)])
>>>

Import now raises the new exception ModuleNotFoundError (subclass of ImportError) when it cannot find a module. Code that current checks for ImportError (in try-except) will still work.


Friday, June 24, 2016

Scott Sanderson, Joe Jevnik - Playing with Python Bytecode - PyCon 2016

Ever wondered what Python is actually doing when it executes your code? Want to learn to hand-craft artisanal Python bytecode? In this talk, we explain CPython's internal code representation, and we demonstrate techniques for modifying code objects for fun and profit.

Thursday, June 23, 2016

Matt Bachmann - Better Testing With Less Code: Property Based Testing With Python - PyCon 2016

Standard unit tests have developers test specific inputs and outputs. This works, but often what breaks code are the cases we did not think about. Property based testing has developers define properties of output and has the computer explore the possible inputs to verify these properties. This talk will introduce property based testing and provide real world examples and patterns.

Wednesday, June 15, 2016

Brett Slatkin - Refactoring Python: Why and how to restructure your code - PyCon 2016

As programs gain complexity, it becomes harder to add features and fix bugs. Reorganizing code is an effective way to make programs more manageable. This talk will show you Pythonic ways to do the most imporant ""refactorings"": Extract variables with __nonzero__; Change signatures with *args and **kwargs; Extract fields and classes with @property; Create stateful closures with __call__; and more!

Wednesday, June 1, 2016

Ned Batchelder - Machete-mode debugging: Hacking your way out of a tight spot - PyCon 2016

When chasing mysterious bugs, it's helpful to use all the tools at your disposal. We'll explore ways to use Python's dynamic tools to help track down the cause of head-scratching problems in large systems. Tools include the inspect module, monkey-patching, trace functions, and the Python mechanisms at work behind them all.

Tuesday, May 3, 2016

MongoDB dump/restore without temporary files

In MongoDB 3.2 new option --archive is available in mongodump and mongorestore which allow to to copy databases without temporary directories:

mongodump --host=localhost:37017 --db=userstate_10b --archive | mongorestore --host=localhost:27017 --drop --archive
MongoDB 3.2 also has a new --gzip option which in this case doesn't make sense to use, because mongodump and mongorestore are running on the same machine.