Skip to content

Instantly share code, notes, and snippets.

View selfboot's full-sized avatar

selfboot selfboot

View GitHub Profile
@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 / 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)
#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 / 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;
}
@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 / 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 / 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_server.py
Created August 7, 2016 03:27
实现了 WSGI 的简单 Web 服务器。
# Tested with Python 2.7.9, Linux & Mac OS X
import socket
import StringIO
import sys
class WSGIServer(object):
address_family = socket.AF_INET
socket_type = socket.SOCK_STREAM
@selfboot
selfboot / Google_apac_2017_1_A.cpp
Created August 22, 2016 00:34
The Constitution of a certain country states that the leader is the person with the name containing the greatest number of different alphabet letters. (The country uses the uppercase English alphabet from A through Z.) For example, the name GOOGLE has four different alphabet letters: E, G, L, and O. The name APAC CODE JAM has eight different let…
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
//ifstream in_file("/Users/feizhao/Desktop/A-small-practice.in");
ifstream in_file("/Users/feizhao/Desktop/A-large-practice.in");
//ifstream in_file("/Users/feizhao/Desktop/demo.in");
@selfboot
selfboot / scroll_toc.js
Last active August 27, 2016 01:19
目录滑动悬浮
$(function() {
var fix = $('#toc'); //滚动悬浮块
var fixTop = fix.offset().top, //滚动悬浮块与顶部的距离
fixHeight = fix.height(); //滚动悬浮块高度
$(window).scroll(function() {
$(":header").each(function() {
var id = $(this).attr('id');
// 跳过标题
if (typeof id != 'undefined'){