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 / setup_networking.sh
Last active October 22, 2019 18:36
Setup networking for a CentOS VM (on Hyper-V)
# Start here: https://gist.github.com/wcarhart/48ce5d7e14a8172352db62af9705c183
# Edit the default network adapter settings
cd /etc/sysconfig/network-scripts/ifcfg-eth0
# IPADDR can be anything in our range
sed -i.bak -e 's/IPADDR = ".*"/IPADDR = "192.168.1.2"/'
# GATEWAY needs to match the gateway defined in Hyper-V
sed -i.bak -e 's/GATEWAY= ".*"/GATEWAY = "192.168.1.1"/'
# Configure DNS
@wcarhart
wcarhart / new-net-nat.ps1
Created October 22, 2019 18:13
Add Network NAT for Hyper-V VM
# list all VMSwitches
Get-VMSwitch
# for each VMSwitch, do the following:
Remove-VMSwitch <VM_SWITCH_NAME>
# list all NetNATs
Get-NetNAT
# for each NetNAT, do the following:
@wcarhart
wcarhart / deploy_django_to_heroku.sh
Last active August 6, 2019 10:25
How to deploy a Django application to Heroku
# How to deploy a Django application to Heroku with PostgreSQL
#========== Pre-requirements ==========#
### 1. make sure Heroku CLI is installed
### read more here: https://devcenter.heroku.com/articles/heroku-cli
### 2. use virtualenv to control python packages
### 3. replace 'projectname' with your project's name throughout this script
### 4. this script is for MacOS and Bash, but is very similar for Linux and other shells
#========== if you're lazy like me, make an alias ==========#
@wcarhart
wcarhart / reset_git.sh
Created August 5, 2019 16:44
Reset a git repository (before commit)
# Reset git repository (before commit)
git reset HEAD \*
git checkout -- .
@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:
@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 / 🐍 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 / 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 / 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
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]) + "}"