Google Has Most of My Email Because It Has All of Yours

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

Unsafe Chrome Sometimes Necessary

In my work – every now and then – I found myself in need of a browser with reduced security checks (mainly to gloss over cross domain XMLHttpRequests and SSL certificate violations) for testing purposes. I didn’t want to take the risk and use my main browser session with these settings, so I made me a script (also available as a Gist). 🙂

If you use oh my ZSH you can save this file in ~/.oh-my-zsh/custom/plugins/chrome-unsafe/chrome-unsafe.plugin.zsh and add “chrome-unsafe” to your list of used plugins in ~/.zshrc 

Summing Booleans For Fun And Profit

I came up with a IMHO nice piece of code while working with and getting to know Python.

This feels clean and obvious. It might not be very efficient though. :/

Doing it this way can be more efficient, since it can leverage generators and doesn’t need to go through the list again with any . But using  sum  over booleans feels hackish and non-obvious … 🙁

MagicMock With Spec

Thanks to @immoralist I’ve learned a new Python testing trick. I didn’t know about the “spec” argument for MagicMock. m(
Let’s see an example:

Here we create a mock object which mimics the interface of  SomeModel  as we would expect, returning mock values for things we access.

Let’s see what happens if we call something else:

It will fail loudly while a mock object without a spec would have returned a mock value as it did in the previous example.

But the magic doesn’t end there. You can still set additional attributes/methods “by hand” and have them not fail even if they aren’t part of the original spec.

Learning new things makes me happy. 😀

Tripping Over Property Setters in Python

In Python there is a simple way to make methods behave like properties using the @property decorator. But this only covers the getter side of things. What if you want to have a setter function for this “property”? Well there is a way. 🙂
Consider the following example:

Now you can use the  foo()  method like a property.

This is a simple way to have a property contain a JSON string but access it as a Python dict, doing (de-)serialization on the fly.
So what if you want to set the value using a dict?

This is can easily trip up even seasoned Python developers. I’ve read code that did exactly this and I (as a novice) had to find out why the code failed. m(
The solution is quite simple … but “non-obvious” (as in: I wouldn’t have thought of that without consulting the docs) 🙁

Notice the method name? The setter and the getter methods have to have the same name!