Category Archives: <code>

View Django Project Settings

Start a Python shell and import settings to view your Django project settings python manage.py shell >>> import settings >>> settings.ROOT_URLCONF ‘myproject.urls’

Posted in <code> | Leave a comment

Short Django Tutorial Notes

Some condensed notes on the Django tutorial (Django 1.3) This is just a short self-reference (or maybe a 2-minute overview), not an actual tutorial. Database Create / update database python manage.py syncdb Models Models define how data is stored. Create a model by inheriting from django.db.models.Model from django.db import models   class Choice(models.Model): poll = models.ForeignKey(Poll) choice = models.CharField(max_length=200) votes … Continue reading

Posted in <code> | Tagged , , , | 1 Comment

Installing Mezzanine (Django based CMS) on Dreamhost via virtualenv

Here’s some notes for installing Mezzanine on Dreamhost using virtualenv. This can be useful for installing on any server where you don’t have permissions to install python packages normally. There’s also notes here for how to set up Passenger to serve your site via Apache. In Dreamhost panel, setup site for use with Passenger & ssh Ssh into your Dreamhost … Continue reading

Posted in <code> | Tagged , , , , , , , , , , , | 3 Comments

Bedtools-python install in Windows

Compiling applications from source may be the MO in Linux, but it is really tedious in Windows. This is not for the faint of heart.

Posted in <code> | Leave a comment

Simple python printing

Simplify python string formatting using locals() or vars(). In python 2 style print keyword usage. Use print( string ) for python 3k. name = ‘moose’ adjective = ‘squishy’   print ‘{name} is {adjective}’.format(**locals()) # moose is squishy   # vars() with no args is same as locals() print ‘{name} is {adjective}’.format(**vars()) # moose is squishy Using vars to access object … Continue reading

Posted in <code> | Leave a comment

explicit is not better than implicit

I’m sick of all the python fangeeks always using the argument that “explicit is better than implicit” to justify python behaviors. I think it’s a good design principle to consider, but it is not a overarching commandment that good code should always be explicit. Convenience and readability is sometimes better than explicit. Use your brain, don’t be a eibti zombie.

Posted in <code> | Leave a comment

Python call easy_install within a Python session

Thanks stack overflow from setuptools.command import easy_install easy_install.main( ["-U","py2app"] )

Posted in <code> | Tagged , | Leave a comment

Python Getters and Setters

A quick note on defining getters and setters using decorators for Python 2.6+ #Must inherit from object #class C: won’t work. class C(object): # Define getter for x #@x.getter doesn’t work because self.x is not defined yet :( @property def x(self): return self._x   # Define setter for x @x.setter def x(self, value): self._x = value   # Define deleter … Continue reading

Posted in <code> | Tagged , , , | Leave a comment

How to setup a SparkleShare Private Server on Ubuntu

*Feb 3, 2011: This is a work in progress… *Todo: nautilus integration not working – missing dependencies? (no ubuntu python-nautilus-dev package?) *Todo: compile a release build instead of a debug build? *Todo: build a ubuntu/debian package? SparkleShare is dropbox-like software that can sync to your own private server. The SparkleShare documentation recommends syncing to GitHub or Gitorius, but all you … Continue reading

Posted in <code>, <linux> | Tagged , , , , , , , | 9 Comments

Python woes

It’s cool that there are so many Python modules available, but it’s kinda a pain to install them all. It’s too bad easy_install doesn’t do better dependency checking. It just errors out when there’s something missing. I couldn’t get numpy to work with the 64 bit version of Python 2.7 for Windows, so I uninstalled the 64 bit version and … Continue reading

Posted in <code> | Leave a comment

Python: Create directory

Simple Python snippet which creates a directory if it doesn’t exist. #! /usr/bin/env python import os   # create directory "mydir" if it doesn’t exist already os.path.exists("mydir") or os.mkdir("mydir")

Posted in <code> | Tagged , , , , , | Leave a comment

Bash: Use pathmunge to manage your PATH

Simplify your bash PATH management with pathmunge. The pathmunge will add a directory to the beginning of your PATH if it is missing from your PATH. This means that you can safely .source ~/.bashrc without ending up with duplicate folders in your path (which you would get if you had used export PATH=/add/this/dir:$PATH). It is also clearer what folders you … Continue reading

Posted in <code> | Tagged , , , , , , | Leave a comment

Python Gripes

It seems like a real object oriented language shouldn’t require “self” as the first argument to all its class methods. It reminds me of Matlab’s hackish inefficient classes. Down with the explicit self! Not having to type semicolons at the end of lines is nice, but needing to type colons at the end of function definitions and if statements is … Continue reading

Posted in <code> | Tagged , , , | Leave a comment

Python ChIP-seq BED file reader

A simple BED file reader in Python. import csv class CommentedFileReader: """ Helper class for file reading. Skips lines starting with ‘#’   tsv_file = csv.reader(CommentedFileReader("inputfile.txt"), delimiter=’\t’) for row in tsv_file: print row[2] # prints column 3 of each line """ def __init__(self, f, commentstring="#"): self.f = open(f, ‘rU’) self.commentstring = commentstring def next(self): line = self.f.next() while line.startswith(self.commentstring): line … Continue reading

Posted in <code> | Tagged , , , , , | Leave a comment

Python: matplotlib plotting with customized axes

I don’t like the Matlab plot axes where you are forced to have a box around your figure. By default, Python’s matplotlib plots like Matlab, but you can customize the axes to your liking. Modified from the matplotlib spine demo import matplotlib.pyplot as plt import numpy as np   fig = plt.figure(facecolor=’white’) x = np.linspace(0,2*np.pi,100) y = 2*np.sin(x)   ax … Continue reading

Posted in <code> | Tagged , , , , , , , , | Leave a comment
<RSS Feed>