Code Snippets

Snippet is a programming term for a small region of re-usable source code or text. Ordinarily, these are formally-defined operative units to incorporate into larger programming modules. Snippets are often used to clarify the meaning of an otherwise "cluttered" function, or to minimize the use of repeated code that is common to other functions.

Accessing Drupal's XMLRPC/Services API from Python

Probably ripped off from somewhere on drupal.org...

import xmlrpclib
 
s = xmlrpclib.ServerProxy('http://mmatienzo/services/xmlrpc')
 
class DrupalNode:
    def __init__(self, title, body, path, ntype='page', uid=1, username='mmatienzo'):
        self.title = title
        self.body = body
        self.path = path
        self.type = ntype
        self.uid = uid
        self.nid = 67
        self.name = username
        self.promote = True
        self.taxonomy = {'3': '3'} #how do i create new taxonomy terms???
try:
    sessid, user = s.system.connect()
    n = DrupalNode('ZA WARUDO!', 'toki wo tomare', 'saworhhhjjjdss')
    s.node.save('roadroallerdawryyy', n)
 
except xmlrpclib.Fault, err:
    print "A fault occurred"
    print "Fault code: %d" % err.faultCode
    print "Fault string: %s" % err.faultString

Auto-Delete Posts patch for WordPress 2.3

Note: This patch is now deprecated as a new version of the plugin has been released.

Because of changes in the database schema for WordPress 2.3, the Auto Delete Posts plugin does not work. I've patched it to work, but note that this patch makes the plugin only work with WordPress 2.3.

MARC21 to CSV

A coworker at MFPOW wanted me to generate a list of records matching certain diverse criteria from our Horizon database. I wrote the following SQL query to get the data out.

SELECT DISTINCT item.bib#
FROM item, bib WHERE bib.bib# = item.item#
AND item.collection NOT IN ('oh', 'icos', 'mi')
AND item.location = 'icos'
AND (bib.text LIKE '%audio%' OR bib.text LIKE '%video%'
OR bib.text LIKE '%cassette%' OR bib.text LIKE '%tape%'
OR bib.text LIKE '%recording%' OR bib.text LIKE '%film%')

I then slapped together this nasty little bit of Python to get it into Excel for easy viewing and formatting.

from pymarc import MARCReader, marc8_to_unicode
import csv, sys
 
r = MARCReader(file(sys.argv[1]))
w = open(sys.argv[2], 'wt')
 
try:
        writer = csv.writer(w, lineterminator='\n')
        writer.writerow(('Bib #','Main Entry','Title'))
        for record in r:
                try: creator = marc8_to_unicode(record.author())
                except: creator = u' '
                creator = creator.encode('iso-8859-1', 'ignore')
                title = marc8_to_unicode(record['245'].formatField())
                title = title.encode('iso-8859-1', 'ignore')
                writer.writerow((record['998']['b'], creator, title))
 
finally: w.close()