Skip to content

Commit 5aad027

Browse files
authored
Some reformatting (suggested by Black) and minor factoring. (GH-20865)
1 parent d71ab4f commit 5aad027

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

Lib/statistics.py

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def _sum(data, start=0):
163163
T = _coerce(int, type(start))
164164
for typ, values in groupby(data, type):
165165
T = _coerce(T, typ) # or raise TypeError
166-
for n,d in map(_exact_ratio, values):
166+
for n, d in map(_exact_ratio, values):
167167
count += 1
168168
partials[d] = partials_get(d, 0) + n
169169
if None in partials:
@@ -261,7 +261,7 @@ def _convert(value, T):
261261
return T(value)
262262
except TypeError:
263263
if issubclass(T, Decimal):
264-
return T(value.numerator)/T(value.denominator)
264+
return T(value.numerator) / T(value.denominator)
265265
else:
266266
raise
267267

@@ -277,8 +277,8 @@ def _find_lteq(a, x):
277277
def _find_rteq(a, l, x):
278278
'Locate the rightmost value exactly equal to x'
279279
i = bisect_right(a, x, lo=l)
280-
if i != (len(a)+1) and a[i-1] == x:
281-
return i-1
280+
if i != (len(a) + 1) and a[i - 1] == x:
281+
return i - 1
282282
raise ValueError
283283

284284

@@ -315,7 +315,7 @@ def mean(data):
315315
raise StatisticsError('mean requires at least one data point')
316316
T, total, count = _sum(data)
317317
assert count == n
318-
return _convert(total/n, T)
318+
return _convert(total / n, T)
319319

320320

321321
def fmean(data):
@@ -403,11 +403,11 @@ def harmonic_mean(data):
403403
else:
404404
raise TypeError('unsupported type')
405405
try:
406-
T, total, count = _sum(1/x for x in _fail_neg(data, errmsg))
406+
T, total, count = _sum(1 / x for x in _fail_neg(data, errmsg))
407407
except ZeroDivisionError:
408408
return 0
409409
assert count == n
410-
return _convert(n/total, T)
410+
return _convert(n / total, T)
411411

412412

