Skip to content

Instantly share code, notes, and snippets.

View wshayes's full-sized avatar

William Hayes wshayes

View GitHub Profile
@wshayes
wshayes / comments
Last active November 15, 2023 11:34 — forked from dmontagu/main.py
[FastAPI app with response shape wrapping] #fastapi
From FastAPI Gitter:
dmontagu @dmontagu 00:14
@wshayes @intrepidOlivia here is a fully self-contained working implementation of a wrapped response
https://gist.github.com/dmontagu/9abbeb86fd53556e2c3d9bf8908f81bb
you can set context data and errors on the starlette Request and they get added to the response at the end
(@intrepidOlivia if you save the contents of that gist to main.py it should be possible to run via uvicorn main:app --reload)
if the endpoint failed in an expected way and you want to return a StandardResponse with no data field, you provide the type of StandardResponse you want to return instead of an instance
import asyncio
import json
import logging
from uuid import UUID
import aio_pika
import websockets.exceptions as ws_exc
from fastapi import APIRouter, Path
from starlette import status
from starlette.websockets import WebSocket
@jkmacc-LANL
jkmacc-LANL / jsonizer.py
Last active January 28, 2017 01:23
JSONVisitor
import json
from parsimonious import Grammar, NodeVisitor
class JSONVisitor(NodeVisitor):
"""
A simple JSON serializer for Parsimonious parse trees.
"""
def generic_visit(self, node, children):
@abargnesi
abargnesi / InferExactMatch_byPrefLabel.sparql
Created November 10, 2015 17:07
Infer exactMatch using case-insensitive string match on prefLabel
Select
PREFIX belv: <http://www.openbel.org/vocabulary/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX fn: <http://www.w3.org/2005/xpath-functions#>
# Bind ?namespaceConceptScheme to the URI of the namespace resource whose concepts you would like equivalenced via concept type and case-insensitive string match.
@Vheissu
Vheissu / PascalCaseToFileCase.js
Last active September 3, 2015 10:34
Converting a Javascript constructor name in PascalCase to file-case. Can be used with ES2015 (formerly ES6) classes to get the actual file name.
// ES2015 classes
class MyClass {
// Would return: my-class
toFileCase() {
var className = this.constructor.name.toString();
var normalised = className.replace(/((?!^)[A-Z])/g, '-$1').toLowerCase();
return normalised;
}
}
@monkeyhouse
monkeyhouse / install + aurelia.json mods
Last active July 29, 2018 22:14
aurelia selectize simple custom element
npm install selectize - save
npm install jquery -save
add this to the aurelia.json file in the dependencies section of the vendor bundle
"jquery",
{
"name": "selectize",
"path": "../node_modules/selectize/dist",
@nolanlawson
nolanlawson / protips.js
Last active February 4, 2024 18:06
Promise protips - stuff I wish I had known when I started with Promises
// Promise.all is good for executing many promises at once
Promise.all([
promise1,
promise2
]);
// Promise.resolve is good for wrapping synchronous code
Promise.resolve().then(function () {
if (somethingIsNotRight()) {
throw new Error("I will be rejected asynchronously!");
@dankrause
dankrause / _hover_example.py
Last active March 8, 2024 18:31
Example code to use the (unofficial, unsupported, undocumented) hover.com DNS API.
import requests
class HoverException(Exception):
pass
class HoverAPI(object):
def __init__(self, username, password):
params = {"username": username, "password": password}
r = requests.post("https://www.hover.com/api/login", params=params)
@informationsea
informationsea / xgmml_networkx.py
Created December 14, 2012 12:01
XGMML importer and exporter for networkx
__author__ = "Yasunobu OKAMURA"
__copyright__ = "Copyright (c) 2012 Y.Okamura"
__license__ = "GPL v3+"
import xml.parsers.expat
import networkx as nx
class XGMMLParserHelper(object):
"""
"""
@hrldcpr
hrldcpr / tree.md
Last active April 15, 2024 15:27
one-line tree in python

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!