From d5ab1ca6148e0ffc2d8b6bc37529e2f4ad86fa35 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Wed, 17 Aug 2022 15:40:06 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- scripts/04_rename_with_slice.py | 2 +- scripts/05_load_json_without_dupes.py | 4 +-- scripts/06_execution_time.py | 4 +-- ...07_benchmark_permissions_loading_django.py | 6 ++-- scripts/08_basic_email_web_crawler.py | 2 +- scripts/09_basic_link_web_crawler.py | 2 +- scripts/10_find_files_recursively.py | 12 ++++--- scripts/11_optimize_images_with_wand.py | 24 ++++++++----- scripts/12_csv_split.py | 34 +++++++------------ scripts/13_random_name_generator.py | 4 +-- scripts/15_check_my_environment.py | 8 ++--- scripts/20_restore_file_from_git.py | 2 +- scripts/25_ip2geolocation.py | 7 ++-- scripts/30_fullcontact.py | 10 ++---- scripts/31_youtube_sentiment.py | 24 ++++++------- scripts/34_git_all_repos.py | 14 +++----- 16 files changed, 71 insertions(+), 88 deletions(-) diff --git a/scripts/04_rename_with_slice.py b/scripts/04_rename_with_slice.py index dd849ef..aac6f8b 100755 --- a/scripts/04_rename_with_slice.py +++ b/scripts/04_rename_with_slice.py @@ -11,4 +11,4 @@ except OSError as e: print(e) else: - print("Renamed {} to {}".format(file, new_file_name)) + print(f"Renamed {file} to {new_file_name}") diff --git a/scripts/05_load_json_without_dupes.py b/scripts/05_load_json_without_dupes.py index 2cbe318..e5ed652 100755 --- a/scripts/05_load_json_without_dupes.py +++ b/scripts/05_load_json_without_dupes.py @@ -1,9 +1,9 @@ def dict_raise_on_duplicates(ordered_pairs): """reject duplicate keys""" - my_dict = dict() + my_dict = {} for key, values in ordered_pairs: if key in my_dict: - raise ValueError("Duplicate key: {}".format(key,)) + raise ValueError(f"Duplicate key: {key}") else: my_dict[key] = values return my_dict diff --git a/scripts/06_execution_time.py b/scripts/06_execution_time.py index 9614bbd..55ed420 100755 --- a/scripts/06_execution_time.py +++ b/scripts/06_execution_time.py @@ -28,7 +28,7 @@ def duration(self): timer = ExecutionTime() -sample_list = list() +sample_list = [] my_list = [random.randint(1, 888898) for num in range(1, 1000000) if num % 2 == 0] -print('Finished in {} seconds.'.format(timer.duration())) +print(f'Finished in {timer.duration()} seconds.') diff --git a/scripts/07_benchmark_permissions_loading_django.py b/scripts/07_benchmark_permissions_loading_django.py index e1e6900..5912a52 100755 --- a/scripts/07_benchmark_permissions_loading_django.py +++ b/scripts/07_benchmark_permissions_loading_django.py @@ -34,9 +34,7 @@ def load_new_perms(): if __name__ == "__main__": - n = 0 - all_times = list() - while n < 10: + all_times = [] + for _ in range(10): create_new_db() load_new_perms() - n += 1 diff --git a/scripts/08_basic_email_web_crawler.py b/scripts/08_basic_email_web_crawler.py index b56c747..9a512e0 100755 --- a/scripts/08_basic_email_web_crawler.py +++ b/scripts/08_basic_email_web_crawler.py @@ -16,6 +16,6 @@ # print the number of links in the list -print("\nFound {} links".format(len(links))) +print(f"\nFound {len(links)} links") for email in emails: print(email) diff --git a/scripts/09_basic_link_web_crawler.py b/scripts/09_basic_link_web_crawler.py index 87e2fab..cc83b08 100755 --- a/scripts/09_basic_link_web_crawler.py +++ b/scripts/09_basic_link_web_crawler.py @@ -20,7 +20,7 @@ def crawl(url): # Find links links = link_re.findall(req.text) - print("\nFound {} links".format(len(links))) + print(f"\nFound {len(links)} links") # Search links for emails for link in links: diff --git a/scripts/10_find_files_recursively.py b/scripts/10_find_files_recursively.py index 0c8e1eb..02f5e55 100755 --- a/scripts/10_find_files_recursively.py +++ b/scripts/10_find_files_recursively.py @@ -7,14 +7,16 @@ def get_file_names(filepath, pattern): - matches = [] if os.path.exists(filepath): + matches = [] for root, dirnames, filenames in os.walk(filepath): - for filename in fnmatch.filter(filenames, pattern): - # matches.append(os.path.join(root, filename)) # full path - matches.append(os.path.join(filename)) # just file name + matches.extend( + os.path.join(filename) + for filename in fnmatch.filter(filenames, pattern) + ) + if matches: - print("Found {} files:".format(len(matches))) + print(f"Found {len(matches)} files:") output_files(matches) else: print("No files found.") diff --git a/scripts/11_optimize_images_with_wand.py b/scripts/11_optimize_images_with_wand.py index a95b8b0..c4e5a74 100755 --- a/scripts/11_optimize_images_with_wand.py +++ b/scripts/11_optimize_images_with_wand.py @@ -13,14 +13,19 @@ def get_image_file_names(filepath, pattern): - matches = [] if os.path.exists(filepath): + matches = [] for root, dirnames, filenames in os.walk(filepath): - for filename in fnmatch.filter(filenames, pattern): - matches.append(os.path.join(root, filename)) # full path + matches.extend( + os.path.join(root, filename) + for filename in fnmatch.filter(filenames, pattern) + ) + if matches: - print("Found {} files, with a total file size of {}.".format( - len(matches), get_total_size(matches))) + print( + f"Found {len(matches)} files, with a total file size of {get_total_size(matches)}." + ) + return matches else: print("No files found.") @@ -29,15 +34,16 @@ def get_image_file_names(filepath, pattern): def get_total_size(list_of_image_names): - total_size = 0 - for image_name in list_of_image_names: - total_size += os.path.getsize(image_name) + total_size = sum( + os.path.getsize(image_name) for image_name in list_of_image_names + ) + return size(total_size) def resize_images(list_of_image_names): print("Optimizing ... ") - for index, image_name in enumerate(list_of_image_names): + for image_name in list_of_image_names: with open(image_name) as f: image_binary = f.read() with Image(blob=image_binary) as img: diff --git a/scripts/12_csv_split.py b/scripts/12_csv_split.py index 43ed1ee..852fd4e 100755 --- a/scripts/12_csv_split.py +++ b/scripts/12_csv_split.py @@ -55,7 +55,7 @@ def get_arguments(): def is_valid_file(parser, file_name): """Ensure that the input_file exists.""" if not os.path.exists(file_name): - parser.error("The file '{}' does not exist!".format(file_name)) + parser.error(f"The file '{file_name}' does not exist!") sys.exit(1) @@ -64,17 +64,15 @@ def is_valid_csv(parser, file_name, row_limit): Ensure that the # of rows in the input_file is greater than the row_limit. """ - row_count = 0 - for row in csv.reader(open(file_name)): - row_count += 1 + row_count = sum(1 for _ in csv.reader(open(file_name))) # Note: You could also use a generator expression # and the sum() function to count the rows: # row_count = sum(1 for row in csv.reader(open(file_name))) if row_limit > row_count: parser.error( - "The 'row_count' of '{}' is > the number of rows in '{}'!" - .format(row_limit, file_name) + f"The 'row_count' of '{row_limit}' is > the number of rows in '{file_name}'!" ) + sys.exit(1) @@ -91,23 +89,18 @@ def parse_file(arguments): # Read CSV, split into list of lists with open(input_file, 'r') as input_csv: datareader = csv.reader(input_csv) - all_rows = [] - for row in datareader: - all_rows.append(row) - + all_rows = list(datareader) # Remove header header = all_rows.pop(0) - # Split list of list into chunks - current_chunk = 1 - for i in range(0, len(all_rows), row_limit): # Loop through list + for current_chunk, i in enumerate(range(0, len(all_rows), row_limit), start=1): # Loop through list chunk = all_rows[i:i + row_limit] # Create single chunk - current_output = os.path.join( # Create new output file - output_path, - "{}-{}.csv".format(output_file, current_chunk) + current_output = os.path.join( + output_path, f"{output_file}-{current_chunk}.csv" ) + # Add header chunk.insert(0, header) @@ -118,12 +111,9 @@ def parse_file(arguments): # Output info print("") - print("Chunk # {}:".format(current_chunk)) - print("Filepath: {}".format(current_output)) - print("# of rows: {}".format(len(chunk))) - - # Create new chunk - current_chunk += 1 + print(f"Chunk # {current_chunk}:") + print(f"Filepath: {current_output}") + print(f"# of rows: {len(chunk)}") if __name__ == "__main__": diff --git a/scripts/13_random_name_generator.py b/scripts/13_random_name_generator.py index 0acb9bf..82ba6ce 100755 --- a/scripts/13_random_name_generator.py +++ b/scripts/13_random_name_generator.py @@ -9,9 +9,7 @@ def random_name_generator(first, second, x): - list of last names - number of random names """ - names = [] - for i in range(x): - names.append("{0} {1}".format(choice(first), choice(second))) + names = ["{0} {1}".format(choice(first), choice(second)) for _ in range(x)] return set(names) diff --git a/scripts/15_check_my_environment.py b/scripts/15_check_my_environment.py index 62e0b8d..de466b4 100755 --- a/scripts/15_check_my_environment.py +++ b/scripts/15_check_my_environment.py @@ -29,9 +29,9 @@ def process(self): def get_config_file(): directory = os.path.dirname(__file__) return { - "development": "{}/../config/development.cfg".format(directory), - "staging": "{}/../config/staging.cfg".format(directory), - "production": "{}/../config/production.cfg".format(directory) + "development": f"{directory}/../config/development.cfg", + "staging": f"{directory}/../config/staging.cfg", + "production": f"{directory}/../config/production.cfg", }.get(ENVIRONMENT, None) CONFIGFILE = get_config_file() @@ -39,7 +39,7 @@ def get_config_file(): if CONFIGFILE is None: sys.exit("Configuration error! Unknown environment set. \ Edit config.py and set appropriate environment") -print("Config file: {}".format(CONFIGFILE)) +print(f"Config file: {CONFIGFILE}") if not os.path.exists(CONFIGFILE): sys.exit("Configuration error! Config file does not exist") print("Config ok ....") diff --git a/scripts/20_restore_file_from_git.py b/scripts/20_restore_file_from_git.py index b1f581b..1f96a4d 100755 --- a/scripts/20_restore_file_from_git.py +++ b/scripts/20_restore_file_from_git.py @@ -4,7 +4,7 @@ file_name = str(input('Enter the file name: ')) commit = check_output(["git", "rev-list", "-n", "1", "HEAD", "--", file_name]) print(str(commit).rstrip()) -call(["git", "checkout", str(commit).rstrip()+"~1", file_name]) +call(["git", "checkout", f"{str(commit).rstrip()}~1", file_name]) """ diff --git a/scripts/25_ip2geolocation.py b/scripts/25_ip2geolocation.py index f593676..f20d6b7 100755 --- a/scripts/25_ip2geolocation.py +++ b/scripts/25_ip2geolocation.py @@ -11,8 +11,7 @@ def get_addresses(filename): all_addresses = [] with open(filename, 'rt') as f: reader = csv.reader(f) - for row in reader: - all_addresses.append(row) + all_addresses.extend(iter(reader)) return all_addresses @@ -23,17 +22,15 @@ def get_geolocation(all_the_ip_address): """ print("Getting geo information...") updated_addresses = [] - counter = 1 # update header header_row = all_the_ip_address.pop(0) header_row.extend(['Country', 'City']) # get geolocation - for line in all_the_ip_address: + for counter, line in enumerate(all_the_ip_address, start=1): print("Grabbing geo info for row # {0}".format(counter)) r = requests.get('https://freegeoip.net/json/{0}'.format(line[0])) line.extend([str(r.json()['country_name']), str(r.json()['city'])]) updated_addresses.append(line) - counter += 1 updated_addresses.insert(0, header_row) return updated_addresses diff --git a/scripts/30_fullcontact.py b/scripts/30_fullcontact.py index 3ee2822..9fd1f66 100644 --- a/scripts/30_fullcontact.py +++ b/scripts/30_fullcontact.py @@ -28,19 +28,15 @@ def get_arguments(): 'media': sys.argv[1], 'user_info': sys.argv[2] } - else: - print('Specify at least 1 argument') - sys.exit() + print('Specify at least 1 argument') + sys.exit() def call_api(contact): url = BASE_URL + '?{0}={1}&apiKey={2}'.format( contact['media'], contact['user_info'], API_KEY) r = requests.get(url) - if r.status_code == 200: - return r.text - else: - return "Sorry, no results found." + return r.text if r.status_code == 200 else "Sorry, no results found." # main diff --git a/scripts/31_youtube_sentiment.py b/scripts/31_youtube_sentiment.py index f0f2129..a3f4783 100644 --- a/scripts/31_youtube_sentiment.py +++ b/scripts/31_youtube_sentiment.py @@ -12,13 +12,15 @@ def get_arguments(): if len(sys.argv) is 2: return sys.argv[1] - else: - print('Specify at least 1 argument') - sys.exit() + print('Specify at least 1 argument') + sys.exit() def get_comments(url): - html = requests.get('https://plus.googleapis.com/u/0/_/widget/render/comments?first_party_property=YOUTUBE&href=' + url) + html = requests.get( + f'https://plus.googleapis.com/u/0/_/widget/render/comments?first_party_property=YOUTUBE&href={url}' + ) + soup = bs4(html.text, 'html.parser') return [comment.string for comment in soup.findAll('div', class_='Ct')] @@ -37,18 +39,16 @@ def calculate_sentiment(comments): for comment in comments: if comment is None: continue - else: - for word in comment.split(' '): - if word in negative_words: - negative += 1 - if word in positive_words: - positive += 1 + for word in comment.split(' '): + if word in negative_words: + negative += 1 + if word in positive_words: + positive += 1 return {'positive': positive, 'negative': negative} def main(): - url = get_arguments() - if url: + if url := get_arguments(): comments = get_comments(url) if len(comments) <= 0: print('This video has no comments.') diff --git a/scripts/34_git_all_repos.py b/scripts/34_git_all_repos.py index b3e2d5b..2f5021b 100644 --- a/scripts/34_git_all_repos.py +++ b/scripts/34_git_all_repos.py @@ -6,13 +6,12 @@ def get_total_repos(group, name): repo_urls = [] page = 1 + url = 'https://api.github.com/{0}/{1}/repos?per_page=100&page={2}' while True: - url = 'https://api.github.com/{0}/{1}/repos?per_page=100&page={2}' r = requests.get(url.format(group, name, page)) if r.status_code == 200: rdata = r.json() - for repo in rdata: - repo_urls.append(repo['clone_url']) + repo_urls.extend(repo['clone_url'] for repo in rdata) if (len(rdata) >= 100): page += 1 else: @@ -25,17 +24,14 @@ def get_total_repos(group, name): def clone_repos(all_repos): - count = 1 print('Cloning...') - for repo in all_repos: - os.system('Git clone ' + repo) + for count, repo in enumerate(all_repos, start=1): + os.system(f'Git clone {repo}') print('Completed repo #{0} of {1}'.format(count, len(all_repos))) - count += 1 if __name__ == '__main__': if len(sys.argv) > 2: - total = get_total_repos(sys.argv[1], sys.argv[2]) - if total: + if total := get_total_repos(sys.argv[1], sys.argv[2]): clone_repos(total) else: