Skip to content

Instantly share code, notes, and snippets.

View kgriffs's full-sized avatar

Kurt Griffiths kgriffs

View GitHub Profile
@kgriffs
kgriffs / example.py
Last active July 19, 2019 22:59
Convert a non-blocking synchronous function to an async coroutine function
import functools
def non_blocking_sync_to_async(func):
"""Convert a non-blocking synchronous function to an async coroutine.
Warning:
This should only be used to wrap non-blocking synchronous functions.
If the function may block for an extended period of time (e.g., it
performs I/O), it should be scheduled to run in an Executor
instead.
@kgriffs
kgriffs / str_compare_timesafe.py
Created July 4, 2019 04:31
Compare Entire Strings in a Time-Stable Way to Mitigate Timing Attacks
def str_compare_timesafe(a, b):
all_chars_match = 0
for c1, c2 in zip(a, b):
all_chars_match |= ord(c1) ^ ord(c2)
return all_chars_match == 0
@kgriffs
kgriffs / on_load.js
Last active July 1, 2019 20:01
Execute Function on DOM or Page Loaded (JavaScript)
// https://stackoverflow.com/questions/11936816/execute-function-after-complete-page-load
document.addEventListener("readystatechange", event => {
if (event.target.readyState === "interactive") { //same as: document.addEventListener("DOMContentLoaded"... // same as jQuery.ready
alert("All HTML DOM elements are accessible");
}
if (event.target.readyState === "complete") {
alert("Now external resources are loaded too, like css,src etc... ");
@kgriffs
kgriffs / example.sh
Created June 7, 2019 19:54
Bash confirmation prompt
read -p "Push new image? [y/N]: " CONTINUE
if [[ $CONTINUE =~ ^[Yy]$ ]]; then
PYENV_VERSION=2.7.16 docker push gcr.io/example-214815/example-app:$CURRENT_VER
PYENV_VERSION=2.7.16 docker push gcr.io/example-214815/example-app:latest
fi
@kgriffs
kgriffs / example.py
Created May 16, 2019 18:42
Falcon - Access Query String Params
import falcon
import falcon.testing
class Resource:
def on_get(self, req, resp):
# See also: https://falcon.readthedocs.io/en/stable/api/request_and_response.html#falcon.Request.get_param
resp.body = req.get_param('a')
resp.content_type = 'text/plain'
@kgriffs
kgriffs / async-interface-proposal.py
Last active August 20, 2020 23:00
Falcon ASGI Interface Proposal
class ChunkyBacon():
def __init__(self, baconator):
self._baconator = baconator
async def on_get(self, req, resp, bacon_id=None):
# Use JSON serializer to send it back in one chunk
resp.media = await self._baconator.get_bacon(bacon_id)
resp.set_header('X-Powered-By', 'Bacon')
@kgriffs
kgriffs / falcon_app_state.md
Last active April 24, 2019 18:46
Falcon Application State

There are at least three or four different approaches you can take to dealing with state in a Falcon app.

The first one is to have a module that exposes a function or attribute with read-only global state that doesn't change in between requests. This is typically used for app configuration that is loaded at startup. There are many variations on this theme, but here is an example showing one approach:

import os

import aumbry
@kgriffs
kgriffs / fix.sh
Last active May 15, 2019 17:02
Fix missing zlib ("zlib not available") on Mac OS when installing python with pyenv.
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
@kgriffs
kgriffs / example.yml
Created October 30, 2018 02:18
Puppeteer chrome deps for CentOS 7
- name: Install puppeteer/chrome deps
yum:
name: "{{ item }}"
state: latest
with_items:
- libX11
- libXcomposite
- libXcursor
- libXdamage
- libXext
@kgriffs
kgriffs / db_to_decimal.py
Created October 17, 2018 23:06
DB to Decimal Utility Functions
def _db_to_decimal(db_number):
if db_number is None:
return None
return Decimal(int(db_number)) / 100
def _decimal_to_db(decimal_number, currency=True):
if decimal_number is None:
return None