This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Reference: http://stackoverflow.com/questions/7159607 " | |
import os | |
import sys | |
def listdir(path): | |
""" | |
recursively walk directory to specified depth | |
:param path: (str) path to list files from |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import sys | |
from codecs import iterencode, iterdecode | |
from io import open | |
PY2 = sys.version_info.major < 3 | |
def csv_file(filename, dialect=csv.excel, encoding=None, **kwds): | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
SPREADSHEET-STYLE COLUMN HEADERS | |
""" | |
from collections import OrderedDict | |
def id2num(s): | |
""" spreadsheet column name to number | |
ref: http://stackoverflow.com/questions/7261936 | |
:param s: str -- spreadsheet column alpha ID (i.e. A, B, ... AA, AB,...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fill_rows(rows, fill_value=None): | |
maxcols = max(map(len, rows)) | |
if min(map(len, rows)) != maxcols: | |
rows = (row + [None] * (maxcols - len(i)) for i in rows) | |
for row in rows: | |
yield row |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Extracts specified columns from a list by taking a list of column indices and converting them in to mininum number of Python | |
slice objects ahead of time before iterating through the list. Then uses slices to extract ranges of columns. | |
""" | |
from itertools import chain | |
def prepare_slices(indices): | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ZeroBasedIndexWarning(Exception): | |
def __init__(self, value): | |
self.value = value | |
def __str__(self): | |
mesg = 'unit-based slice indices start at 1, not 0' | |
return "'{}', {}.".format(self.value, mesg) | |
def getint(s): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
SQL SELECT clause parser | |
~~~~~~~~~~~~~~~~~~~~~~~ | |
Creates a dictionary of keyword: SQL-text for each SELECT statement clause. | |
Code is mindful of quoting & respects proper order of keywords. | |
Current version does not support subqueries. | |
""" | |
import re |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xlrd | |
sheet = xlrd.open_workbook(filename).sheet_by_index(0) | |
rows = (sheet.row_values(i) for i in range(sheet.nrows)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def indices2slices(indices): | |
""" | |
convert a list of discrete indices into a minimal list of slices | |
:returns: [(start1:stop1:step1), (start2:stop2:step2) ...)] | |
""" | |
slices = [] | |
if len(indices) == 1: | |
slices = [indices[0]] | |
elif len(indices) > 1: | |
start, stop, step = (indices[0], None, None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Parses single or double-quoted strings while preserving escaped quote chars | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
1. captures the first quote character (either a single-quote or double-quote) | |
and stores it in a group named "quote" | |
2. takes zero or more characters up until it matches the character that is | |
stored in the group named "quote" (i.e. named referenced group) providing | |
that the matching "quote" character is not escaped (preceeded by a backslash | |
character, i.e. negative look behind assertion). All matching characters |
OlderNewer