This file contains hidden or 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 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='' |
This file contains hidden or 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
| #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:” |
This file contains hidden or 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
| #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:” |
This file contains hidden or 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
| #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 |
This file contains hidden or 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
| # 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. |
This file contains hidden or 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
| from collections import defaultdict | |
| d = defaultdict(list) | |
| s = defaultdict(set) | |
| e = {} | |
| f = {} | |
| d['a'].append(1) |
This file contains hidden or 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
| #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 |
This file contains hidden or 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
| #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 |
This file contains hidden or 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
| #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:” |
This file contains hidden or 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
| records = [ | |
| ('testa', 1, 2), | |
| ('testb','this is testb'), | |
| ('testa', 3, 4) | |
| ] | |
| def do_testa(x,y): | |
| print('testa', x, y) | |
| def do_testb(s): |
NewerOlder