Tag Archives: python

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

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

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

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>