GithubHelp home page GithubHelp logo

andreipashkin / python-tee Goto Github PK

View Code? Open in Web Editor NEW

This project forked from algrebe/python-tee

0.0 1.0 0.0 14 KB

Python library to tee stderr / stdout to a file

License: MIT License

Python 100.00%

python-tee's Introduction

python-tee

Build Status PyPI version

Python library to tee stderr / stdout to a file

Installation

pip install tee

Quick Start

tee_test.py

import sys
from tee import StdoutTee, StderrTee

with StdoutTee("mystdout.txt"), StderrTee("mystderr.txt"):
    sys.stdout.write("[stdout] hello\n")
    sys.stderr.write("[stderr] hello\n")
    sys.stdout.write("[stdout] world\n")
    sys.stderr.write("[stderr] world\n")

sys.stdout.write("[stdout] not going to be written to file\n")
sys.stderr.write("[stderr] not going to be written to file\n")
$ python tee_test.py

[stdout] hello
[stderr] hello
[stdout] world
[stderr] world
[stdout] not going to be written to file
[stderr] not going to be written to file

$ cat mystdout.txt 
[stdout] hello
[stdout] world

$ cat mystderr.txt 
[stderr] hello
[stderr] world

Filters

StdoutTee and StderrTee take filters as parameters which run before writing to a file or the stream. These filters are callables that take the message to be written as input and return either None or a new message.

I find them particularly useful when you want to write colorized output to the stream, but strip out the control characters when writing to a file, especially when using fabric.

import re
import tee
from fabric.api import run

def _remove_control_chars(message):
    return re.sub(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]', "", message)

def echo_color():
    with tee.StdoutTee("fabout.txt", mode="a", file_filters=[_remove_control_chars]):
        run("""echo -e "\E[1;32mHello World \E[4;31mLets add some\E[0m\E[1;34m color" && tput sgr0""")
fab -H localhost echo_color

python-tee's People

Contributors

algrebe avatar sigiesec avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.