Skip to content

Commit

Permalink
Name temp files using channel (for file creation)
Browse files Browse the repository at this point in the history
- this addresses the problem pointed out in Issue #7
  • Loading branch information
shailshouryya committed Nov 15, 2020
1 parent 989bb41 commit 5d7bfad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
32 changes: 14 additions & 18 deletions python/dev/file/create_file.py
Expand Up @@ -50,24 +50,20 @@ def scroll_to_bottom(url, driver, scroll_pause_time):
def time_writer_function(writer_function):
@functools.wraps(writer_function)
def wrapper_timer(*args, **kwargs):
start_time = time.perf_counter()
extension = writer_function.__name__.split('_')[-1]
temp_file = f'yt_videos_list_temp.{extension}'
print(f'Opened {temp_file}, writing video information to file....')

# check name of file and number of videos written
file_name, videos_written = writer_function(*args, **kwargs)
file_name = f'{file_name}.{extension}'
os.replace(temp_file, file_name)

end_time = time.perf_counter()
total_time = end_time - start_time

start_time = time.perf_counter()
extension = writer_function.__name__.split('_')[-1]
print(f'Opening a temp file and writing video information to the file....')
file_name, videos_written = writer_function(*args, **kwargs) # writer_function() writes to temp_{file_name}
end_time = time.perf_counter()
total_time = end_time - start_time
temp_file = f'temp_{file_name}.{extension}' # determine temp_{file_name} for wrapper_timer() scope
final_file = f'{file_name}.{extension}'
print(f'Finished writing to {temp_file}')
print(f'{videos_written} videos written to {temp_file}')
print(f'Closing {temp_file}')
print(f'Successfully completed write, renamed {temp_file} to {file_name}')
print(f'It took {total_time} seconds to write all {videos_written} videos to {file_name}{NEWLINE}')
os.replace(temp_file, final_file) # rename temp_{file_name} to {file_name}.{extension} here AFTER everything else finishes to ensure atomicity
print(f'Successfully completed write, renamed {temp_file} to {final_file}')
print(f'It took {total_time} seconds to write all {videos_written} videos to {final_file}{NEWLINE}')
return wrapper_timer


Expand All @@ -94,7 +90,7 @@ def write_to_txt(list_of_videos, file_name, reverse_chronological):
total_videos, total_writes, video_number, incrementer = prepare_output(list_of_videos, reverse_chronological)
markdown_formatting = False
spacing = f'{NEWLINE}' + ' '*4
with open('yt_videos_list_temp.txt', 'w') as txt_file:
with open(f'temp_{file_name}.txt', 'w') as txt_file:
txt_writer(txt_file, markdown_formatting, reverse_chronological, list_of_videos, spacing, video_number, incrementer, total_writes)
return file_name, total_videos

Expand All @@ -104,15 +100,15 @@ def write_to_md(list_of_videos, file_name, reverse_chronological):
total_videos, total_writes, video_number, incrementer = prepare_output(list_of_videos, reverse_chronological)
markdown_formatting = True
spacing = f'{NEWLINE}' + '- ' + f'{NEWLINE}'
with open('yt_videos_list_temp.md', 'w') as md_file:
with open(f'temp_{file_name}.md', 'w') as md_file:
txt_writer(md_file, markdown_formatting, reverse_chronological, list_of_videos, spacing, video_number, incrementer, total_writes)
return file_name, total_videos


@time_writer_function
def write_to_csv(list_of_videos, file_name, reverse_chronological):
total_videos, total_writes, video_number, incrementer = prepare_output(list_of_videos, reverse_chronological)
with open('yt_videos_list_temp.csv', 'w', newline='', encoding='utf-8') as csv_file:
with open(f'temp_{file_name}.csv', 'w', newline='', encoding='utf-8') as csv_file:
fieldnames = ['Video Number', 'Video Title', 'Video URL', 'Watched?', 'Watch again later?', 'Notes']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
Expand Down
26 changes: 13 additions & 13 deletions python/yt_videos_list/file/create_file.py
Expand Up @@ -37,20 +37,20 @@ def scroll_to_bottom(url, driver, scroll_pause_time):
def time_writer_function(writer_function):
@functools.wraps(writer_function)
def wrapper_timer(*args, **kwargs):
start_time = time.perf_counter()
extension = writer_function.__name__.split('_')[-1]
temp_file = f'yt_videos_list_temp.{extension}'
print(f'Opened {temp_file}, writing video information to file....')
start_time = time.perf_counter()
extension = writer_function.__name__.split('_')[-1]
print(f'Opening a temp file and writing video information to the file....')
file_name, videos_written = writer_function(*args, **kwargs)
file_name = f'{file_name}.{extension}'
os.replace(temp_file, file_name)
end_time = time.perf_counter()
total_time = end_time - start_time
end_time = time.perf_counter()
total_time = end_time - start_time
temp_file = f'temp_{file_name}.{extension}'
final_file = f'{file_name}.{extension}'
print(f'Finished writing to {temp_file}')
print(f'{videos_written} videos written to {temp_file}')
print(f'Closing {temp_file}')
print(f'Successfully completed write, renamed {temp_file} to {file_name}')
print(f'It took {total_time} seconds to write all {videos_written} videos to {file_name}{NEWLINE}')
os.replace(temp_file, final_file)
print(f'Successfully completed write, renamed {temp_file} to {final_file}')
print(f'It took {total_time} seconds to write all {videos_written} videos to {final_file}{NEWLINE}')
return wrapper_timer
def prepare_output(list_of_videos, reverse_chronological):
total_videos = len(list_of_videos)
Expand All @@ -72,21 +72,21 @@ def write_to_txt(list_of_videos, file_name, reverse_chronological):
total_videos, total_writes, video_number, incrementer = prepare_output(list_of_videos, reverse_chronological)
markdown_formatting = False
spacing = f'{NEWLINE}' + ' '*4
with open('yt_videos_list_temp.txt', 'w') as txt_file:
with open(f'temp_{file_name}.txt', 'w') as txt_file:
txt_writer(txt_file, markdown_formatting, reverse_chronological, list_of_videos, spacing, video_number, incrementer, total_writes)
return file_name, total_videos
@time_writer_function
def write_to_md(list_of_videos, file_name, reverse_chronological):
total_videos, total_writes, video_number, incrementer = prepare_output(list_of_videos, reverse_chronological)
markdown_formatting = True
spacing = f'{NEWLINE}' + '- ' + f'{NEWLINE}'
with open('yt_videos_list_temp.md', 'w') as md_file:
with open(f'temp_{file_name}.md', 'w') as md_file:
txt_writer(md_file, markdown_formatting, reverse_chronological, list_of_videos, spacing, video_number, incrementer, total_writes)
return file_name, total_videos
@time_writer_function
def write_to_csv(list_of_videos, file_name, reverse_chronological):
total_videos, total_writes, video_number, incrementer = prepare_output(list_of_videos, reverse_chronological)
with open('yt_videos_list_temp.csv', 'w', newline='', encoding='utf-8') as csv_file:
with open(f'temp_{file_name}.csv', 'w', newline='', encoding='utf-8') as csv_file:
fieldnames = ['Video Number', 'Video Title', 'Video URL', 'Watched?', 'Watch again later?', 'Notes']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
Expand Down

0 comments on commit 5d7bfad

Please sign in to comment.