Commits

Jason R. Coombs committed 558b571

Removing sandbox

  • Participants
  • Parent commits 466b545

Comments (0)

Files changed (11)

File sandbox/bluehost.py

-"""
-Fabric recipes for bluehost
-"""
-
-import urllib2
-import os
-import sys
-import types
-import contextlib
-
-from fabric.api import run, cd
-from fabric.contrib import files
-
-@contextlib.contextmanager
-def _tarball_context(url):
-	"""
-	Get a tarball, extract it, change to that directory, yield, then
-	clean up.
-	"""
-	build_dir = os.path.basename(url).replace('.tar.gz', '').replace(
-		'.tgz', '')
-	run('wget {url} -O - | tar xz'.format(**vars()))
-	try:
-		with cd(build_dir):
-			yield build_dir
-	finally:
-		run('rm -R {build_dir}'.format(**vars()))
-
-def _url_module_import(url):
-	"""
-	Grab a python file from a URL and import it as a module
-	"""
-	data = urllib2.urlopen(url).read()
-	name = os.path.basename(url)
-	name, ext = os.path.splitext(name)
-	module = sys.modules.setdefault(name, types.ModuleType(name))
-	module.__file__ = url
-	exec data in module.__dict__
-	return module
-
-def install_distribute():
-	"""
-	Install distribute (setuptools) into the user's .local profile
-	"""
-	# get the latest version info from the installer_script
-	distribute_setup = _url_module_import('http://python-distribute.org/distribute_setup.py')
-	download_url = '{DEFAULT_URL}distribute-{DEFAULT_VERSION}.tar.gz'.format(**vars(distribute_setup))
-	#prefix = '--prefix={prefix}'.format(**vars()) if prefix else ''
-	with _tarball_context(download_url):
-		run('python2.6 setup.py install --user')
-
-def install_cherrypy(url_base = '/cp'):
-	"""
-	Install a CherryPy application as a FCGI application on `url_base`.
-	"""
-	run('.local/bin/easy_install cherrypy')
-	run('.local/bin/easy_install flup')
-	
-	url_base = url_base.strip('/')
-	# set up the FCGI handler
-	files.append('public_html/.htaccess', [
-		'AddHandler fcgid-script .fcgi',
-		'RewriteRule ^{url_base}/(.*)$ /cgi-bin/cherryd.fcgi/$1 [last]'.format(**vars()),
-	])
-	# install the cherrypy conf
-	files.append('public_html/cgi-bin/cherryd.conf', [
-		'[global]',
-		'server.socket_file=None',
-		'server.socket_host=None',
-		'server.socket_port=None',
-	])
-	
-	# install the cherrypy fcgi handler
-	files.append('public_html/cgi-bin/cherryd.fcgi', [
-		'#!/bin/sh',
-		'~/.local/bin/cherryd -P modules -c cherryd.conf -f -i app',
-	])
-	run('chmod 755 public_html/cgi-bin/cherryd.fcgi')
-	
-	run('mkdir -p public_html/cgi-bin/modules')
-	files.append('public_html/cgi-bin/modules/app.py', [
-		'import cherrypy',
-		'class Application:',
-		'\t"Define your application here"',
-		'cherrypy.tree.mount(Application(), "/cgi-bin/cherryd.fcgi")',
-	])

File sandbox/cheesestrap.py

-
-import urllib2
-import urlparse
-import zipfile
-import tarfile
-import functools
-
-# create a custom importer
-
-class CheeseshopLoader(object):
-	"stubbed"
-	
-def exception_ignore(iterable):
-	while True:
-		try:
-			item = next(iterable)
-		except StopIteration:
-			raise
-		except Exception:
-			pass
-		
-class CheeseshopFinder(object):
-	"""
-	Custom import finder for URL-based resources
-	"""
-	url = 'http://cheeseshop'
-	
-	def find_module(fullname, path=None):
-		# currently, the fullname must be available on the URL
-		possible_urls = self.get_urls(fullname)
-		data = next(
-			exception_ignore(
-				itertools.imap(self.open_url, possible_urls)
-			))
-		_, ext = os.path.splitext(data.url)
-		targz_handler = functools.partial(tarfile.open, mode='r|gz')
-		handler_cls = [zipfile.ZipFile, targz_handler]['gz' in ext]
-		handler = handler_cls(data)
-
-	def open_url(self, url):
-		return urllib2.urlopen(url)
-
-	def get_urls(self, fullname):
-		for ext in '.egg', '.zip', '.tar.gz':
-			yield urlparse.urljoin(self.url, fullname + ext)

