Skip to content

Commit d3030e0

Browse files
committed
Format, add code formatting tooling
Introduce riot command for formatting code using [`black`](https://black.readthedocs.io/en/stable/) and [`isort`](https://github.com/PyCQA/isort). Instructions are added to the README as well.
1 parent cfb6cd2 commit d3030e0

File tree

14 files changed

+137
-69
lines changed

14 files changed

+137
-69
lines changed

.github/workflows/test.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ on:
55
- master
66
pull_request:
77
jobs:
8+
check:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/setup-python@v2
12+
with:
13+
python-version: '3.9'
14+
- uses: actions/checkout@v2
15+
with:
16+
fetch-depth: 0
17+
- run: pip install riot==0.8
18+
- run: riot -v run check_fmt
819
test:
920
strategy:
1021
matrix:

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,12 @@ Edit the generated file to include notes on the changes made in the commit/PR
8787
and add commit it.
8888

8989

90+
### Formatting
91+
92+
Format code with
93+
94+
riot run fmt
95+
96+
9097
## References
9198
[1] Charles Masson and Jee E Rim and Homin K. Lee. DDSketch: A fast and fully-mergeable quantile sketch with relative-error guarantees. PVLDB, 12(12): 2195-2205, 2019. (The code referenced in the paper, including our implementation of the the Greenwald-Khanna (GK) algorithm, can be found at: https://github.com/DataDog/sketches-py/releases/tag/v0.1 )

ddsketch/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from .ddsketch import DDSketch
22

3+
34
__all__ = ["DDSketch"]

ddsketch/ddsketch.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@
3535
<a href="https://github.com/DataDog/sketches-js/">JavaScript</a>
3636
"""
3737

38-
from .exception import IllegalArgumentException, UnequalSketchParametersException
38+
from .exception import IllegalArgumentException
39+
from .exception import UnequalSketchParametersException
3940
from .mapping import LogarithmicMapping
40-
from .store import CollapsingHighestDenseStore, CollapsingLowestDenseStore, DenseStore
41+
from .store import CollapsingHighestDenseStore
42+
from .store import CollapsingLowestDenseStore
43+
from .store import DenseStore
4144

4245

4346
DEFAULT_REL_ACC = 0.01 # "alpha" in the paper

ddsketch/mapping.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
when computing the index. Other mappings can approximate the logarithmic
1717
mapping, while being less computationally costly.
1818
"""
19-
from abc import ABC, abstractmethod
19+
from abc import ABC
20+
from abc import abstractmethod
2021
import math
2122
import sys
2223

@@ -52,17 +53,17 @@ def __init__(self, relative_accuracy, offset=0.0):
5253

5354
@classmethod
5455
def from_gamma_offset(cls, gamma, offset):
55-
""" Constructor used by pb.proto """
56+
"""Constructor used by pb.proto"""
5657
relative_accuracy = (gamma - 1.0) / (gamma + 1.0)
5758
return cls(relative_accuracy, offset=offset)
5859

5960
@abstractmethod
6061
def _log_gamma(self, value):
61-
""" Return (an approximation of) the logarithm of the value base gamma """
62+
"""Return (an approximation of) the logarithm of the value base gamma"""
6263

6364
@abstractmethod
6465
def _pow_gamma(self, value):
65-
""" Return (an approximation of) gamma to the power value """
66+
"""Return (an approximation of) gamma to the power value"""
6667

6768
def key(self, value):
6869
"""
@@ -102,7 +103,7 @@ def _pow_gamma(self, value):
102103

103104
def _cbrt(x):
104105
# type: (float) -> float
105-
y = abs(x)**(1./3.)
106+
y = abs(x) ** (1.0 / 3.0)
106107
if x < 0:
107108
return -y
108109
return y
@@ -127,7 +128,7 @@ def _log2_approx(self, value):
127128
return significand + (exponent - 1)
128129

129130
def _exp2_approx(self, value):
130-
""" inverse of _log2_approx """
131+
"""inverse of _log2_approx"""
131132
exponent = math.floor(value) + 1
132133
mantissa = (value - exponent + 2) / 2.0
133134
return math.ldexp(mantissa, exponent)
@@ -158,15 +159,15 @@ def __init__(self, relative_accuracy, offset=0.0):
158159
self._multiplier /= self.C
159160

160161
def _cubic_log2_approx(self, value):
161-
""" approximates log2 using a cubic polynomial """
162+
"""approximates log2 using a cubic polynomial"""
162163
mantissa, exponent = math.frexp(value)
163164
significand = 2 * mantissa - 1
164165
return (
165166
(self.A * significand + self.B) * significand + self.C
166167
) * significand + (exponent - 1)
167168

168169
def _cubic_exp2_approx(self, value):
169-
""" Derived from Cardano's formula """
170+
"""Derived from Cardano's formula"""
170171

171172
exponent = math.floor(value)
172173
delta_0 = self.B * self.B - 3 * self.A * self.C
@@ -175,7 +176,10 @@ def _cubic_exp2_approx(self, value):
175176
- 9 * self.A * self.B * self.C
176177
- 27 * self.A * self.A * (value - exponent)
177178
)
178-
cardano = _cbrt((delta_1 - ((delta_1 * delta_1 - 4 * delta_0 * delta_0 * delta_0)**.5)) / 2)
179+
cardano = _cbrt(
180+
(delta_1 - ((delta_1 * delta_1 - 4 * delta_0 * delta_0 * delta_0) ** 0.5))
181+
/ 2
182+
)
179183
significand_plus_one = (
180184
-(self.B + cardano + delta_0 / cardano) / (3 * self.A) + 1
181185
)

ddsketch/store.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
We start with 128 bins and grow the store in chunks of 128 unless specified
88
otherwise."""
99

10-
from abc import ABC, abstractmethod
10+
from abc import ABC
11+
from abc import abstractmethod
1112
import math
1213

1314

@@ -167,7 +168,7 @@ def _shift_bins(self, shift):
167168
self.offset -= shift
168169

169170
def _center_bins(self, new_min_key, new_max_key):
170-
""" center the bins; this changes the offset"""
171+
"""center the bins; this changes the offset"""
171172
middle_key = new_min_key + (new_max_key - new_min_key + 1) // 2
172173
self._shift_bins(self.offset + self.length() // 2 - middle_key)
173174

pyproject.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[tool.isort]
2+
force_single_line = true
3+
lines_after_imports = 2
4+
force_sort_within_sections = true
5+
known_first_party = "ddsketch"
6+
default_section = "THIRDPARTY"
7+
skip = [".riot/", ".venv/", "ddsketch/pb"]
8+
line_length = 120
9+
10+
[tool.black]
11+
exclude = '''
12+
(
13+
/(
14+
\.riot
15+
| ddsketch/pb.*
16+
| \.venv.*
17+
| \.eggs
18+
)/
19+
)
20+
'''

riotfile.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,27 @@
2222
Venv(
2323
name="reno",
2424
command="reno {cmdargs}",
25+
)
26+
],
27+
),
28+
Venv(
29+
pkgs={
30+
"black": "==21.7b0",
31+
"isort": latest,
32+
"toml": latest,
33+
},
34+
venvs=[
35+
Venv(
36+
name="black",
37+
command="black {cmdargs}",
38+
),
39+
Venv(
40+
name="fmt",
41+
command="isort . && black .",
42+
),
43+
Venv(
44+
name="check_fmt",
45+
command="isort --check . && black --check .",
2546
),
2647
],
2748
),

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import setuptools
22

3+
34
with open("README.md", "r", encoding="utf-8") as fh:
45
long_description = fh.read()
56

tests/datasets.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
# This product includes software developed at Datadog (https://www.datadoghq.com/).
44
# Copyright 2020 Datadog, Inc.
55

6-
from abc import ABC, abstractmethod, abstractproperty
6+
from abc import ABC
7+
from abc import abstractmethod
8+
from abc import abstractproperty
79

810
import numpy as np
911

0 commit comments

Comments
 (0)