Skip to content

Instantly share code, notes, and snippets.

@c1ay
c1ay / iris.py
Last active August 7, 2020 07:07
使用python 类型标注的解析字段,适用于rpc调用,字段懒加载
import typing
from abc import ABCMeta
INSTANCE_ATTR = 'instance_attr'
class UndefinedType:
def __repr__(self):
return 'UndefinedType'
@c1ay
c1ay / dynamic_field.py
Created March 31, 2020 08:36
rest frame work 动态返回字段
import typing
from collections import OrderedDict
from django.db import models
from django.db.models.fields import related_descriptors
from rest_framework import serializers
from rest_framework.fields import SkipField
from rest_framework.relations import PKOnlyObject
@c1ay
c1ay / go_example_websocket_novnc.go
Created August 2, 2019 08:22 — forked from x32net/go_example_websocket_novnc.go
Example: Go Websocket binary proxy noVnc
// Go Websocket binary proxy noVnc
package main
import (
"flag"
"golang.org/x/net/websocket"
"io"
"log"
"net"
"net/http"
@c1ay
c1ay / async_exmaple.py
Created August 16, 2017 06:32
用yield单线程实现生产者,消费者模型.
import time
def consumer():
r = ''
while True:
n = yield r
if not n:
return
@c1ay
c1ay / weighted_random.py
Last active May 25, 2017 03:11
python 权重随机
import random
import bicsect
import itertools
class WeightedRandomGenerator(object):
def __init__(self, weights):
self.totals = []
running_total = 0
# py2
@c1ay
c1ay / method_dispatcher.py
Last active March 30, 2017 08:36
tornado 基于函数分发url
from tornado.web import RequestHandler, HTTPError
from tornado.escape import json_encode, json_decode
class MethodDispatcher(RequestHandler):
allow_methods = ()
def set_default_headers(self):
origin = None
if self.request.headers.get('Origin'):