Working Ninja
2021-12-31T14:10:22

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 …

READ MORE

2019-06-06T20:49:55

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 …

READ MORE

2019-01-26T09:36:57

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: …

READ MORE

2018-09-26T07:41:35

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 (forms.py) there is only one thing we need to do to make this happen. Within our view (views.py), …

READ MORE

2018-08-19T10:21:07

Create unicode string "a".

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

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

READ MORE

2018-07-27T22:01:09

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%') …

READ MORE

2018-07-25T21:21:23

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'), …

READ MORE

2018-06-29T20:53:23

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 …

READ MORE

2018-04-22T10:28:39

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 …

READ MORE

2017-12-29T16:14:00

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.

pip-review

If …

READ MORE

2017-08-31T17:38:03

Here's the generalized snippet:

any(dict.get('key') == search_value for dict in list)

A full example:

authors = [
    {'author': 'Michael Crichton', title: 'Jurassic Park'},
    {'author': 'C.S. Lewis', title: 'Til We Have Faces'}
]

if any(book.get('author') == 'Michael Crichton' for book in books):
    print('Found Michael Crichton!')

Or less specifically:

if any('Lewis' in …

READ MORE

2017-08-29T17:47:18

Using the ManifestStaticFilesStorage storage backend alters the filename of our static files. For example, style.css becomes something like style.3d94ea84cd8a.css. When collectstatic is run and finds a file that has changed, the MD5 portion of the filename will be updated (style.3d94ea84cd8a.css -> style.1d74ea7349df.css). This prevents browsers and other caching technologies (e.g. …

READ MORE

2017-08-27T08:56:34

Here's how I set up logging where:

  • Log files rotate nightly (at midnight).
  • We keep 10 total log files (as history).
import logging
import os

# Set up logging
logger = logging.getLogger(__file__)
handler_kwargs = {
    'filename': os.path.join(base_dir, 'file.log'),
    'when': 'midnight',
    'backupCount': 10
}
handler = handlers.TimedRotatingFileHandler(**handler_kwargs)
formatter = logging.Formatter('%(asctime)s %(levelname)s …

READ MORE

2017-08-27T08:44:20

Python 2.7 is not provided on CentOS 6 because Yum depends on Python 2.6. Simply installing 2.7 over 2.6 will break Yum. Thankfully, Red Hat provides us with an alternate installation method through their SCL project.

First, get Python 2.7 installed:
https://www.softwarecollections.org/en/scls/rhscl/python27/

Next, add the project path to the top …

READ MORE

2017-08-24T20:59:14

Here's a custom manager for a class (Change) that gets all approved changes where "today's date" falls between the Change.start_date and Change.end_date.

# models.py
import datetime
...
# Custom Change Manager that only pulls active, approved changes
class ApprovedChanges(models.Manager):
    def get_queryset(self):
        today = datetime.date.today()
        return super(ApprovedChanges, self).get_queryset().filter(
            approved=True,
            start_date__lte=today,
            end_date__gte=today …

READ MORE

2017-08-24T07:47:57
# Simple List
', '.join([author.name for author in authors])

# Django
', '.join([author.name for author in book.author_set.all()])

Both will output "Author Name, Author 2 Name, ..., Author x Name"

READ MORE

2017-05-29T10:58:45

I just started my first Django project with Python 3 in earnest. One of the first things I have noticed is that reload isn't available by default from the Django shell. It needs to be imported:

>>> from importlib import reload
>>> reload(<module>)

This became a pretty tedious process and …

READ MORE

2017-04-26T18:20:11

The following code allows us to monkey patch the json() method that is returned by requests.post().

import requests
from mock import MagicMock


@mock.patch.object(requests, 'post')
def my_function(self, post):
    attrs = {'json.return_value': {'id': 1}}
    post.return_value = MagicMock(**attrs)

    r = requests.post()
    r.json()['id']

Source: https://docs.python.org/dev/library/unittest.mock.htmll#unittest.mock.NonCallableMock

READ MORE

2017-04-03T20:16:40
from sqlalchemy.sql import text
from sqlalchemy import create_engine

import secrets


engine = create_engine(
    'mssql+pyodbc://{}:{}@MSSQL'.format(
        secrets.username, secrets.password
    )
)
conn = engine.connect()

s = text("SELECT * FROM users WHERE name = :name")
result = conn.execute(s, name=name).fetchall()
print result

For further information on installing and configuring unixODBC (requirement for pyodbc) on Linux: …

READ MORE

2017-02-16T15:41:13
#!/usr/bin/python
import sys


try: 
    hex_val = str(sys.argv[1])
except Exception as e:
    sys.exit('Please provide a hex code as an argument (e.g. hex2rgb 333333).')

r = int(hex_val[0:2], 16)
g = int(hex_val[2:4], 16)
b = int(hex_val[4:6], 16)

print('{}, {}, {}'.format(r, g, b))

Append your ~/.bash_aliases with:

alias hex2rgb="~/bin/hex2rgb.py"

Load the new alias:

$ …

READ MORE

2016-11-21T23:42:35
import logging

logging_config = {
    'filename': '/var/log/app_log',
    'format': '%(asctime)s [%(levelname)s] %(message)s',
    'level': logging.INFO
}
logging.basicConfig(**logging_config)

logging.info('Our logged message.')

This will output the following to /var/log/app_log:

2016-11-21 23:50:53,677 [INFO] Our logged message.

Source: https://docs.python.org/2/howto/logging.html#logging-basic-tutorial

READ MORE

2016-11-21T23:35:33

The Old Way

import os

os.system('find /home/user/documents -type f -iname "*.doc"')

The New, Recommended Way

import subprocess

subprocess.check_call([
    'find',
    '/home/user/documents',
    '-type', 'f',
    '-iname', '*.doc'
])

Bonus Tidbit

At this point, we can incorporate some nice exception handling:

import subprocess

try:
    subprocess.check_call([
        'find',
        '/home/user/documents',
        '-type', 'f',
        '-iname', '*.doc'
    ])
except subprocess.CallProcessError …

READ MORE

2016-11-11T16:55:21
import json

polls = Poll.objects.all()

# Convert our QuerySet into a list so polls can be serialized
print json.dumps(list(p), indent=2)
JsonResponse({'polls': list(polls)})

Source: http://stackoverflow.com/questions/7650448/django-serialize-queryset-values-into-json

READ MORE

2016-11-03T20:05:28
import json
import psycopg2
from psycopg2.extras import RealDictCursor

cursor = conn.cursor(cursor_factory=RealDictCursor)
objects = cursor.fetchall()

print json.dumps(objects, indent=2)

# Django
#JsonResponse({'objects': objects})

Source: https://www.peterbe.com/plog/from-postgres-to-json-strings

READ MORE

2016-09-06T07:51:45
#!/usr/bin/env python

import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

import django
django.setup()

from app.models import Class

# Your scripting here.

Sources:
https://docs.djangoproject.com/en/1.10/intro/tutorial02/#playing-with-the-api
https://docs.djangoproject.com/en/1.10/topics/settings/#on-the-server-mod-wsgi

READ MORE

2016-08-29T22:49:43

Set the end date with the current date and time and the start date 1 week prior:

import datetime

start_date = datetime.datetime.now() - datetime.timedelta(days=7)
end_date = datetime.datetime.now()

READ MORE

2016-02-04T08:04:04
class AbstractClass(models.Model):
    common_attribute = models.CharField()

    class Meta:
        abstract = True


class SpecializedClass(models.Model):
    specialized_attribute = models.CharField()

    class Meta:
        abstract = True


class AnotherSpecializedClass(models.Model):
    another_specialized_attribute = models.CharField()

    class Meta:
        abstract = True


class MultipleInheritance(AbstractClass, SpecializedClass):
    pass


class TripleMulitpleInheritance(AbstractClass, SpecializedClass, AnotherSpecializedClass):
    pass

Sources: https://docs.djangoproject.com/en/1.9/topics/db/models/#abstract-base-classes,
https://docs.djangoproject.com/en/1.9/topics/db/models/#multiple-inheritance

READ MORE

2016-01-11T21:47:37

Print all digits in a string as one integer:

>>> string = "a1b2c3"
>>> integer = int(filter(str.isdigit, string))
>>> print integer
123
>>> type(integer)
<type 'int'>

Or, if you would like a list of all digits:

>>> string = "a1b2c3"
>>> import re
>>> integer_list = re.findall('\d+', string)
>>> print …

READ MORE