Created
December 30, 2013 01:27
-
-
Save Widdershin/8176774 to your computer and use it in GitHub Desktop.
Hack for unix/perl style find and replace in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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