Skip to content

Instantly share code, notes, and snippets.

class UserNameValidator(object):
"""A simple user name validator to prohibit banned words"""
not_allowed = set(['parrot','dog','kitten','ibis',])
substitutions = {'1':'i','0':'o','7':'t'}
def is_valid(self, name):
name = name.lower()
#Substitute values as needed in name
for find,replace in UserNameValidator.substitutions.items():
@gregpinero
gregpinero / misc_image_manipulations_and_filters.py
Created September 14, 2015 12:35
Using numpy and scipy on images, applying filters, etc
import sys
from time import time
from PIL import Image
import numpy as np
from scipy.ndimage.filters import generic_filter, gaussian_filter
def trace(fn):
"""A decorator to time your functions"""
@gregpinero
gregpinero / crosstab.py
Created June 3, 2015 13:48
Create a cross tab / pivot table in Python
import itertools
def crosstab(rows, columns, col_idx_for_columns, lst_col_idx_for_rows, value_col_idx, fill_val, format_func=None):
"""Take col_idx_to_cross_tab and make its unique values be new columns at the end filled with
value_col_idx values. col idx arguments are 0 based.
This is basically a simplified pivot table creator with the limitations that you can only have one field in the columns
and there is no aggregation.
@gregpinero
gregpinero / utilities.py
Last active August 29, 2015 14:10
Python Helper Functions
import re
import datetime
from time import time
import sys
import math
import zipfile
import pickle
from cStringIO import StringIO
from dateutil.relativedelta import relativedelta
@gregpinero
gregpinero / matview.sql
Created November 21, 2014 17:50
MySQL - Creating Materialized View
/*
name:
mv:
app:
note:
*/
@gregpinero
gregpinero / format.py
Created November 21, 2014 17:50
Python Formatting Functions
import re
import datetime
import math
def format_timeinterval(start, end=None):
if not end:
end = datetime.now()
return format_timedelta(end - start)
def format_secondsdelta(seconds):
@gregpinero
gregpinero / soap_client.py
Last active August 29, 2015 14:10
Simple SOAP Client
""""""
Sample script using Lyons ABA web service methods
""""""
import httplib
import urllib
import xml.dom.minidom
#-------SETTINGS-------
@gregpinero
gregpinero / SQL Server Audit Logging.sql
Last active August 29, 2015 14:10
SQL Server Audit Log
--Log Insert
CREATE TRIGGER LogInsert ON [dbo].[CustomerProduct]
FOR INSERT
AS
INSERT changelog SELECT TOP 1 getdate(), 'Insert', 'CustomerProduct', CustomerID, ProductID, 'PaymenttypeID', '', Paymenttypeid FROM inserted
INSERT changelog SELECT TOP 1 getdate(), 'Insert', 'CustomerProduct', CustomerID, ProductID, 'ExpirationDate', '', convert(varchar(12), ExpirationDate, 110) FROM inserted
INSERT changelog SELECT TOP 1 getdate(), 'Insert', 'CustomerProduct', CustomerID, ProductID, 'Rate', '', Rate FROM inserted
@gregpinero
gregpinero / Perl Syntax
Created November 21, 2014 17:47
Perl Syntax Basics
Perl syntax basics:
Perl ===
Start interactive interpreter:
perl -de 1
==== Syntax Basics ====
<pre><nowiki>
data types: $scalars, @lists, and %hashes
""my"" makes a variable be in the scope of its block and lower. Use ""our"" for globals.
Put ""use strict"" at beginning of file to be required to declare variables.
@gregpinero
gregpinero / read_write_fastq
Created July 8, 2013 17:48
Quick functions for reading and writing fastq files.
"""Quick functions for reading and writing FASTQ"""
def read_fastq(filehandle):
''' Return dictionary with 'seq_id', 'seq', 'qual_id', and 'qual' '''
record_line = 0
read_number = 0
fastq_record = dict()
for line in filehandle:
record_line += 1