Skip to content

Commit

Permalink
Merge pull request #9 from Wenzhi-Ding/dev
Browse files Browse the repository at this point in the history
Resolve #8
  • Loading branch information
Wenzhi-Ding authored Oct 11, 2022
2 parents 57337d9 + 8e8cd4d commit 095678e
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 10 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This is definitely an easy function, I believe many guys have written their own

## Installation
```bash
pip install py_reminder==1.0.1
pip install py_reminder==1.0.2
```

## Initial configuration
Expand Down Expand Up @@ -51,6 +51,17 @@ If you want to specify the receiver:
@monitor(task='This is a task', to='[email protected]')
```

If you would like to only send email when error caught:
```python
@monitor(task='This is a task', mute_success=True)
```

If you would like to switch on and off the notification as a whole:
```python
arg = True
@monitor(task='This is a task', disable=arg)
```

Another way to use it without decorator:
```python
from py_reminder import send_email
Expand Down
2 changes: 1 addition & 1 deletion py_reminder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

name = "py_reminder"

__version__ = "1.0.1"
__version__ = "1.0.2"

__doc__ = """
py_reminder: a simple decorator help you monitor your task progress.
Expand Down
8 changes: 5 additions & 3 deletions py_reminder/py_reminder.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def send_email(task, time_start="",args=None, kwargs=None, error='', to=''):
del msg


def monitor(task='Your Task', to=''):
def monitor(task='Your Task', to='', mute_success=False, disable=False):
'''
This is a decorator to monitor the progress/status of your task.
Refer to sample code if you don't know how to use decorator.
Expand Down Expand Up @@ -127,10 +127,12 @@ def wrapper_decorator(*args, **kwargs):
ts = timer()
try:
value = func(*args, **kwargs)
send_email(task=task, time_start=ts, args=args, kwargs=kwargs, error='', to=to)
if not disable and not mute_success:
send_email(task=task, time_start=ts, args=args, kwargs=kwargs, error='', to=to)
return value
except Exception as e:
logger.error(e, exc_info=True)
send_email(task=task, time_start=ts, args=args, kwargs=kwargs, error='%s\n%s\n%s' % sys.exc_info(), to=to)
if not disable:
send_email(task=task, time_start=ts, args=args, kwargs=kwargs, error='%s\n%s\n%s' % sys.exc_info(), to=to)
return wrapper_decorator
return decorator
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[project]
name = "py_reminder"
version = "1.0.1"
version = "1.0.2"
authors = [
{ name="Wenzhi Ding", email="[email protected]" },
]
description = "A convenient email sender for monitoring your task"
readme = "README.md"
requires-python = ">=3.7"
requires-python = ">=3.6"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
Expand Down
1 change: 0 additions & 1 deletion requirements.txt

This file was deleted.

15 changes: 15 additions & 0 deletions tests/disable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from py_reminder import monitor

@monitor('This success should be disabled', disable=True)
def foo(a=3, b=4):
return a + b

@monitor('This error should be disabled', disable=True)
def err_foo(a=3, b=4):
raise Exception('This is an error')

r = foo(4, b=1)
print(r)

r = err_foo(4, b=1)
print(r)
6 changes: 4 additions & 2 deletions tests/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def foo(a=3, b=4):
def err_foo(a=3, b=4):
raise Exception('This is an error')

foo(4, b=1)
r = foo(4, b=1)
print(r)

err_foo(4, b=1)
r = err_foo(4, b=1)
print(r)
15 changes: 15 additions & 0 deletions tests/mute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from py_reminder import monitor

@monitor('This success should be muted', mute_success=True)
def foo(a=3, b=4):
return a + b

@monitor('This error should not be disabled', mute_success=True)
def err_foo(a=3, b=4):
raise Exception('This is an error')

r = foo(4, b=1)
print(r)

r = err_foo(4, b=1)
print(r)

0 comments on commit 095678e

Please sign in to comment.