Switch to mathutil from pyutil (it has tests, and has already been ported to

Python 3).
This commit is contained in:
Itamar Turner-Trauring 2020-07-08 12:02:58 -04:00
parent 016c7754a6
commit 229583c596
2 changed files with 14 additions and 64 deletions

View File

@ -17,6 +17,7 @@ if PY2:
PORTED_MODULES = [
"allmydata.util.assertutil",
"allmydata.util.humanreadable",
"allmydata.util.mathutil",
"allmydata.util.namespace",
"allmydata.util._python3",
]

View File

@ -1,71 +1,20 @@
"""
A few commonly needed functions.
Backwards compatibility for direct imports.
Ported to Python 3.
"""
import math
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
def div_ceil(n, d):
"""
The smallest integer k such that k*d >= n.
"""
return (n/d) + (n%d != 0)
from future.utils import PY2
if PY2:
from builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, int, list, object, range, str, max, min # noqa: F401
def next_multiple(n, k):
"""
The smallest multiple of k which is >= n.
"""
return div_ceil(n, k) * k
def pad_size(n, k):
"""
The smallest number that has to be added to n so that n is a multiple of k.
"""
if n%k:
return k - n%k
else:
return 0
def is_power_of_k(n, k):
return k**int(math.log(n, k) + 0.5) == n
def next_power_of_k(n, k):
if n == 0:
x = 0
else:
x = int(math.log(n, k) + 0.5)
if k**x < n:
return k**(x+1)
else:
return k**x
def ave(l):
return sum(l) / len(l)
def log_ceil(n, b):
"""
The smallest integer k such that b^k >= n.
log_ceil(n, 2) is the number of bits needed to store any of n values, e.g.
the number of bits needed to store any of 128 possible values is 7.
"""
p = 1
k = 0
while p < n:
p *= b
k += 1
return k
def log_floor(n, b):
"""
The largest integer k such that b^k <= n.
"""
p = 1
k = 0
while p <= n:
p *= b
k += 1
return k - 1
def round_sigfigs(f, n):
fmt = "%." + str(n-1) + "e"
return float(fmt % f)
# The API importers expect:
from pyutil.mathutil import div_ceil, next_multiple, pad_size, is_power_of_k, next_power_of_k, ave, log_ceil, log_floor, round_sigfigs