Skip to content

Instantly share code, notes, and snippets.

View mattrasband's full-sized avatar

Matt Rasband mattrasband

  • The Grid
  • 09:42 (UTC -06:00)
View GitHub Profile
@mattrasband
mattrasband / authorization_code.py
Last active June 12, 2017 00:45
OAuth2 Client Flow Samples: Authorization Code
#!/usr/bin/env python3
from urllib.parse import urlencode
import requests
from flask import abort, current_app, Flask, redirect, render_template_string, request, url_for
app = Flask(__name__)
app.config.update(
client_id='',
client_secret='',
@mattrasband
mattrasband / chromecastdownloader.py
Created June 6, 2017 03:33
Download chromecast background
#!/usr/bin/env python3
import collections
import concurrent.futures
import json
import os
import pathlib
import re
import requests
@mattrasband
mattrasband / register-ip-with-ec2.sh
Created May 22, 2017 15:54
Add a public IP to an AWS security group and schedule automatic removal
#!/usr/bin/env bash
# You need to set some vars somewhere:
# SECURTITY_GROUP=<your_security_group_id>
# on mac you need to enable running the `at` command:
# $ sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist
current_ip=$(curl -s http://checkip.amazonaws.com/)
echo "Current IP: ${current_ip}"
@mattrasband
mattrasband / UserService.java
Created April 19, 2017 04:26
Example of Spring 4.2's @TransactionalEventListener
@Service
@Slf4j
public class UserService {
private final ApplicationEventPublisher applicationEventPublisher;
private final UserRepository userRepository;
UserService(ApplicationEventPublisher applicationEventPublisher,
UserRepository userRepository) {
this.applicationEventPublisher = applicationEventPublisher;
this.userRepository = userRepository;
@mattrasband
mattrasband / ticktock-worker.py
Last active January 3, 2017 00:54
ticktock-worker.py
#!/usr/bin/env python3.6
import asyncio
import aioamqp
async def main(loop=None):
if loop is None:
loop = asyncio.get_event_loop()
transport, protocol = await aioamqp.connect()
channel = await protocol.channel()
@mattrasband
mattrasband / worker.java
Last active January 3, 2017 00:53
ticktock.java
@Service
public class MyWorker {
@RabbitListener(bindings =
@QueueBinding(value = @Queue(
value = "${spring.application.name}.worker.my_job",
durable = "false",
// ensure that the message expires in some reasonable period,
// a safe number is 1/2 the time between events
arguments = {
@Argument(name = "x-message-ttl",
@mattrasband
mattrasband / schedule.yml
Created December 30, 2016 20:45
ticktock-schedule.yml
ticktock:
scheduler:
cron:
"0 * * * * *": "cron.minute.1"
"0 */5 * * * *": "cron.minute.5"
"0 */2 * * * *": "cron.minute.2"
"0 */10 * * * *": "cron.minute.10"
"0 */15 * * * *": "cron.minute.15"
"0 */30 * * * *": "cron.minute.30"
"0 */45 * * * *": "cron.minute.45"
@mattrasband
mattrasband / iterable_queue.py
Created October 8, 2016 20:53
Naive Asyncio Play
"""
Naive implementation of an iterable queue in asyncio
"""
#!/usr/bin/env python3
import asyncio
class Queue(asyncio.Queue):
async def __aiter__(self):
import asyncio
async def func_two():
await asyncio.sleep(2.5)
raise RuntimeError
async def func_one():
try:
await func_two()
except RuntimeError as e:
import asyncio
def long_running_system_task():
import time
print('starting background task')
# Normally this would block the event loop, but it's okay
# when called in another thread with an executor :)
time.sleep(3)
return 'finished tasks'