记录一下,经常用到的一些python小工具
1.验证一个字符串是否为url
regex = re.compile( r'^(?:http|ftp)s?://' # http:// or https:// r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?.)+(?:[A-Z]{2,6}.?|[A-Z0-9-]{2,}.?)|' #domain... r'localhost|' #localhost... r'd{1,3}.d{1,3}.d{1,3}.d{1,3})' # ...or ip r'(?::d+)?' # optional port r'(?:/?|[/?]S+)$', re.IGNORECASE)
2.地址是否为有效的email地址
email_re = re.compile( r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom # quoted-string, see also http://tools.ietf.org/html/rfc2822#section-3.2.5 r'|^"([ 01- 10 13 14 16- 37!#-[]-177]|\[ 01- 11 13 14 16-177])*"' r')@(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?.)+[A-Z]{2,6}.?$', re.IGNORECASE)
3.检测目录下的文件是否发生变化
_mtimes = {} _win = (sys.platform == "win32") def code_changed(): global _mtimes, _win for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())): if filename.endswith(".pyc") or filename.endswith(".pyo"): filename = filename[:-1] if not os.path.exists(filename): continue # File might be in an egg, so it can't be reloaded. stat = os.stat(filename) mtime = stat.st_mtime if _win: mtime -= stat.st_ctime if filename not in _mtimes: _mtimes[filename] = mtime continue if mtime != _mtimes[filename]: _mtimes = {} return True return False
4.python切分队列
if __name__ == '__main__': a=[1,2,3,4,5] print [a[i:i+4] for i in range(0,len(a),4)]
待续.....