Skip to content

Instantly share code, notes, and snippets.

@EndingCredits
Created December 8, 2019 19:55
Show Gist options
  • Save EndingCredits/396196f1011397dc3bf6cbb8a355c477 to your computer and use it in GitHub Desktop.
Save EndingCredits/396196f1011397dc3bf6cbb8a355c477 to your computer and use it in GitHub Desktop.
def match(path, expr):
names = expr.split('.')[1:]
curr_name = 0
curr_path = 0
wildpath = False
while curr_path < len(path):
el = path[curr_path]
if curr_name == len(names):
print("Unexpected end of expr, match failed!")
return False
name = names[curr_name]
if name == el:
print("Match: ", el, " to ", name)
curr_name += 1
wildpath = False
curr_path += 1
elif not name: #wildpath
print("Entering wildpath")
wildpath = True
curr_name += 1
elif wildpath:
print("No match ", el, " to ", name, " (wildpath)")
curr_path += 1
else:
print("No match ", el, " to ", name, ", match failed")
return False
if curr_name < len(names):
print("Elements of expression remaning: ", ".".join(names[curr_name:]), ", match failed!")
return False
return True
path = [ 'a', 'd', 'c', 'd', 'e' ]
expr = "..a..d.e"
print(match(path,expr))
#TODO: Split by '..' into exact matches and then match each eact match individually
def match2(path, expr):
exact_exprs = expr.split('..')
print("Found sub-expressions: ", ", ".join(exact_exprs))
offset = 0
for expr in exact_exprs:
print("Matching sub-expression ", expr, " to path ", path[offset:])
if not expr:
continue
names = [ name for name in expr.split(".") if name ]
print("Trying to match ", expr, " to ", path)
found_result = None
for i in range(offset, len(path)):
sub_path = path[i:i+len(names)]
if names == sub_path:
found_result = True
offset = i+len(names)
print("Found match at offset", i)
break
if not found_result:
print("Could not find a match!")
return False
return True
path = [ 'a', 'd', 'c', 'd', 'e' ]
expr = ".a..d...d.e"
print(match2(path,expr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment