Skip to content

Instantly share code, notes, and snippets.

View siteshen's full-sized avatar

siteshen siteshen

View GitHub Profile
@siteshen
siteshen / url_fetcher.py
Last active August 22, 2021 02:41
A simple multi-threaded URL fetcher.
import urllib2
from Queue import Queue
from threading import Thread
# global var
remaind_urls = Queue()
fetched_urls = set()
@siteshen
siteshen / jsend_tornado.py
Created September 18, 2013 16:49
A Mixin for Tornado and JSend specification. Django version: https://gist.github.com/siteshen/7688169
class JSendMixin(object):
"""http://labs.omniti.com/labs/jsend
JSend is a specification that lays down some rules for how JSON
responses from web servers should be formatted.
JSend focuses on application-level (as opposed to protocol- or
transport-level) messaging which makes it ideal for use in
REST-style applications and APIs.
"""
@siteshen
siteshen / django_admin_readonly.py
Created November 2, 2013 03:33
Django admin with read-only permission.
from django.contrib import admin
class ReadOnlyAdmin(admin.ModelAdmin):
"""A ModelAdmin with read only permission."""
def get_list_display(self, request):
return self.get_readonly_fields(request)
def get_readonly_fields(self, request, obj=None):
readonly_fields = []
@siteshen
siteshen / django_json_example.py
Created November 4, 2013 04:10
A ready to use Django JSONResponseMixin and an instance/JSON convertor.
from .models import Profile
from .django_json_mixin import JSONListView, JSONDetailView
class AccountList(JSONListView):
model = Profile
class AccountDetail(JSONDetailView):
@siteshen
siteshen / pil_crop.py
Created November 11, 2013 09:34
Simple avatar crop example, test if PIL does work. If something went wrong, just pip uninstall PIL && pip uninstall pillow && pip install pillow.
from PIL import Image
import six
import sys
# from django-avatar
def create_thumbnail(filename, size, output):
orig = open(filename)
image = Image.open(orig)
quality = 95
@siteshen
siteshen / mime.types.nginx.conf
Last active December 28, 2015 12:39
A sample nginx snippet with the following rules: 1. serve all files in /upload/ (and sub directory) except /upload/attachment; 2. serve only image files in /upload/attachment/ (and sub directory).
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/x-javascript js;
application/atom+xml atom;
application/rss+xml rss;
@siteshen
siteshen / jsend_django.py
Last active December 29, 2015 15:08
A Mixin for Django and JSend specification. Tornado version: https://gist.github.com/siteshen/6612013
class JSendMixin(object):
"""http://labs.omniti.com/labs/jsend
JSend is a specification that lays down some rules for how JSON
responses from web servers should be formatted.
JSend focuses on application-level (as opposed to protocol- or
transport-level) messaging which makes it ideal for use in
REST-style applications and APIs.
"""
@siteshen
siteshen / cas_client_tornado.py
Created January 6, 2014 06:08
A Mixin for Tornado and CAS client specification.
from urllib import urlencode, urlopen
import json
from tornado.web import RequestHandler
class CasClientMixin(object):
@property
def cas_server_url(self):
return 'http://www.example.com/cas'
@siteshen
siteshen / nginx_uid.py
Created January 11, 2014 07:53
Nginx user_id encoder/decoder from http://stackoverflow.com/a/19037624/739667
# http://stackoverflow.com/a/19037624/739667
import base64
import socket
import struct
def decode_cookie(cookie):
"""decode a u cookie into an uid
:param cookie: a cookie value that will be decoded into a uid
@siteshen
siteshen / scheduler.py
Created February 25, 2014 10:37
A simple threading-based task scheduler.
from Queue import Queue, PriorityQueue
from threading import Thread
class Scheduler(Thread):
def __init__(self, *args, **kwargs):
super(Scheduler, self).__init__(*args, **kwargs)
self.tasks = Queue()
self.workers = PriorityQueue()