Skip to content

Instantly share code, notes, and snippets.

View svvitale's full-sized avatar

Scott Vitale svvitale

View GitHub Profile
@svvitale
svvitale / singleton.py
Created April 16, 2014 02:51
Mixin enabling any python class to be a ZMQ inproc listener
class _Singleton(type):
"""Singleton class from Stack Overflow:
http://stackoverflow.com/a/6798042
"""
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(_Singleton, cls).__call__(*args, **kwargs)
@svvitale
svvitale / EventLog.cs
Last active July 11, 2021 09:26
C# class that reads RFID cards and inserts each new read into a local MongoDB store. Reads are then pushed to a RESTful web service in a background task.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Forms;
using RFIDeas_pcProxAPI;
using System.Net;
using System.Threading;
using System.IO;
@svvitale
svvitale / engage.view.js
Created April 16, 2014 03:17
Backbone View implementing "Engage" console for Spigot Labs
define([
'jquery',
'jqueryui',
'jquery_cookie',
'underscore',
'backbone',
'app/views/tapEngage.view',
// Using the Require.js text! plugin, we are loaded raw text
// which will be used as our views primary template
@svvitale
svvitale / either_or.py
Last active August 29, 2015 14:02
A unittest/nose decorator for grouping tests together with an expected failure rate. A use case would be if you've written two tests, one of which should always pass. This decorator will skip failures until the expected failure count is exceeded or the grouping completes without reaching the expected failure count.
import nose
import functools
_either_or_map = dict()
def either_or(key='default', expected_failures=1):
def either_or_decorator(test):
global _either_or_map
@svvitale
svvitale / Comprehension.py
Last active August 29, 2015 14:15
Comprehensions or Loops?
def get(self, request):
return {
"rooms": [
{attrName: attrVal for attrName, attrVal in model_to_dict(x).items()
if attrName in RoomView.ROOM_PUBLIC_FIELDS}
for x in Room.objects.all()
]
}
@svvitale
svvitale / decorated_view.py
Created March 31, 2015 05:04
Comparison of Traditional Django Views to Decorated Views for APIs
from django.views.generic import View
from django.shortcuts import get_object_or_404
from .decorators import json
from .models import RoomModel
from django.http import HttpResponse
class RoomView(View):
"""Room view class that implements the primary HTTP verbs (POST, GET, PUT, DELETE).
@svvitale
svvitale / setup.py
Created June 14, 2016 20:33
setup.py with -isystem includes
from setuptools import setup
from distutils.core import Extension
from distutils.command.build import build
import os
from subprocess import call
import multiprocessing
from glob import glob
@svvitale
svvitale / gen.py
Created January 24, 2017 21:36
What does this do?
import random
import string
import re
allowable = re.sub(r'[IO10]', '', (string.ascii_uppercase + string.digits))
def combos(place):
for char in allowable:
def fib_iterative(n):
sequence = (0, 1)
if n < 2:
return sequence[n]
for _ in range(n - 2):
sequence = (sequence[1], sequence[0] + sequence[1])
return sequence[0] + sequence[1]
@svvitale
svvitale / standard_api_client.py
Created November 14, 2017 03:01
Requests.Session -> Async
import requests
class MyApiClient(requests.Session):
def do_something(self):
response = self.post('/some/url', json={
'param1': 'a',
'param2': 'b'
})
if response.status_code == requests.codes.ok: