Summing Booleans For Fun And Profit

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

incomplete_items = [
    item.quantity_ordered > item.quantity_delivered for item in order.items
]
if any(incomplete_items):
    do_something()

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

has_incomplete_items = sum(
    item.quantity_ordered > item.quantity_delivered for item in order.items
)
if has_incomplete_items:
    do_something()

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:

from mock import MagicMock

class SomeModel(object):
    foo = "f**"
    bar = "b**"

m = MagicMock(spec=SomeModel)

Here we create a mock object which mimics the interface of 

SomeModel

  as we would expect, returning mock values for things we access.

>>> m.foo
<MagicMock name='mock.foo' id='4506756880'>
>>> m.bar
<MagicMock name='mock.bar' id='4506754192'>

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

>>> m.baz
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../env/lib/python2.7/site-packages/mock.py", line 658, in __getattr__
    raise AttributeError("Mock object has no attribute %r" % name)
AttributeError: Mock object has no attribute 'baz'

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.

>>> m.baz = "bazzzzz"
>>> m.baz
'bazzzzz'

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:

import json

class SomeModel(object):
  _foo = '{"foo":["bar", "baz"]}'

  @property
  def foo(self):
    return json.loads(self._foo)

  @foo.setter
  def foo_setter(self, new_value):
    self._foo = json.dumps(new_value)

m = SomeModel()

Now you can use the 

foo()

  method like a property.

>>> m.foo
{u'foo': [u'bar', u'baz']}

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?

>>> m.foo = ["foo", "bar"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: can't set attribute

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) 🙁

  @foo.setter
  def foo(self, new_value)
    self._foo = json.dumps(new_value)

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

A Rainbow of Thoughts

[…] His vast, mild head overhung by a canopy of vapor, engendered by his incommunicable contemplations, and that vapor—as you will sometimes see it—glorified by a rainbow as if Heaven itself had put its seal upon his thoughts. For, d’ye see, rainbows do not visit the clean air; they only irradiate vapor. And so, through all the thick mists of the dim doubts in my mind, divine intuitions now and then shoot, enkindling my fog with a heavenly ray. And for this I thank God; for all have doubts; but doubts or denials, few along with them, have intuitions.
— H. Melville, 1851, Moby-Dick, Chapter 85

I can’t express how much this last sentiment resonated with me … this is exactly the feeling when you’re looking for a solution to a problem for days and in one moment: there it is; the solution laid out so clearly … when God lifts the cloak of ignorance for a second, allowing you an insight into what seemed incomprehensible just moments ago … a flash of insight sparking, kindling further thoughts … fearing it will dim out before it can be burned into memory … *sigh* those moments -.-

Hayao Miyazaki And His Women

Watching Kari Gurashi no Arrietty by Studio Ghibli something occurred to me. Thinking back to my last year in high school where I watched almost all movies produced by Studio Ghibli until then (and by extension Hayao Miyazaki) the main character was “always” a female with strangely freakish history, fate or ability. Let’s “prove” this scientifically-ish … 🙂

Theory: Studio Ghibli movies by Hayao Miyazaki have this peculiarity of a strange/freakish main female character.

Data source: Wikipedia’s list of Studio Ghibli films from 1984-2014.

Let’s see how far we get. 🙂

Nausicaä of the Valley of the Wind: pre Ghibli, but still … the heroine has a supernatural connection with the giant insect-like Ohmu … fulfilling an ancient prophecy for good measure. pass
Castle in the Sky: Sheeta is of royal decent from an ancient civilization building flying cities. pass
Grave of the Fireflies: not by Hayao Miyazaki … phew I had almost forgot about this one. 😛 skip
My Neighbor Totoro: two curious girls befriending ghosts/spirits/trolls of the forest. pass
Kiki’s Delivery Service: the main character is a witch running a delivery service. … need I say more? pass
Only Yesterday: not by Hayao Miyazaki again. 😛 skip
Porco Rosso: no “main” female character: strike 1!
Pom Poko: phew … not by Hayao Miyazaki … Tanuki to the rescue! 🙂 skip
Whisper of the Heart: … I don’t remember the plot any more … so, strike 2!
Princess Mononoke: raised-by-wolves warrior princess of the woods. pass
My Neighbors the Yamadas: haha … I was almost worried … not by Hayao Miyazaki 😀 skip
Spirited Away: Chihiro has to master her passage from childhood to adulthood in the spirit world … also falling in love with a boy-dragon-hybrid-formshifter-thing. pass
The Cat Returns: not by Hayao Miyazaki, but in this film the main character Haru is offered the hand of the Prince of the Cat Kingdom for saving him … too bad it doesn’t count. 🙂 skip/pass
Howl’s Moving Castle: The main character, Sophie, gets cursed and is transformed into an old woman. she works at the hand of the wizard Howl to remove the curse. pass
Tales from Earthsea: not by Hayao Miyazaki. skip
Ponyo: Let me quote the first line of the plot section in the Wikipedia article of the film “Brunhilde [also named Ponyo] is a fish-girl who lives with her father Fujimoto, a once-human wizard who now lives underwater, and her numerous smaller sisters.” pass
Arrietty: is only 10cm tall! pass
From Up on Poppy Hill: knowingly agreeing to incest (at least from what they knew at that time) … really?!? pass
The Wind Rises: tragic story, but no “main” female character. strike 3! 🙁
The Tale of Princess Kaguya: not by Hayao Miyazaki, but she starts out as a “miniature girl inside a glowing bamboo shoot” skip/pass
When Marnie Was There: not by Hayao Miyazaki … (not seen yet) skip

Summary: 21 Studio Ghibli films; 13 of them by/with Hayao Miyazaki; of which 10 fit the theory, with 3 “outliers” and 1 “unexamined” data point.

Not bad for a hunch. 😉

Update 14.09.2013:
Updated the list and the summary after seeing From Up on Poppy Hill.

Update 11.08.2014:
Updated the list and the summary after seeing The Wind Rises and the The Tale of Princess Kaguya.