File sandbox/deform_ex.py

-import colander, deform
-import cherrypy
-
-class Schema(colander.Schema):
-	text = colander.SchemaNode(colander.String())
-
-def page(formtext):
-	return '<html><body>%s</body></html>' % formtext
-
-class Server:
-	@cherrypy.expose
-	def index(self, **params):
-		form = deform.Form(Schema(), buttons=('submit',))
-		form['text'].widget = deform.widget.TextInputWidget(size=60)
-		if params:
-			try:
-				data = form.validate(params.items())
-			except deform.ValidationFailure, e:
-				return page(e.render())
-			return page('OK - your data was %s' % data)
-
-		return page(form.render())
-
-cherrypy.quickstart(Server())

File sandbox/frink-rpc.py

-import sys
-from SimpleXMLRPCServer import SimpleXMLRPCServer
-
-sys.path.append(r'c:\users\jaraco\downloads\frink.jar')
-
-from frink.parser import Frink
-
-f = Frink()
-
-server = SimpleXMLRPCServer(('', 8000))
-print "Listening on port 8000..."
-server.register_function(f.parseString, "parse")
-server.serve_forever()

File sandbox/gg_classes.py

Empty file removed.

File sandbox/gg_fixers.py

-import re
-import optparse
-import os
-import csv
-from xml.etree import ElementTree as etree
-
-def write_data(data, filename):
-	'''
-	Given data (a list of row), write it to filename
-	Print each row as it's being added to the file
-	'''
-	
-	# open the output file for writing
-	file = open(filename, 'w')
-	spamWriter = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
-	for row in data:
-		assert isinstance(row, list)
-		print row
-		spamWriter.writerow(row)
-
-def example_using_mysql():
-	import mysql
-	connection = mysql.connect('my.databaseserver.com', username='', password='')
-	res = connection.execute('select * from blah')
-	for row in res:
-		do_something(row) # define do_something elsewhere
-
-def fix_body(body):
-	text = body.text
-	# sometimes the body text is empty; if so, just skip
-	if text is None:
-		return
-	key = '}\n\n\n\n'
-	index = text.rfind(key)
-	if index > 0:
-		# the chop point is the index we found plus the length of those
-		#  characters
-		chop = index+len(key)
-		# remove everything after the chop point
-		fixed_text = text[chop:]
-		# now that we've patched the text, replace it in the body
-		body.text = fixed_text
-	body.text = body.text.replace('&amp;nbsp;', '')
-
-def get_sense_ids(senses):
-	"""
-	Take a list of SenseMakingItem tags and get the ids for each
-	
-	equivalent to
-	
-	ids = [sense.attrib['ID'] for sense in senses]
-	"""
-	ids = []
-	for sense in senses:
-		id = sense.attrib['ID']
-		ids.append(id)
-	return ids
-
-def get_location_for_sense(sense, label = 'S: STORY TAKES PLACE'):
-	indexing = sense.find('Indexing')
-	questions = indexing.findall('QuestionIndex')
-	for question in questions:
-		# find the one with the ID S: STORY TAKES PLACE
-		if question.attrib['Id'] == label:
-			return question.find('Answer').text
-	# return None
-
-def decode_body_strings(bodies):
-	body_strings = []
-	for body in bodies:
-		body_string = body.text.encode('ascii', 'replace')
-		body_strings.append(body_string)
-	return body_strings
-
-def assemble_rows(tree):
-	"""
-	Given a root element of a tree, return a list of rows suitable for
-	passing to write_data.
-	"""
-	senses = tree.getiterator('SenseMakingItem')
-	bodies = tree.getiterator('Body')
-	ids = get_sense_ids(senses)
-	locations = [get_location_for_sense(sense) for sense in senses]
-	body_texts = [body.text for body in bodies]
-	rows = zip(ids, locations, body_texts)
-	return rows
-
-def construct_rows(tree):
-	"""
-	Similar to assemble_rows, except is sense-oriented.
-	"""
-	senses = tree.getiterator('SenseMakingItem')
-	# create an empty list of row
-	rows = []
-	for sense in senses:
-		body_text = sense.find('Body').text
-		location = get_location_for_sense(sense)
-		id = sense.attrib['ID']
-		row = [id, location, body_text]
-		# append the new row to the list of accumulated rows
-		rows.append(row)
-	return rows
-
-def construct_row(sense):
-	"""
-	For a single sense, construct a row of the interesting columns"
-	"""
-	body_text = sense.find('Body').text
-	location = get_location_for_sense(sense)
-	id = sense.attrib['ID']
-	row = [id, location, body_text]
-	return row
-
-def construct_rows_functional(tree):
-	senses = tree.getiterator('SenseMakignItem')
-	# call construct row on each sense and return a list of the results
-	return map(construct_row, senses)
-
-def patch_xml_file(filename):
-	"""Read the xml file, fix the body tags, then save it to output.xml"""
-	global tree
-	tree = etree.parse(filename)
-	bodies = tree.getiterator('Body')
-	for body in bodies:
-		fix_body(body)
-	# re-write the modified xml back to a file
-	tree.write('output.xml', encoding='utf-8')
-
-def read_raw_file(filename):
-	"""
-	Given a filename of some file, read in the data, and
-	optionally transform the data.
-	"""
-	file = open(filename)
-	data = file.read()
-	return process_data(data)
-
-def process_data(data):
-	"""
-	Take dirty data and return clean data
-	"""
-	# Now you have the file as a string in the variable called data.
-	# Now you can use regular expressions to locate the chunks and replace them.
-	start = re.escape('<w:')
-	end = re.escape('latin;}')
-	anything = '.*?'  # the question mark means get the shortest possible match
-	pattern_string = start + anything + end
-	print "using pattern:", pattern_string
-	pattern = re.compile(start + anything + end, re.DOTALL) # I use dotall to include carriage returns
-
-	# now replace all matches with the empty string
-	replacement = ''
-	clean_data = pattern.sub(replacement, data)
-	return clean_data
-
-def save_clean_data(data):
-	# to make this quick and easy, just use output.dat as the filename
-	# to save.
-	file = open('output.dat', 'w')
-	file.write(data)
-
-class Story:
-	"""
-	A Story object for collecting info about various stories
-	"""
-	def __init__(self):
-		self.needs = set()
-
-	def add_need(self, need):
-		# make all needs lower case (or upper if you like)
-		need = need.lower()
-		self.needs.add(need)
-
-	def has_need(self, need):
-		"Return true if the supplied need is covered by this story"
-		need = need.lower()
-		return need in self.needs
-
-	def get_as_row(self):
-		"""Return a sequence of things useful for feeding to CSV"""
-		return self.text, self.id, self.location
-
-	def calculate_location(self):
-		# use open calais and geonames with self.needs and other
-		#  attributes of this object.
-		pass
-
-	# use __str__ to show how this object should be represented
-	def __str__(self):
-		return "Story object with %s needs" % self.needs
-
-def get_need_count(stories, need):
-	count = 0
-	for story in stories:
-		if story.has_need(need):
-			# count = count + 1
-			count += 1
-	return count
-
-"""
-Create a story and add needs manually
-my_story = Story()
-my_story.add_need('food')
-my_story.add_need('social relations')
-"""
-
-def story_from_xml_node(node):
-	"""
-	node should be a SenseMakingItem node from the dataset
-	"""
-	# create the new blank story (instance of the Story class)
-	story = Story()
-	question_nodes = node.find('Indexing').findall('QuestionIndex')
-	for question in question_nodes:
-		if question.attrib['Id'] != 'NEED':
-			# This is not a need node, so skip to the next question
-			continue
-		answers = question.findall('Answer')
-		for answer in answers:
-			story.add_need(answer.text)
-	story.text = node.find('Body').text
-	story.location = get_location_for_sense(node)
-	story.id = sense.attrib['ID']
-	return story
-
-def load_stories(filename):
-	tree = etree.parse(filename)
-	senses = tree.getiterator('SenseMakingItem')
-	stories = map(story_from_xml_node, senses)
-	return stories
-
-def main():
-	"The main entry point into our program"
-	# parse the command line arguments
-	parser = optparse.OptionParser()
-	# provide no options
-	# now parse options and arguments
-	options, args = parser.parse_args()
-	# check to make sure a filename was provided
-	if not len(args) == 1:
-		print "You didn't give me a filename"
-		raise SystemExit(1)
-	filename = args[0]
-	if not os.path.isfile(filename):
-		print "The name you gave me doesn't appear to be a file"
-		raise SystemExit(1)
-	#patch_xml_file(filename)
-	global stories
-	stories = load_stories(filename)
-	print "There are", get_need_count(stories, 'Food'), "stories covering food"
-
-if __name__=='__main__':
-	pass 
-	main()

