Repository: hugovk/pypistats

Issue:
Add commas to downloads in table

| category | downloads |
|----------|----------:|
|      2.6 |      1968 |
|      2.7 |   1594594 |
|      3.1 |         3 |
|      3.2 |       244 |
|      3.3 |      1824 |

->


| category | downloads |
|----------|----------:|
|      2.6 |      1,968 |
|      2.7 |   1,594,594 |
|      3.1 |         3 |
|      3.2 |       244 |
|      3.3 |      1,824 |


Discussion:

Patch:
diff --git a/pypistats/__init__.py b/pypistats/__init__.py
index 7e0da54..1b99b6f 100644
--- a/pypistats/__init__.py
+++ b/pypistats/__init__.py
@@ -5,7 +5,7 @@ Python interface to PyPI Stats API
 https://pypistats.org/api
 """
 import requests
-from pytablewriter import Align, MarkdownTableWriter
+from pytablewriter import Align, Format, MarkdownTableWriter
 
 from . import version
 
@@ -145,6 +145,25 @@ def _percent(data):
     return data
 
 
+def _custom_list(input_list, special_item, default_value, special_value):
+    """Given:
+        input_list = [1, 2, 3, 4]
+        special_item = 3
+        default_value = 0
+        special_value = 100
+    Return:
+        [0, 0, 100, 0]
+    """
+    new_list = [default_value] * len(input_list)
+
+    try:
+        new_list[input_list.index(special_item)] = special_value
+    except ValueError:
+        pass
+
+    return new_list
+
+
 def _tabulate(data):
     """Return data in table"""
 
@@ -163,12 +182,11 @@ def _tabulate(data):
     header_list.remove("downloads")
     writer.header_list = header_list
 
-    # Custom alignment
-    writer.align_list = [Align.AUTO] * len(header_list)
-    try:
-        writer.align_list[header_list.index("percent")] = Align.RIGHT
-    except ValueError:
-        pass
+    # Custom alignment and format
+    writer.align_list = _custom_list(header_list, "percent", Align.AUTO, Align.RIGHT)
+    writer.format_list = _custom_list(
+        header_list, "downloads", Format.NONE, Format.THOUSAND_SEPARATOR
+    )
 
     return writer.dumps()
 
diff --git a/pypistats/cli.py b/pypistats/cli.py
index 71c991b..6625f62 100644
--- a/pypistats/cli.py
+++ b/pypistats/cli.py
@@ -171,6 +171,9 @@ def _last_month():
 
 
 def main():
+    cli.add_argument(
+        "-v", "--version", action="version", version=f"%(prog)s {pypistats.__version__}"
+    )
     args = cli.parse_args()
     if args.subcommand is None:
         cli.print_help()
diff --git a/setup.py b/setup.py
index e4e54e9..825c0d5 100644
--- a/setup.py
+++ b/setup.py
@@ -21,7 +21,7 @@ setup(
     keywords=["PyPI", "downloads", "statistics", "stats", "BigQuery"],
     packages=find_packages(),
     entry_points={"console_scripts": ["pypistats = pypistats.cli:main"]},
-    install_requires=["pytablewriter>=0.32.0", "python-dateutil", "requests"],
+    install_requires=["pytablewriter>=0.33.0", "python-dateutil", "requests"],
     python_requires=">=3.6",
     classifiers=[
         "Development Status :: 3 - Alpha",


Test patch:
diff --git a/tests/test_pypistats.py b/tests/test_pypistats.py
index 47da4e3..690b135 100644
--- a/tests/test_pypistats.py
+++ b/tests/test_pypistats.py
@@ -134,15 +134,15 @@ class TestPypiStats(unittest.TestCase):
 | category |    date    | downloads |
 |----------|------------|----------:|
 |      2.6 | 2018-08-15 |        51 |
-|      2.7 | 2018-08-15 |     63749 |
+|      2.7 | 2018-08-15 |    63,749 |
 |      3.2 | 2018-08-15 |         2 |
 |      3.3 | 2018-08-15 |        40 |
-|      3.4 | 2018-08-15 |      6095 |
-|      3.5 | 2018-08-15 |     20358 |
-|      3.6 | 2018-08-15 |     35274 |
-|      3.7 | 2018-08-15 |      6595 |
+|      3.4 | 2018-08-15 |     6,095 |
+|      3.5 | 2018-08-15 |    20,358 |
+|      3.6 | 2018-08-15 |    35,274 |
+|      3.7 | 2018-08-15 |     6,595 |
 |      3.8 | 2018-08-15 |         3 |
-| null     | 2018-08-15 |      1019 |
+| null     | 2018-08-15 |     1,019 |
 """
 
         # Act
@@ -278,3 +278,18 @@ class TestPypiStats(unittest.TestCase):
 
         # Assert
         self.assertEqual(output, SAMPLE_DATA_RECENT)
+
+    def test__custom_list(self):
+        # Arrange
+        input_list = [1, 2, 3, 4]
+        special_item = 3
+        default_value = 0
+        special_value = 100
+
+        # Act
+        output = pypistats._custom_list(
+            input_list, special_item, default_value, special_value
+        )
+
+        # Assert
+        self.assertEqual(output, [0, 0, 100, 0])