Skip to content

Commit

Permalink
Versions are strings (#67)
Browse files Browse the repository at this point in the history
Versions are strings
  • Loading branch information
hugovk authored Jul 30, 2019
2 parents f81bcfa + 19968e5 commit 65349de
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 62 deletions.
54 changes: 29 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Get recent downloads:
$ pypistats recent pillow
| last_day | last_month | last_week |
|---------:|-----------:|----------:|
| 61877 | 3527341 | 865484 |
| 280,842 | 7,065,928 | 1,709,689 |
```

Help for another subcommand:
Expand Down Expand Up @@ -123,36 +123,40 @@ Get version downloads:
$ pypistats python_minor pillow --last-month
| category | percent | downloads |
|----------|--------:|----------:|
| 2.7 | 46.64% | 1,512,429 |
| 3.6 | 30.34% | 983,838 |
| 3.5 | 12.53% | 406,429 |
| 3.7 | 6.12% | 198,558 |
| 3.4 | 3.41% | 110,552 |
| null | 0.84% | 27,380 |
| 3.3 | 0.05% | 1,599 |
| 2.6 | 0.05% | 1,581 |
| 3.2 | 0.01% | 246 |
| 3.8 | 0.00% | 133 |
| 2.4 | 0.00% | 7 |
| Total | | 3,242,752 |
| 2.7 | 35.94% | 2,189,327 |
| 3.6 | 31.83% | 1,938,870 |
| 3.7 | 18.71% | 1,139,642 |
| 3.5 | 11.29% | 687,782 |
| 3.4 | 1.23% | 74,673 |
| null | 0.94% | 57,476 |
| 3.8 | 0.04% | 2,147 |
| 2.6 | 0.01% | 826 |
| 3.3 | 0.00% | 212 |
| 3.2 | 0.00% | 28 |
| 2.4 | 0.00% | 6 |
| 3.9 | 0.00% | 5 |
| 2.8 | 0.00% | 1 |
| Total | | 6,090,995 |
```

The table is Markdown, ready for pasting in GitHub issues and PRs:

| category | percent | downloads |
|----------|--------:|----------:|
| 2.7 | 46.64% | 1,512,429 |
| 3.6 | 30.34% | 983,838 |
| 3.5 | 12.53% | 406,429 |
| 3.7 | 6.12% | 198,558 |
| 3.4 | 3.41% | 110,552 |
| null | 0.84% | 27,380 |
| 3.3 | 0.05% | 1,599 |
| 2.6 | 0.05% | 1,581 |
| 3.2 | 0.01% | 246 |
| 3.8 | 0.00% | 133 |
| 2.4 | 0.00% | 7 |
| Total | | 3,242,752 |
| 2.7 | 35.94% | 2,189,327 |
| 3.6 | 31.83% | 1,938,870 |
| 3.7 | 18.71% | 1,139,642 |
| 3.5 | 11.29% | 687,782 |
| 3.4 | 1.23% | 74,673 |
| null | 0.94% | 57,476 |
| 3.8 | 0.04% | 2,147 |
| 2.6 | 0.01% | 826 |
| 3.3 | 0.00% | 212 |
| 3.2 | 0.00% | 28 |
| 2.4 | 0.00% | 6 |
| 3.9 | 0.00% | 5 |
| 2.8 | 0.00% | 1 |
| Total | | 6,090,995 |

These are equivalent (in May 2019):

Expand Down
13 changes: 12 additions & 1 deletion src/pypistats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
import pkg_resources
import requests
from appdirs import user_cache_dir
from pytablewriter import HtmlTableWriter, MarkdownTableWriter, RstSimpleTableWriter
from pytablewriter import (
HtmlTableWriter,
MarkdownTableWriter,
RstSimpleTableWriter,
String,
)
from pytablewriter.style import Align, Style
from slugify import slugify

Expand Down Expand Up @@ -285,18 +290,24 @@ def _tabulate(data, format="markdown"):
writer.style_list = len(header_list) * [Style(thousand_separator=",")]
else:
style_list = []
type_hints = []

for item in header_list:
align = None
thousand_separator = None
type_hint = None
if item == "percent":
align = Align.RIGHT
elif item == "downloads":
thousand_separator = ","
elif item == "category":
type_hint = String
style = Style(align=align, thousand_separator=thousand_separator)
style_list.append(style)
type_hints.append(type_hint)

writer.style_list = style_list
writer.type_hints = type_hints

return writer.dumps()

Expand Down
92 changes: 56 additions & 36 deletions tests/test_pypistats.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
"last_month": 3_254_221,
"last_week": 761_649,
}
SAMPLE_DATA_VERSION_STRINGS = [
{"category": "3.1", "date": "2018-08-15", "downloads": 10},
{"category": "3.10", "date": "2018-08-15", "downloads": 1},
]


