So you want to mock python’s open.

It’s kind of silly. You can’t use the decorator form of mock.patch since this version doesn’t return anything, so you have to use the context manager version.

mock_open = mock.mock_open()
with mock.patch('', mock_open, create=True):
    thing.you_are_testing_that_uses_open(your, params, here)


Must Read This Later

A comparison of scripting languages.


Bad Error Message is Unhelpful

So, I’m still learning Django and have a long ways to go, but the following error is extremely unhelpful:

“TemplateSyntaxError: Invalid block tag: ‘else’, expected ‘else’ or ‘endif’”

The problem is, Django doesn’t do else if, but my editor does highlight it as if it’s correct. You then get this error message, but have to run through the docs to figure out that they want you to nest if/else blocks to create “else if.” Slightly frustrating, and causes me to write some questionably ugly code. Blegh.


__new__ vs. __init__

While running around debugging some timeouts with a coworker we came across __new__. Of course we were then curious as to what __new__ was and how it was different than __init__.  There was a good explanation on StackOverflow.  Although I understand it now, I’m not sure when I would need to subclass an immutable type.  Anyone?


I’m going to waste so much time with this



The Balloonist

Not my joke, but hilarious:

A woman in a hot air balloon realized she was lost.

She reduced altitude and spotted a man below. She descended a bit more and shouted: ‘Excuse me, can you help me? I promised a friend I would meet him an hour ago but I don’t know where I am..’

The man below replied, ‘You’re in a hot air balloon hovering approximately 30 feet above the ground. You’re between 40 and 41 degrees north latitude and between 59 and 60 degrees west longitude.’

‘You must be an Engineer,’ said the balloonist.

‘I am,’ replied the man, ‘how did you know?’

‘Well,’ answered the balloonist, ‘everything you have told me is probably technically correct, but I’ve no idea what to make of your information and the fact is, I’m still lost. Frankly, you’ve not been much help at all. If anything, you’ve delayed my trip by your talk.’

The man below responded, ‘You must be in Management.’

‘I am,’ replied the balloonist, ‘but how did you know?’

‘Well,’ said the man, ‘you don’t know where you are or where you’re going. You have risen to where you are, due to a large quantity of hot air. You made a promise, which you’ve no idea how to keep, and you expect people beneath you to solve your problems. The fact is you are in exactly the same position you were in before we met, but now, somehow, it’s my f**king fault.’


MacVim BClose

There are two ways to close buffers in MacVim:

1) :bd (Buffer Delete)
This deletes one buffer, but the problem is, it won’t open it again during that session.

2) :q (quit)
This closes ALL the buffers and the app.

Neither does what I want most of the time, but I found this:

It’s a small plugin to allow you to close buffers without closing the window or not being able to get back to hit. Saweet!


Git Is Finicky

So, we have a release happening soon, and I needed to cherry-pick a few essential changes into the release candidate and upon trying to push them, I was told everything was up to date. ORLY NAO? Apparently, every now and again Git decides it wants to mess with me, and today is one of those days. Local branches created from the release candidate just don’t want to push without this workaround from stackoverflow.

99% of the time I love git. 1% of the time it makes my life hell. Could be a PICNIC error.


Tweepy – Python Twitter Library

It‘s pretty awesome and easy to use.  The whole oauth circle is a nice thing to know, but it’s major headache the first time around.  Tweepy hides a lot of the not so fun parts and sets up a simple interface.  So, I’ll assume we can forward past the whole registering a Twitter account in order to get a consumer and secret key and move on.  So in order to hook up your user to their Twitter account for a web app, it’s just these easy steps (note, the code is chopped from some prod code, so a few things have been pseudo-coded for generic reasons):

1) Get the auth url:

def get_authorization_url(user_id):
    auth = tweepy.OAuthHandler(consumer_key, secret_key)
    url = auth.get_authorization_url()
    return url #or redirect to it, or whatever you want

2) Receive their token and verifier, then store for later use:

def authorize(user_id, token, verifier):
    auth = tweepy.OAuthHandler(consumer_key, secret_key)
        auth.set_request_token(token, secret_key)
        token = auth.get_access_token(verifier=verifier)
        attach_token_to_user(user_id, token.to_string())
        return WHATEVERYOUWANT
    except tweepy.error.TweepError as e:
        log.exception("some exception")
        raise InvalidAuthorizationError() #my custom error

3) Use later:

def post_to_my_feed(user_id, twitter_username, tweet):
        token_string = get_auth_token_for_user(user_id)
        if not token_string:
            raise AccountNotLinkedError()
        token = tweepy.oauth.OAuthToken.from_string(token_string)
        auth = tweepy.OAuthHandler(consumer_key, secret_key)
        auth.set_access_token(token.key, token.secret)
        api = tweepy.API(auth)
        return WHATEVERYOUWANT
    except tweepy.error.TweepError as e:
        log.exception('Something went wrong: appropriate error message')
        raise SomethingWentWrongException("some good message")


MacVim PEP-8 Helper

To follow along with PEP-0008 standards, there is a command that will highlight any characters beyond the 79 char limit.  It’s a useful little reminder that you’ve reached your limit.

:match ErrorMsg '\%>79v.\+'

Older posts «