Skip to content

Instantly share code, notes, and snippets.

@Julian-Nash
Julian-Nash / http_status_code_enum.py
Last active November 20, 2019 20:34
HttpStatusCode - A handy little enum
import enum
@enum.unique
class HTTPStatusCode(enum.IntEnum):
""" Enumeration for HTTP status codes.
Example usage:
HttpStatusCode.OK.value # 100
HttpStatusCode.INTERNAL_SERVER_ERROR.value # 500
@Julian-Nash
Julian-Nash / .bashrc
Last active January 3, 2019 23:30
WSL quick & nice terminal setup w/Nord color scheme
# Add the following to the end of your .bashrc
# Basic PS1
export PS1="\u@wsl: \w\n$ "
# Quick access to wsl directory
alias wsl="cd /mnt/c/wsl"
alias ..="cd .."
# cd to wsl folder on launch (All working files shared between the linux subsystem and windows)
@Julian-Nash
Julian-Nash / clean_urls.py
Created October 12, 2018 00:51
clean & assemble urls. Quick and dirty solution - need to refactor
import re
from urllib.parse import urlsplit
links = [
# eligible links
"google.com/home",
"http://google.com/home",
"https://google.com/home",
"www.google.com/home",
@Julian-Nash
Julian-Nash / flask_mongodb_bcrypt_user_login.py
Last active August 30, 2018 00:13
Simple user login route & password hashing/checking functions for flask using mongoengine and bcrypt
# mongoengine class
class User(DynamicDocument):
date_created = DateTimeField(default=datetime.datetime.utcnow)
username = StringField(unique=True)
password = BinaryField(required=True)
email = EmailField(unique=True)
admin = BooleanField(default=False)
data_sets = DictField()
# Hashing & password checking functions
@Julian-Nash
Julian-Nash / flask_session_wrapper.py
Last active August 30, 2018 00:06
Flask session wrapper - Control view access with this decorator
def in_session(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("LOGGED_IN") == False:
return redirect(url_for('login'))
return f(*args, **kwargs)
return decorated_function
# Usage --------------------------------------------------------
@Julian-Nash
Julian-Nash / graph_temps.py
Created June 6, 2018 18:20
parsing temperature logger data from raspberry pi
import matplotlib.pyplot as plt
data_points = {}
with open("test_data.txt", "r") as f:
for line in f:
date, temp = line.split("|")
temp = temp.strip()
data_points.update({date: temp})
@Julian-Nash
Julian-Nash / resize.sh
Last active April 28, 2018 19:24
Python 3. Convert bulk images from any file type to PNG with progress bar and save in new directory
#!/bin/bash
arg=$1
red='\033[1;31m'
yellow='\033[1;33m'
cyan='\033[1;36m'
purple='\033[1;35m'
nl='\n'
nc='\033[0m'
@Julian-Nash
Julian-Nash / validate_form.js
Created April 11, 2018 21:15
Form validation & fetch POST request using materialize.css & Javascript
// trim polyfill to clear whitespace
if (!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "");
};
}
var submit_btn = document.getElementById("acc_form_btn");
submit_btn.addEventListener("click", function() {
@Julian-Nash
Julian-Nash / send_gmail.py
Last active April 9, 2018 23:39
Python 3 gmail API example
# Follow guide on https://developers.google.com/gmail/api/quickstart/python to obtain credentials
import httplib2
import os
import oauth2client
from oauth2client import client, tools
import base64
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from apiclient import errors, discovery
@Julian-Nash
Julian-Nash / fetch_post.js
Last active April 3, 2018 12:55
Get form data on click and send json obj to server in fetch post. ES5 & ES6
// ES6
document.getElementById("post_btn").addEventListener("click", (e) => {
e.preventDefault();
let user_credentials = {
first_name: document.getElementById("first_name").value,
last_name: document.getElementById("last_name").value,
};
fetch("http://127.0.0.1:5000/testing_fetch", {
method: "POST",