from functools import wraps
from time import time

from django_statsd.clients import statsd


class UnauthorizedException(Exception):
    """Failure to log into the email server."""
    pass


class NewsletterException(Exception):
    """Error when trying to talk to the the email server."""

    def __init__(self, msg=None, error_code=None, status_code=None):
        self.error_code = error_code
        self.status_code = status_code
        super(NewsletterException, self).__init__(msg)


class NewsletterNoResultsException(NewsletterException):
    """
    No results were returned from the mail server (but the request
    didn't report any errors)
    """
    pass


def get_timer_decorator(prefix):
    """
    Decorator for timing and counting requests to the API
    """
    def decorator(f):
        @wraps(f)
        def wrapped(*args, **kwargs):
            starttime = time()
            e = None
            try:
                resp = f(*args, **kwargs)
            except NewsletterException as e:  # noqa
                pass
            except Exception:
                raise

            totaltime = int((time() - starttime) * 1000)
            statsd.timing(prefix + '.timing', totaltime)
            statsd.timing(prefix + '.{}.timing'.format(f.__name__), totaltime)
            statsd.incr(prefix + '.count')
            statsd.incr(prefix + '.{}.count'.format(f.__name__))
            if e:
                raise
            else:
                return resp

        return wrapped

    return decorator

from functools import wraps
from time import time

from django_statsd.clients import statsd


class UnauthorizedException(Exception):
    """Failure to log into the email server."""
    pass


class NewsletterException(Exception):
    """Error when trying to talk to the the email server."""

    def __init__(self, msg=None, error_code=None, status_code=None):
        self.error_code = error_code
        self.status_code = status_code
        super(NewsletterException, self).__init__(msg)


class NewsletterNoResultsException(NewsletterException):
    """
    No results were returned from the mail server (but the request
    didn't report any errors)
    """
    pass


def get_timer_decorator(prefix):
    """
    Decorator for timing and counting requests to the API
    """
    def decorator(f):
        @wraps(f)
        def wrapped(*args, **kwargs):
            starttime = time()

            def record_timing():
                totaltime = int((time() - starttime) * 1000)
                statsd.timing(prefix + '.timing', totaltime)
                statsd.timing(prefix + '.{}.timing'.format(f.__name__), totaltime)
                statsd.incr(prefix + '.count')
                statsd.incr(prefix + '.{}.count'.format(f.__name__))

            try:
                resp = f(*args, **kwargs)
            except NewsletterException:
                record_timing()
                raise

            record_timing()
            return resp

        return wrapped

    return decorator

Refactor the timing decorator to be less confusing
Refactor the timing decorator to be less confusing

Also means that we don't have to ignore a flake8 error.

  from functools import wraps
  from time import time
  
  from django_statsd.clients import statsd
  
  
  class UnauthorizedException(Exception):
      """Failure to log into the email server."""
      pass
  
  
  class NewsletterException(Exception):
      """Error when trying to talk to the the email server."""
  
      def __init__(self, msg=None, error_code=None, status_code=None):
          self.error_code = error_code
          self.status_code = status_code
          super(NewsletterException, self).__init__(msg)
  
  
  class NewsletterNoResultsException(NewsletterException):
      """
      No results were returned from the mail server (but the request
      didn't report any errors)
      """
      pass
  
  
  def get_timer_decorator(prefix):
      """
      Decorator for timing and counting requests to the API
      """
      def decorator(f):
          @wraps(f)
          def wrapped(*args, **kwargs):
              starttime = time()
-             e = None
+ 
+             def record_timing():
+                 totaltime = int((time() - starttime) * 1000)
+                 statsd.timing(prefix + '.timing', totaltime)
+                 statsd.timing(prefix + '.{}.timing'.format(f.__name__), totaltime)
+                 statsd.incr(prefix + '.count')
+                 statsd.incr(prefix + '.{}.count'.format(f.__name__))
+ 
              try:
                  resp = f(*args, **kwargs)
-             except NewsletterException as e:  # noqa
+             except NewsletterException:
+                 record_timing()
-                 pass
-             except Exception:
                  raise
  
+             record_timing()
-             totaltime = int((time() - starttime) * 1000)
-             statsd.timing(prefix + '.timing', totaltime)
-             statsd.timing(prefix + '.{}.timing'.format(f.__name__), totaltime)
-             statsd.incr(prefix + '.count')
-             statsd.incr(prefix + '.{}.count'.format(f.__name__))
-             if e:
-                 raise
-             else:
-                 return resp
+             return resp
  
          return wrapped
  
      return decorator
  
Refactor the timing decorator to be less confusing
## Code Before:
from functools import wraps
from time import time

from django_statsd.clients import statsd


class UnauthorizedException(Exception):
    """Failure to log into the email server."""
    pass


class NewsletterException(Exception):
    """Error when trying to talk to the the email server."""

    def __init__(self, msg=None, error_code=None, status_code=None):
        self.error_code = error_code
        self.status_code = status_code
        super(NewsletterException, self).__init__(msg)


class NewsletterNoResultsException(NewsletterException):
    """
    No results were returned from the mail server (but the request
    didn't report any errors)
    """
    pass


def get_timer_decorator(prefix):
    """
    Decorator for timing and counting requests to the API
    """
    def decorator(f):
        @wraps(f)
        def wrapped(*args, **kwargs):
            starttime = time()
            e = None
            try:
                resp = f(*args, **kwargs)
            except NewsletterException as e:  # noqa
                pass
            except Exception:
                raise

            totaltime = int((time() - starttime) * 1000)
            statsd.timing(prefix + '.timing', totaltime)
            statsd.timing(prefix + '.{}.timing'.format(f.__name__), totaltime)
            statsd.incr(prefix + '.count')
            statsd.incr(prefix + '.{}.count'.format(f.__name__))
            if e:
                raise
            else:
                return resp

        return wrapped

    return decorator

## Instruction:
Refactor the timing decorator to be less confusing
## Code After:
from functools import wraps
from time import time

from django_statsd.clients import statsd


class UnauthorizedException(Exception):
    """Failure to log into the email server."""
    pass


class NewsletterException(Exception):
    """Error when trying to talk to the the email server."""

    def __init__(self, msg=None, error_code=None, status_code=None):
        self.error_code = error_code
        self.status_code = status_code
        super(NewsletterException, self).__init__(msg)


class NewsletterNoResultsException(NewsletterException):
    """
    No results were returned from the mail server (but the request
    didn't report any errors)
    """
    pass


def get_timer_decorator(prefix):
    """
    Decorator for timing and counting requests to the API
    """
    def decorator(f):
        @wraps(f)
        def wrapped(*args, **kwargs):
            starttime = time()

            def record_timing():
                totaltime = int((time() - starttime) * 1000)
                statsd.timing(prefix + '.timing', totaltime)
                statsd.timing(prefix + '.{}.timing'.format(f.__name__), totaltime)
                statsd.incr(prefix + '.count')
                statsd.incr(prefix + '.{}.count'.format(f.__name__))

            try:
                resp = f(*args, **kwargs)
            except NewsletterException:
                record_timing()
                raise

            record_timing()
            return resp

        return wrapped

    return decorator
