joblib ---- a Python Parallel and Function Disk Cache Tool

用途

  • 加速函数执行

    • 函数结果缓存
    • 第二次执行,直接返回缓存的结构,无需执行代码块
  • 并行运算
  • 替代 pickle

    • 更高效
  • 函数式编程

Memory 类

初始化

  • Memory(cachedir: str|None='your/given/path', verbose: int=0)
  • 参数

pickle -- Python Serialization Tool

Protocol 协议

  • 协议间不兼容

    • 老版本pickle 不能识别新版本 pickle dump 结果

函数

  • pickle.dumps

    • 参数

      • protocol: int

        • 0 ~ pickle.HIGHEST_PROTOCOL
  • pickle.loads
  • pickle.dump
  • pickle.load

注意 缺陷和陷阱

序列化失败

  • 不能序列化 __main__ 模块中的函数

Path 对象和 in 运算符

判断

  • Path.is_absolute()
  • Path.is_relative_to(*other)
  • Path.is_reserved()
  • Path.match(pattern)

    • 通配符匹配

      • eg: '*.py'
  • Path.exists()
  • Path.samefile(other: Path)
  • Path.is_dir()
  • Path.is_file()
  • Path.is_symlink()
  • Path.is_mount()
  • Path.is_socket()
  • Path.is_fifo()
  • Path.is_block_device()
  • Path.is_char_device()

修改组分(返回新 Path)

  • Path.with_name(name)

    • 替换 basename
  • Path.with_stem(stem)

    • 替换 stem (除去 suffix 部分)
  • Path.with_suffix(suffix)