Skip to content

Instantly share code, notes, and snippets.

@NMastracchio
Created August 8, 2015 23:51
Show Gist options
  • Save NMastracchio/a395de6881d850135e56 to your computer and use it in GitHub Desktop.
Save NMastracchio/a395de6881d850135e56 to your computer and use it in GitHub Desktop.
import pprint
#
# Gets the patterns that will be searched for. Returns a list of pattern lists
#
def getPatterns():
patterns = []
try:
numPatterns = int(raw_input("How many patterns? "))
except ValueError:
print "Please enter a valid number."
getPatterns()
print """
Enter the patterns that you would like to search for. For a blank,
enter an underscore.
(If more than 5 symbols are entered, only the first 5 will be used.)
Example:
+ - _ + -
"""
for n in range(numPatterns):
pattern = raw_input("Pattern " + str(n + 1) + ": ")
mappedPattern = map(str, pattern.split())
mappedPattern = [char.replace('_', ' ') for char in mappedPattern]
patterns.append(mappedPattern)
print "Searching for the following patterns:"
for n in range(numPatterns):
if len(patterns[n]) > 5:
patterns[n] = patterns[n][:5]
print patterns[n]
return patterns
#
# Check for return character, put each character in the appropriate place
#
def charAppender(store, n, filename, character):
if character == "\r":
character = " "
patternsArr = store[filename]["patterns"] = store[filename].get("patterns",[])
try:
patternsArr[n].append(character)
except:
patternsArr.append([])
patternsArr[n].append(character)
#
# Searches for the patterns in each of the files in the filenames list.
# As per the specs, MD3.1, W3.1, & W3.7 are not included.
#
def extractPatterns(patterns):
# Rows 14-18
# Columns 18, 25, 30 35, 40, 47, 53, 58, 64, 69, 75
MDfilenames = [
#"MD3.1",
"MD3.2",
"MD3.3",
"MD3.4",
"MD3.5",
"MD3.6"
]
MDcolumns = [17, 24, 29, 34, 39, 46, 52, 57, 63, 68, 74]
# Rows 14-18
# Colums 18, 25, 29, 33, 38, 43, 48, 54, 58, 64, 69, 75
Wfilenames = [
#"W3.1",
"W3.2",
"W3.3",
"W3.4",
"W3.5",
"W3.6",
#"W3.7"
]
Wcolumns = [17, 24, 28, 32, 37, 42, 47, 53, 57, 63, 68, 74]
store = {}
for filename in MDfilenames:
store[filename] = {}
fp = open("./ANALYSIS/" + filename)
for i, line in enumerate(fp):
if i == 13:
for n in range(len(MDcolumns)):
charAppender(store, n, filename, line[MDcolumns[n]])
elif i == 14:
for n in range(len(MDcolumns)):
charAppender(store, n, filename, line[MDcolumns[n]])
elif i == 15:
for n in range(len(MDcolumns)):
charAppender(store, n, filename, line[MDcolumns[n]])
elif i == 16:
for n in range(len(MDcolumns)):
charAppender(store, n, filename, line[MDcolumns[n]])
elif i == 17:
for n in range(len(MDcolumns)):
charAppender(store, n, filename, line[MDcolumns[n]])
elif i > 17:
break
fp.close()
for filename in Wfilenames:
store[filename] = {}
fp = open("./ANALYSIS/" + filename)
for i, line in enumerate(fp):
if i == 13:
for n in range(len(Wcolumns)):
charAppender(store, n, filename, line[Wcolumns[n]])
elif i == 14:
for n in range(len(Wcolumns)):
charAppender(store, n, filename, line[Wcolumns[n]])
elif i == 15:
for n in range(len(Wcolumns)):
charAppender(store, n, filename, line[Wcolumns[n]])
elif i == 16:
for n in range(len(Wcolumns)):
charAppender(store, n, filename, line[Wcolumns[n]])
elif i == 17:
for n in range(len(Wcolumns)):
charAppender(store, n, filename, line[Wcolumns[n]])
elif i > 17:
break
fp.close()
return store
#
# Takes the entered patterns and compares them to the extracted patterns
#
def findMatches(patterns, data):
pp = pprint.PrettyPrinter(indent=2)
for pattern in range(len(patterns)): #for each pattern...
for key, value in data.iteritems(): # for each key in data dict
for n in range(len(data[key]["patterns"])):
if patterns[pattern] == data[key]["patterns"][n]:
print "Match found in " + key
raw_input("Press Enter to continue...")
patterns = getPatterns()
data = extractPatterns(patterns)
findMatches(patterns, data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment