Skip to content

Commit fb65b17

Browse files
Fixed some issues.
Made sure that the current functions work and tested them on the problemsets.
1 parent 9495062 commit fb65b17

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

main.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
# Rules for prefixes:
1111
# 1. File names are obligated to start with a prefix if there is one.
12-
# 2. Prefixes can't contain any underscores ('_') or digits.
12+
# 2. Prefixes can't contain any whitespaces, underscores ('_') or digits.
1313
# 3. Prefixes are obligated to be terminated with an underscore
1414

1515
def validate_prefix(prefix):
16-
return ''.join((char for char in prefix if char not in '_0123456789')) # Deleting unwanted characters
16+
return ''.join((char for char in prefix if char not in ' _0123456789')) # Deleting unwanted characters
1717

1818

1919
def find_end_of_prefix(file):
@@ -48,7 +48,8 @@ def main_file_name(directory, prefix=''):
4848
def renamer(prefix, base_dir):
4949
prefix = validate_prefix(prefix)
5050
for directory in os.listdir(base_dir):
51-
new_dir_name = base_dir + '/' + prefix + '_' + directory
51+
prefix_end = find_end_of_prefix(directory) + 1
52+
new_dir_name = base_dir + '/' + prefix + '_' + directory[prefix_end:]
5253
directory = base_dir + '/' + directory
5354
if os.path.isdir(directory):
5455
for file in os.listdir(directory):
@@ -59,7 +60,9 @@ def renamer(prefix, base_dir):
5960
# TODO: probably worth it to find a good way to get rid of this 'if' block
6061
if file[prefix_end].isalpha(): # Adding a corresponding number to the files with solutions
6162
tmp = ''
62-
for char in directory:
63+
for char in os.path.basename(directory):
64+
# TODO: introduce a flag to check if we have stumbled upon a number, and use it to stop the
65+
# loop when we stumble upon an underscore
6366
if char.isdigit():
6467
tmp += char
6568
if tmp != '':
@@ -86,7 +89,7 @@ def imp_adjustment(base_dir):
8689
file_data = f.read()
8790

8891
pos_1 = file_data.find('from') + 5
89-
pos_2 = file_data.find('import') - 1
92+
pos_2 = file_data.find('import', pos_1) - 1
9093
file_data = file_data[:pos_1] + main_file_name(directory) + file_data[pos_2:]
9194

9295
with open(directory + '/' + file, 'w') as f:

0 commit comments

Comments
 (0)