Skip to content

Instantly share code, notes, and snippets.

View vimiix's full-sized avatar
🧱
Coding

Vimiix Yao vimiix

🧱
Coding
View GitHub Profile
@vimiix
vimiix / github.css
Created February 2, 2019 07:34 — forked from theconektd/github.css
Github Markdown CSS - for Markdown Editor Preview
body {
font-family: Helvetica, arial, sans-serif;
font-size: 14px;
line-height: 1.6;
padding-top: 10px;
padding-bottom: 10px;
background-color: white;
padding: 30px; }
body > *:first-child {
@vimiix
vimiix / docker-swarm-mode.sh
Created January 24, 2019 07:15
在docker swarm模式下,通过docker-machine 创建docker集群实例(virtual box 驱动虚拟机)
#!/bin/bash
# Swarm mode using Docker Machine
managers=3
workers=3
# 创建管理节点虚拟机
echo "======> Creating $managers manager machines ...";
for node in $(seq 1 $managers);
@vimiix
vimiix / parse_hex_to_rgb.js
Created January 15, 2019 13:01
解析字符串的颜色码,判断颜色是暖色还是冷色
function hexToRgb (hex) {
var rgb = [];
hex = hex.substr(1); //去除前缀 # 号
if (hex.length === 3) { // 处理 "#abc" 成 "#aabbcc"
hex = hex.replace(/(.)/g, '$1$1');
}
hex.replace(/../g, function(color){
@vimiix
vimiix / my_thread_pool.py
Created December 28, 2018 08:39
自定义线程池
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Python的线程池实现
import Queue
import threading
import sys
import time
@vimiix
vimiix / cache.py
Created May 29, 2018 07:21
使用全局变量做数据缓存
#coding: utf8
import hashlib
import pickle
import time
CACHE = {}
def is_obsolete(entry, duration):
return time.time() - entry['time'] > duration
@vimiix
vimiix / brower.py
Created April 10, 2018 11:13 — forked from the5fire/brower.py
简单理解socket模拟浏览器请求
# author: the5fire.com
import re
import socket
from collections import namedtuple
RE_URL = re.compile(r'http://(.*)')
RE_CONTENT_LENGTH = re.compile(b'.*Content-Length: (\d+)')
EOF = '\r\n\r\n'
Response = namedtuple('Response', ['header', 'body'])
@vimiix
vimiix / python_gc.md
Created March 13, 2018 11:00
详细说明Python垃圾回收的原理

三个方面说清楚垃圾回收机制:

  • 一对象的引用计数机制
  • 二垃圾回收机制
  • 三内存池机制

一、对象的引用计数机制

Python内部使用引用计数,来保持追踪内存中的对象,所有对象都有引用计数。

@vimiix
vimiix / reverse_linked_list.py
Created March 6, 2018 08:32
翻转单链表
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
# write code here
if pHead is None or pHead.next is None:

process_request

  • 一个请求来到middelware层,进入的第一个方法。一般情况我们可以在这里做一些校验,比如用户登录,或者HTTP中是否有认证头之类的验证。这个方法需要两种返回值,HttpResponse或者None,如果返回HttpResponse,那么接下来的处理方法只会执行process_response,其他的方法将不会被执行。这里需要注意的是,如果你的middleware在settings配置的MIDDLEWARE_CLASS的第一个的话,那么剩下的middleware也不会被执行。另外一个返回值是None,如果返回None,那么Django会继续执行其他的方法。

process_view

  • 这个方法是在process_request之后执行的,参数如上面代码所示,其中的func就是我们将要执行的view方法,因此我们要统计一个view的执行时间,可以在这里来做。它的返回值跟process_request一样,HttpResponse/None,逻辑也是一样。如果返回None,那么Django会帮你执行view函数,从而得到最终的Response。

process_template_response

  • 执行完上面的方法,并且Django帮忙我们执行完view之后,拿到最终的response,如果是使用了模板的Response(是指通过return render(request, 'index.html', context={})的方式返回Response,就会来到这个方法中。这个方法中我们可以对response做一下操作,比如Content-Type设置,或者其他HEADER的修改/增加。

process_response

@vimiix
vimiix / get_first_item_from_OrderedDict.py
Created February 27, 2018 07:42
从有序字典中获取第一个元素的方法
from collections import OrderedDict
od = OrderedDict(zip('foo', 'bar'))
# 方法1
od.keys()[-1] # 仅适用于2
od.values()[-1]
od.items()[-1]
list(od.items())[-1] # 兼容 3.x