Skip to content

Instantly share code, notes, and snippets.

View limboinf's full-sized avatar
🎯
Focusing

limbo limboinf

🎯
Focusing
View GitHub Profile
@limboinf
limboinf / api_sign_token.py
Created June 29, 2016 15:24
api请求和验证。
# coding=utf-8
"""
Api请求验证
:copyright: (c) 2016 by fangpeng(@beginman.cn).
:license: MIT, see LICENSE for more details.
"""
import uuid
import base64
import struct
@limboinf
limboinf / find-cmd-exists.py
Created July 1, 2016 08:12
在pyprof2calltree这个项目中学到的在系统中查看命令是否存在
# coding=utf-8
"""
在pyprof2calltree这个项目中学到的在系统中查看命令是否存在
https://github.com/pwaller/pyprof2calltree/blob/master/pyprof2calltree.py
:copyright: (c) 2016 by fangpeng(@beginman.cn).
:license: MIT, see LICENSE for more details.
"""
import os
import sys
@limboinf
limboinf / sorted_list_operator.py
Created July 1, 2016 09:23
python bisect模块构造 有序list操作
# coding=utf-8
"""
python bisect模块构造 有序list操作
:copyright: (c) 2016 by fangpeng(@beginman.cn).
:license: MIT, see LICENSE for more details.
"""
import bisect
@limboinf
limboinf / Bisection.py
Created July 1, 2016 09:47
bisect 二分查找源码分析
# coding=utf-8
"""
bisect实现源码就是**二分查找算法**,**二分查找的对象是:有序数组。这点特别需要注意。要先把数组排好序再操作。**
**基本步骤:**
- 从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束;
- 如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从中间元素开始比较。
- 如果在某一步骤数组为空,则代表找不到。
@limboinf
limboinf / pretty_main.py
Created July 1, 2016 09:58
Python之父教你写main()函数.
# coding=utf-8
"""
Python之父教你写main()函数
----------------------------------
main()函数使其接受一个可选参数 argv,支持在交互式shell中调用该函数,
我们就可以动态地提供 argv 的值。
让main()函数的返回值指示退出状态(exit status)并且,
main()函数中的sys.exit(n)调用全部变成return n。
@limboinf
limboinf / go_select_fib.go
Created July 4, 2016 05:35
go select with fibonacci
package main
import (
"fmt"
)
func fib(c, quit chan int) {
x, y := 0, 1
// select 语句使得一个goroutine在多个通讯操作上等待
// select 会阻塞,直到条件分支中的某个可以继续执行,如果有多个可执行,则随机执行一个
@limboinf
limboinf / two-ways-to-check-req-fields.py
Last active July 4, 2016 09:19
Two ways to check the request of the integrity of the fields
# coding=utf-8
"""
Two ways to check the request of the integrity of the fields
valid_req_field: Primitive way
valid_req_field_by_compress: itertools.compress()
"""
import itertools
def valid_req_field(req_data, *args):
@limboinf
limboinf / tornado-async-spider.py
Last active June 12, 2018 08:21
tornado异步爬虫示例
# coding=utf-8
"""
tornado异步爬虫示例
"""
import time
from datetime import timedelta
from bs4 import BeautifulSoup
from tornado.httpclient import AsyncHTTPClient
from tornado import ioloop, gen, queues
@limboinf
limboinf / simple-event-loop.py
Created July 11, 2016 13:37
下面是一个简单事件驱动实现Echo服务器。 事件驱动的核心就是都有一个循环来轮询socket的活跃性并执行响应。 事件驱动型I/O的一个潜在的优势在于它可以在不使用线程或进程的情况下同时处理成百上千个socket。 事件循环一次处理一个事件,不需要任何其他的并发原语参与。 事件驱动型I/O的缺点在于并没有涉及真正的并发。如果任何一个事件处理方法阻塞了或者 执行了一个耗时较长的计算,那么就会阻塞整个程序的执行进程。 对于阻塞型或者需要长时间运行的计算,可以通过将任务发送给单独的线程或进程来解决。 但是将线程和进程同事件循环进行协调需要较高的技巧。常常用concurrent.futures模块来实现。
# coding=utf-8
"""
《Python Cookbook》11.12 理解事件驱动型I/O
事件驱动I/O是一种将基本的I/O操作(即:读和写)转换成事件的技术,而我们必须在程序中去处理这种事件。
比如当socket上都到数据时,这成为一个"接收事件", 由我们提供的回调方法或函数去负责处理以此响应这个事件。
一个事件驱动框架可能会以一个基类作为起始点,实现一系列基本的事件处理方法。
:copyright: (c) 2016 by fangpeng(@beginman.cn).
:license: MIT, see LICENSE for more details.
"""
@limboinf
limboinf / malloc_and_qsort_array.c
Created July 27, 2016 09:40
《C和指针》TOP11 为一个指定的数组分配内存并排序处理。
#include <stdio.h>
#include <stdlib.h>
int comp(const void *, const void *);
int main()
{
int *array;
int n_values;
int i;