Skip to content

Instantly share code, notes, and snippets.

Avatar

Tom Morris tommorris

View GitHub Profile
@tommorris
tommorris / ast-modify.py
Created May 19, 2020
supporting code for IAM blog post
View ast-modify.py
def modify_policy(filename, queue_name):
with open(filename, "r") as fh:
policy = json.load(policy)
allowed_queue = [f"arn:aws:sqs:*:*:{queue_name}"]
for idx, statement in enumerate(policy["Statement"])
services = list(set([x.split(":")[0] for x in statement["Action"]]))
if services == ['sqs']:
policy[idx]['Action'] = allowed_queue
return policy
@tommorris
tommorris / JSON-LineOriented.json.groovy
Last active Nov 29, 2019
a really hacky version of DataGrip's JSON-Groovy.json.groovy to emit "line-oriented" JSON
View JSON-LineOriented.json.groovy
/*
* Available context bindings:
* COLUMNS List<DataColumn>
* ROWS Iterable<DataRow>
* OUT { append() }
* FORMATTER { format(row, col); formatValue(Object, col) }
* TRANSPOSED Boolean
* plus ALL_COLUMNS, TABLE, DIALECT
*
* where:
@tommorris
tommorris / query_tubes.sparql
Created Nov 1, 2019
SPARQL query to extract all London Underground lines (not Overground or Crossrail etc.)
View query_tubes.sparql
SELECT DISTINCT ?item ?itemLabel ?adj ?adjLabel ?line ?lineLabel
WHERE
{
?item wdt:P31 wd:Q14562709 .
?item wdt:P197 ?adj .
?item p:P197 ?adjStmt .
?adjStmt pq:P81 ?line .
?line wdt:P361 wd:Q20075 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
View operator.py
from functools import reduce
import operator
nums = [1, 2, 3, 4, 5]
reduce(operator.add, nums)
@tommorris
tommorris / prefix_check.py
Last active Jun 15, 2019
Little code sample for blog post
View prefix_check.py
PREFIXES = ["https://tommorris.org"]
def prefix_check(url):
for prefix in PREFIXES:
if url.startswith(prefix):
return url
return ""
@tommorris
tommorris / qr_function.py
Created Jun 7, 2019
Google Cloud Function for creating QR codes
View qr_function.py
import io
from flask import send_file
import qrcode
def make_qr(data, box_size=10, border=2):
qr = qrcode.QRCode(version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=box_size,
border=border)
qr.add_data(data)
@tommorris
tommorris / issue.md
Created Mar 6, 2019
An issue with GitHub Pages settings
View issue.md

I have a site that uses GitHub Pages - offlineapps.info. It used to not serve an HTTPS certificate, but I moved the DNS around today so it is now pointing to GitHub Pages using an ANAME.

The SSL certificate works fine (and Let's Encrypt is a great addition to GitHub Pages), but the UI has a slight issue. I go to the repos Settings page and see this...

screenshot 2019-03-06 at 21 52 14

So I tick the box. A green tick appears.

screenshot 2019-03-06 at 21 53 09

@tommorris
tommorris / function.py
Created Jan 31, 2019
Sample Google Cloud function
View function.py
import nexmo
from flask import jsonify
def send_sms(request):
data = request.get_json()
# NEXMO_API_KEY and NEXMO_API_SECRET are in env vars
# which are set in the Google Cloud function
client = nemxo.Client()
@tommorris
tommorris / 7segment.scala
Last active Nov 22, 2018
A Scala implementation of tomscott's 7-segment display problem
View 7segment.scala
import scala.util.matching.Regex
import scala.io.Source
val matcher: Regex = "[gkmqvwxzio]".r
def displayableOn7SegmentDisp(str: String) = matcher.findAllIn(str).isEmpty
val filename = "/Users/tom/Desktop/words_alpha.txt"
val words = Source.fromFile(filename).getLines.toStream
val longestWord = words.filter(displayableOn7SegmentDisp).sortWith(_.length > _.length).head
println(longestWord)
@tommorris
tommorris / get_nexmo_keys.sh
Last active Nov 2, 2018
An example of how to retrieve API keys from 1Password vault
View get_nexmo_keys.sh
eval $(op signin <yoursubdomain>) # this will prompt you to log in
export NEXMO_API_KEY=$(op get item Nexmo | jq '.details.sections[]? | select(.fields) | .fields[] | select(.t == "key").v')
export NEXMO_API_SECRET=$(op get item Nexmo | jq '.details.sections[]? | select(.fields) | .fields[] | select(.t == "secret").v')
You can’t perform that action at this time.