Working Ninja

I recently wanted to test logging in different users (that didn't exist in the database) and couldn't find a clear answer for how to do so--perhaps this is a sign I am traveling where I shall not! So be it, here is my solution:

import os
import subprocess

import unittest
from unittest import mock

from app import create_app
from app.db import init_db

BASE_DIR = os.path.dirname(os.path.realpath(__file__))
DATABASE = os.path.join(BASE_DIR, 'instance', 'db-testing.sqlite3')

def client(app):
    with app.test_client() as client:
        with app.app_context():
        return client

class ViewsTests(unittest.TestCase):
    def setUp(self):
        app = create_app({
            'TESTING': True,
            'SECRET_KEY': '0123456789',
            'DATABASE': DATABASE
        self.client = client(app)

    def tearDown(self):
        # Remove Testing Database'rm {}'.format(DATABASE))

    def test_log_in_render(self):
        rv = self.client.get('/')

        assert b'log in to continue' in

    def test_logged_in(self, current_user):
        attrs = {
            'email': '[email protected]',
            'name': 'Foo Bar'
        current_user.return_value = mock.Mock(is_authenticated=True, **attrs)

        rv = self.client.get('/')

        assert b'Logged in as Foo Bar' in



A situation arose where I was unable to use nc to send some metrics to a local Graphite server. Thanksfully, Python was installed (albeit, quite a dated version) that allowed me to set up a socket to send the data over the wire.

import commands
import socket
import time

def get_device_stats(device):
    # Retrieve 1 second average from `iostat` for device.
    status, output = commands.getstatusoutput('iostat 1 2 -kd -p /dev/%s | awk NR==7' % device)
    values = output.split()
    timestamp = int(time.time())

    send_metric(device, 'iops', values[1], timestamp)
    send_metric(device, 'read', values[2], timestamp)
    send_metric(device, 'write', values[3], timestamp)

def send_metric(device, metric, value, timestamp):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('', 2003))
    # New-line character is important here, otherwise Graphite will not parse the sent data.
    s.sendall(' %s %s\n' % (device, metric, value, timestamp))

devices = ['sda', 'sdb', 'sdc']

for device in devices:



Objective: Sort dictionary by nested dictionary value

Let's say we want to sort the following JSON by the nested dictionary value of count.

  "Mail": {
    "count": 20,
    "users": ["lukeskywalker", "darthvader"]
  "Droid Sync": {
    "count": 5,
    "users": ["lukeskywalker"]

Method 1: lambda

apps_sorted = sorted(apps.items(), key=lambda x: (x[1]['count']))

Method 2: key function

def sort_by_count(x):
    # key=lambda x: (x[1]['count'])
    key, data = x
    return data['count']

apps_sorted = sorted(apps.items(), key=sort_by_count)



By default, a ModelForm is populated with all its objects from the database. Sometimes it's desirable to limit these results as they are returned to the view. After setting up our ModelForm ( there is only one thing we need to do to make this happen. Within our view (, we update the queryset as follows:

class PollForm(ModelForm):
    class Meta:
        model = Poll
        fields = ('question',)

form = PollForm()
form.fields['question'].queryset = Poll.objects.filter(question__startswith="How")

This will filter Polls that start with "How".



Create unicode string "a".

>>> a = u'\u2019'
>>> a

Convert to ASCII string (ASCII is default for Python 2 str()).

>>> str(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 0: ordinal not in range(128)

Encode the unicode string to UTF-8 (overriding the default of ASCII).

>>> str(a.encode('utf-8'))
>>> print str(a.encode('utf-8'))

For a greater understanding, see Ned Batchelder's great "Pragmatic Unicode, or, How do I stop the pain?" (2012) video.


Let's say we want to query our Polls app for all Questions that have popular (more than 5 votes) choices that contain Python in the choice_text.

We could query our database as follows:

SELECT * FROM question JOIN (SELECT * FROM choice WHERE vote_count > 5 AND choice_text LIKE '%Python%') choice ON = choice.question_id;

Or, it can be codified in a potentially more comprehendable, OOP/ORM format with SQLAlchemy.

First, we set up our schema by declaring the relationship and the parameters of the join:

class Question(Base):
    __tablename__ = 'question'

    id = Column(Integer, primary_key=True)

    popular_choices = relationship('Choice', primaryjoin='and_( == Choice.question_id, Choice.vote_count > 5')

Next, we query with SQLAlchemy's ORM:

popular_python_choices = session.query(Question) \
    .filter(Question.popular_choices.contains('Python') \

Not quite as terse as straight SQL but hopefully it is clear how, after the schema is set up, calling Question.popular_choices.contains('Python') provides increased readability (reads like English) and reusability (replace our filter Python with whatever you wish!).

SQLAlchemy provides another good example.


Here's the most straightforward and succinct way I've found to mock patch a function that returns different (but specific) results with each call, shamelessly copied from Python Docs:

>>> values = {'a': 1, 'b': 2, 'c': 3}
>>> def side_effect(arg):
...     return values[arg]
>>> mock.side_effect = side_effect
>>> mock('a'), mock('b'), mock('c')
(1, 2, 3)

And, now that we have our side_effect function defined, we can pass new values to return:

>>> mock.side_effect = [5, 4, 3, 2, 1]
>>> mock(), mock(), mock()
(5, 4, 3)



After updating mysql-connector-python to 8.0.11 (released April 19, 2018), I received an "OperationalError: 1043 (08S01): Bad handshake" error when querying a MySQL database. I found that I needed to set 'use_pure: True' in the connection string.

From the MySQL folks:

The C extension was added in version 2.1.1 and is enabled by default as of 8.0.11. The use_pure option determines whether the Python or C version of this connector is enabled and used.1

For example2:

import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '',
  'database': 'employees',
  'use_pure': True,

cnx = mysql.connector.connect(**config)

Since I don't have the C extension installed on my server, I reverted back to the Python implementation. Though it is good to know that the C extension can improve performance for large queries--something to tuck away for future use.



If you've ever touched a date or time related field via the Django API, you'll have noticed the big warning that the date and/or time is not localized. While the warning is nice there isn't much more in the way of how to localize the time. Here are two ways that I usually go about it:

>>> timezone.make_aware() 
>>> timezone.localtime()

Both are found in django.utils's timezone module. If you're using Django Extensions, timezone is already loaded when you run ./ shell_plus. If you're not, simply run from django.utils import timezone.

For usage:

>>> help(timezone.make_aware)
>>> help(timezone.localtime)

It's easy to forget to update packages after they've been installed as they silently live in the background working as intended. Here are two ways to check what needs updating:

pip list --outdated 

Out of the box, pip provides a quick way to query which packages have updates.


If you're looking for something that helps install the packages that are outdated, check out pip-review. pip-review has both an --auto option which automatically installs all updates it finds and an --interactive which will prompt you which packages you'd like to update.