File sandbox/mp.py

-import multiprocessing
-
-def f(foo, bar=None):
-	result = foo + bar if bar else foo
-	return result, bar is not None
-
-def pool_in_globals():
-	return 'pool' in globals()
-
-def process_name():
-	return multiprocessing.current_process().name
-
-pool = multiprocessing.Pool(1)
-
-class MyClass(object):
-
-	def do_it(self):
-		args = [3,4]
-		kwargs = dict()
-		print 'result is', pool.apply(f, args, kwargs)
-		print 'result is', pool.apply(f, [3])
-		try:
-			print 'result is', pool.apply(f, [3,'x'])
-		except TypeError:
-			print 'type error'
-		print 'pool_in_globals() ->', pool.apply(pool_in_globals, [])
-		print 'process name in main:', process_name()
-		print 'process name in child:', pool.apply(process_name)
-
-if __name__ == '__main__':
-	o = MyClass()
-	o.do_it()

File sandbox/pick_winner.py

-from __future__ import print_function
-
-import random
-import operator
-from collections import namedtuple
-
-from jaraco.util.dictlib import RangeMap
-
-Entrant = namedtuple('Entrant', 'id volume')
-entrants = [
-	Entrant(1, 1),
-	Entrant(2, 5),
-	Entrant(3, 3),
-	Entrant(4, 1),
-	# ...
-]
-
-def calculate_accumulated_volumes(entrants):
-	# make acc_volume there so we have total
-	global acc_volume
-	acc_volume = 0
-	for entrant in entrants:
-		acc_volume += entrant.volume
-		yield acc_volume
-
-
-# create a map where a volume between 0 and total_volume produces gives
-#  a weighted selection of Entrants.
-
-weighted_entrants = RangeMap(zip(calculate_accumulated_volumes(entrants), entrants),
-	key_match_comparator=operator.lt)
-
-# test our bounds
-assert weighted_entrants[0] == Entrant(1,1)
-assert weighted_entrants[0.99999] == Entrant(1,1)
-assert weighted_entrants[1] == Entrant(2,5)
-assert weighted_entrants[6] == Entrant(3,3)
-try:
-	weighted_entrants[acc_volume]
-except KeyError:
-	pass
-else:
-	raise ValueError("Expected exception")
-
-
-# now pick an entrant
-for x in range(3):
-	index = random.uniform(0, acc_volume)
-	pick = weighted_entrants[index]
-	print('picked', pick, '(%s)' % index)

