
import json
from flask import Blueprint, request, abort
from .utils import JSONError
from .calc import calculate_scores


api = Blueprint('api', __name__, url_prefix='/v1')


class ScoresResponse(object):
    def __init__(self, scores, errors):
        self.scores = scores
        self.errors = errors

    def to_json(self):
        return {'scores': self.scores, 'errors': list(self.errors)}


@api.route('/score', methods=['POST'])
def score():
    """Calculate the score for a given result set."""

    try:
        results = json.loads(request.data)['results']
    except (ValueError, KeyError):
        abort(400)

    if not isinstance(results, list):
        abort(400)

    try:
        results = calculate_scores(results)
    except Exception as err:
        return JSONError('CALCULATION_ERROR', code=-1, message=str(err))\
            .to_error()

    return ScoresResponse(*results)


import json
from flask import Blueprint, request, abort
from .utils import JSONError
from .calc import calculate_scores


api = Blueprint('api', __name__, url_prefix='/v1')


class ScoresResponse(object):
    def __init__(self, scores, errors):
        self.scores = scores
        self.errors = errors

    @property
    def sorted_scores(self):
        # Sort by descending by value
        return dict(sorted(self.scores.items(), key=lambda x: -x[1]))

    def to_json(self):
        return {'scores': self.sorted_scores, 'errors': list(self.errors)}


@api.route('/score', methods=['POST'])
def score():
    """Calculate the score for a given result set."""

    try:
        results = json.loads(request.data)['results']
    except (ValueError, KeyError):
        abort(400)

    if not isinstance(results, list):
        abort(400)

    try:
        results = calculate_scores(results)
    except Exception as err:
        return JSONError('CALCULATION_ERROR', code=-1, message=str(err))\
            .to_error()

    return ScoresResponse(*results)

Return sorted values for scores
Return sorted values for scores

  
  import json
  from flask import Blueprint, request, abort
  from .utils import JSONError
  from .calc import calculate_scores
  
  
  api = Blueprint('api', __name__, url_prefix='/v1')
  
  
  class ScoresResponse(object):
      def __init__(self, scores, errors):
          self.scores = scores
          self.errors = errors
  
+     @property
+     def sorted_scores(self):
+         # Sort by descending by value
+         return dict(sorted(self.scores.items(), key=lambda x: -x[1]))
+ 
      def to_json(self):
-         return {'scores': self.scores, 'errors': list(self.errors)}
+         return {'scores': self.sorted_scores, 'errors': list(self.errors)}
  
  
  @api.route('/score', methods=['POST'])
  def score():
      """Calculate the score for a given result set."""
  
      try:
          results = json.loads(request.data)['results']
      except (ValueError, KeyError):
          abort(400)
  
      if not isinstance(results, list):
          abort(400)
  
      try:
          results = calculate_scores(results)
      except Exception as err:
          return JSONError('CALCULATION_ERROR', code=-1, message=str(err))\
              .to_error()
  
      return ScoresResponse(*results)
  
Return sorted values for scores
## Code Before:

import json
from flask import Blueprint, request, abort
from .utils import JSONError
from .calc import calculate_scores


api = Blueprint('api', __name__, url_prefix='/v1')


class ScoresResponse(object):
    def __init__(self, scores, errors):
        self.scores = scores
        self.errors = errors

    def to_json(self):
        return {'scores': self.scores, 'errors': list(self.errors)}


@api.route('/score', methods=['POST'])
def score():
    """Calculate the score for a given result set."""

    try:
        results = json.loads(request.data)['results']
    except (ValueError, KeyError):
        abort(400)

    if not isinstance(results, list):
        abort(400)

    try:
        results = calculate_scores(results)
    except Exception as err:
        return JSONError('CALCULATION_ERROR', code=-1, message=str(err))\
            .to_error()

    return ScoresResponse(*results)

## Instruction:
Return sorted values for scores
## Code After:

import json
from flask import Blueprint, request, abort
from .utils import JSONError
from .calc import calculate_scores


api = Blueprint('api', __name__, url_prefix='/v1')


class ScoresResponse(object):
    def __init__(self, scores, errors):
        self.scores = scores
        self.errors = errors

    @property
    def sorted_scores(self):
        # Sort by descending by value
        return dict(sorted(self.scores.items(), key=lambda x: -x[1]))

    def to_json(self):
        return {'scores': self.sorted_scores, 'errors': list(self.errors)}


@api.route('/score', methods=['POST'])
def score():
    """Calculate the score for a given result set."""

    try:
        results = json.loads(request.data)['results']
    except (ValueError, KeyError):
        abort(400)

    if not isinstance(results, list):
        abort(400)

    try:
        results = calculate_scores(results)
    except Exception as err:
        return JSONError('CALCULATION_ERROR', code=-1, message=str(err))\
            .to_error()

    return ScoresResponse(*results)
