python - Best way to avoid copy-paste and hardcoding -
' hello, community.
problem: have many regex patterns, such as
r'to.*school', r'built.*in'
and etc. in every case, should execute code, varies situation situation.
example: if pattern 'to.*school', want find verb before 'to' , that's why write like:
for num, part in enumerate(sentence): if part == 'to': result = sentence[num-1]
if pattern 'built.*in', want find time , that's why write like:
for num, part in enumerate(sentence): if part == 'in': result = sentence[num+1]
so there's problem - how can avoid copy-pasting code, if there's on 500 patterns , each pattern has own way result?
my thoughts: understand should kind of database, stores patterns , solutions, how execute solution, if it's string? i'm totally lost.
if there sufficient regularity in code need write function accepts sentence , other things determine it. called parametrisation. example above, presumably simplified examples, you'd have
def process(sentence, parttest, offset): num, part in enumerate(sentence): if part == parttest: return sentence[num+offset]
and calls first , second examples respectively
result = process( sentence, 'to', -1) result2 = process( sentence, 'in', +1)
now can obtain parameters (parttest, offset) database. judging post there may regular expression in string form retrieved database well, , process
extended include regular expression string compiled on demand.
optimize later: keep local cache of compiled regular expressions in dict, or pickle them, because repeatedly compiling same 1 may nontrivial waste of cpu.
hope helps.
Comments
Post a Comment