Thursday, January 3, 2013

Fetching the entire U.S. Code of Federal Regulations

While working on a side project I was looking for a good data set of large text files to test the speeds in searching and displaying of large amounts of data.  Somehow long lorem ipsum seemed boring and wouldn't provide a good set of data for searching (most or all documents would be returned). So I decided to use the next best thing, the U.S. Code of Federal Regulations.

The House of Representatives provides downloadable text files of each title so I wrote the following Python script to grab them all and save them for use in my project.

import os
import urllib2
import codecs

ENCODING = 'utf-8' # saved files should be UTF-8 encoded
END_OF_TEXT = u'\u0003' # for use by Histrionics

url_stub = 'http://uscode.house.gov/download/pls/Title_{0}.txt'

save_dir = os.path.join('C:\Users\Bob User','cfr_notes')

def write_char(fp,char):
    """
    Write the character to the file, catching any errors
    """
    try:
        fp.write(char)
    except UnicodeDecodeError:
        # skip any un-decodable characters for now
        # see: http://wiki.python.org/moin/UnicodeDecodeError
        pass

for i in range(1,52,1):
    n = str(i).zfill(2)
    url = url_stub.format(n)
    data = urllib2.urlopen(url)
    content = data.read()
    title = 'CFR Title {0}'.format(n)
    filepath = os.path.join(save_dir,'{0}.txt'.format(title))
    print 'Writing: {0}'.format(title)
    with codecs.open(filepath,encoding=ENCODING,mode='wb') as f:
        for char in content:
            write_char(f,char)
        # The following is all specific to the Histrionics project
        f.write('\n')
        f.write(END_OF_TEXT + '\n')
        f.write(u'title:{0}'.format(title))

Thursday, December 13, 2012

PySide UIC Error

If you get the following error when running pyside-uic try removing any button groups and rerunning.
C:\Users\Bob\Berry-GUI>pyside-uic MainWindow.ui -o MainWindow.py
An unexpected error occurred.
Check that you are using the latest version of PySide and report the error to
http://bugs.openbossa.org, including the ui file used to trigger the error.
The problem may be related to the one linked below.
http://srinikom.github.com/pyside-bz-archive/1156.html

Friday, November 30, 2012

PyInstaller & QtXml

After running PyInstaller on a program I'm developing using the PySide module I get an error about a missing QtXml library.  To fix it I had to explicitly import it so that PyInstaller knew to grab it.

# Import Qt modules
from PySide import QtCore, QtGui
from PySide import QtXml # explicit inclusion for pyinstaller

I think this may be related to my use of stylesheets in the application.

Welcome

This blog contains a collection of random coding issues and solutions I run in to, among other things...
# Syntax highlighting provided by:
link = 'http://alexgorbatchev.com/SyntaxHighlighter/'
print 'SyntaxHighlighter'.format(link)