Skip to content

Instantly share code, notes, and snippets.

Avatar

Sean Hammond seanh

View GitHub Profile
@seanh
seanh / walk.py
Created Mar 18, 2009
Recursively walk a directory in Python with os.walk.
View walk.py
"""Recursively walk a directory with os.walk."""
import os
base_dir = '/home/seanh' # The directory to walk
for root, dirs, files in os.walk(base_dir):
print 'root: ',root
print ' dirs:'
for d in dirs:
print ' ',d
@seanh
seanh / MyClass.java
Created May 25, 2009
Get a Swing GUI up and running. (Handy for testing swing components.)
View MyClass.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyClass {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
@seanh
seanh / gist:165218
Created Aug 10, 2009
Read a file in Python
View gist:165218
# The built-in function `open` opens a file and returns a file object.
# Read mode opens a file for reading only.
try:
f = open("file.txt", "r")
try:
# Read the entire contents of a file at once.
string = f.read()
# OR iterate over the file line-by-line:
for line in f:
# Do something with line.
@seanh
seanh / gist:232829
Created Nov 12, 2009
Writing text files in Python
View gist:232829
# Write mode creates a new file or overwrites the existing content of the file.
# Write mode will _always_ destroy the existing contents of a file.
try:
# This will create a new file or **overwrite an existing file**.
f = open("file.txt", "w")
try:
f.write('blah') # Write a string to a file
f.writelines(lines) # Write a sequence of strings to a file
finally:
f.close()
@seanh
seanh / gist:232824
Created Nov 12, 2009
Import a module from a directory (Python)
View gist:232824
"""When you want to import a python file but that file is not in the same directory as the python file you're importing from (e.g. it's in a subdir). You need to add the directory that contains the file you want to import to your path environment variable sys.path. sys.path contains all the places python will look for a file when you do an import."""
import sys
sys.path.append('./markdown-1.7')
from markdown import Markdown
@seanh
seanh / README.markdown
Created Mar 26, 2012
virtualenv bootstrap script for creating a virtual environment and installing CKAN into it
View README.markdown

To create a virtual environment and install CKAN into it:

git clone git://gist.github.com/2206132.git
python 2206132/ckan-bootstrap.py pyenv

This makes a directory pyenv in the current working directory, creates a virtual environment in the pyenv dir, and installs CKAN (and all CKAN's dependencies) into the virtual environment.

Activate the virtual environment and run the CKAN tests:

@rufuspollock
rufuspollock / data.json
Created Oct 1, 2012
CKAN - Load Demo Data
View data.json
{
"datasets": {
"adur_district_spending": {
"author": "Lucy Chambers",
"author_email": "",
"extras": {
"spatial-text": "Adur, West Sussex, South East England, England, United Kingdom",
"spatial": "{ \"type\": \"Polygon\", \"coordinates\": [ [ [-0.3715, 50.8168],[-0.3715, 50.8747], [-0.2155, 50.8747], [-0.2155, 50.8168], [-0.3715, 50.8168] ] ] }"
},
"license": "License Not Specified",
@seanh
seanh / README.md
Created Nov 19, 2012 — forked from rufuspollock/data.json
A script that pulls some demo data (gold prices, Malawi aid projects, etc.) from datahub.io and pushes them to your CKAN instance.
View README.md

Installation

virtualenv load_demo_data
. load_demo_data/bin/activate
mkdir load_demo_data/src
cd load_demo_data/src
git clone git://gist.github.com/4110454.git load_demo_data
pip install -r load_demo_data/pip-requirements.txt
@seanh
seanh / gist:232821
Created Nov 12, 2009
Creating and removing files and directories with Python os module
View gist:232821
"""This is done with the os module, which has lots of methods for handling files and dirs.
<http://docs.python.org/lib/os-file-dir.html>
Effbot's page on the os module: <http://effbot.org/librarybook/os.htm>
The shutil module is useful here also: <http://docs.python.org/lib/module-shutil.html>
"""
import os
@seanh
seanh / gist:232828
Created Nov 12, 2009
List all the files in a directory with a one-line list comprehension (Python)
View gist:232828
# http://www.diveintopython.org/file_handling/os_module.html
# Use a one-line list comprehension to get all the files in a given directory with a given extension.
import os
dir = '.'
ext = '.txt'
txt_files = [f for f in os.listdir(dir) if os.path.isfile(os.path.join(dir,f)) and f.endswith(ext)]
# os.path.join joins a directory and a filename into a path. You can also split a path name into directory and file with
# os.path.split(), and you can split a filename with extension into filename and extension with os.path.splitext()
You can’t perform that action at this time.