python 3.4 vc++编译配置_Python 3.4+:扩展pathlib.Path

以下代码是我首先尝试的,但some_path.with_suffix('.jpg')明显返回pathlib.PosixPath对象(我在Linux上)而不是我的PosixPath版本,因为我没有重新定义with_suffix。我是否需要复制pathlib中的所有内容,或者有更好的方法吗?Python 3.4+:扩展pathlib.Path

import os

import pathlib

from shutil import rmtree

class Path(pathlib.Path):

def __new__(cls, *args, **kwargs):

if cls is Path:

cls = WindowsPath if os.name == 'nt' else PosixPath

self = cls._from_parts(args, init=False)

if not self._flavour.is_supported:

raise NotImplementedError("cannot instantiate %r on your system"

% (cls.__name__,))

self._init()

return self

def with_stem(self, stem):

"""

Return a new path with the stem changed.

The stem is the final path component, minus its last suffix.

"""

if not self.name:

raise ValueError("%r has an empty name" % (self,))

return self._from_parsed_parts(self._drv, self._root,

self._parts[:-1] + [stem + self.suffix])

def rmtree(self, ignore_errors=False, οnerrοr=None):

"""

Delete the entire directory even if it contains directories/files.

"""

rmtree(str(self), ignore_errors, onerror)

class PosixPath(Path, pathlib.PurePosixPath):

__slots__ =()

class WindowsPath(Path, pathlib.PureWindowsPath):

__slots__ =()

2014-11-15

Joschua

+0

也许有一个函数装饰器转换'pathlib.Path'结果到你的'Path'类,然后使用'__metaclass__'或类装饰器将这个装饰器应用到所有的类方法。 –

+1

我不明白你为什么需要这样做。 'with_suffix()'调用'_from_parsed_pa​​rts()',调用'object .__ new __(cls)'。 'cls'是你的自定义类,而不是'pathlib'中的任何东西,所以我没有看到你在这里最终得到了一个'pathlib'类。有人有主意吗?也许OP需要重写'__repr __()'来看看区别? –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值