pypistats icon indicating copy to clipboard operation
pypistats copied to clipboard

Add the ability to retrieve stats as a deserialized/unformatted `dict`

Open jwodder opened this issue 1 year ago • 2 comments

When I use this library, I don't want it to give me a formatted string; I want the actual stats instead. Currently, I achieve this by doing json.loads(pypistats.recent(package, format="json")), but it would be more convenient if pypistats had a built-in way to get the unformatted stats.

jwodder avatar May 01 '24 14:05 jwodder

Something like this?

diff --git a/src/pypistats/__init__.py b/src/pypistats/__init__.py
index 3bd7ac4..7395288 100644
--- a/src/pypistats/__init__.py
+++ b/src/pypistats/__init__.py
@@ -169,6 +169,9 @@ def pypi_stats_api(
     data = _percent(data)
     data = _grand_total(data)
 
+    if format == "dict":
+        return data
+
     if color != "no" and format in ("markdown", "pretty", "rst", "tsv"):
         data = _colourify(data)

And then call pypi_stats_api(format="dict")

hugovk avatar May 02 '24 06:05 hugovk

@hugovk Yes, that would work.

jwodder avatar May 02 '24 10:05 jwodder