• python常用点小记


    记录一下,经常用到的一些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-10131416-37!#-[]-177]|\[01-11131416-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)]

    待续.....

  • 相关阅读:
    promise 理解
    强化学习的概念
    Ubuntu安装机器学习环境步骤
    jsp文件复制到web项目出错
    jdbc导致的问题
    C#窗体-猜数字
    软件工程结对作业01
    第二阶段冲刺10天 第3天进展报告
    第二阶段冲刺10天 第2天进展报告
    第二阶段冲刺10天 第1天进展报告
  • 原文地址:https://www.cnblogs.com/xiamuyouren/p/3297271.html
Copyright © 2020-2023  润新知