Skip to content

Instantly share code, notes, and snippets.

View dongguosheng's full-sized avatar

董国盛 dongguosheng

  • Sogou
  • Beijing
View GitHub Profile
@dongguosheng
dongguosheng / multi-threading_simple.py
Last active December 19, 2015 17:19
a simple multi-threading in Python
import threading
import urllib
import time
class Mythread(threading.Thread):
'''Download pictures.'''
def __init__(self, url):
threading.Thread.__init__(self)
self.url = url
print 'init thread.'
@dongguosheng
dongguosheng / multi-threading_queue.py
Last active December 19, 2015 17:19
multi-threading with Queue in Python
import threading
import urllib
import Queue
import time
class Mythread(threading.Thread):
'''Download pictures.'''
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
@dongguosheng
dongguosheng / test.py
Last active December 19, 2015 17:19
a simple threadpool in Python
import time
import threadpool
starttime = time.time()
def job(args):
print args[0]
print args[1]
def main():
@dongguosheng
dongguosheng / quicksort.c
Last active December 29, 2015 15:29
A quicksort implementation collection in C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void qsort_last(int s[], int start_index, int end_index); //select the last element as the pivot
void qsort_first(int s[], int start_index, int end_index); //select the first element as the pivot
void qsort_mid(int s[], int start_index, int end_index); //select the middle element as the pivot
void qsort_rand(int s[], int start_index, int end_index); //select a random index's value as the pivot
void qsort_mot(int s[], int start_index, int end_index); //median of three partitioning + insertion sort
void qsort_clrs(int s[], int start_index, int end_index); //select the last element as the pivot, CLRS version
@dongguosheng
dongguosheng / MyString.cpp
Last active December 31, 2015 06:19
A string class implemented to practice C++.
#include "MyString.h"
MyString::MyString(void) : capacity(INITIAL_CAPACITY), size(0)
{
s_ptr = new char[INITIAL_CAPACITY];
*s_ptr = '\0';
}
MyString::~MyString(void)
{
@dongguosheng
dongguosheng / crawler.py
Last active August 2, 2017 09:21
A simple crawler based on requests and pyquery.
# -*- coding: utf-8 -*-
'''
1. Construct the url with num_iid, eg: http://a.m.tmall.com/i15110720150.htm, 15110720150 is the num_iid.
2. Get the html text.
3. Parse the img urls and insert the num_iid and img urls into sqlite.
'''
import requests
from pyquery import PyQuery as pq
@dongguosheng
dongguosheng / AutoSwitch.py
Last active January 3, 2016 08:29
TJU更改pppoe后查询流量和账户余额,以及切换账户的小脚本。顺便也体验了一下getter和setter的语法糖和yaml。 之前为固定IP时,切换账户的同时需要切换IP以及mac地址,当时粗暴地使用os.system+cmd和_winreg模块拼凑而成。使用cPickle存储。 运行环境为win server 2003,使用pyinstaller-2.0做成单exe。
# -*- coding: utf-8 -*-
import requests
from pyquery import PyQuery as pq
import yaml
from hashlib import md5
from datetime import datetime
import smtplib
from email.mime.text import MIMEText
@dongguosheng
dongguosheng / TestJsoup.java
Last active August 29, 2015 13:56
第一次使用jsoup
package test_jsoup.test_jsoup;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class App
{
public static void main( String[] args ) throws Exception
@dongguosheng
dongguosheng / StringUtils.cpp
Last active September 8, 2015 03:15
C++ utils
#include <string>
#include <vector>
#include <sstream>
using std::vector;
using std::string;
using std::stringstream;
vector<string> &split(const string &s, char delim, vector<string> &result) {
stringstream ss(s);
@dongguosheng
dongguosheng / df_mp.py
Last active August 29, 2015 14:05
Hadoop Streaming计算tfidf
#! /usr/bin/python
import sys
def read_input(file):
'''
read input tf mapper-reducer file
class:Queries\tword_1:val_1,word_2:val_2,.....
'''
for line in file: