Working Ninja

Toggle all <option>'s as checked/unchecked with the click of a <button>.


    jQuery("select :checkbox").each(function(){
        this.checked = !this.checked;



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)

Give your hand some relief from reaching for Ctrl and take advantage of the close proximity of Caps Lock.

# Note any previous values for xdb-options (most likely none)
gsettings get org.gnome.desktop.input-sources xkb-options

# Set our xkb-options (plus anything from our get, if needed)
gsettings set org.gnome.desktop.input-sources xkb-options "['ctrl:nocaps']"

Now to retrain your brain ;)




You should name a variable using the same care with which you name a first-born child (xxi).

Quality is the result of a million selfless acts of care--not just of any great method that descends from the heavens. That these acts are simple doesn't mean that they are simplistic, and it hardly means that they are easy. They are nonetheless the fabric of greatness and, more so, of beauty, in any endeavor. To ignore them is not yet to be fully human (xxii).

Chapter 2: Meaningful Names

Clarity is king (25).

Our goal, as authors, is to make our code as easy as possible to understand. We want our code to be a quick skim, not an intense study. We want to use the popular paperback model whereby the author is responsible for making himself clear and not the academic model where it is the scholar's job to dig the meaning out of the paper (27).

Chapter 3: Functions

Functions are the first line of organization in any program (31).

The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that (34).


So, another way to know that a function is doing more than "one thing" is if you can extract another function from it with a name that is not merely a restatement of its implementation (36).

Master programmers think of systems as stories to be told rather than programs to be written (49).

Chapter 4: Comments

The proper use of comments is to compensate for our failure to express ourself in code. Note that I used the word failure. I meant it. Comments are always failures. We must have them because we cannot always figure out how to express ourselves without them, but their use is not a cause for celebration (54).

The older a comment is, and the farther away it is from the code it describes, the more likely it is to be just plain wrong. The reason is simple. Programmers can't realistically maintain them (54).

Truth can only be found in one place: the code (54).

It takes only a few seconds of thought to explain most of your intent in code. In many cases it's simply a matter of creating a function that says the same thing as the comment you want to write (55).

Plopping in a comment just because you feel you should or because the process requires it, is a hack (59).

Any comment that forces you to look in another module for the meaning of that comment has failed to communicate to you and is not worth the bits it consumes (60).

Others who see that commented-out code won't have the courage to delete it (68).

Chapter 5 - Formatting

Each line represents an expression or a clause, and each group of lines represents a complete thought. Those thoughts should be separated from each other with blank lines (78).

Have you ever hunted up the chain of inheritance for the definition of a variable or function? This is frustrating because you are trying to understand what the system does, but you are spending your time and mental energy on trying to locate and remember where the peices are. Concepts that are closely related should be kept vertically close to each other. Clearly this rule doesn't work for concepts that belong in separate files. But then closely related concepts should not be separated into different files unless you have a very good reason.  Indeed, this is one of the reasons that protected variables should be avoided (80).

All quotes from Clean Code: A Handbook of Agile Software Craftsmanship by Robert C Martin.



Enable the "Start up automatically after a power failure" feature found within OS X without OS X installed.


  1. Find the LPC controller via lspci.
  2. Reference the LPC controller datasheet to find the register to update.
  3. Update the register with setpci.
  4. Test.
  5. Add the setpci command to crontab.

I'll be performing the following steps on a 2008 MacBook 4,1. Similar steps should be successful with other models.

1. Find the LPC controller via lscpi.

$ lspci | grep LPC
00:1f.0 ISA bridge: Intel Corporation 82801HM (ICH8M) LPC Interface Controller (rev 04)

Note the PCI address (00:1f.0) of our LPC Interface Controller. This will be referenced later with the setpci command.

2. Reference the LPC controller datasheet to find the register to update.

Searching the Internet for "82801HM datasheet" yields this datasheet from Intel. After opening the PDF, find the page for "AFTERG3_EN" via the Appendix A Register Bit Index (at the end of the PDF): GEN_PMCON_3—General PM Configuration 3 Register (PM—D31:F0)  

Offset Address: A4h
Default Value: 00h
Lockable: No
Attribute: R/W, R/WC
Size: 16-bit
Usage: ACPI, Legacy
Power Well: RTC

Bit: 0
Description: AFTERG3_EN — R/W. This bit determines what state to go to when power is re-applied after a power failure (G3 state). This bit is in the RTC well and is not cleared by any type of reset except writes to CF9h or RTCRST#.

0 = System will return to S0 state (boot) after power is re-applied.
1 = System will return to the S5 state (except if it was in S4, in which case it will return to S4). In the S5 state, the only enabled wake event is the Power Button or any enabled wake event that was preserved through the power failure. NOTE: Bit will be set when THRMTRIP#-based shutdown occurs.

From the above, we find that our register location is 0xA4 (A4h) with bit 0 (AFTERG3_EN) being the one that controls whether or not to power on the device after a power failure. Setting this bit to 0 will power on the machine.

3. Update the register with setpci.

Get the current value (should be 1):

$ sudo setpci -v -s 00:1f.0 0xa4.b

Set the bit to 0, per our datasheet:

$ sudo setpci -v -s 00:1f.0 0xa4.b=0

This is where I get lost. I do not see how we are setting just bit 0 to 0 with the above line. Searching online yields the same command for similar devices and different commands for others (who's manuals I cannot find online).

Verify that the bit is now set to 0:

$ sudo setpci -v -s 00:1f.0 0xa4.b

4. Test.

Shutdown the machine and disconnect the power supply.

5. Final Things

The AFTERG3_EN bit is reset to its default at reboot, so lets add the above command to our crontab (as root):

@reboot /usr/bin/setpci -s 00:1f.0 0xa4.b=0



Completely disable systemd power-saving modules:

sudo systemctl mask

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 The calls were numerous and consumed large amounts of CPU.

Sources: and


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.


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/
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

""" """

Initialize our signals when Django loads:

# polls/
from django.apps import AppConfig

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

""" """

Lastly, tell Django to use our AppConfig (polls/

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

""" """

Additional types of signals:


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/ Or better yet, if you haven't already, create a local config for Pulse audio at ~/.config/pulse/

load-module module-switch-on-connect

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



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