Skip to content

Instantly share code, notes, and snippets.

@Widdershin
Created December 30, 2013 01:27
Show Gist options
  • Save Widdershin/8176774 to your computer and use it in GitHub Desktop.
Save Widdershin/8176774 to your computer and use it in GitHub Desktop.
Hack for unix/perl style find and replace in Python
import inspect
class Find(object):
def __div__(self, find_term):
frame = inspect.stack()[0][0].f_back
line = inspect.getframeinfo(frame)[3][0]
equal_index = line.index("=")
variable_name = line[:equal_index - 1].strip()
return Replace(frame.f_locals[variable_name], find_term)
class Replace(object):
def __init__(self, string, find_term):
self.string = string
self.find_term = find_term
def __div__(self, replace):
return self.string.replace(self.find_term, replace)
s = Find()
test_string = "This is a test string"
test_string = s/"string"/"potato"
print(test_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment