https://github.com/gabrielemaurina/capturepy
Capture the standard output and the standard error of functions, methods and any blocks of code.
https://github.com/gabrielemaurina/capturepy
capture decorator stderr stdout
Last synced: 7 months ago
JSON representation
Capture the standard output and the standard error of functions, methods and any blocks of code.
- Host: GitHub
- URL: https://github.com/gabrielemaurina/capturepy
- Owner: GabrieleMaurina
- License: mit
- Created: 2020-12-10T08:57:55.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2020-12-12T15:21:33.000Z (almost 5 years ago)
- Last Synced: 2025-03-06T01:36:13.165Z (7 months ago)
- Topics: capture, decorator, stderr, stdout
- Language: Python
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# capturepy
Capturepy captures the standard output and the standard error of functions, methods and any blocks of code. It is useful when testing, debugging and working with libraries.
### Install
`pip install capturepy`
It requires python 3.8 or higher.
### Usage
See the examples.py file for a complete list of examples.
This module is based on the class Capture, which can be use as a decorator, or with the 'with' statement.
Here are some examples:
##### Capturing output of function
```python
from capturepy import Capture
print('Capture stdout and stderr of function using the Capture decorator.')@Capture
def my_func(x):
print('first')
print('second', end='', file=sys.stderr)
return x**2output, result = my_func(4)
print('The captured output is:')
print(output)
print('The function returned:')
print(result)
```##### Capturing only standard error of code using "with" statement
```python
from capturepy import Capture
print('Capture only stderr of code using the "with" statement.')with Capture(stdout=False) as capture:
print('first')
print('second', end='', file=sys.stderr)
stderr = capture.get()print('The captured stderr is:')
print(stderr)
```