Skip to content

Instantly share code, notes, and snippets.

View selfboot's full-sized avatar

selfboot selfboot

View GitHub Profile
@selfboot
selfboot / update_gfwlist.sh
Created August 5, 2016 01:26 — forked from VincentSit/update_gfwlist.sh
Automatically update the PAC for ShadowsocksX. Only tested on OS X.
#!/bin/bash
# update_gfwlist.sh
# Author : VincentSit
# Copyright (c) http://xuexuefeng.com
#
# Example usage
#
# ./whatever-you-name-this.sh
#
# Task Scheduling (Optional)
@selfboot
selfboot / simple_web_server.py
Created July 25, 2016 07:43
A simple web server.
import socket
HOST, PORT = '', 8000
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
listen_socket.listen(1)
print 'Serving HTTP on port %s ...' % PORT
while True:
@selfboot
selfboot / natural_time.py
Created July 16, 2016 12:39
Returns string representing "time since", 3 days ago, 5 hours ago etc. For datetime values, returns a string representing how many seconds, minutes or hours ago it was – falling back to the timesince format if the value is more than a day old.
from datetime import datetime
@app.template_filter()
def natural_time(dt, default="just now"):
"""
Returns string representing "time since" e.g.
3 days ago, 5 hours ago etc.
"""
now = datetime.utcnow()
@selfboot
selfboot / shared_ptr_demo.cpp
Last active July 16, 2016 07:54
shared_ptr名如其名,它允许多个该智能指针共享地“拥有”同一堆分配对象的内存;由于它的资源是可以共用的,所以也就可以透过operator=等方法,来分享shared_ptr所使用的资源。由于shared_ptr内部实现上使用的是引用计数这种方法,所以一旦一个shared_ptr指针放弃了“所有权”,其它的shared_ptr对对象的引用并不会发生变化;只有在引用计数归零的时候,shared_ptr才会真正的释放所占有的堆内存空间的。
#include <iostream>
#include <memory>
using namespace std;
void Func1(shared_ptr<int> a)
{
cout<<"Enter Func1"<<endl;
cout<<"Ref count: "<<a.use_count()<<endl;
cout<<"Leave Func1"<<endl;
}
#include <iostream>
using namespace std;
int main()
{
unique_ptr<int> up1(new int(10)); // 不能复制的unique_ptr
// unique_ptr<int> up2 = up1; // 这样是错的
cout<<*up1<<endl;
unique_ptr<int> up3 = move(up1); // 现在up3是数据唯一的unique_ptr智能指针
@selfboot
selfboot / malloc_demo.cpp
Created July 16, 2016 05:49
The function malloc is used to allocate a certain amount of memory during the execution of a program. The malloc function will request a block of memory from the heap. If the request is granted, the operating system will reserve the requested amount of memory. When the amount of memory is not needed anymore, you must return it to the operating s…
#include<stdio.h>
#include <cstdlib>
int main()
{
int *ptr_one;
ptr_one = (int *)malloc(sizeof(int));
if (ptr_one == 0)
@selfboot
selfboot / greenlet_demo.py
Created July 4, 2016 12:00
greenlet simple demos.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Last Modified time: 2016-07-04 19:58:30
from greenlet import greenlet
def test1():
print "gr1 ", greenlet.getcurrent()
@selfboot
selfboot / multi_thread.py
Created June 28, 2016 02:51
对于IO密集型任务来说,多线程仍然有一定的效果。因为在I/O请求的时候,当一个线程因I/O请求阻塞,可以调用另一个线程来继续发起对另一个Url的请求。当第一个线程的I/O请求得到回应时,它再次得到运行。
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Last Modified time: 2016-06-28 10:50:58
import urllib2
import time
from threading import Thread
# 在大量访问url的时候,多线程很有优势,实际上也是空间换时间
URLs = ["http://www.baidu.com",
@selfboot
selfboot / speedtest.py
Created June 27, 2016 09:15
Simple website speed test tools.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Last Modified time: 2016-06-27 17:03:42
import requests
import os
def get_speed_test(src_path, result_path, timeout=10):
all_sites = _get_sites_(src_path)
results = []
@selfboot
selfboot / mobileadapt.py
Created June 27, 2016 09:14
Check whether the site is mobile friendly using the query interface of bing.com. Can check many sites at the same time asynchronously with gevent.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Last Modified time: 2016-06-27 17:03:25
from gevent import monkey
monkey.patch_all()
import time
import requests
from lxml import html as HTML
import gevent
from os.path import isfile