Skip to content

Instantly share code, notes, and snippets.

@billiepander
billiepander / [python] get_unread_email_and_download_attachments
Created March 1, 2018 09:20
python3 to download certain unread email's attatchment
import email
import imaplib
import os
from email.header import decode_header
detach_dir = '.'
if 'attachments' not in os.listdir(detach_dir):
os.mkdir('attachments')
import os
@billiepander
billiepander / [golang]iter struct & get tag
Last active August 24, 2017 07:10
useful to convert struct to map use specific tag as key
package main
import (
"fmt"
"reflect"
)
type User struct{
Name string `json:"name" bson:"b_name"`
Age int `json:"age" bson:"b_age"`
func NestedMapToFlatMap(a_map map[string]interface{}) map[string]interface{} {
new_map := map[string]interface{}{}
for key, val := range a_map{
t:=reflect.TypeOf(val)
if t.Kind() == reflect.Map{
mapval, _ := val.(map[string]interface{})
for i,j :=range mapval{
new_map[i] = j
}
} else {
@billiepander
billiepander / [golang] TaskRunner 4 timeout & signal control
Last active April 25, 2017 03:04
代码来源于微信公众号flysnow_org,添加了对于[部分]返回结果的处理
package main
import (
"errors"
"os"
"os/signal"
"time"
"log"
)
var ErrTimeOut = errors.New("执行者执行超时")
var ErrInterrupt = errors.New("执行者被中断")
import os, sys
os.execv(sys.executable, ['python'] + sys.argv)
# sys.executable: the python path you are using which will auto know if you are using virtualenv
# e.g: /home/pd/.virtualenvs/competehunt_main/bin/python' [so if it not what you want, write it manually]
# of course, sys.argv is the current file name if you donnot add extra params [yes, add it if need to]
# so ['python'] + sys.argv ==> ["python", "current.py"]
@billiepander
billiepander / child_process
Last active February 26, 2026 05:46
[Python] child process signal control
import os
import signal
import time
import sys
pid = os.getpid()
received = False
def signal_usr1(signum, frame):
package main
import (
"net/http"
httptransport "github.com/go-kit/kit/transport/http"
"os"
"github.com/go-kit/kit/log"
"github.com/go-redis/redis"
"flag"
@billiepander
billiepander / python ini & exit decorator
Last active April 21, 2017 05:47
1: Take care, as kill -9 will drop the python interpreter immediately, so if -9 happens, no way to clean up; 2. you may wonder why not use try: ... finally: ... to do cleanup, cause finally block will only get executed when inner exception happens, that means outer like kill $PID wont trigger it too, which is often used
import os
import signal
import time
import requests
from functools import partial
from redis import Redis
redis_conn = Redis(host='xxx', port=xxxx, password='xxxx')
def ini_life():
@billiepander
billiepander / app.py
Last active March 5, 2017 16:24
flask-migrate example. 1: $ python manage.py init # to create migrations folder 2: $ python manage.py migrate # generate operation file 3:$ python manage.py upgrade # run the file 4&later: run 2, 3 after field s change to makemigrations
from flask import Flask, request, jsonify
from users import User
from ext import db
app = Flask(__name__)
app.config.from_object('config')
db.init_app(app)
# with app.app_context():
@billiepander
billiepander / get_ride_of_punctuations
Created February 22, 2017 08:52
同时去掉一段话的中英文标点
from zhon import hanzi # pip install zhon
import string
from itertools import chain
a_string = a_string.translate(
str.maketrans({key: None for key in chain(hanzi.punctuation, string.punctuation)})
)