Skip to content

Instantly share code, notes, and snippets.

@codetestcode
codetestcode / gist:d4bacb06ce988321d366
Created July 30, 2014 14:28
log Search with color
import sys
import re
LOG_LOC = 'data/server.log'
ERROR_DEF = '((?:2|1)\\d{3}(?:-|\\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9])(,)(\\d\\d\\d)( )(\\[.*?\\])( )(ERROR))'
WARN_DEF = '((?:2|1)\\d{3}(?:-|\\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9])(,)(\\d\\d\\d)( )(\\[.*?\\])( )(WARN))'
INFO_DEF = '((?:2|1)\\d{3}(?:-|\\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9])(,)(\\d\\d\\d)( )(\\[.*?\\])( )(INFO))'
EIN_DEF = '(EIN)(\\d)(\\d)(\\d)(\\d)'
EXCEPTION_DEF=''
@codetestcode
codetestcode / gist:211b686347177006dfbe
Created July 10, 2014 01:33
Remove Duplicates from a sequence while maintaining order
#Remove Duplicates from a sequence while Maintaining order
#Problem
#You want to elimeinate the Duplicates values in a sequence , but preserver the order of the remaining items
#Solution
#If the values in the sequence are hashable, the problem can be easily solved using a set and a generator. For example:”
@codetestcode
codetestcode / gist:ff55945de9835a56b8fd
Created July 9, 2014 05:27
1.10 Remove Duplicates from a sequence while Maintaining order
#Remove Duplicates from a sequence while Maintaining order
#Problem
#You want to elimeinate the Duplicates values in a sequence , but preserver the order of the remaining items
#Solution
#If the values in the sequence are hashable, the problem can be easily solved using a set and a generator. For example:”
@codetestcode
codetestcode / gist:249e605d157408152ea8
Last active August 29, 2015 14:03
Finding Commonalities in Two Dictionaries
#1.9 Finding Commonalities in Two Dictionaries
#Problem
# You have two dictionaries and want to find out what they might in common( same keys, same values, etc).
# Solution
# Consider Two Dictionary:
a = { 'x': 1, 'y': 2,'z': 3}
b = { 'w': 10, 'x': 11, 'y': 2}
#find keys in common
@codetestcode
codetestcode / gist:17aa3a5c9c19fbb0d7a5
Created July 6, 2014 04:28
Keeping a dictionary in order
# Keeping a dictionary in order
# Problem
# You want to create a dictionary , and you also want control to orer of items when iterating or serializing.
# Solution
# To control the order if items in a dictionary, you can use a OrderedDict from the collections module.
@codetestcode
codetestcode / gist:de3667c79479437f3ce2
Created July 6, 2014 04:08
Mapping keys to multi values in a dictionary
from collections import defaultdict
d = defaultdict(list)
s = defaultdict(set)
e = {}
f = {}
d['a'].append(1)
@codetestcode
codetestcode / gist:c4bc3787bf02af7f8728
Created July 5, 2014 21:21
Implemented A Priority Queue
#Implemented A Priority Queue
#Problem
# Implement priority queue that sorts items by a given priority and always returns the highest priority on each pop operation
#Solution
#The following class uses the heapq module to implement a simple priority queue
@codetestcode
codetestcode / gist:10d09c5d2a287624665e
Last active August 29, 2015 14:03
Finding the Largest or Smallet N items
#Finding the Largest or Smallet N items
#Problem
# You want to make a list of the largest or smallest N items in a collection
#Solution
# the heapq module has two function - nlargest() and nsmallest() - that do exactly what you want
import heapq
import random
@codetestcode
codetestcode / gist:9334bd47ce8dd92bcbb2
Created July 5, 2014 17:23
Keeping the Last N Items
#Keeping the Last N Items
#Problem
# “You want to keep a limited history of the last few items seen during iteration or during some other kind of processing.”
#Solution
# “Keeping a limited history is a perfect use for a collections.deque.
# For example, the following code performs a simple text match on a
# sequence of lines and yields the matching line along with the
# previous N lines of context when found:”
@codetestcode
codetestcode / gist:a960326c73c66a8d54e4
Created July 4, 2014 22:11
Extended Unpacking Items 1_2a
records = [
('testa', 1, 2),
('testb','this is testb'),
('testa', 3, 4)
]
def do_testa(x,y):
print('testa', x, y)
def do_testb(s):