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
def between_markers(text: str, begin: str, end: str) -> str: | |
a = text.index(begin) + len(begin) if begin in text else 0 | |
b = text.index(end) if end in text else len(text) | |
return text[a:b] | |
if __name__ == '__main__': | |
print('Example:') | |
print(between_markers('What is >apple<', '>', '<')) |
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
def remove_all_before(items, border): | |
if not items: | |
return items | |
nl = [] | |
for i in range(len(items)): | |
if items[i] == border: | |
nl.append(items[i:]) | |
elif border not in items: |
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
class Pizza: | |
def __init__(self, sides, ingredients): | |
self.ingredients = ingredients | |
self.sides = sides | |
@classmethod | |
def hawaiian(cls): | |
return cls(['garlic bread', 'coke'], ['ham', 'cheese', 'tomato sauce']) |
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 time | |
def time_it(func): | |
def wrapper(*args, **kwargs): | |
start_time = time.time() | |
return_func = func(*args, **kwargs) | |
now = time.time() | |
print(f"Took {now - start_time:.2f} seconds to complete.") | |
return return_func |
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 random | |
def random_id(func): | |
def wrapper(*args, **kwargs): | |
rv = func(*args, **kwargs) | |
print(f"Function ID:{random.randint(0, 100)}") | |
return rv | |
return wrapper |
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 pygame | |
import random | |
# Constants | |
WIDTH = 800 | |
HEIGHT = 600 | |
FPS = 75 | |
# Pygame initializations | |
pygame.init() |
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 | |
from bs4 import BeautifulSoup | |
import csv | |
# Requests the website + telling BS4 how to parse the page. | |
source = requests.get('https://www.coreyms.com').text | |
soup = BeautifulSoup(source, 'lxml') | |
csv_file = open('scraped.csv', 'w') | |
csv.writer = csv.writer(csv_file) |
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 webbrowser | |
import pyperclip | |
import sys | |
# ['mapit.py', '870', 'Valencia', 'St.'] | |
if len(sys.argv) > 1: | |
# ['mapit.py', '870', 'Valencia', 'St.'] --> '870 Valencia St' | |
address = ' '.join(sys.argv[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 bs4 | |
import requests | |
def get_ebay_price(product_url): | |
res = requests.get(product_url) | |
res.raise_for_status() | |
soup = bs4.BeautifulSoup(res.text, 'html.parser') | |
page_elem = soup.select('#prcIsum') |
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
#! python3 | |
import re | |
import pyperclip | |
import pprint | |
# Create a regex for phone numbers | |
phone_regex = re.compile(r''' | |
# 415-555-0000, 555-0000, (415) 555-0000, 555-0000 ext 12345, ext. 12345, x12345 |