Skip to content

Instantly share code, notes, and snippets.

View mgd020's full-sized avatar

mgd020 mgd020

View GitHub Profile
@mgd020
mgd020 / random.js
Last active June 19, 2020 05:52
Generate a cryptographically random String with a given length
function getRandomBase64String(length) {
var array = new Uint8Array(Math.ceil(length * 3 / 4));
crypto.getRandomValues(array);
return btoa(String.fromCharCode.apply(null, array)).slice(0, length);
}
function getRandomHexString(length) {
var array = new Uint32Array(Math.ceil(length / 8));
crypto.getRandomValues(array);
return Array.prototype.map.call(array, i => i.toString(16).padStart(2, '0')).join('').slice(0, length);
@mgd020
mgd020 / sam.py
Created April 1, 2020 11:59
Lambda wrapper for wsgi app
import base64
import cgi
import codecs
import io
import sys
from urllib.parse import urlencode
from wsgiref.util import FileWrapper
from .wsgi import application
@mgd020
mgd020 / strip_image.py
Created March 7, 2020 10:34
Strip info from Pillow image, correcting rotation
from PIL import Image
EXIF_ORIENTATION = 274
EXIF_ORIENTATION_TRANSPOSE = [
None,
None,
Image.FLIP_LEFT_RIGHT,
@mgd020
mgd020 / progress.py
Created December 6, 2019 00:32
terminal progress bar in python
import sys, re
class TerminalController:
"""
A class that can be used to portably generate formatted output to
a terminal.
`TerminalController` defines a set of instance variables whose
values are initialized to the control sequence necessary to
perform a given action. These can be simply included in normal
@mgd020
mgd020 / force-replace-print.js
Created December 6, 2019 00:29
Override print dialog
function forceOpen() {
var url = "http://www.google.com";
window.open(url) ? location.reload() : (location.href = url);
}
window.addEventListener("beforeprint", function (e) { e.preventDefault(), forceOpen() })
window.matchMedia("print").addListener(function (e) { e.matches && forceOpen() })
@mgd020
mgd020 / xml_sax_parse_string.py
Last active December 2, 2019 05:18
Parse an XML snippet with regular expressions only.
import re
from xml.sax.saxutils import unescape
tags_pattern = re.compile(r"</(?P<close>\S+)>|<(?P<open>[^>!?/\s]+)(\s+(?P<attrs>.*?))?(?P<empty>/)?\s*>|<!--(?P<comment>.*?)-->|(?P<cdata><!\[CDATA\[)")
attrs_pattern = re.compile(r"\s*(?P<name>\S+)\s*=\s*(?P<quote>\"|')(?P<value>.*?)(?P=quote)")
cdata_close_pattern = re.compile(r"\]\]>")
escaped_entities = {"&quot;": '"', "&apos;": "'"}
@mgd020
mgd020 / jira2es.py
Created October 30, 2019 04:17
parse JIRA api and put it into ES for analysis
"""
requirements:
jira<3
elasticsearch<7
elasticsearch-dsl<7
raven<7
python-dateutil<3
jsonpointer<3
"""
import re
import sys
from collections import Mapping, Container
from itertools import chain
from array import array
def deep_getsizeof(o):
seen = set()
todo = {id(o): o}
import sys, re
class TerminalController:
"""
A class that can be used to portably generate formatted output to
a terminal.
`TerminalController` defines a set of instance variables whose
values are initialized to the control sequence necessary to
perform a given action. These can be simply included in normal
function forceOpen() {
var url = "http://www.google.com";
window.open(url) ? location.reload() : (location.href = url);
}
window.addEventListener("beforeprint", function (e) { e.preventDefault(), forceOpen() })
window.matchMedia('print').addListener(function (e) { e.matches && forceOpen() })