Last active
August 3, 2022 16:00
-
-
Save AMMullan/713801c69a15f7ce0d48767ea9537f1d to your computer and use it in GitHub Desktop.
Useful Python Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def equal_dicts(d1, d2, ignore_keys=None): | |
''' Dictionary Comparison | |
Takes 2 lists and does a comparison, excluding the keys from ignore_keys | |
Arguments: | |
d1 {dict} -- First Dict | |
d2 {dict} -- Second Dict | |
Keyword Arguments: | |
ignore_keys {list} -- List of keys to ignore (default: {None}) | |
Returns: | |
bool -- Result of comparison | |
''' | |
if not ignore_keys: | |
return d1 == d2 | |
ignored = set(ignore_keys) | |
for k1, v1 in d1.items(): | |
if k1 not in ignored and (k1 not in d2 or d2[k1] != v1): | |
return False | |
for k2, v2 in d2.items(): | |
if k2 not in ignored and k2 not in d1: | |
return False | |
return True | |
def remove_keys(d, remove_keys): | |
''' Dictionary Cleaner | |
Takes a dictionary and returns it | |
minus the keys from remove_keys | |
Arguments: | |
d {[type]} -- [description] | |
remove_keys {[type]} -- [description] | |
Returns: | |
list -- dict w/o remove_keys | |
''' | |
return {x: d[x] for x in d if x not in remove_keys} | |
def natural_sort(lst): | |
''' Natural Sorting | |
Will take the given list and sort it naturally | |
Arguments: | |
lst {list} -- List of items to sort | |
Returns: | |
list -- Sorted list | |
''' | |
def convert(text): | |
if text.isdigit(): | |
return int(text) | |
else: | |
return text.lower() | |
def alphanum_key(key): | |
return [ | |
convert(c) | |
for c in re.split('([0-9]+)', key) | |
] | |
return sorted(lst, key=alphanum_key) | |
def supernet(cidrs): | |
''' Subnets to Supernet Calculator | |
Will take a list of subnets can calculate the supernet | |
i.e. [10.10.0.0/26, 10.10.64.0/26, 10.10.128.0/26] = 10.10.0.24 | |
Arguments: | |
cidrs {list} -- Input CIDR's | |
Returns: | |
string -- The Supernet from the Input CIDR's | |
''' | |
subnets = natural_sort(cidrs) | |
mask, network = subnets[0].split('/') | |
new_net = int(network) - round(len(subnets) / 2) | |
return f'{mask}/{new_net}' | |
def boto3_tag_list_to_dict(tags_list): | |
''' Convert boto3 tags to Python dictionary | |
Takes a boto3 tag list of dicts and converts to a Py dict | |
Arguments: | |
tags_list {list} -- List of dicts | |
Returns: | |
dict -- Dictionary of Tags | |
''' | |
tags_dict = {} | |
if not tags_list: | |
return {} | |
for tag in tags_list: | |
if 'key' in tag and not tag['key'].startswith('aws:'): | |
tags_dict[tag['key']] = tag['value'] | |
elif 'Key' in tag and not tag['Key'].startswith('aws:'): | |
tags_dict[tag['Key']] = tag['Value'] | |
return tags_dict | |
def dict_to_boto3_tag_list(tags_dict): | |
''' Convert dictionary to boto3 tag set | |
Arguments: | |
tags_dict {dict} -- Dictionary of tags | |
Returns: | |
list(dict) -- Boto3 Tag Set | |
''' | |
tags_list = [] | |
for k, v in tags_dict.items(): | |
tags_list.append({'Key': k, 'Value': v}) | |
return tags_list | |
def which(program): | |
''' Python equivalent of Linux "which" command | |
Searches PATH for the binary name and returns the path or None | |
Arguments: | |
program {str} -- Binary Name | |
Returns: | |
bool|None -- Path of program or None | |
''' | |
import os | |
def is_exe(fpath): | |
return os.path.isfile(fpath) and os.access(fpath, os.X_OK) | |
fpath, fname = os.path.split(program) | |
if fpath: | |
if is_exe(program): | |
return program | |
else: | |
for path in os.environ.get("PATH", os.pathsep).split(os.pathsep): | |
exe_file = os.path.join(path, program) | |
if is_exe(exe_file): | |
return exe_file | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment