Skip to content

Instantly share code, notes, and snippets.

@RussellLuo
RussellLuo / dot_chained_attrs
Created April 5, 2014 03:51
A simple class for generating dot chained attributes
#!/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]
@RussellLuo
RussellLuo / dotted_dict
Last active August 29, 2015 13:58
A simple function for converting dict to dotted-dict.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class DottedDict(object):
pass
def dotted_dict(src_dict):
assert isinstance(src_dict, dict)
@RussellLuo
RussellLuo / dummy_request_in_django
Last active August 29, 2015 14:01
An example showing how to use the dummy request in Django unit testing.
#!/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):
@RussellLuo
RussellLuo / oneline_print
Created June 16, 2014 03:48
Print messages just on one line. That is, old messages will be replaced by new ones one the same line.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
def oneline_print(message):
sys.stdout.write('\r%s' % message)
sys.stdout.flush()
@RussellLuo
RussellLuo / post_pv
Created July 17, 2014 03:53
An example showing how to use @hybrid_property in SQLAlchemy.
#!/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()
@RussellLuo
RussellLuo / index.html
Last active August 29, 2015 14:04
An example showing how to use sockjs-tornado.
<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() {
@RussellLuo
RussellLuo / limit_uploaded_image
Last active August 29, 2015 14:21
Limit the format and size (width and height) of the uploaded image in Django.
from cStringIO import StringIO
from PIL import Image
from django.http import HttpResponse
ALLOWED_FORMAT = (
'JPEG',
'PNG',
)
@RussellLuo
RussellLuo / grpc_pi.py
Last active April 15, 2020 10:35
gRPC client interface for Python: generation script and mocking class.
# -*- 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
@RussellLuo
RussellLuo / pretty_print_json.sh
Last active May 12, 2017 06:27
JSON pretty printer in one line.
# 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"))'
@RussellLuo
RussellLuo / time_span_range.py
Created May 24, 2018 08:33
Returns a list of time tuples that represents a series of timespans between a time range.
# -*- 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
"""