Working Ninja
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)

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

>>> str(a.encode('utf-8'))
'\xe2\x80\x99'
>>> 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.

2018-07-27T22:37:04

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 question.id = 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_(Question.id == 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') \
    .all()

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.

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

Source: https://docs.python.org/3/library/unittest.mock.html#quick-guide

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 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': '127.0.0.1',
  '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.

Sources:
1https://dev.mysql.com/doc/connector-python/en/connector-python-cext.html
2https://dev.mysql.com/doc/connector-python/en/connector-python-example-connecting.html
 

2018-04-22T10:50:51

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 ./manage.py shell_plus. If you're not, simply run from django.utils import timezone.

For usage:

>>> help(timezone.make_aware)
>>> help(timezone.localtime)
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 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.

2017-11-18T10:43:09

This post outlines that bare minimum that needs to be done to set up a post_save signal using Django. One rationale behind using a signal is that a model instance can be saved without the user having to wait for additional code to process (e.g. consuming an external API). This lets the user quickly save a form and be on their merry way while in the background the model save() method triggers our signal which executes additional code. This all happens without incuring any wait time for the user.

Create a new file to hold our signals:

# polls/signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import Polls


@receiver(post_save, sender=Polls)
def email_poll_author(sender, **kwargs):
    # Code to email poll author

""" https://docs.djangoproject.com/en/stable/topics/signals/#connecting-to-signals-sent-by-specific-senders """

Initialize our signals when Django loads:

# polls/apps.py
from django.apps import AppConfig


class PollsConfig(AppConfig):
    def ready(self):
        from . import signals

""" https://docs.djangoproject.com/en/stable/ref/applications/#django.apps.AppConfig.ready """

Lastly, tell Django to use our AppConfig (polls/apps.py):

# polls/__init__.py
default_app_config = 'polls.apps.PollsConfig'

""" https://docs.djangoproject.com/en/stable/ref/applications/#for-application-authors """

Signals: https://docs.djangoproject.com/en/stable/topics/signals/
Additional types of signals: https://docs.djangoproject.com/en/stable/ref/signals/

2017-09-01T17:53:32

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 book.get('author') for book in books):
    print('Found Lewis!')
2017-08-30T17:52:40

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. Cloudflare) from referring to an outdated static file.

Here are the minimum settings / steps to get this going:

# settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'static'),

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'staticfiles'),
]

After setup (and after updates to any files contained within STATICFILES_DIRS), make sure to run ./manage.py collectstatic. This is easy to forget!

More:
https://docs.djangoproject.com/en/stable/ref/contrib/staticfiles/#manifeststaticfilesstorage
https://docs.djangoproject.com/en/stable/ref/settings/#staticfiles-storage

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 %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)

# Now log something
logger.info('Log something informative.')
logger.warning('Log a warning.')
logger.error('Log an error.')
logger.critical('Log a critical error.')

More examples and configurations: https://docs.python.org/2.7/library/logging.html