def fix_stuff(m):...
# that what's available in m
replacement_text = m.group(1) + global_var1 + global_var2
return replacement_text
new_text = re.sub(im_pattern,fix_stuff,md_text)
I'm looking for some advice for how to write this in a clean way
I want to replace some text using a regex-pattern, but before creating replacement text I need to some file checking/copying etc. My code right now look something like this:
def fix_stuff(m):
# Do various things that involves for info
# that what's available in m
replacement_text = m.group(1) + global_var1 + global_var2
return replacement_text
and the call comes here
global_var1 = "bla bla"
global_var2 = "pff"
new_text = re.sub(im_pattern,fix_stuff,md_text)
The "problem" is that I've currently written some code that works but it uses global variables ... and I don't like global variables. I assume there is a better way to write this, but how?
You could use pass an anonymous function (a lambda) to re.sub:
I'm looking for some advice for how to write this in a clean way
I want to replace some text using a regex-pattern, but before creating replacement text I need to some file checking/copying etc. My code right now look something like this:
def fix_stuff(m):
# Do various things that involves for info
# that what's available in m
replacement_text = m.group(1) + global_var1 + global_var2
return replacement_text
and the call comes here
global_var1 = "bla bla"
global_var2 = "pff"
new_text = re.sub(im_pattern,fix_stuff,md_text)
The "problem" is that I've currently written some code that works but it uses global variables ... and I don't like global variables. I assume there is a better way to write this, but how?
= jem
I want to replace some text using a regex-pattern, but before creating replacement text I need to some file checking/copying etc. My code
right now look something like this:
def fix_stuff(m):
# Do various things that involves for info
# that what's available in m
replacement_text = m.group(1) + global_var1 + global_var2
return replacement_text
and the call comes here
global_var1 = "bla bla"
global_var2 = "pff"
new_text = re.sub(im_pattern,fix_stuff,md_text)
The "problem" is that I've currently written some code that works but
it uses global variables ... and I don't like global variables. I
assume there is a better way to write this, but how?
I'm looking for some advice for how to write this in a clean way
...
The "problem" is that I've currently written some code that works but it uses global variables ... and I don't like global variables. I assume there is a better way to write this, but how?
I'm looking for some advice for how to write this in a clean way
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 02:08:07 |
Calls: | 10,385 |
Calls today: | 2 |
Files: | 14,057 |
Messages: | 6,416,581 |