Skip to content
This repository has been archived by the owner on May 26, 2024. It is now read-only.

Commit

Permalink
add doc strings
Browse files Browse the repository at this point in the history
  • Loading branch information
hirusha-adi committed Jan 20, 2024
1 parent b1b09c5 commit b18f7e8
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions search.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime

# configure logger
# Configure logger
log_format = '(%(asctime)s) [%(levelname)s] %(message)s'
log_file_name = datetime.now().strftime('%Y-%m-%d_%H-%M-%S') + '.log'
log_file_path = os.path.join(os.getcwd(), log_file_name)
Expand All @@ -18,39 +18,63 @@
logger = logging.getLogger(__name__)

def check(fpath, target):
"""
Check if the target word is present in the paragraphs of a given Word document.
@param fpath: The file path of the Word document.
@type fpath: str
@param target: The word to search for in the document.
@type target: str
@return: True if the word is found, False otherwise.
@rtype: bool
"""
try:
doc = Document(fpath)
for paragraph in doc.paragraphs:
if target in paragraph.text:
return True
return False
except Exception as e:
logger.error(f"Error processing {fpath}: {e}")
logger.error("Error processing %s: %s" % (fpath, e))
return False

def process_file(file):
"""
Process a Word document file, checking if a target word is present.
@param file: A tuple containing the file name and the target word.
@type file: tuple
@return: None
@rtype: None
"""
fname, target = file
fpath = os.path.join(os.getcwd(), fname)
if check(fpath, target):
logger.info(f"'{target}' found in {fname}")
logger.info("'%s' found in %s" % (target, fname))
else:
logger.debug(f"'{target}' not found in {fname}")
logger.debug("'%s' not found in %s" % (target, fname))

def main():
"""
Main function to search for a word in .docx files in the current directory.
@return: None
@rtype: None
"""
parser = argparse.ArgumentParser(description='Search for a word in .docx files in the current directory.')
parser.add_argument('word', type=str, help='The word to search for')
args = parser.parse_args()

target = args.word
fall = [(fname, target) for fname in os.listdir() if fname.endswith(".docx")]
file_list = [(fname, target) for fname in os.listdir() if fname.endswith(".docx")]

with ThreadPoolExecutor() as executor:
executor.map(process_file, fall)
executor.map(process_file, file_list)

if __name__ == "__main__":
import time
start_time = time.time()
main()
end_time = time.time()
execution_time = end_time - start_time
logger.debug(f"Execution Time: {execution_time} seconds")
logger.debug("Execution Time: %s seconds" % execution_time)

0 comments on commit b18f7e8

Please sign in to comment.