File sandbox/print_args.py

-from __future__ import print_function
-import sys
-print(sys.argv, len(sys.argv))

File sandbox/test-frink-server.py

-import xmlrpclib
-
-proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
-
-def frink(string):
-	result = proxy.parse(string)
-	print 'frink says %(string)s is %(result)s' % vars()
-
-frink('2+2')
-frink('2+x')
-frink('F[C[35]]')

File sandbox/xml_to_json.py

-import json
-import lxml
-
-class objectJSONEncoder(json.JSONEncoder):
-	"""A specialized JSON encoder that can handle simple lxml objectify types
-	>>> from lxml import objectify
-	>>> obj = objectify.fromstring("<Book><price>1.50</price><author>W. Shakespeare</author></Book>")
-	>>> objectJSONEncoder().encode(obj)
-	'{"price": 1.5, "author": "W. Shakespeare"}'
-	"""
-	def default(self,o):
-		if isinstance(o, lxml.objectify.IntElement):
-			return int(o)
-		if isinstance(o, lxml.objectify.NumberElement) or isinstance(o, lxml.objectify.FloatElement):
-			return float(o)
-		if isinstance(o, lxml.objectify.ObjectifiedDataElement):
-			return str(o)
-		if hasattr(o, '__dict__'):
-			#For objects with a __dict__, return the encoding of the __dict__
-			return o.__dict__
-		return json.JSONEncoder.default(self, o)