Skip to content

Instantly share code, notes, and snippets.

Avatar
🐍

Julian Julian-Nash

🐍
View GitHub Profile
@Julian-Nash
Julian-Nash / __init__.py
Created Jul 21, 2021
Sanic Motor Example
View __init__.py
from pathlib import Path
import os
from sanic import Sanic
from motor.motor_asyncio import AsyncIOMotorClient
from app.config import get_config
from app.blueprints import view, ws, api
View highlight_code.py
from pygments import highlight
from pygments.formatters.html import HtmlFormatter
from pygments.lexers import guess_lexer, get_lexer_for_filename, get_lexer_by_name
class HTMLCodeHighlighter:
""" Generate highlighted HTML snippets of code (pip install Pygments)"""
@classmethod
def _highlight(cls, code: str, lexer):
@Julian-Nash
Julian-Nash / doubly_linked_list.py
Last active Dec 22, 2020
Doubly Linked List. Python implementation
View doubly_linked_list.py
from typing import Optional, Union
class DoublyLinkedListNode:
def __init__(self, value: Optional = None, prev: Optional = None, next: Optional = None):
self.value = value
self.prev = prev
self.next = next
View profiler.py
""" Flask application profiler
Will run the Flask application and log profile data to stdout.
The `restrictions` parameter passed to `ProfilerMiddleware` is used to limit the number of items listed
in the report.
"""
import os
from werkzeug.middleware.profiler import ProfilerMiddleware
@Julian-Nash
Julian-Nash / json_response.py
Last active Sep 10, 2020
Flask JSON responder class - Class with static methods for returning JSON data by calling methods mapping to RFC HTTP status codes
View json_response.py
from flask import jsonify, make_response, Response
from typing import Optional, Any
from http import HTTPStatus
class JSONResponse(object):
@classmethod
def _send_response(cls, status: int, data: Any, headers: Optional[dict] = None):
@Julian-Nash
Julian-Nash / http_status_code_enum.py
Last active Nov 20, 2019
HttpStatusCode - A handy little enum
View http_status_code_enum.py
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 / upload.html
Created Feb 22, 2019
File upload with progress bar, cancel and percent complete - XMLHTTPRequest (Bootstrap 4)
View upload.html
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
@Julian-Nash
Julian-Nash / .bashrc
Last active Jan 3, 2019
WSL quick & nice terminal setup w/Nord color scheme
View .bashrc
# 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 / flask_sitemap_generator.py
Last active Jun 20, 2022
Flask dynamic sitemap generator
View flask_sitemap_generator.py
@app.route("/sitemap")
@app.route("/sitemap/")
@app.route("/sitemap.xml")
def sitemap():
"""
Route to dynamically generate a sitemap of your website/application.
lastmod and priority tags omitted on static pages.
lastmod included on dynamic content such as blog posts.
"""
from flask import make_response, request, render_template
@Julian-Nash
Julian-Nash / clean_urls.py
Created Oct 12, 2018
clean & assemble urls. Quick and dirty solution - need to refactor
View clean_urls.py
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",