Skip to content

Instantly share code, notes, and snippets.

Luo Peng RussellLuo

Block or report user

Report or block RussellLuo

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@RussellLuo
RussellLuo / base_encoder.py
Created Feb 7, 2020
An encoder that can encode a decimal number to a string in any base, and vice versa.
View base_encoder.py
class BaseEncoder:
"""
An encoder that can encode a decimal number to a string in any base, and vice versa.
Example usage:
>>> import string
>>> chars_62 = string.digits + string.ascii_letters[:52]
>>> encoder = BaseEncoder(chars_62)
>>> encoder.encode(10765432100123456789)
@RussellLuo
RussellLuo / gst_jpeg_encoder.py
Last active Jul 18, 2018
JPEG encoder based on gstreamer.
View gst_jpeg_encoder.py
# -*- coding: utf-8 -*-
"""
JPEG encoder based on gstreamer.
Many thanks to the following code and docs:
- The usage of `appsrc` in Python: https://github.com/tamaggo/gstreamer-examples/blob/master/test_gst_appsrc_testvideo_mp4mux.py
- The usage of `appsink` in Python: https://gist.github.com/willpatera/7984486
- The usage of both `appsrc` and `appsink` in C++: https://github.com/dkorobkov/gstreamer-appsrc-appsink-example/blob/master/JpegGstEncoder.cpp
@RussellLuo
RussellLuo / time_span_range.py
Created May 24, 2018
Returns a list of time tuples that represents a series of timespans between a time range.
View time_span_range.py
# -*- 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
"""
@RussellLuo
RussellLuo / pretty_print_json.sh
Last active May 12, 2017
JSON pretty printer in one line.
View pretty_print_json.sh
# 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 / grpc_pi.py
Last active Mar 6, 2020
gRPC client interface for Python: generation script and mocking class.
View grpc_pi.py
# -*- 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 / limit_uploaded_image
Last active Aug 29, 2015
Limit the format and size (width and height) of the uploaded image in Django.
View limit_uploaded_image
from cStringIO import StringIO
from PIL import Image
from django.http import HttpResponse
ALLOWED_FORMAT = (
'JPEG',
'PNG',
)
@RussellLuo
RussellLuo / index.html
Last active Aug 29, 2015
An example showing how to use sockjs-tornado.
View index.html
<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 / post_pv
Created Jul 17, 2014
An example showing how to use @hybrid_property in SQLAlchemy.
View post_pv
#!/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 / oneline_print
Created Jun 16, 2014
Print messages just on one line. That is, old messages will be replaced by new ones one the same line.
View oneline_print
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
def oneline_print(message):
sys.stdout.write('\r%s' % message)
sys.stdout.flush()
@RussellLuo
RussellLuo / dummy_request_in_django
Last active Aug 29, 2015
An example showing how to use the dummy request in Django unit testing.
View dummy_request_in_django
#!/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):
You can’t perform that action at this time.