Skip to content

Instantly share code, notes, and snippets.

View dkarchmer's full-sized avatar

David Karchmer dkarchmer

View GitHub Profile
@dkarchmer
dkarchmer / returnObservableWithNestedForkJoin.ts
Created February 19, 2017 17:50
Example of Angular2/TypeScript function returning an Observable after executing two nested forkJoin
public fetchABunchOfData (rootObj: RootObj): Observable<Project> {
// Returned original RootObj as an Observer when nested resource is fetched
let returnedData = new ReplaySubject(1);
let firstObservable = Observable.forkJoin(
this._cloud.getSomeRestData(rootObj.id),
this._cloud.getOtherRestData(rootObj.id)
);
// Execute first two Rest calls (someData and OtherData)
@dkarchmer
dkarchmer / django-eb-fab.py
Last active July 3, 2017 10:20
Fabric based deploy script for a Python/Django Server with a Docker based worker
'''
Example of a deployment script for:
- A Django based server running as an Elastic Beanstalk server tier, with Load Balancing
(Assumes a pre-configured RDS database - best practice)
- A Docker based running as an Elastic Beanstalk worker tier
Assumes following directory structure
- ./server # With Django based project and appropriate .ebextensions
- ./worker # with Docker based project and appropriate .ebextensions, Dockerfile and Dockerrun.aws.json file
# (similar to https://github.com/dkarchmer/aws-eb-docker-django)
@dkarchmer
dkarchmer / view_api_with_pagination.py
Created April 24, 2016 01:45
How to paginate a custom detail_route get on djangorestframework
# ViewSets define the view behavior.
class FooViewSet(viewsets.ModelViewSet):
lookup_field = 'slug'
queryset = Foo.objects.all()
serializer_class = FooSerializer
def get_queryset(self):
"""
This view should return a list of all records
@dkarchmer
dkarchmer / djangorestframework-client.py
Last active August 29, 2017 03:11
Semi-generic Python Rest Client, assuming a DjangoRestFramework back-end (and using TokenAuthentication)
__author__ = 'dkarchmer'
"""
See https://gist.github.com/dkarchmer/d85e55f9ed5450ba58cb
This API generically supports DjangoRestFramework based APIs
It is based on https://github.com/samgiles/slumber, but customized for
Django Rest Frameworks, and the use of TokenAuthentication.
Usage:
# Assuming
# v1_api_router.register(r'some_model', SomeModelViewSet)
@dkarchmer
dkarchmer / serverless_get_s3_file.py
Created September 7, 2017 17:19
Serverless function snippet to return an S3 file
def get(event, context):
response = _get_error_responce('Incorrect URL format: Use /foobar/<a>/<b>/?type=<url/file>')
if 'queryStringParameters' in event and 'pathParameters' in event:
if event['pathParameters'] and 'a' in event['pathParameters'] and 'b' in event['pathParameters']:
a = event['pathParameters']['a']
b = event['pathParameters']['b']
@dkarchmer
dkarchmer / mac--bash_profile
Last active April 17, 2018 17:16
Sample .bash_profile for a new Mac (See new-mad.md setup)
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
# pip should only run if there is a virtualenv currently activated
export PIP_REQUIRE_VIRTUALENV=true
gpip(){
# gpip allows to install on global env
PIP_REQUIRE_VIRTUALENV="" pip "$@"
}
@dkarchmer
dkarchmer / rds-cache-tasks.py
Last active April 30, 2018 19:31
Invoke Tasks to create RDS and ElasticCache instances
import os
import sys
import time
import boto3
import botocore
import pprint
from invoke import run, task
DB_CONFIG = {
# SecurityGroup='sg-name" with ingress on port 5432 and source from itself
@dkarchmer
dkarchmer / ffmpeg-mosaic.js
Last active June 7, 2018 01:44
Node.js based script to build a mosaic of four videos (using fluent-ffmpeg)
// Based on http://pythonhackers.com/p/fluent-ffmpeg/node-fluent-ffmpeg
// and https://trac.ffmpeg.org/wiki/Create%20a%20mosaic%20out%20of%20several%20input%20videos
// Usage:
// node ffmpeg-mosaic.js file1.mp2 file2.mp4 file3.mp4 file4.mp4
// Generates out.mp4
var ffmpeg = require('fluent-ffmpeg');
var command = ffmpeg()
import argparse
import time
import sys
from iotile.core.hw import HardwareManager
def build_parser():
parser = argparse.ArgumentParser(description="Print broadcast readings from a specific device")
parser.add_argument('-d', '--dev', type=lambda x: int(x, 0), help="Optional, the UUID of the device you want to see broadcasts from (0x21 for example)")
parser.add_argument('-i', '--interval', type=float, default=1.0, help="The minimum interval in seconds between two reported readings (only applies when -d is given), defaults to 1s")
@dkarchmer
dkarchmer / django-gulpfile.js
Created December 8, 2015 17:31
Example of a Gulp file for processing a Django base template and releasing statics to cloudfront
/* Assumes the following directory structure:
* ./images # With original images
* ./base # With the index.html, css, js to be used to create a base template
* # This directory containts its own gulpfile (maybe coming from a yeoman or similar)
* ./server # Actual Django project, with a templates subdirectory
*/
var gulp = require('gulp');
var chug = require( 'gulp-chug' );
var awspublish = require('gulp-awspublish');