def stub__cache_filename(*args):
Expand Down Expand Up @@ -162,15 +166,15 @@ def test__tabulate_noarg(self):
expected_output = """
| category | date | downloads |
|----------|------------|----------:|
| 2.6 | 2018-08-15 | 51 |
| 2.7 | 2018-08-15 | 63,749 |
| 3.2 | 2018-08-15 | 2 |
| 3.3 | 2018-08-15 | 40 |
| 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 |
| 2.6 | 2018-08-15 | 51 |
| 2.7 | 2018-08-15 | 63,749 |
| 3.2 | 2018-08-15 | 2 |
| 3.3 | 2018-08-15 | 40 |
| 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 | 1,019 |
"""

Expand All @@ -186,15 +190,15 @@ def test__tabulate_markdown(self):
expected_output = """
| category | date | downloads |
|----------|------------|----------:|
| 2.6 | 2018-08-15 | 51 |
| 2.7 | 2018-08-15 | 63,749 |
| 3.2 | 2018-08-15 | 2 |
| 3.3 | 2018-08-15 | 40 |
| 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 |
| 2.6 | 2018-08-15 | 51 |
| 2.7 | 2018-08-15 | 63,749 |
| 3.2 | 2018-08-15 | 2 |
| 3.3 | 2018-08-15 | 40 |
| 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 | 1,019 |
"""

Expand All @@ -213,15 +217,15 @@ def test__tabulate_rst(self):
========== ============ ===========
category date downloads
========== ============ ===========
2.6 2018-08-15 51
2.7 2018-08-15 63,749
3.2 2018-08-15 2
3.3 2018-08-15 40
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
2.6 2018-08-15 51
2.7 2018-08-15 63,749
3.2 2018-08-15 2
3.3 2018-08-15 40
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 1,019
========== ============ ===========
""" # noqa: W291
Expand All @@ -246,47 +250,47 @@ def test__tabulate_html(self):
</thead>
<tbody>
<tr>
<td align="right">2.6</td>
<td align="left">2.6</td>
<td align="left">2018-08-15</td>
<td align="right">51</td>
</tr>
<tr>
<td align="right">2.7</td>
<td align="left">2.7</td>
<td align="left">2018-08-15</td>
<td align="right">63,749</td>
</tr>
<tr>
<td align="right">3.2</td>
<td align="left">3.2</td>
<td align="left">2018-08-15</td>
<td align="right">2</td>
</tr>
<tr>
<td align="right">3.3</td>
<td align="left">3.3</td>
<td align="left">2018-08-15</td>
<td align="right">40</td>
</tr>
<tr>
<td align="right">3.4</td>
<td align="left">3.4</td>
<td align="left">2018-08-15</td>
<td align="right">6,095</td>
</tr>
<tr>
<td align="right">3.5</td>
<td align="left">3.5</td>
<td align="left">2018-08-15</td>
<td align="right">20,358</td>
</tr>
<tr>
<td align="right">3.6</td>
<td align="left">3.6</td>
<td align="left">2018-08-15</td>
<td align="right">35,274</td>
</tr>
<tr>
<td align="right">3.7</td>
<td align="left">3.7</td>
<td align="left">2018-08-15</td>
<td align="right">6,595</td>
</tr>
<tr>
<td align="right">3.8</td>
<td align="left">3.8</td>
<td align="left">2018-08-15</td>
<td align="right">3</td>
</tr>
Expand Down Expand Up @@ -665,6 +669,22 @@ def test_python_minor_monthly(self):
# Assert
self.assertEqual(json.loads(output), json.loads(expected_output))

def test_versions_are_strings(self):
# Arrange
data = copy.deepcopy(SAMPLE_DATA_VERSION_STRINGS)
expected_output = """
| category | date | downloads |
|----------|------------|----------:|
| 3.1 | 2018-08-15 | 10 |
| 3.10 | 2018-08-15 | 1 |
"""

# Act
output = pypistats._tabulate(data, format="markdown")

# Assert
self.assertEqual(output.strip(), expected_output.strip())


# pytest's capsys cannot be used in a unittest class
def test__print_verbose_print(capsys):
Expand Down

0 comments on commit 65349de

Please sign in to comment.