413413
# FIXME: investigate ways to calculate medians without sorting? Quickselect?
@@ -428,11 +428,11 @@ def median(data):
428428
n = len(data)
429429
if n == 0:
430430
raise StatisticsError("no median for empty data")
431-
if n%2 == 1:
432-
return data[n//2]
431+
if n % 2 == 1:
432+
return data[n // 2]
433433
else:
434-
i = n//2
435-
return (data[i - 1] + data[i])/2
434+
i = n // 2
435+
return (data[i - 1] + data[i]) / 2
436436

437437

438438
def median_low(data):
@@ -451,10 +451,10 @@ def median_low(data):
451451
n = len(data)
452452
if n == 0:
453453
raise StatisticsError("no median for empty data")
454-
if n%2 == 1:
455-
return data[n//2]
454+
if n % 2 == 1:
455+
return data[n // 2]
456456
else:
457-
return data[n//2 - 1]
457+
return data[n // 2 - 1]
458458

459459

460460
def median_high(data):
@@ -473,7 +473,7 @@ def median_high(data):
473473
n = len(data)
474474
if n == 0:
475475
raise StatisticsError("no median for empty data")
476-
return data[n//2]
476+
return data[n // 2]
477477

478478

479479
def median_grouped(data, interval=1):
@@ -510,15 +510,15 @@ class 3.5-4.5, and interpolation is used to estimate it.
510510
return data[0]
511511
# Find the value at the midpoint. Remember this corresponds to the
512512
# centre of the class interval.
513-
x = data[n//2]
513+
x = data[n // 2]
514514
for obj in (x, interval):
515515
if isinstance(obj, (str, bytes)):
516516
raise TypeError('expected number but got %r' % obj)
517517
try:
518-
L = x - interval/2 # The lower limit of the median interval.
518+
L = x - interval / 2 # The lower limit of the median interval.
519519
except TypeError:
520520
# Mixed type. For now we just coerce to float.
521-
L = float(x) - float(interval)/2
521+
L = float(x) - float(interval) / 2
522522

523523
# Uses bisection search to search for x in data with log(n) time complexity
524524
# Find the position of leftmost occurrence of x in data
@@ -528,7 +528,7 @@ class 3.5-4.5, and interpolation is used to estimate it.
528528
l2 = _find_rteq(data, l1, x)
529529
cf = l1
530530
f = l2 - l1 + 1
531-
return L + interval*(n/2 - cf)/f
531+
return L + interval * (n / 2 - cf) / f
532532

533533

534534
def mode(data):
@@ -554,8 +554,7 @@ def mode(data):
554554
If *data* is empty, ``mode``, raises StatisticsError.
555555
556556
"""
557-
data = iter(data)
558-
pairs = Counter(data).most_common(1)
557+
pairs = Counter(iter(data)).most_common(1)
559558
try:
560559
return pairs[0][0]
561560
except IndexError:
@@ -597,7 +596,7 @@ def multimode(data):
597596
# For sample data where there is a positive probability for values
598597
# beyond the range of the data, the R6 exclusive method is a
599598
# reasonable choice. Consider a random sample of nine values from a
600-
# population with a uniform distribution from 0.0 to 100.0. The
599+
# population with a uniform distribution from 0.0 to 1.0. The
601600
# distribution of the third ranked sample point is described by
602601
# betavariate(alpha=3, beta=7) which has mode=0.250, median=0.286, and
603602
# mean=0.300. Only the latter (which corresponds with R6) gives the
@@ -643,9 +642,8 @@ def quantiles(data, *, n=4, method='exclusive'):
643642
m = ld - 1
644643
result = []
645644
for i in range(1, n):
646-
j = i * m // n
647-
delta = i*m - j*n
648-
interpolated = (data[j] * (n - delta) + data[j+1] * delta) / n
645+
j, delta = divmod(i * m, n)
646+
interpolated = (data[j] * (n - delta) + data[j + 1] * delta) / n
649647
result.append(interpolated)
650648
return result
651649
if method == 'exclusive':
@@ -655,7 +653,7 @@ def quantiles(data, *, n=4, method='exclusive'):
655653
j = i * m // n # rescale i to m/n
656654
j = 1 if j < 1 else ld-1 if j > ld-1 else j # clamp to 1 .. ld-1
657655
delta = i*m - j*n # exact integer math
658-
interpolated = (data[j-1] * (n - delta) + data[j] * delta) / n
656+
interpolated = (data[j - 1] * (n - delta) + data[j] * delta) / n
659657
result.append(interpolated)
660658
return result
661659
raise ValueError(f'Unknown method: {method!r}')
@@ -689,9 +687,9 @@ def _ss(data, c=None):
689687
T, total, count = _sum((x-c)**2 for x in data)
690688
# The following sum should mathematically equal zero, but due to rounding
691689
# error may not.
692-
U, total2, count2 = _sum((x-c) for x in data)
690+
U, total2, count2 = _sum((x - c) for x in data)
693691
assert T == U and count == count2
694-
total -= total2**2/len(data)
692+
total -= total2 ** 2 / len(data)
695693
assert not total < 0, 'negative sum of square deviations: %f' % total
696694
return (T, total)
697695

@@ -740,7 +738,7 @@ def variance(data, xbar=None):
740738
if n < 2:
741739
raise StatisticsError('variance requires at least two data points')
742740
T, ss = _ss(data, xbar)
743-
return _convert(ss/(n-1), T)
741+
return _convert(ss / (n - 1), T)
744742

745743

746744
def pvariance(data, mu=None):
@@ -784,7 +782,7 @@ def pvariance(data, mu=None):
784782
if n < 1:
785783
raise StatisticsError('pvariance requires at least one data point')
786784
T, ss = _ss(data, mu)
787-
return _convert(ss/n, T)
785+
return _convert(ss / n, T)
788786

789787

790788
def stdev(data, xbar=None):
@@ -993,7 +991,7 @@ def overlap(self, other):
993991
if not isinstance(other, NormalDist):
994992
raise TypeError('Expected another NormalDist instance')
995993
X, Y = self, other
996-
if (Y._sigma, Y._mu) < (X._sigma, X._mu): # sort to assure commutativity
994+
if (Y._sigma, Y._mu) < (X._sigma, X._mu): # sort to assure commutativity
997995
X, Y = Y, X
998996
X_var, Y_var = X.variance, Y.variance
999997
if not X_var or not Y_var:

0 commit comments

Comments
 (0)