Repository: hugovk/pypistats

Issue:
Sort table by downloads

| category | downloads |
|----------|----------:|
|      2.6 |      1968 |
|      2.7 |   1594594 |
|      3.1 |         3 |
|      3.2 |       244 |
|      3.3 |      1824 |

->


| category | downloads |
|----------|----------:|
|      2.7 |   1,594,594 |
|      2.6 |      1,968 |
|      3.3 |      1,824 |
|      3.2 |       244 |
|      3.1 |         3 |


Discussion:

Patch:
diff --git a/pypistats/__init__.py b/pypistats/__init__.py
index 21c78e2..719f31a 100644
--- a/pypistats/__init__.py
+++ b/pypistats/__init__.py
@@ -15,7 +15,13 @@ BASE_URL = "https://pypistats.org/api/"
 
 
 def pypi_stats_api(
-    endpoint, params=None, output="table", start_date=None, end_date=None, total=True
+    endpoint,
+    params=None,
+    output="table",
+    start_date=None,
+    end_date=None,
+    sort=True,
+    total=True,
 ):
     """Call the API and return JSON"""
     if params:
@@ -40,6 +46,10 @@ def pypi_stats_api(
     if output == "json":
         return res
 
+    # Only sort for table
+    if sort:
+        res["data"] = _sort(res["data"])
+
     data = res["data"]
     return _tabulate(data)
 
@@ -63,6 +73,17 @@ def _filter(data, start_date=None, end_date=None):
     return data
 
 
+def _sort(data):
+    """Sort by downloads"""
+
+    # Only sum lists of dicts, not a single dict
+    if isinstance(data, dict):
+        return data
+
+    data = sorted(data, key=lambda k: k["downloads"], reverse=True)
+    return data
+
+
 def _total(data):
     """Sum all downloads per category, regardless of date"""
 


Test patch:
diff --git a/tests/test_pypistats.py b/tests/test_pypistats.py
index c925377..cad9a90 100644
--- a/tests/test_pypistats.py
+++ b/tests/test_pypistats.py
@@ -7,6 +7,19 @@ import unittest
 
 import pypistats
 
+SAMPLE_DATA = [
+    {"category": "2.6", "date": "2018-08-15", "downloads": 51},
+    {"category": "2.7", "date": "2018-08-15", "downloads": 63749},
+    {"category": "3.2", "date": "2018-08-15", "downloads": 2},
+    {"category": "3.3", "date": "2018-08-15", "downloads": 40},
+    {"category": "3.4", "date": "2018-08-15", "downloads": 6095},
+    {"category": "3.5", "date": "2018-08-15", "downloads": 20358},
+    {"category": "3.6", "date": "2018-08-15", "downloads": 35274},
+    {"category": "3.7", "date": "2018-08-15", "downloads": 6595},
+    {"category": "3.8", "date": "2018-08-15", "downloads": 3},
+    {"category": "null", "date": "2018-08-15", "downloads": 1019},
+]
+
 
 class TestPypiStats(unittest.TestCase):
     def test__filter_no_filters_no_change(self):
@@ -115,18 +128,7 @@ class TestPypiStats(unittest.TestCase):
 
     def test__tabulate(self):
         # Arrange
-        data = [
-            {"category": "2.6", "date": "2018-08-15", "downloads": 51},
-            {"category": "2.7", "date": "2018-08-15", "downloads": 63749},
-            {"category": "3.2", "date": "2018-08-15", "downloads": 2},
-            {"category": "3.3", "date": "2018-08-15", "downloads": 40},
-            {"category": "3.4", "date": "2018-08-15", "downloads": 6095},
-            {"category": "3.5", "date": "2018-08-15", "downloads": 20358},
-            {"category": "3.6", "date": "2018-08-15", "downloads": 35274},
-            {"category": "3.7", "date": "2018-08-15", "downloads": 6595},
-            {"category": "3.8", "date": "2018-08-15", "downloads": 3},
-            {"category": "null", "date": "2018-08-15", "downloads": 1019},
-        ]
+        data = SAMPLE_DATA
         expected_output = """
 | category |    date    | downloads |
 |----------|------------|----------:|
@@ -148,6 +150,38 @@ class TestPypiStats(unittest.TestCase):
         # Assert
         self.assertEqual(output.strip(), expected_output.strip())
 
+    def test__sort(self):
+        # Arrange
+        data = SAMPLE_DATA
+        expected_output = [
+            {"category": "2.7", "date": "2018-08-15", "downloads": 63749},
+            {"category": "3.6", "date": "2018-08-15", "downloads": 35274},
+            {"category": "3.5", "date": "2018-08-15", "downloads": 20358},
+            {"category": "3.7", "date": "2018-08-15", "downloads": 6595},
+            {"category": "3.4", "date": "2018-08-15", "downloads": 6095},
+            {"category": "null", "date": "2018-08-15", "downloads": 1019},
+            {"category": "2.6", "date": "2018-08-15", "downloads": 51},
+            {"category": "3.3", "date": "2018-08-15", "downloads": 40},
+            {"category": "3.8", "date": "2018-08-15", "downloads": 3},
+            {"category": "3.2", "date": "2018-08-15", "downloads": 2},
+        ]
+
+        # Act
+        output = pypistats._sort(data)
+
+        # Assert
+        self.assertEqual(output, expected_output)
+
+    def test__sort_recent(self):
+        # Arrange
+        data = {"last_day": 123002, "last_month": 3254221, "last_week": 761649}
+
+        # Act
+        output = pypistats._sort(data)
+
+        # Assert
+        self.assertEqual(output, data)
+
     def test__total(self):
         # Arrange
         from data.python_minor import data