示例#1
0
def getExcel(file, filters={}):
	"""Given a specific Excel file (string) and a set of filters (dictionary
	   key-values pairs), will return a CSV-formatted table of the matching data
	   entries from that file (including a header row). Defaults to the first
	   sheet in the workbook; other sheets can be specified by name or
	   zero-based index using the '_sheet' parameter in the URL's query segment.
	"""
	wb = xlrd.open_workbook(file)
	if '_sheet' not in filters:
		warnings.warn('No worksheet specified (_sheet filter expected); defaulting to first worksheet')
		sheet = 0
	else:
		sheet = filters.pop('_sheet')
	if type(sheet) is type(''):
		ws = wb.sheet_by_name(sheet)
	else:
		ws = wb.sheet_by_index(int(sheet))
	header = [basic.parseStrValue(h.value) for h in ws.row(0)]
	all = []
	for i in range(1,ws.nrows):
		d = {}
		row = ws.row(i)
		for j,h in enumerate(header):
			entry = row[j]
			if entry.ctype == 4:
				value = str(entry.value == 1)
			else:
				value = str(entry.value)
			d[h] = basic.parseStrValue(value)
		all.append(d)
	dicts = basic.filter(all, filters)
	if len(dicts) > 0:
		return formatCsv(dicts)
	else:
		raise Exception('No matching data entries found')
示例#2
0
def parseQueryValues(query):
	"""Returns a copy of the query dictionary for which values in key-value
	   pairs have been parsed from string representations into primitives.
	"""
	for q in query:
		query[q] = basic.parseStrValue(query[q][0])
	return query
示例#3
0
def getSqlite(file, filters={}):
	"""Given a specific SQLite file (string) and a set of filters (dictionary
	   key-values pairs), will return a CSV-formatted table of the matching data
	   entries from that file (including a header row). Requres a '_table'
	   parameter in filters to query from the appropriate table.
	"""
	if '_table' not in filters:
		raise Exception('No table specified (_table filter expected)')
	table = filters.pop('_table')
	filtStrs = []
	for key,value in filters.iteritems():
		if type(value) is type(0.):
			filtStrs.append('%s=%s' % (key,str(value)))
		else:
			filtStrs.append("%s='%s'" % (key,str(value)))
	qry = 'SELECT * FROM %s%s' % (table, ' WHERE ' + ' AND '.join(filtStrs) if len(filters) > 0 else '')
	conn = sqlite3.connect(file)
	curs = conn.execute(qry)
	all = [r for r in curs]
	if len(all) > 0:
		dicts = []
		header = [d[0] for d in curs.description]
		for row in all:
			d = {}
			for ndx,h in enumerate(header):
				value = row[ndx]
				if type(value) is type(0.):
					d[h] = value
				else:
					d[h] = basic.parseStrValue(value)
			dicts.append(d)
		return formatCsv(dicts)
	else:
		raise Exception('No matching data entries found')
示例#4
0
def parseElement(el):
	"""Recursively converts an XML element to a dictionary object. After the
	   initial pass, children are checked for single-value elements that are
	   duplicated as element attributes. Meta-attributes include:
	      _tag, the tag name of the element
		  _children, a list of dictionaries defining child elements
		  _text, the parsed value of any element text content
	   All attribute and text values are parsed for boolean and numeric values.
	"""
	d = el.attrib
	for k in d.keys():
		d[k] = basic.parseStrValue(d[k])
	d['_tag'] = el.tag
	d['_children'] = [parseElement(child) for child in el]
	if el.text is not None:
		d['_text'] = basic.parseStrValue(el.text.strip())
	if len(d['_children']) > 0:
		for ch in d['_children']:
			if ch.keys() == ['_text','_tag']:
				d[ch['_tag']] = ch['_text']
	else:
		d.pop('_children')
	return d
示例#5
0
def getCsv(file, filters={}):
	"""Given a specific CSV file (string) and a set of filters (dictionary
	   key-values pairs), will return a CSV-formatted table of the matching data
	   entries from that file (including a header row).
	"""
	all = []
	with open(file) as f:
		reader = csv.DictReader(f)
		for row in reader:
			for field in row:
				row[field] = basic.parseStrValue(row[field])
			all.append(row)
	dicts = basic.filter(all, filters)
	if len(dicts) > 0:
		return formatCsv(dicts)
	else:
		raise Exception('No matching data entries found')
示例#6
0
文件: basic.py 项目: Tythos/pyroclast
	def test_logical(self):
		self.assertEqual(basic.parseStrValue('true'), True)
		self.assertEqual(basic.parseStrValue('TRUE'), True)
		self.assertEqual(basic.parseStrValue('FALSE'), False)
		self.assertEqual(basic.parseStrValue('false'), False)
示例#7
0
文件: basic.py 项目: Tythos/pyroclast
	def test_numeric(self):
		n = -3.14e-19
		self.assertEqual(basic.parseStrValue(str(n)), n)
示例#8
0
文件: basic.py 项目: Tythos/pyroclast
	def test_string(self):
		s = 'testing'
		self.assertEqual(basic.parseStrValue(s), s)