-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransposer.py
60 lines (52 loc) · 2.21 KB
/
transposer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from logging import ERROR
import os
from lib.utils import read_file, get_longest_word, reverse_string, get_txt_files_in_directory
'''
encapsulates methods for scwx challenge to find largest word in file, transpose it, and display both
possible improvements:
configuration options for cleaning text (ex: remove end-of-line punctuation) and splitting text
ability to inject/change configuration
'''
class Transposer():
def __init__(self):
pass
# Read the contents of a file, find the longest word, reverse(transpose) the word
# return the longest and transposed word
def get_longest_and_transposed_word_from_file(self, filepath):
file_contents = read_file(filepath)
if len(file_contents) == 0:
raise Exception(f"{filepath} is empty\n")
longest = get_longest_word(file_contents)
reversed_word = reverse_string(longest)
return (longest, reversed_word)
# given a path to a file or directory, yield the longest word of each file
def transpose(self, path):
transposition = ""
filepaths = []
try:
if os.path.isdir(path):
files = get_txt_files_in_directory(path)
if len(files) == 0:
return "This directory contains no .txt files"
for file in files:
filepaths.append(os.path.join(path, file))
elif os.path.isfile(path):
if not path.endswith(".txt"):
return "The supplied file must be a .txt"
filepaths.append(path)
else:
return "Invalid path"
except OSError as oe:
transposition += str(oe)
except PermissionError as pe:
transposition += str(pe)
except Exception as e:
transposition += str(e)
for file in filepaths:
try:
transposition += f"Transposition for { os.path.basename(file) }:\n"
(longest, reversed_word) = Transposer().get_longest_and_transposed_word_from_file(file)
transposition += f"{longest}\n{reversed_word}\n\n"
except Exception as e:
transposition += f"{str(e)}\n"
return transposition