Working Ninja
2018-01-06T17:16:21

Completely disable systemd power-saving modules:

sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

Disable systemd lid switch (for laptops):

sed -i -e 's/HandleLidSwitch=suspend/HandleLidSwitch=ignore/' /etc/systemd/logind.conf

The above became necessary as systemd-logind was detecting that the lid was closed and calling suspend.target. The calls were numerous and consumed large amounts of CPU.

Sources:
https://askubuntu.com/a/858617 and https://unix.stackexchange.com/a/52645

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-10-25T18:17:54

Before stumbling upon this module, whenever I connected to my audio receiver via Bluetooth, I had to change the Pulseaudio sink as well as set the default output. Thankfully there is an easy fix that will do both of these when a new sink is available.

Add the following to /etc/pulse/default.pa. Or better yet, if you haven't already, create a local config for Pulse audio at ~/.config/pulse/default.pa:

.include /etc/pulse/default.pa
load-module module-switch-on-connect

Now if only I had stumbled upon this before attempting to write a custom udev rule!

Source: https://askubuntu.com/questions/158241/automatically-change-sound-input-output-device#158250

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-27T09:20:02

Getting Real by 37signals (available online) is a good mind stretcher. It provided much of the content for Rework (also by 37signals). Both books provided a lot of counter-intuitive business and development advice that works rather well in the software industry. Rework is one of my favorite business books with great illustrations (also available online).

Favorite chapters (from Getting Real):

Maybe just read it all =)

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

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 of the script:

import sys
sys.path.insert(0,'/path/to/project')
sys.path.insert(0,'/path/to/project/env/lib/python2.7/site-packages')

Lastly, call the script via Cron:

cat /path/to/project/script.py | scl enable python27 python
2017-08-24T21:01:28

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
        )

class Change(models.Model):
    ...
    approved_changes = ApprovedChanges()

In our view, we could then call all changes for "today" that have been approved:

# views.py
def todays_changes(self):
   changes = Changes.approved_changes.all()
   ...

Source: https://docs.djangoproject.com/en/stable/topics/db/managers/