Skip to content

Instantly share code, notes, and snippets.

@cleiveliu
Last active August 13, 2021 03:19
Show Gist options
  • Select an option

  • Save cleiveliu/ed5ecfc7dc074a85ccf3d11cd38ea827 to your computer and use it in GitHub Desktop.

Select an option

Save cleiveliu/ed5ecfc7dc074a85ccf3d11cd38ea827 to your computer and use it in GitHub Desktop.

Revisions

  1. cleiveliu revised this gist Mar 30, 2021. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions Python-context-manager-example.md
    Original file line number Diff line number Diff line change
    @@ -4,22 +4,23 @@
    import contextlib
    import sys

    log_file = "/var/log/your.log"

    def your_task():
    print("The test !")


    @contextlib.contextmanager
    def close_stdout():
    def close_stdout(redirect_to="/var/log/your.log"):
    raw_stdout = sys.stdout
    file = open(log_file, 'a+')
    file = open(redirect_to, "a+")
    sys.stdout = file

    yield

    sys.stdout = raw_stdout
    file.close()

    with close_stdout():

    with close_stdout(redirect_to="./test.log"):
    your_task()
    ```
  2. cleiveliu revised this gist Feb 5, 2021. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion Python-context-manager-example.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    # http://magic.iswbm.com/zh/latest/c05/c05_09.html
    [source](http://magic.iswbm.com/zh/latest/c05/c05_09.html)

    ```Python
    import contextlib
    import sys
  3. cleiveliu revised this gist Feb 5, 2021. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions Python-context-manager-example.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,12 @@
    # http://magic.iswbm.com/zh/latest/c05/c05_09.html
    ```Python
    import contextlib
    import sys

    log_file="/var/log/you.log"
    log_file = "/var/log/your.log"

    def you_task():
    pass
    def your_task():
    print("The test !")

    @contextlib.contextmanager
    def close_stdout():
    @@ -19,5 +20,5 @@ def close_stdout():
    file.close()

    with close_stdout():
    you_task()
    your_task()
    ```
  4. cleiveliu renamed this gist Feb 5, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. cleiveliu created this gist Feb 5, 2021.
    23 changes: 23 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    # http://magic.iswbm.com/zh/latest/c05/c05_09.html
    ```Python
    import contextlib

    log_file="/var/log/you.log"

    def you_task():
    pass

    @contextlib.contextmanager
    def close_stdout():
    raw_stdout = sys.stdout
    file = open(log_file, 'a+')
    sys.stdout = file

    yield

    sys.stdout = raw_stdout
    file.close()

    with close_stdout():
    you_task()
    ```