Skip to content

Instantly share code, notes, and snippets.

View yaobinwen's full-sized avatar

Yaobin Wen yaobinwen

View GitHub Profile
@yaobinwen
yaobinwen / lockfile.py
Created June 25, 2017 23:01 — forked from ionrock/lockfile.py
A file locking example
"""
A file lock implementation that tries to avoid platform specific
issues. It is inspired by a whole bunch of different implementations
listed below.
- https://bitbucket.org/jaraco/yg.lockfile/src/6c448dcbf6e5/yg/lockfile/__init__.py
- http://svn.zope.org/zc.lockfile/trunk/src/zc/lockfile/__init__.py?rev=121133&view=markup
- http://stackoverflow.com/questions/489861/locking-a-file-in-python
- http://www.evanfosmark.com/2009/01/cross-platform-file-locking-support-in-python/
- http://packages.python.org/lockfile/lockfile.html
@yaobinwen
yaobinwen / module_watcher.py
Created September 26, 2016 18:15 — forked from eberle1080/module_watcher.py
Automatically reload python module / package on file change
#!/usr/bin/env python
# Author: Chris Eberle <eberle1080@gmail.com>
# Watch for any changes in a module or package, and reload it automatically
import pyinotify
import imp
import os
class ModuleWatcher(pyinotify.ProcessEvent):
"""
@yaobinwen
yaobinwen / find_and_delete_bak_files.sh
Created September 2, 2016 15:37
Find backup files recursively in a directory and delete them
#!/bin/bash
# In this case, the backup file names end up with '~'.
for f in $(find . -print | grep -i '~$'); do rm $f; done
@yaobinwen
yaobinwen / cube.mtl
Created August 2, 2016 18:10 — forked from noonat/cube.mtl
Cube OBJ and MTL file
newmtl cube
Ns 10.0000
Ni 1.5000
d 1.0000
Tr 0.0000
Tf 1.0000 1.0000 1.0000
illum 2
Ka 0.0000 0.0000 0.0000
Kd 0.5880 0.5880 0.5880
Ks 0.0000 0.0000 0.0000
@yaobinwen
yaobinwen / send_mail.py
Created July 7, 2016 21:49 — forked from vjo/send_mail.py
[Python] Send email with embedded image and application attachment
#! /usr/bin/python
import smtplib
from optparse import OptionParser
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
@yaobinwen
yaobinwen / bash.py
Created March 3, 2016 16:30 — forked from kirpit/bash.py
Enables to run subprocess commands in a different thread with TIMEOUT option!
#! /usr/bin/env python
import threading
import subprocess
import traceback
import shlex
class Command(object):
"""
Enables to run subprocess commands in a different thread with TIMEOUT option.
@yaobinwen
yaobinwen / trie.cpp
Created November 5, 2015 18:38
Trie tree implementation
// https://leetcode.com/problems/implement-trie-prefix-tree/
// Implement a trie with insert, search, and startsWith methods.
//
// Note:
// You may assume that all inputs are consist of lowercase letters a-z.
class TrieNode {
private:
char ch;
std::map<char, TrieNode *> next;
@yaobinwen
yaobinwen / tree_dfs.cpp
Created November 2, 2015 04:59
Depth-first search in a binary tree
// Refer to: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
bool dfs_node(TreeNode * root, TreeNode * node, std::vector<TreeNode *> & path) {
if (!root) {
return false;
}
path.push_back(root);
if (root == node) {
@yaobinwen
yaobinwen / convert_str.cpp
Created October 23, 2015 16:43
C++: Convert a string into a number
#include <sstream>
template < typename _Ty >
_Ty convert_str(const std::string & str) {
_Ty ret = _Ty();
std::stringstream ss;
ss << str;
ss >> ret;
return ret;
}
@yaobinwen
yaobinwen / split.cpp
Created October 23, 2015 16:36
C++: Split a string according to the given separator char.
std::vector<std::string> split_str(std::string & str, const char sep = ' ') {
std::vector<std::string> strs;
size_t pos = 0;
while (pos != (size_t)(-1)) {
pos = str.find_first_of(sep);
strs.push_back(str.substr(0, pos));
str = str.substr(pos+1);
}