Created
February 21, 2020 00:04
-
-
Save jvrmaia/d65df031afb17dc067400c87154e1d41 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 re | |
import random | |
import time | |
WORD_LIST = [ | |
'glibc-x86_64.tar.gz', | |
'rock_pesado.mp3', | |
'spotify_64bits.tar.gz', | |
'firefox-x86-64.tar.gz', | |
] | |
class Timer(): | |
def __init__(self): | |
self.start = time.time() | |
def __enter__(self): | |
return self | |
def __exit__(self, exc_type, exc_val, exc_tb): | |
end = time.time() | |
runtime = end - self.start | |
msg = 'The function took {time} seconds to complete' | |
print(msg.format(time=runtime)) | |
def far_without_lib(word_list): | |
return [it.replace('x86_64', '64bits') for it in word_list] | |
def far_with_regex(word_list): | |
pattern = r'x86_64' | |
replace = r'64bits' | |
return [re.sub(pattern, replace, it) for it in word_list] | |
if __name__ == "__main__": | |
with Timer(): | |
print(far_without_lib(WORD_LIST)) | |
with Timer(): | |
print(far_with_regex(WORD_LIST)) |
This file contains hidden or 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
$ python find-and-replace.py | |
['glibc-64bits.tar.gz', 'rock_pesado.mp3', 'spotify_64bits.tar.gz', 'firefox-x86-64.tar.gz'] | |
The function took 2.7894973754882812e-05 seconds to complete | |
['glibc-64bits.tar.gz', 'rock_pesado.mp3', 'spotify_64bits.tar.gz', 'firefox-x86-64.tar.gz'] | |
The function took 0.0001327991485595703 seconds to complete |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment