WriteableList.py

#!/usr/bin/env python

"""
$Id: WriteableList.py 276 2001-11-05 13:47:05Z david $

This module provides the WriteableList class, which makes it possible
to write out a list in valid Python syntax.
"""

from UserList import UserList


class WriteableList (UserList):
	def __init__ (self, other=None):
		UserList.__init__(self)
		if type(other) == type([]):
			self.data = other
		elif isinstance(other, UserList):
			self.data = other.data
	
	def write (self, filename, listname="data"):
		if not filename[-3:] == ".py":
			filename = filename + ".py"
		f = open(filename, "w")
		f.write("# %s generated by WriteableList.py\n" % filename)
		f.write("%s = [\n" % listname)
		for datum in self.data:
			f.write("\t%s,\n" % repr(datum))
		f.write("]\n")
		f.close()