Skip to content

Instantly share code, notes, and snippets.

@ph87
ph87 / mailer.py
Last active December 23, 2015 01:49
一个发简单邮件用的脚本
#!/usr/local/bin/python2.7
#encoding:utf8
import sys
import os
import datetime
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from optparse import OptionParser
@ph87
ph87 / redis-server
Created April 3, 2014 03:01
redis-server
#!/bin/sh
# /etc/rc.d/init.d/redis-server
# From - http://www.codingsteps.com/install-redis-2-6-on-amazon-ec2-linux-ami-or-centos/
#
# redis - this script starts and stops the redis-server daemon
# Originally from - https://raw.github.com/gist/257849/9f1e627e0b7dbe68882fa2b7bdb1b2b263522004/redis-server
#
# chkconfig: - 85 15
# description: Redis is a persistent key-value database
# processname: redis-server
@ph87
ph87 / generics.py
Last active August 29, 2015 14:01
override related django rest framework's generics pagination for only get the count of result by passing param page_size = 0
# read the codes commented by ph87
def paginate_queryset(self, queryset, page_size=None):
"""
Paginate a queryset if required, either returning a page object,
or `None` if pagination is not configured for this view.
"""
deprecated_style = False
if page_size is not None:
warnings.warn('The `page_size` parameter to `paginate_queryset()` '
@ph87
ph87 / sumchecker.py
Last active August 29, 2015 14:02
Sum Check Schema for IOS 7064 Mod 11-2 (Unique Identifier for People, People's Republic of China)
code = '12346830281720938x' # check sum equal 10, maped to 'x'
sum = 0
for i in code[:-1]:
sum = ( sum + int(i, 10)) * 2
sum_mod = sum % 11
sum_check = (12 - sum_mod ) % 11
sum_check = 'x' if sum_check == 10 else str(sum_check)
assert sum_check == code[-1]
@ph87
ph87 / save_to_evernote.scpt
Last active September 11, 2016 06:39
A very simple applescript save given text to default notebook.
on run {input, parameters}
set content to (input as text)
set AppleScript's text item delimiters to "\n"
set textList to every text item of content
set theTitle to item 1 of textList
tell application "Evernote"
create note title theTitle with text content notebook "default"
synchronize
end tell
display notification "Success" with title "Save to Evernote!"
@ph87
ph87 / v2ex_309785.py
Last active October 1, 2016 10:06
A script handle the quiz from https://www.v2ex.com/t/309785
from md5 import md5
PREFIX = 'v2ex'
LETTERS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
TARGET = '3bed22f7f496e84b035a996522baa7594c27c7e5718a78bfddf9012904b70eb755d67f90c8de149ead7ee674b024f38c216642030c2d54cb1dd657dd66342c99c239f3c31fd399fc052a9b7861f2073d2b9f47811dd77fd544d570c34bf5f349d110389979571714694a5054238465ca38ba26c25fb1a32b4d0a3b93666b09b3'
prefix_md5 = md5(PREFIX).hexdigest()
import re
import logging
from logging import handlers as logging_handlers
import email
from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr
from StringIO import StringIO
logger = logging.getLogger(__name__)
@ph87
ph87 / merge_sort.py
Last active January 19, 2017 11:37
implementation of merge sorting
from unittest import (
main,
TestCase,
)
from random import Random
def merge(lst):
if len(lst) <= 1:
return lst
import sys
data = {}
for i in xrange(0, 1000000):
lst.append(i)
size = sys.getsizeof(list)
if data.get(size):
pass
else:
data[size] = i
rev_data = {i[1]: i[0] for i in data.items()}
@ph87
ph87 / vimrc
Last active April 16, 2017 16:06
保存 python 文件时执行测试
autocmd BufWritePost *.py exec ":call PyFlakeTest()"
function! PyFlakeTest()
silent exec "!flake8 % > /dev/null"
if v:shell_error
echohl ErrorMsg
echomsg "不好!有毛病!"
exec "!flake8 %"
echohl None
endif
endfunc