Skip to content

Instantly share code, notes, and snippets.

@bpeterso2000
bpeterso2000 / dir_walk_with_depth_control.py
Last active December 5, 2022 02:24
Recursively walk a directory to a specified depth
" 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
@bpeterso2000
bpeterso2000 / csv_unicode_reader_py2n3.py
Last active August 29, 2015 13:58
CSV Unicode reader with dual support for Python 2.6+ & 3.x
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):
"""
@bpeterso2000
bpeterso2000 / spreadsheet_column_ids.py
Last active August 29, 2015 13:58
Convert between column numbers & spreadsheet column names (alpha ID's)
"""
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,...)
@bpeterso2000
bpeterso2000 / fill_rows_to_square_up_list.py
Last active August 29, 2015 13:59
Fill missing columns to square up rows.
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
@bpeterso2000
bpeterso2000 / slice_columns_from_rows.py
Last active August 29, 2015 13:59
Efficiently extracts column indices from a list.
"""
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):
"""
@bpeterso2000
bpeterso2000 / unit_to_zero_base_slice.py
Last active August 29, 2015 13:59
Convert a string containing a column number range (unit-based slice) into a Python zero-based slice tuple (start, stop, step)
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):
@bpeterso2000
bpeterso2000 / sql_select_clause_tokenizer.py
Last active August 29, 2015 13:59
Uses the Python tokenizer as a helper for creating a dictionary of SQL SELECT clauses
"""
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
@bpeterso2000
bpeterso2000 / excel_row_generator.py
Created April 17, 2014 04:23
Generator of rows from the first worksheet in Excel file
import xlrd
sheet = xlrd.open_workbook(filename).sheet_by_index(0)
rows = (sheet.row_values(i) for i in range(sheet.nrows))
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)
@bpeterso2000
bpeterso2000 / parse_python_quoted_strings.py
Last active August 29, 2015 14:00
Single regular expression for parsing Python single or double-quoted strings while preserving any escaped quote 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