This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
class DotChainedAttrs(object): | |
def __getattr__(self, name): | |
__dict__ = object.__getattribute__(self, '__dict__') | |
if name not in __dict__: | |
__dict__[name] = DotChainedAttrs() | |
return __dict__[name] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
class DottedDict(object): | |
pass | |
def dotted_dict(src_dict): | |
assert isinstance(src_dict, dict) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from django.test import TestCase | |
from django.core.urlresolvers import reverse | |
from django.test.client import RequestFactory | |
class RequestTest(TestCase): | |
def setUp(self): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
def oneline_print(message): | |
sys.stdout.write('\r%s' % message) | |
sys.stdout.flush() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from sqlalchemy import Column, Integer | |
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy.orm import Session | |
from sqlalchemy.ext.hybrid import hybrid_property | |
Base = declarative_base() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script> | |
<script> | |
var sock = new SockJS('http://' + window.location.host + '/realtime-news'); | |
sock.onopen = function() { | |
console.log('open'); | |
}; | |
sock.onmessage = function(e) { | |
console.log('message', e.data); | |
}; | |
sock.onclose = function() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from cStringIO import StringIO | |
from PIL import Image | |
from django.http import HttpResponse | |
ALLOWED_FORMAT = ( | |
'JPEG', | |
'PNG', | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding=utf-8 -*- | |
"""Generate a pythonic interface based on the code generated by `grpcio-tools`. | |
Example: | |
$ python grpc_pi.py --proto-package-name='xx' --pb2-module-name='python.path.xx_pb2' | |
""" | |
import argparse | |
import itertools |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Method 1 | |
# Pros: really short | |
# Cons: unicode will be escaped | |
echo '{"hello":"你好"}' | python -mjson.tool | |
# Method 2 | |
# Pros: avoid unicode escapes, output string is always UTF-8 encoded | |
# Cons: a little long | |
echo '{"hello":"你好"}' | python -c 'import json, sys; print(json.dumps(json.load(sys.stdin), ensure_ascii=False, indent=2).encode("utf-8"))' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
def time_span_range(begin, end, span): | |
"""Returns a list of time tuples that represents a series of timespans between a time range. | |
:param begin: begin time | |
:param end: end time | |
:param span: span of each timespan | |
""" |
OlderNewer