Skip to content
This repository has been archived by the owner on Dec 4, 2022. It is now read-only.

Commit

Permalink
Remove f-strings to become python3.4 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurcerveira committed Mar 17, 2020
1 parent de0ef12 commit 453d037
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Total execution time: 00:00:00

## Supported versions

- Python 3.6 and above
- Python 3.4 and above

## Lincense

Expand Down
24 changes: 14 additions & 10 deletions email_function_logger/function_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io

from datetime import datetime
from contextlib import redirect_stdout, redirect_stderr
from contextlib import redirect_stdout
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

Expand All @@ -21,31 +21,35 @@ def log_function(*args, **kwargs):
arguments = get_function_arguments(args, kwargs)

# Add subject to email
subject = f"Function '{function.__name__}' execution log"
subject = "Function '{}' execution log".format(function.__name__)
message['Subject'] = subject

# Start email body text
text = f"Function {function.__name__}({arguments}) finished its execution.\n\n"
text = "Function {}({}) finished its execution.\n\n".format(
function.__name__, arguments)

start_time = datetime.now()
text += f"Start time: {start_time:%b %d %H:%M:%S}\n"
text += "Start time: {0:%b %d %H:%M:%S}\n".format(start_time)

f = io.StringIO()
with redirect_stdout(f):
return_value = function(*args, **kwargs)

text_output = f.getvalue()

text += f'Function text output:\n{text_output}' if text_output else 'No text output\n'
text += f'Function returned: {return_value}\n' if return_value else 'No returned value\n'
text += 'Function text output:\n{}'.format(
text_output) if text_output else 'No text output\n'
text += 'Function returned: {}\n'.format(
return_value) if return_value else 'No returned value\n'

end_time = datetime.now()
text += f"End time: {end_time:%b %d %H:%M:%S}\n"
text += "End time: {0:%b %d %H:%M:%S}\n".format(end_time)

total = (end_time - start_time).seconds
hours, remainder = divmod(total, 3600)
minutes, seconds = divmod(remainder, 60)
text += f'\nTotal execution time: {hours:02d}:{minutes:02d}:{seconds:02d}\n'
text += '\nTotal execution time: {0:02d}:{1:02d}:{2:02d}\n'.format(
hours, minutes, seconds)

# Add body to email
message.attach(MIMEText(text, 'plain'))
Expand All @@ -59,7 +63,7 @@ def log_function(*args, **kwargs):

def get_email_info():
for var in ENVIRON_VAR:
yield os.environ.get(var) if os.environ.get(var) else input(f'{var}: ')
yield os.environ.get(var) if os.environ.get(var) else input('{}: '.format(var))


def authenticate_email(email, password):
Expand All @@ -80,7 +84,7 @@ def create_message(email, recipient):

def get_function_arguments(args, kwargs):
args_repr = [repr(a) for a in args]
kwargs_repr = [f"{k}={v!r}" for k, v in kwargs.items()]
kwargs_repr = ["{0}={1!r}".format(k, v) for k, v in kwargs.items()]
arguments = ", ".join(args_repr + kwargs_repr)

return arguments
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="email-function-logger",
version="0.3",
version="0.4",
author="Arthur Cerveira",
author_email="aacerveira@inf.ufpel.edu.br",
description="A decorator to log information about a function and send it to your email",
Expand All @@ -20,5 +20,5 @@
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
python_requires='>=3.4',
)

0 comments on commit 453d037

Please sign in to comment.