短いコードなんだけと、割と高度なことをやっている…
import os
import time
import glob
import typing as T
def search_file(file_location: str, search_string: str) -> bool:
with open(file_location, "r", encoding="utf8") as file:
return search_string in file.read()
def search_files_sequentially(file_locations: T.List[str], search_string: str) -> None:
for file_name in file_locations:
result = search_file(file_name, search_string)
if result:
print(f"Found word in file: `{file_name}`")
if __name__ == "__main__":
file_locations = list(
glob.glob(f"{os.path.abspath(os.getcwd())}/*.py"))
search_string = input("what word are you trying to find?: ")
start_time = time.perf_counter()
search_files_sequentially(file_locations, search_string)
process_time = time.perf_counter() - start_time
print(f"PROCESS TIME: {process_time}")
$ python3 find_files_sequential.py
what word are you trying to find?: queue
Found word in file: `/home/vagrant/dev/rust/parallel/python/thread_pool.py`
Found word in file: `/home/vagrant/dev/rust/parallel/python/pipeline.py`
Found word in file: `/home/vagrant/dev/rust/parallel/python/message_queue.py`
PROCESS TIME: 0.004676494048908353
OK, これをRustで書きたい…