https://alpha.app.net/riyad/post/16862808
Tag: Programming
Struggles of a Systems Programmer
James Mickens has probably written the most hilarious essay about the struggles of a systems programmer. xD
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). 🙂
Tip:
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
Sequel Pro
A friend recommended Sequel Pro as a GUI for MySQL databases. After trying it out for a day I must say I’m pleasantly surprised. 🙂
ADN: Another Git Cheatsheet
https://alpha.app.net/riyad/post/14760717
Google’s Shell Style Guide
If you find yourself writing Shell scripts have a look at Google’s awesome style guide.
This Is Why You Shouldn’t Interrupt a Programmer
ADN: Python Development Has Nothing To Do With Zoology
https://alpha.app.net/riyad/post/12202500
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. 😀