Skip to content

Commit

Permalink
fix total method on counters, remove deptry
Browse files Browse the repository at this point in the history
  • Loading branch information
stringertheory committed Jun 7, 2023
1 parent ed6a5ea commit 636bee5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 36 deletions.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ check: ## Run code quality tools.
@poetry lock --check
@echo "🚀 Linting code: Running pre-commit"
@poetry run pre-commit run -a
@echo "🚀 Checking for obsolete dependencies: Running deptry"
@poetry run deptry .

.PHONY: test
test: ## Test the code with pytest
Expand Down
18 changes: 16 additions & 2 deletions json_explorer/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ def oxford_join(items, pluralize=False):
return "{}, or {}".format(", ".join(items[:-1]), items[-1])


class Counter(collections.Counter):
"""The 'total' method is only available on a collections.Counter
in python >= 3.10, so add the method to be compatible with earlier
versions.
"""

def total(self):
try:
return super().total()
except AttributeError:
return sum(self.values())


class TripleCounter(dict):
def increment(self, keys, amount):
try:
Expand All @@ -40,13 +54,13 @@ def increment(self, keys, amount):
if a not in self:
self[a] = {}
if b not in self[a]:
self[a][b] = collections.Counter()
self[a][b] = Counter()
self[a][b][c] += amount

def tree(self):
result = {}
for a, sub_dict in self.items():
sub_counter = collections.Counter()
sub_counter = Counter()
for b, c_counter in sub_dict.items():
sub_counter[b] += c_counter.total()

Expand Down
67 changes: 38 additions & 29 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ packages = [
greet = "json_explorer.explore:main"

[tool.poetry.dependencies]
python = ">=3.8,<4.0"
python = ">=3.7,<4.0"

[tool.poetry.group.dev.dependencies]
pytest = "^7.2.0"
pytest-cov = "^4.0.0"
deptry = "^0.6.4"
pre-commit = "^2.20.0"
tox = "^3.25.1"
beautifulsoup4 = "^4.12.2"
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[tox]
skipsdist = true
envlist = py38, py39, py310, py311
envlist = py37, py38, py39, py310, py311

[gh-actions]
python =
3.7: py37
3.8: py38
3.9: py39
3.10: py310
Expand Down

0 comments on commit 636bee5

Please sign in to comment.