Python: re.sub replace function does not accept additional arguments - how to avoid a global variable?

I am trying to increase all the timestamps (forms "HH: MM: SS") in a text file by a few seconds specified by the command line parameter for my program.

Here is a simplified version of my efforts:

import re from datetime import datetime, timedelta time_diff = timedelta(seconds=10) def replace_time(matchobj): if matchobj.group(1) not in [None, '']: return (datetime.strptime(matchobj.group(1), "%H:%M:%S") + time_diff).strftime("%H:%M:%S") print re.sub('(\d\d:\d\d:\d\d)', replace_time, "01:27:55") 

This works great: the result of doing this 01:28:05 is what I want.

However, I heard that I should use global variables as little as possible. So I was wondering if there is an easy way to pass time_diff as a replace_time argument instead of using a global variable.

I tried the obvious, but this failed:

 def replace_time(matchobj, time_diff): if matchobj.group(1) not in [None, '']: return (datetime.strptime(matchobj.group(1), "%H:%M:%S") + time_diff).strftime("%H:%M:%S") time_diff = timedelta(seconds=10) print re.sub('(\d\d:\d\d:\d\d)', replace_time(matchobj, time_diff), "01:27:55") 

with this error: NameError: name 'matchobj' is not defined , so I cannot pass matchobj directly.

I reviewed the standard re page and the standard re howto , but cannot find the information I need. How can I avoid using a global variable here? Can I somehow pass an additional argument to the replace_time function? Thanks in advance.

+6
source share
2 answers

You can wrap the function in closure as follows:

 def increment_by(time_diff): def replace_time(matchobj): if matchobj.group(1) not in [None, '']: return (datetime.strptime(matchobj.group(1), "%H:%M:%S") + time_diff).strftime("%H:%M:%S") return replace_time time_diff = timedelta(seconds=10) print re.sub('(\d\d:\d\d:\d\d)', increment_by(time_diff), "01:27:55") 

Or you can use partial from stdlib as follows:

 from functools import partial def replace_time(time_diff, matchobj): if matchobj.group(1) not in [None, '']: return (datetime.strptime(matchobj.group(1), "%H:%M:%S") + time_diff).strftime("%H:%M:%S") time_diff = timedelta(seconds=10) print re.sub('(\d\d:\d\d:\d\d)', partial(replace_time, time_diff), "01:27:55") 
+11
source

There is nothing wrong with your current approach. time_diff written only once, and then all subsequent calls are read. This affects the constancy of the module.

You are having problems with the general global state when you have multiple threads accessing an object, and at least one of the threads is writing. This is not happening here, and you have nothing to worry about.

+1
source

Source: https://habr.com/ru/post/899920/


All Articles