Skip to content

Instantly share code, notes, and snippets.

View wcarhart's full-sized avatar
🦉
Hoot hoot

Will Carhart wcarhart

🦉
Hoot hoot
View GitHub Profile
@wcarhart
wcarhart / ansi_cheats.py
Last active June 21, 2021 14:49
Python ANSI codes cheat sheet
# CURSOR
"\033[<N>A" # Move cursor up N lines (defaults to 1)
"\033[<N>B" # Move cursor down N lines (defaults to 1)
"\033[<N>C" # Move cursor right N lines (defaults to 1)
"\033[<N>D" # Move cursor left N lines (defaults to 1)
"\033[K" # Clear contents of line
# COLORS
"\033[90m" # light grey
"\033[91m" # red
@wcarhart
wcarhart / restart.sh
Created July 19, 2018 19:04
Restart Touch Bar on Macbook Pro
pkill "Touch Bar agent"
killall "ControlStrip"
@wcarhart
wcarhart / instructions.md
Last active August 31, 2018 05:51
Instructions for installing Sublime packages (ST3)

Installing Package Manager:

  1. Press Ctrl+` to open Sublime's command prompt.
  2. Paste in:
import urllib.request,os,hashlib; h = '6f4c264a24d933ce70df5dedcf1dcaee' + 'ebe013ee18cced0ef93d5f746d80ef60'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)

If the above command fails, it's likely because it needs to be updated. Go here for the source of the above command.

  1. Restart Sublime.
@wcarhart
wcarhart / pypi_upload.sh
Last active February 6, 2020 23:50
Upload Python package to PyPI
python3 setup.py bdist_wheel
python3 -m twine upload dist/*
def jsonize(self):
variables = [var for var in dir(self) if not var.startswith(('_', '__')) and not callable(getattr(self, var))]
return "{" + ",".join([f"\"{var}\": \"{getattr(self, var)}\"" for var in variables]) + "}"
@wcarhart
wcarhart / undo_git.sh
Last active August 1, 2019 21:54
Completely remove a file from git history
# Remove a commit from git history
## Detach from the remote
git remote rm origin
## Make necessary changes. If you want to keep a file locally but not remotely:
git rm --cached $FILE
echo $FILE >> .gitignore
git add .gitignore
@wcarhart
wcarhart / nyt.py
Created March 29, 2019 16:44
Get the NYT headlines for the day!
import requests, re
from bs4 import BeautifulSoup
def get_nyt_headlines():
r = requests.get('https://www.nytimes.com/')
soup = BeautifulSoup(r.text, 'html.parser')
headers = soup.find_all('h2')
spans = [tag.find_all('span') for tag in headers]
headlines = [re.findall("<span>(.*?)</span>", str(span[0]))[0] for span in spans if not len(span) == 0]
for index, headline in enumerate(headlines):
@wcarhart
wcarhart / 🐍 PyTricks.py
Last active February 26, 2023 13:46
List of Python tricks from Dan Bader's (realpython.com) newsletter
# List of Python tricks from Dan Bader's (realpython.com) newsletter
# ===================================== #
# How to merge two dictionaries (3.5+)
x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
z = {**x, **y}
print(z)
# {'c': 4, 'a': 1, 'b': 3}
@wcarhart
wcarhart / send_gmail.py
Last active September 23, 2020 19:09
Send an email via the Gmail API (you'll need to supply your token.pickle with your credentials). Code sample from blog post: https://www.willcarh.art/blog/automating-emails-in-python/
import os
import sys
import pickle
import base64
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from email.mime.text import MIMEText
def get_gmail_api_instance():
@wcarhart
wcarhart / initiate_git_lfs.sh
Last active February 6, 2020 23:52
Initialize Git LFS and start tracking files with it
# Initialize Git LFS and start tracking files with it
## Install Git LFS (MacOS)
brew install git-lfs
## Change to repository
cd $PROJECT_REPO
## If you've already added or committed the file(s) you'd like to track with LFS,
## you'll need to edit them out of the Git history. See: