You are here

python

Technical Notes

These notes are my personal online notebook of useful commands and "how-to's". You are welcome to make use of them if you find them helpful. They obviously don't come with any warranty! Click on one of the category tags above for the notes in any category.

python OptionParser extended to allow multiple commands

OptionParser is designed for a fixed set of options. But if the first argument is a command that can affect which options are available, then OptionParser can still be used with some clever coding.

#!/usr/bin/env python
# $Id: options.py 17 2013-11-07 03:43:22Z andrewfn $
"""Demo of using OptionParser where the first argument is a command that can take variable options.
The help system is totally automated and the framework is very easy to use."""
ver = 1.0 #put version number here
use = {}
#put the commands in here, with the integer giving the number of extra arguments
Topic: 

Python links

Topic: 

Misc Python Scripts

Here are some misc scripts.

Topic: 

Drupal backup script

First it is necessary to create a backup user on the database which has (only) read access to everything. This is because its password will be embedded in the backup script which reduces security. This can be done with:

BKUSR=backup
BKPW=backuppassword
BKDIR=/var/www/dbbackup
mkdir -p $BKDIR
echo "GRANT SELECT, LOCK TABLES ON *.* TO '$BKUSR'@'localhost' IDENTIFIED BY '$BKPW'; FLUSH PRIVILEGES;" | mysql -u root -p

In the backup script, first some variables are set up

BKUSR=backup
BKPW=backuppassword
BKDIR=/var/www/dbbackup
SITE=cfaw.info

Subscribe to RSS - python