Skip to content

Instantly share code, notes, and snippets.

@RussellLuo
RussellLuo / appx_gin_example.go
Last active June 10, 2021 13:10
An example illustrates how to manage Gin-based HTTP applications in appx.
package main
import (
"context"
"fmt"
"net/http"
"github.com/RussellLuo/appx"
"github.com/gin-gonic/gin"
)
@RussellLuo
RussellLuo / werror.go
Created May 28, 2020 08:08
Enhanced error handling based on new features introduced in Go 1.13
package werror
import (
"fmt"
)
type Error struct {
Err error // the underlying error
Str string
}
@RussellLuo
RussellLuo / httpclient.go
Created May 27, 2020 08:16
An HTTP client creator for Golang
package httpclient
import (
"net"
"net/http"
"time"
)
// Options includes common Client configurations that need to be tuned.
type Options struct {
@RussellLuo
RussellLuo / base_encoder.py
Created February 7, 2020 05:13
An encoder that can encode a decimal number to a string in any base, and vice versa.
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 April 25, 2022 12:34
JPEG encoder based on gstreamer.
# -*- 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 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
"""
@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 / 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 / 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 / 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() {