Facebook startet Videos automatisch, selbst wenn man nicht im WLAN ist, selbst im Ausland … das geht dann nicht nur aufs Datenvolumen sogar richtig ins Geld.
Facebook startet Videos automatisch, selbst wenn man nicht im WLAN ist, selbst im Ausland … das geht dann nicht nur aufs Datenvolumen sogar richtig ins Geld.
You have an obsession … it intrigues you … there is a mystery … you have a hunch … you consult experts … you try it … you succeed. It’s still not a proof … but you’re convinced … you feel good. 😀
Tim’s Vermeer is an awesome documentary following Tim Jenison trying to figure out what technique 17th century painter Johannes Vermeer may have used to capture lighting, color and minute details of the painted scenes. It’s an awesome way to watch the geek’s mind at work. 😉
Eins, Zwei, Drei Junitage im Wind
Ein Schwur zwei Herzen verbindtIm Streben nach Wissen, stets erpicht
Funkelnd, in strahlendem LichtStehn eines Tages vor ihrem Herrn
Wenn seinr Gnade Teil sie wernEine simple Frage ich mich frag:
Ist es auch ein Junitag?
If you write software in Python you come to a point where you are testing a piece of code that expects a more or less elaborate dictionary as an argument to a function. As a good software developer we want that code properly tested but we want to use minimal fixtures to accomplish that.
So, I was looking for something that behaves like a dictionary, that you can give explicit return values for specific keys and that will give you some sort of a “default” return value when you try to access an “unknown” item (I don’t care what as long as there is no Exception raised (e.g.
KeyError
)).
My first thought was “why not use MagicMock?” … it’s a useful tool in so many situations.
from mock import MagicMock m = MagicMock(foo="bar")
But using MagicMock where dict is expected yields unexpected results.
>>> # this works as expected >>> m.foo 'bar' >>> # but this doesn't do what you'd expect >>> m["foo"] <MagicMock name='mock.__getitem__()' id='4396280016'>
First of all attribute and item access are treated differently. You setup MagicMock using key word arguments (i.e. “dict syntax”), but have to use attributes (i.e. “object syntax”) to access them.
Then I thought to yourself “why not mess with the magic methods?”
__getitem__
and
__getattr__
expect the same arguments anyway. So this should work:
m = MagicMock(foo="bar") m.__getitem__.side_effect = m.__getattr__
Well? …
>>> m.foo 'bar' >>> m["foo"] <MagicMock name='mock.foo' id='4554363920'>
… No!
By this time I thought “I can’t be the first to need this” and started searching in the docs and sure enough they provide an example for this case.
d = dict(foo="bar") m = MagicMock() m.__getitem__.side_effect = d.__getitem__
Does it work? …
>>> m["foo"]
'bar'
>>> m["bar"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../env/lib/python2.7/site-packages/mock.py", line 955, in __call__
return _mock_self._mock_call(*args, **kwargs)
File ".../env/lib/python2.7/site-packages/mock.py", line 1018, in _mock_call
ret_val = effect(*args, **kwargs)
KeyError: 'bar'
Well, yes and no. It works as long as you only access those items that you have defined to be in the dictionary. If you try to access any “unknown” item you get a
KeyError
.
After trying out different things the simplest answer to accomplish what I set out to do seems to be sub-classing defaultdict.
from collections import defaultdict class MagicDict(defaultdict): def __missing__(self, key): result = self[key] = MagicDict() return result
And? …
>>> m["foo"]
'bar'
>>> m["bar"]
defaultdict(None, {})
>>> m.foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'MagicDict' object has no attribute 'foo'
Indeed, it is. 😀
Well, not quite. There are still a few comfort features missing (e.g. a proper
__repr__
). The whole, improved and tested code can be found in this Gist:
Scott Meyers’ hilarious talk about C++ idiosyncrasies. 😀
Die über 700 Jahre alte und seit dem 2. Weltkrieg verschollene Kundige Rolle ist wieder aufgetaucht. 😀
Türk Dil Kurumu (Turkish Language Association) has approved “özçekim” as the correct Turkish word for selfie. 😀

Guessing the average age of the participants from the picture the discussion must have been a lot of fun. xD
So basically you write something LISP-ish in JavaScript to get HTML?
Benjamin Mako Hill has followed up on an interesting thought: in a world where many people use Gmail, just how many of your daily emails also land on Google’s servers even if you aren’t using their services? … For him it turns out more than 50%. o.O
Very well put:
Has humanity ever built a border that can withstand human will?