Skip to content

Instantly share code, notes, and snippets.

View matutter's full-sized avatar

Mat Utter matutter

View GitHub Profile
@matutter
matutter / moment_to_datetime.py
Created August 31, 2019 01:06
Shows how ms-since-epoch TS that are truncated to fit in int32_t space will look.
from subprocess import check_output
from datetime import datetime
ms_since_epoch = check_output([
"node",
"-e",
"console.log(require('moment')().valueOf())"
])
dt = datetime.fromtimestamp(float(ms_since_epoch) / 1000.0)
print(ms_since_epoch)
print(dt)
@matutter
matutter / example.py
Last active October 2, 2019 22:09
aiohttp dynamic resource in subapp prefix
#!/usr/bin/python3.7
# pytest -s test_subapps.py
# pip install pytest aiohttp pytest-aiohttp
from aiohttp import web
import pytest
def handle_1(request):
return web.Response(text="OK 1")
from tarfile import TarFile, TarInfo, open as OpenTarfile
from tempfile import NamedTemporaryFile, TemporaryDirectory
import shutil
file_data = [
(b'1239EAD09123FCC', "/data/local/tmp/me"),
(b'aaaaaaaaaaaaaaa', "/data/local/tmp/a"),
(b'bbbbbbbbbbbbbbb', "/data/local/tmp/b")
]
@matutter
matutter / markdown.css
Created January 15, 2020 02:54
Stylesheet for styling pandoc generated markdown to HTML
html {
font-size: 100%;
overflow-y: scroll;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
body {
color: #444;
font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif;
@matutter
matutter / pika-consumer.py
Created January 15, 2020 03:20
Pika and Tornado
# Updated from these out of date docs.
# https://pika.readthedocs.io/en/stable/examples/tornado_consumer.html
from pika.adapters.tornado_connection import TornadoConnection
import pika
import logging
import coloredlogs
coloredlogs.install()
@matutter
matutter / sqla_bulk_update.py
Last active January 26, 2023 03:56
Shows a bulk update using SQLAlchemy ORM.
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Boolean, String
from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///:memory:', echo=True)
session = sessionmaker(bind=engine)()
Base = declarative_base()
class User(Base):
@matutter
matutter / gql_poly.py
Last active January 18, 2020 16:15
Dynamic generation of graphql interface based on polymorphic ORM inheritance
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Boolean, String, String, Integer, ForeignKey
from sqlalchemy.orm import sessionmaker
from secrets import token_hex
from json import dumps
import graphene
from graphene_sqlalchemy import SQLAlchemyObjectType, SQLAlchemyConnectionField
from graphene.relay import Connection
from collections import defaultdict
@matutter
matutter / poly_delete.py
Created January 26, 2020 05:30
SQLAlchemy cascade delete with polymorphic tables
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, String, Integer, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///:memory:', echo=False)
session = sessionmaker(bind=engine)()
Base = declarative_base()
@matutter
matutter / FilteredConnectionExample.py
Created February 6, 2020 04:10
Example on adding filtering to connection fields.
class RoleInput(graphene.InputObjectType):
role = String()
class FilteredConnectionField(SQLAlchemyConnectionField):
def __init__(self, type, input_type, *args, **kwargs):
fields = {name: field.type() for name, field in input_type._meta.fields.items()}
kwargs.update(fields)
super().__init__(type, *args, **kwargs)
@matutter
matutter / stf-compose.yml
Last active June 24, 2020 12:20
Compose file to run STF on a local machine.
##
# Usage:
# Run `docker-compose up`
# Navigate to localhost:7100 in your browser
version: "3.6"
services:
stf:
image: openstf/stf
command: bin/stf local