Need help in refactoring my python script

N

n179911

I have a python script which process a file line by line, if the line
matches a regex, it calls a function to handle it.

My question is is there a better write to refactor my script. The
script works, but as it is, i need to keep indent to the right of the
editor as I add more and more regex for my file.

Thank you for any idea.
Now my code end up like this:

for line in fi.readlines():

result= reg1.match(line)

if result:
handleReg1(result)

else:
result = reg2.match(line)

if result:
handleReg2(result)
else:
result = reg3.match(line)

if result:
handleReg3(result)
else:
result = reg4.match(line)

if result:
handleReg4(result)
else:
result = reg5.match(line)

if result:
handleReg5(result)
 
P

Peter Otten

n179911 said:
I have a python script which process a file line by line, if the line
matches a regex, it calls a function to handle it.

My question is is there a better write to refactor my script. The
script works, but as it is, i need to keep indent to the right of the
editor as I add more and more regex for my file.

Thank you for any idea.
Now my code end up like this:

for line in fi.readlines():

result= reg1.match(line)

if result:
handleReg1(result)

else:
result = reg2.match(line)

if result:
handleReg2(result)
else:
result = reg3.match(line)

if result:
handleReg3(result)
else:
result = reg4.match(line)

if result:
handleReg4(result)
else:
result = reg5.match(line)

if result:
handleReg5(result)

If the handlers are indeed functions you can put them into a list:

match_handler_pairs = [
(reg1.match, handleReg1),
(reg2.match, handleReg2),
...
]

for line in fi: # look Ma, no readlines()
for match, handler in match_handler_pairs:
result = match(line)
if result:
handler(result)
break

Peter
 
M

MRAB

n179911 said:
I have a python script which process a file line by line, if the line
matches a regex, it calls a function to handle it.

My question is is there a better write to refactor my script. The
script works, but as it is, i need to keep indent to the right of the
editor as I add more and more regex for my file.

Thank you for any idea.
Now my code end up like this:

for line in fi.readlines():

This will read all the lines and then iterate through them. You can
iterate through the file line by line directly with:

for line in fi:
...
result= reg1.match(line)
if result:
handleReg1(result)
else:
result = reg2.match(line)
if result:
handleReg2(result)
else:
result = reg3.match(line)
if result:
handleReg3(result)
else:
result = reg4.match(line)
if result:
handleReg4(result)
else:
result = reg5.match(line)
if result:
handleReg5(result)

Build a list of the regexes and their functions:

actions = []
actions.append((reg1, handleReg1))
actions.append((reg2, handleReg2))
actions.append((reg3, handleReg3))
actions.append((reg4, handleReg4))
actions.append((reg5, handleReg5))

for line in fi:
for reg, func in actions:
result = reg.match(line)
if result:
func(result)
break
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top