其他分享
首页 > 其他分享> > 如何创建一个类似字符串的类?

如何创建一个类似字符串的类?

作者:互联网

我有一个上下文管理器,它将输出捕获到一个字符串,用于在with语句下缩进的代码块.此上下文管理器生成一个自定义结果对象,当块完成执行时,该对象将包含捕获的输出.

from contextlib import contextmanager

@contextmanager
def capturing():
    "Captures output within a 'with' block."
    from cStringIO import StringIO

    class result(object):
        def __init__(self):
            self._result = None
        def __str__(self):
            return self._result

    try:
        stringio = StringIO()
        out, err, sys.stdout, sys.stderr = sys.stdout, sys.stderr, stringio, stringio
        output = result()
        yield output
    finally:
        output._result, sys.stdout, sys.stderr = stringio.getvalue(), out, err
        stringio.close()

with capturing() as text:
    print "foo bar baz",

print str(text)   # prints "foo bar baz"

当然,我不能只返回一个字符串,因为字符串是不可变的,因此用户从with语句返回的字符串在代码块运行后无法更改.但是,在使用str之后显式地将结果对象转换为字符串是一种拖累(我还使用了将对象调用为一点语法糖).

那么是否可以使结果实例像字符串一样,因为它确实在命名时返回一个字符串?我尝试实现__get__,但似乎只适用于属性.或者我想要的是不可能的?

解决方法:

如何创建一个类似字符串的类?
子类str

import os
class LikeAStr(str):
    '''Making a class like a str object; or more precisely
    making a str subclass with added contextmanager functionality.'''

    def __init__(self, diff_directory):
        self._iwd = os.getcwd()
        self._cwd = diff_directory

    def __enter__(self):
        return self

    def __exit__(self, ext_typ, exc_value, traceback):
        try: os.chdir(self._iwd) # might get deleted within the "with" statement
        except: pass

    def __str__(self):
        return self._cwd

    def __repr__(self):
        return repr(self._cwd)


astr = LikeAStr('C:\\')

with LikeAStr('C:\\') as astr:
    print 1, os.getcwd()
    os.chdir( astr ) # expects str() or unicode() not some other class
    print 2, os.getcwd()
    #

# out of with block
print 3, os.getcwd()
print 4, astr == 'C:\\'

输出:

1 D:\Projects\Python\
2 C:\
3 D:\Projects\Python\
4 True

标签:python,string,with-statement
来源: https://codeday.me/bug/20190721/1497351.html