Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/04_rename_with_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 14-14 refactored with the following changes:

4 changes: 2 additions & 2 deletions scripts/05_load_json_without_dupes.py
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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function dict_raise_on_duplicates refactored with the following changes:

else:
my_dict[key] = values
return my_dict
4 changes: 2 additions & 2 deletions scripts/06_execution_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 31-34 refactored with the following changes:

6 changes: 2 additions & 4 deletions scripts/07_benchmark_permissions_loading_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 37-42 refactored with the following changes:

2 changes: 1 addition & 1 deletion scripts/08_basic_email_web_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@


# print the number of links in the list
print("\nFound {} links".format(len(links)))
print(f"\nFound {len(links)} links")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 19-19 refactored with the following changes:

for email in emails:
print(email)
2 changes: 1 addition & 1 deletion scripts/09_basic_link_web_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function crawl refactored with the following changes:


# Search links for emails
for link in links:
Expand Down
12 changes: 7 additions & 5 deletions scripts/10_find_files_recursively.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_file_names refactored with the following changes:

This removes the following comments ( why? ):

# just file name
# matches.append(os.path.join(root, filename))  # full path

output_files(matches)
else:
print("No files found.")
Expand Down
24 changes: 15 additions & 9 deletions scripts/11_optimize_images_with_wand.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_image_file_names refactored with the following changes:

This removes the following comments ( why? ):

# full path

return matches
else:
print("No files found.")
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_total_size refactored with the following changes:

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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function resize_images refactored with the following changes:

with open(image_name) as f:
image_binary = f.read()
with Image(blob=image_binary) as img:
Expand Down
34 changes: 12 additions & 22 deletions scripts/12_csv_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function is_valid_file refactored with the following changes:

sys.exit(1)


Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function is_valid_csv refactored with the following changes:

sys.exit(1)


Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_file refactored with the following changes:

This removes the following comments ( why? ):

# Split list of list into chunks
# Create new chunk
# Create new output file


# Add header
chunk.insert(0, header)

Expand All @@ -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__":
Expand Down
4 changes: 1 addition & 3 deletions scripts/13_random_name_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function random_name_generator refactored with the following changes:

return set(names)


Expand Down
8 changes: 4 additions & 4 deletions scripts/15_check_my_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_config_file refactored with the following changes:

}.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}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 42-42 refactored with the following changes:

if not os.path.exists(CONFIGFILE):
sys.exit("Configuration error! Config file does not exist")
print("Config ok ....")
2 changes: 1 addition & 1 deletion scripts/20_restore_file_from_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 7-7 refactored with the following changes:



"""
Expand Down
7 changes: 2 additions & 5 deletions scripts/25_ip2geolocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_addresses refactored with the following changes:

return all_addresses


Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_geolocation refactored with the following changes:

updated_addresses.insert(0, header_row)
return updated_addresses

Expand Down
10 changes: 3 additions & 7 deletions scripts/30_fullcontact.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_arguments refactored with the following changes:



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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function call_api refactored with the following changes:



# main
Expand Down
24 changes: 12 additions & 12 deletions scripts/31_youtube_sentiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_arguments refactored with the following changes:



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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_comments refactored with the following changes:

soup = bs4(html.text, 'html.parser')
return [comment.string for comment in soup.findAll('div', class_='Ct')]

Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function calculate_sentiment refactored with the following changes:

return {'positive': positive, 'negative': negative}


def main():
url = get_arguments()
if url:
if url := get_arguments():
Comment on lines -50 to +51
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

comments = get_comments(url)
if len(comments) <= 0:
print('This video has no comments.')
Expand Down
14 changes: 5 additions & 9 deletions scripts/34_git_all_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_total_repos refactored with the following changes:

if (len(rdata) >= 100):
page += 1
else:
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function clone_repos refactored with the following changes:


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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 37-38 refactored with the following changes:

clone_repos(total)

else:
Expand Down