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

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 as e:
    print e.returncode
    print e.cmd
    print e.output

See the official Python Documentation for more usage and recommendations on the subprocess module.

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

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

2016-11-02T20:35:11

Server
Install NFS server components:

# apt-get install nfs-kernel-server portmap

Add the "root" of all shares (if this is your only share, this should be all you need):

# echo "/home 192.168.1.0/24(ro,fsid=0)" >> /etc/exports

Add additional shares:

# echo "/home/joe 192.168.1.0/24(rw,sync,no_subtree_check)" >> /etc/exports

Update NFS exports table:

# exportfs -avr

Restart NFS server:

# service nfs-kernel-server restart

Don't forget to allow NFS through iptables.

Client

# apt-get install nfs-common
# mount server:/share /share

Prosit!

Notes

Slow initial directory listing is caused by aliases (specifically color output). Explanation here: http://www.novell.com/support/kb/doc.php?id=7005969. Use \ls -la instead of ll as a workaround.

Sources:
https://wiki.debian.org/NFS
https://help.ubuntu.com/community/SettingUpNFSHowTo
https://www.centos.org/docs/5/html/Deployment_Guide-en-US/s1-nfs-server-config-exports.html
http://www.tldp.org/HOWTO/NFS-HOWTO/intro.html
http://nfs.sourceforge.net/nfs-howto/ar01s07.html

2016-11-01T16:55:19

Before adding a script to cron and finding that it doesn't work because of a PATH issue, here's a handy command to test:

env -i ./script

This will run your script in the same environment that cron will.

2016-09-15T22:41:01

Here is a quote I just now came across that wonderfully sums up why I enjoy programming.

The goal of this book is to teach you to think like a computer scientist. This way of thinking combines some of the best features of mathematics, engineering, and natural science. Like mathematicians, computer scientists use formal languages to denote ideas (specifically computations). Like engineers, they design things, assembling components into systems and evaluating tradeoffs among alternatives. Like scientists, they observe the behavior of complex systems, form hypotheses, and test predictions.

The single most important skill for a computer scientist is problem solving. Problem solving means the ability to formulate problems, think creatively about solutions, and express a solution clearly and accurately.

How to Think Like a Computer Scientist by Peter Wentworth, Jeffrey Elkner, Allen B. Downey, and Chris Meyers

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

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()
2016-05-01T11:09:13