Skip to content

Instantly share code, notes, and snippets.

@raeq
raeq / dict_07_comp.py
Created August 2, 2020 20:16
Dictionary Comprehension
asci_uppercase: dict = {i: chr(+i) for i in range(65, 91, 1)}
digits: dict = {i: chr(+i) for i in range(48, 58, 1)}
asci_lowercase: dict = {i: chr(+i) for i in range(97, 123, 1)}
asci_punctuation: dict = {i: chr(+i) for i in range(32, 48, 1)}
asci_punctuation.update({i: chr(+i) for i in range(123, 127, 1)})
asci_punctuation.update({i: chr(+i) for i in range(91, 97, 1)})
asci_punctuation.update({i: chr(+i) for i in range(58, 65, 1)})
asci_extended: dict = {i: chr(+i) for i in range(128, 255, 1)}
asci_system: dict = {i: hex(i) for i in range(0, 32, 1)}
@raeq
raeq / dict_06_byvalue.py
Last active August 2, 2020 13:54
Get index pertaining to a value in nested dictionary
import json
import requests
def get_key_by_value(dictionary: dict, val: str) -> object:
"""get_values_by_key.
Args:
dictionary (dict): dictionary
val (str): value
@raeq
raeq / dict_05_bykey.py
Created August 2, 2020 12:13
Find all nested keys in a dict
import json
import requests
def get_values_by_key(dictionary: dict, key: str) -> dict:
"""get_values_by_key.
Args:
dictionary (dict): dictionary
key (str): key
@raeq
raeq / dict_04_twolists.py
Created August 1, 2020 18:25
Return a dict from two lists
def dict_from_two_lists(keys: list, values: list) -> dict:
"""
Args:
keys: The list of keys for the dictionary
values: The list of values for the dictionary
Returns: A dictionary of key:value pairs
"""
@raeq
raeq / dict_03_merge.py
Created July 30, 2020 16:20
Merge two dictionaries.
def merge_dictionaries(left: dict, right: dict) -> dict:
"""Merge two dictionaries using a shallow copy."""
temp: dict = left.copy()
temp.update(right)
return temp
countries: dict = {
"Taiwan": 36193,
"Canada": 9984670,
@raeq
raeq / dict_02_sort.py
Created July 30, 2020 15:56
Sorting a dictionary
"""
Sorting a dictionary to an Iterable of tuples.
https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_area
"""
countries: dict = {
"Taiwan": 36193,
"Canada": 9984670,
"United States": 9525067,
"Russia": 17098246,
import requests
import json
def recursive_key_values(dictionary):
"""recursive_key_values.
Print all keys and values anywhere in a dictionary
Args:
dictionary: any dictionary
@raeq
raeq / strings_22_char_class.py
Last active July 21, 2020 08:25
Check to See if a String Contains a Character Class
import string
def has_character_classes(input_string: str) -> tuple:
has_uppercase = False
has_lowercase = False
has_digits = False
has_whitespace = False
@raeq
raeq / strings_11_markers.py
Created July 19, 2020 13:53
Use REGEX to find text between two markers.
import re
def between(first: str = "", second: str = "", input_string="") -> str:
m = re.search(f"{first}(.+?){second}", input_string)
if m:
return m.group(1)
else:
@raeq
raeq / strings_7_random.py
Created July 19, 2020 13:37
Generate random strings
import string
import secrets
def generate_random_string(length: int = 0) -> str:
result = "".join(
secrets.choice(string.ascii_letters + string.digits)
for _ in range(length))
return result