-
Notifications
You must be signed in to change notification settings - Fork 0
Sourcery refactored master branch #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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}") | ||
|
Comment on lines
-3
to
+6
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| else: | ||
| my_dict[key] = values | ||
| return my_dict | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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.') | ||
|
Comment on lines
-31
to
+34
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Comment on lines
-37
to
-42
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,6 @@ | |
|
|
||
|
|
||
| # print the number of links in the list | ||
| print("\nFound {} links".format(len(links))) | ||
| print(f"\nFound {len(links)} links") | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| for email in emails: | ||
| print(email) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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") | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| # Search links for emails | ||
| for link in links: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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:") | ||
|
Comment on lines
-10
to
+19
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
| output_files(matches) | ||
| else: | ||
| print("No files found.") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)}." | ||
| ) | ||
|
|
||
|
Comment on lines
-16
to
+28
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
| 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 | ||
| ) | ||
|
|
||
|
Comment on lines
-32
to
+40
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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: | ||
|
Comment on lines
-40
to
+46
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| with open(image_name) as f: | ||
| image_binary = f.read() | ||
| with Image(blob=image_binary) as img: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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!") | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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}'!" | ||
| ) | ||
|
|
||
|
Comment on lines
-67
to
+75
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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" | ||
| ) | ||
|
|
||
|
Comment on lines
-94
to
102
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
|
|
||
| # 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__": | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return set(names) | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,17 +29,17 @@ 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", | ||
|
Comment on lines
-32
to
+34
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| }.get(ENVIRONMENT, None) | ||
|
|
||
| CONFIGFILE = 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}") | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| if not os.path.exists(CONFIGFILE): | ||
| sys.exit("Configuration error! Config file does not exist") | ||
| print("Config ok ....") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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]) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
|
|
||
|
|
||
| """ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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 | ||
|
Comment on lines
-26
to
-36
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| updated_addresses.insert(0, header_row) | ||
| return updated_addresses | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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() | ||
|
Comment on lines
-31
to
+32
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| 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." | ||
|
Comment on lines
-40
to
+39
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| # main | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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() | ||
|
Comment on lines
-15
to
+16
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| 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}' | ||
| ) | ||
|
|
||
|
Comment on lines
-21
to
+23
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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 | ||
|
Comment on lines
-40
to
+46
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return {'positive': positive, 'negative': negative} | ||
|
|
||
|
|
||
| def main(): | ||
| url = get_arguments() | ||
| if url: | ||
| if url := get_arguments(): | ||
|
Comment on lines
-50
to
+51
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| comments = get_comments(url) | ||
| if len(comments) <= 0: | ||
| print('This video has no comments.') | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Comment on lines
+9
to
+14
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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 | ||
|
Comment on lines
-28
to
-33
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| 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]): | ||
|
Comment on lines
-37
to
+34
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| clone_repos(total) | ||
|
|
||
| else: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines
14-14refactored with the following changes:use-fstring-for-formatting)