This file contains hidden or 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 subprocess | |
import ast | |
def exec_string(input_string: str = "") -> str: | |
result = subprocess.getoutput(input_string) | |
return result | |
def eval_string(input_string: str = "") -> str: |
This file contains hidden or 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 string | |
def remove_punctuation(input_string: str = "") -> str: | |
return input_string.translate(str.maketrans("", "", string.punctuation)) | |
assert remove_punctuation("Hello!") == "Hello" | |
assert remove_punctuation("He. Saw! Me?") == "He Saw Me" |
This file contains hidden or 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 urllib.parse | |
def encode_url(url: str = "") -> str: | |
return urllib.parse.quote(url) | |
def decode_url(url: str = "") -> str: | |
return urllib.parse.unquote(url) |
This file contains hidden or 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 base64 | |
def encode_b64(input_string: str = "") -> object: | |
return base64.b64encode(input_string.encode("utf-8")) | |
def decode_b64(input_string: str = "") -> object: | |
return base64.b64decode(input_string).decode("utf-8") |
This file contains hidden or 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 nltk | |
def tokenize_text(input_str: str = "") -> list: | |
return nltk.wordpunct_tokenize(input_str) | |
assert tokenize_text("Good muffins cost $3.88\nin New York.") == [ | |
"Good", | |
"muffins", |
This file contains hidden or 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 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 |
This file contains hidden or 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 between(first: str = "", second: str = "", input_string="") -> str: | |
m = re.search(f"{first}(.+?){second}", input_string) | |
if m: | |
return m.group(1) | |
else: |
This file contains hidden or 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 string | |
def has_character_classes(input_string: str) -> tuple: | |
has_uppercase = False | |
has_lowercase = False | |
has_digits = False | |
has_whitespace = False |
This file contains hidden or 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 requests | |
import json | |
def recursive_key_values(dictionary): | |
"""recursive_key_values. | |
Print all keys and values anywhere in a dictionary | |
Args: | |
dictionary: any dictionary |
This file contains hidden or 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
""" | |
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, |