python clint.textui.maxwidth with contextmanager

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python clint.textui.maxwidth with contextmanager相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from clint.textui import max_width
from contextlib import contextmanager

MAXWIDTH = None

def _maxwidth(width):
    global MAXWIDTH
    MAXWIDTH = width

def maxwidth(width):
    _maxwidth(width)
    return _width_context()

def puts(string):
    if MAXWIDTH is None:
        print(string)
    else:
        print(max_width(string, cols=MAXWIDTH))

@contextmanager
def _width_context():
    try:
        yield
    finally:
        MAXWIDTH = None


def main():
    """The output is::
        $ python indent.py
        hello
        world
        hello world
    """
    with maxwidth(6):
        puts('hello world')

    with maxwidth(20):
        puts('hello world')



if __name__ == '__main__':
    main()

以上是关于python clint.textui.maxwidth with contextmanager的主要内容,如果未能解决你的问题,请参考以下文章

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python

python初识