1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| #!/usr/bin/python
# -*- coding: utf-8 -*-
import re
def estProche(patbase, pat):
return pat[0] in patbase[0] and ord(pat[1]) in patbase[1] and pat[2] in patbase[2]
def cherchePremier(liste, debut, patbase):
for i in range(debut + 1, len(liste)):
if estProche(patbase, liste[i]):
return i
return None
def normaliseTriplet(triplet):
if len(triplet) != 3:
return None
return [int(triplet[0]), triplet[1].strip(), int(triplet[2])]
def rangefy(triplet, delta):
bornes = ([ord('a'), ord('z') + 1], [ord('A'), ord('Z') + 1])[triplet[1].isupper()]
return [
range(max(triplet[0] - delta[0], 0), triplet[0] + delta[0] + 1)
, range(max(ord(triplet[1]) - delta[1], bornes[0]), min(ord(triplet[1]) + delta[1] + 1, bornes[1]))
, range(max(triplet[2] - delta[2], 0), triplet[2] + delta[2] + 1)
]
def normalisePatterns(patterns):
if len(patterns) != 2:
return None
pat = [normaliseTriplet(x.split(',')) for x in patterns]
return rangefy(pat[0], prox1), rangefy(pat[1], prox2)
description = "(15, S, 25), (7, c, 4), (15, S, 25), (9, d, 3), (8, b, 4) "
patterns = ["13, S, 22", "8, b, 4"]
prox1 = [2, 0, 3]
prox2 = [1, 1, 1]
tripletsStrings = re.findall("\([a-zA-Z0-9 ,]*\)", description)
desc = [normaliseTriplet(x[1:-1].split(',')) for x in tripletsStrings]
P1, P2 = normalisePatterns(patterns)
for i in [i for i in range(0, len(desc)) if estProche(P1, desc[i])]:
y = cherchePremier(desc, i, P2)
if y:
print tripletsStrings[i], ',', tripletsStrings[y], ' -- (', i, ',', y, ')' |
Partager