update scripts

* fixed version updates in release script
* formatted Python scripts with `black` - The uncompromising code formatter (https://black.readthedocs.io/)
* f-string all the things
This commit is contained in:
Anders Dalvander 2024-11-23 11:24:39 +01:00
parent 6801b0ca20
commit b9793e1cd4
5 changed files with 268 additions and 190 deletions

View File

@ -1,100 +1,122 @@
# text parts # text parts
processed_files = { } processed_files = {}
# authors # authors
for filename in ['AUTHORS', 'CONTRIBUTORS']: for filename in ["AUTHORS", "CONTRIBUTORS"]:
with open(filename, encoding='utf8') as f: with open(filename, encoding="utf8") as f:
text = '' text = ""
for line in f: for line in f:
if filename == 'AUTHORS': if filename == "AUTHORS":
text += '// fast_float by ' + line text += "// fast_float by " + line
if filename == 'CONTRIBUTORS': if filename == "CONTRIBUTORS":
text += '// with contributions from ' + line text += "// with contributions from " + line
processed_files[filename] = text + '//\n//\n' processed_files[filename] = text + "//\n//\n"
# licenses # licenses
for filename in ['LICENSE-MIT', 'LICENSE-APACHE', 'LICENSE-BOOST']: for filename in ["LICENSE-MIT", "LICENSE-APACHE", "LICENSE-BOOST"]:
lines = [] lines = []
with open(filename, encoding='utf8') as f: with open(filename, encoding="utf8") as f:
lines = f.readlines() lines = f.readlines()
# Retrieve subset required for inclusion in source # Retrieve subset required for inclusion in source
if filename == 'LICENSE-APACHE': if filename == "LICENSE-APACHE":
lines = [ lines = [" Copyright 2021 The fast_float authors\n", *lines[179:-1]]
' Copyright 2021 The fast_float authors\n',
*lines[179:-1]
]
text = '' text = ""
for line in lines: for line in lines:
line = line.strip() line = line.strip()
if len(line): if len(line):
line = ' ' + line line = " " + line
text += '//' + line + '\n' text += "//" + line + "\n"
processed_files[filename] = text processed_files[filename] = text
# code # code
for filename in [ 'constexpr_feature_detect.h', 'float_common.h', 'fast_float.h', 'ascii_number.h', for filename in [
'fast_table.h', 'decimal_to_binary.h', 'bigint.h', 'digit_comparison.h', 'parse_number.h']: "constexpr_feature_detect.h",
with open('include/fast_float/' + filename, encoding='utf8') as f: "float_common.h",
text = '' "fast_float.h",
"ascii_number.h",
"fast_table.h",
"decimal_to_binary.h",
"bigint.h",
"digit_comparison.h",
"parse_number.h",
]:
with open("include/fast_float/" + filename, encoding="utf8") as f:
text = ""
for line in f: for line in f:
if line.startswith('#include "'): continue if line.startswith('#include "'):
continue
text += line text += line
processed_files[filename] = '\n' + text processed_files[filename] = "\n" + text
# command line # command line
import argparse import argparse
parser = argparse.ArgumentParser(description='Amalgamate fast_float.') parser = argparse.ArgumentParser(description="Amalgamate fast_float.")
parser.add_argument('--license', default='TRIPLE', choices=['DUAL', 'TRIPLE', 'MIT', 'APACHE', 'BOOST'], help='choose license') parser.add_argument(
parser.add_argument('--output', default='', help='output file (stdout if none') "--license",
default="TRIPLE",
choices=["DUAL", "TRIPLE", "MIT", "APACHE", "BOOST"],
help="choose license",
)
parser.add_argument("--output", default="", help="output file (stdout if none")
args = parser.parse_args() args = parser.parse_args()
def license_content(license_arg): def license_content(license_arg):
result = [] result = []
if license_arg == 'TRIPLE': if license_arg == "TRIPLE":
result += [ result += [
'// Licensed under the Apache License, Version 2.0, or the\n', "// Licensed under the Apache License, Version 2.0, or the\n",
'// MIT License or the Boost License. This file may not be copied,\n', "// MIT License or the Boost License. This file may not be copied,\n",
'// modified, or distributed except according to those terms.\n', "// modified, or distributed except according to those terms.\n",
'//\n' "//\n",
] ]
if license_arg == 'DUAL': if license_arg == "DUAL":
result += [ result += [
'// Licensed under the Apache License, Version 2.0, or the\n', "// Licensed under the Apache License, Version 2.0, or the\n",
'// MIT License at your option. This file may not be copied,\n', "// MIT License at your option. This file may not be copied,\n",
'// modified, or distributed except according to those terms.\n', "// modified, or distributed except according to those terms.\n",
'//\n' "//\n",
] ]
if license_arg in ('DUAL', 'TRIPLE', 'MIT'): if license_arg in ("DUAL", "TRIPLE", "MIT"):
result.append('// MIT License Notice\n//\n') result.append("// MIT License Notice\n//\n")
result.append(processed_files['LICENSE-MIT']) result.append(processed_files["LICENSE-MIT"])
result.append('//\n') result.append("//\n")
if license_arg in ('DUAL', 'TRIPLE', 'APACHE'): if license_arg in ("DUAL", "TRIPLE", "APACHE"):
result.append('// Apache License (Version 2.0) Notice\n//\n') result.append("// Apache License (Version 2.0) Notice\n//\n")
result.append(processed_files['LICENSE-APACHE']) result.append(processed_files["LICENSE-APACHE"])
result.append('//\n') result.append("//\n")
if license_arg in ('TRIPLE', 'BOOST'): if license_arg in ("TRIPLE", "BOOST"):
result.append('// BOOST License Notice\n//\n') result.append("// BOOST License Notice\n//\n")
result.append(processed_files['LICENSE-BOOST']) result.append(processed_files["LICENSE-BOOST"])
result.append('//\n') result.append("//\n")
return result return result
text = ''.join([
processed_files['AUTHORS'], processed_files['CONTRIBUTORS'], text = "".join(
[
processed_files["AUTHORS"],
processed_files["CONTRIBUTORS"],
*license_content(args.license), *license_content(args.license),
processed_files['constexpr_feature_detect.h'], processed_files["constexpr_feature_detect.h"],
processed_files['float_common.h'], processed_files['fast_float.h'], processed_files["float_common.h"],
processed_files['ascii_number.h'], processed_files['fast_table.h'], processed_files["fast_float.h"],
processed_files['decimal_to_binary.h'], processed_files['bigint.h'], processed_files["ascii_number.h"],
processed_files['digit_comparison.h'], processed_files['parse_number.h']]) processed_files["fast_table.h"],
processed_files["decimal_to_binary.h"],
processed_files["bigint.h"],
processed_files["digit_comparison.h"],
processed_files["parse_number.h"],
]
)
if args.output: if args.output:
with open(args.output, 'wt', encoding='utf8') as f: with open(args.output, "wt", encoding="utf8") as f:
f.write(text) f.write(text)
else: else:
print(text) print(text)

View File

@ -1,36 +1,38 @@
import sys
from math import floor from math import floor
def log2(x): def log2(x):
"""returns ceil(log2(x)))""" """returns ceil(log2(x)))"""
y = 0 y = 0
while((1<<y) < x): while (1 << y) < x:
y = y + 1 y = y + 1
return y return y
for q in range(1,17+1): for q in range(1, 17 + 1):
d = 5**q d = 5 ** q
b = 127 + log2(d) b = 127 + log2(d)
t = 2** b t = 2 ** b
c = t//d + 1 c = t // d + 1
assert c < 2**128 assert c < 2 ** 128
assert c >= 2**127 assert c >= 2 ** 127
K = 2**127 K = 2 ** 127
if(not(c * K * d<=( K + 1) * t)): if not (c * K * d <= (K + 1) * t):
print(q) print(q)
top = floor(t/(c * d - t)) top = floor(t / (c * d - t))
sys.exit(-1) sys.exit(-1)
for q in range(18, 344+1): for q in range(18, 344 + 1):
d = 5**q d = 5 ** q
b = 64 + 2*log2(d) b = 64 + 2 * log2(d)
t = 2**b t = 2 ** b
c = t//d + 1 c = t // d + 1
assert c > 2**(64 +log2(d)) assert c > 2 ** (64 + log2(d))
K = 2**64 K = 2 ** 64
if(not(c * K * d<=( K + 1) * t)): if not (c * K * d <= (K + 1) * t):
print(q) print(q)
top = floor(t/(c * d - t)) top = floor(t / (c * d - t))
sys.exit(-1) sys.exit(-1)
print("all good") print("all good")

View File

@ -9,25 +9,25 @@ all_tqs = []
# Appendix B of Number parsing at a gigabyte per second. # Appendix B of Number parsing at a gigabyte per second.
# Software: Practice and Experience 2021;51(8):17001727. # Software: Practice and Experience 2021;51(8):17001727.
for q in range(-342, -27): for q in range(-342, -27):
power5 = 5**-q power5 = 5 ** -q
z = 0 z = 0
while (1 << z) < power5: while (1 << z) < power5:
z += 1 z += 1
b = 2 * z + 2 * 64 b = 2 * z + 2 * 64
c = 2**b // power5 + 1 c = 2 ** b // power5 + 1
while c >= (1 << 128): while c >= (1 << 128):
c //= 2 c //= 2
all_tqs.append(c) all_tqs.append(c)
for q in range(-27, 0): for q in range(-27, 0):
power5 = 5**-q power5 = 5 ** -q
z = 0 z = 0
while (1 << z) < power5: while (1 << z) < power5:
z += 1 z += 1
b = z + 127 b = z + 127
c = 2**b // power5 + 1 c = 2 ** b // power5 + 1
all_tqs.append(c) all_tqs.append(c)
for q in range(0, 308 + 1): for q in range(0, 308 + 1):
power5 = 5**q power5 = 5 ** q
while power5 < (1 << 127): while power5 < (1 << 127):
power5 *= 2 power5 *= 2
while power5 >= (1 << 128): while power5 >= (1 << 128):
@ -44,6 +44,7 @@ def continued_fraction(numer, denom):
numer, denom = denom, rem numer, denom = denom, rem
return cf return cf
# Given a continued fraction [a0; a1, a2, ..., an], returns # Given a continued fraction [a0; a1, a2, ..., an], returns
# all the convergents of that continued fraction # all the convergents of that continued fraction
# as pairs of the form (numer, denom), where numer/denom is # as pairs of the form (numer, denom), where numer/denom is
@ -58,17 +59,22 @@ def convergents(cf):
p_n = a_n * p_n_minus_1 + p_n_minus_2 p_n = a_n * p_n_minus_1 + p_n_minus_2
q_n = a_n * q_n_minus_1 + q_n_minus_2 q_n = a_n * q_n_minus_1 + q_n_minus_2
convergents.append((p_n, q_n)) convergents.append((p_n, q_n))
p_n_minus_2, q_n_minus_2, p_n_minus_1, q_n_minus_1 = p_n_minus_1, q_n_minus_1, p_n, q_n p_n_minus_2, q_n_minus_2, p_n_minus_1, q_n_minus_1 = (
p_n_minus_1,
q_n_minus_1,
p_n,
q_n,
)
return convergents return convergents
# Enumerate through all the convergents of T[q] / 2^137 with denominators < 2^64 # Enumerate through all the convergents of T[q] / 2^137 with denominators < 2^64
found_solution = False found_solution = False
for j, tq in enumerate(all_tqs): for j, tq in enumerate(all_tqs):
for _, w in convergents(continued_fraction(tq, 2**137)): for _, w in convergents(continued_fraction(tq, 2 ** 137)):
if w >= 2**64: if w >= 2 ** 64:
break break
if (tq*w) % 2**137 > 2**137 - 2**64: if (tq * w) % 2 ** 137 > 2 ** 137 - 2 ** 64:
print(f"SOLUTION: q={j-342} T[q]={tq} w={w}") print(f"SOLUTION: q={j-342} T[q]={tq} w={w}")
found_solution = True found_solution = True
if not found_solution: if not found_solution:

View File

@ -8,129 +8,176 @@ import subprocess
import io import io
import os import os
import fileinput import fileinput
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
sys.stdout.write("Sorry, requires Python 3.x or better\n") sys.stdout.write("Sorry, requires Python 3.x or better\n")
sys.exit(1) sys.exit(1)
def colored(r, g, b, text): def colored(r, g, b, text):
return "\033[38;2;{};{};{}m{} \033[38;2;255;255;255m".format(r, g, b, text) return f"\033[38;2;{r};{g};{b}m{text} \033[38;2;255;255;255m"
def extractnumbers(s): def extractnumbers(s):
return tuple(map(int,re.findall(r"(\d+)\.(\d+)\.(\d+)",str(s))[0])) return tuple(map(int, re.findall(r"(\d+)\.(\d+)\.(\d+)", str(s))[0]))
def toversionstring(major, minor, rev): def toversionstring(major, minor, rev):
return str(major)+"."+str(minor)+"."+str(rev) return f"{major}.{minor}.{rev}"
def topaddedversionstring(major, minor, rev):
return str(major)+str(minor).zfill(3)+str(rev).zfill(3)
print("Calling git rev-parse --abbrev-ref HEAD") print("Calling git rev-parse --abbrev-ref HEAD")
pipe = subprocess.Popen(["git", "rev-parse", "--abbrev-ref", "HEAD"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) pipe = subprocess.Popen(
["git", "rev-parse", "--abbrev-ref", "HEAD"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
branchresult = pipe.communicate()[0].decode().strip() branchresult = pipe.communicate()[0].decode().strip()
if(branchresult != "main"): if branchresult != "main":
print(colored(255, 0, 0, "We recommend that you release on main, you are on '"+branchresult+"'")) print(
colored(
255,
0,
0,
f"We recommend that you release on main, you are on '{branchresult}'",
)
)
ret = subprocess.call(["git", "remote", "update"]) ret = subprocess.call(["git", "remote", "update"])
if(ret != 0): if ret != 0:
sys.exit(ret) sys.exit(ret)
print("Calling git log HEAD.. --oneline") print("Calling git log HEAD.. --oneline")
pipe = subprocess.Popen(["git", "log", "HEAD..", "--oneline"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) pipe = subprocess.Popen(
["git", "log", "HEAD..", "--oneline"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
uptodateresult = pipe.communicate()[0].decode().strip() uptodateresult = pipe.communicate()[0].decode().strip()
if(len(uptodateresult) != 0): if len(uptodateresult) != 0:
print(uptodateresult) print(uptodateresult)
sys.exit(-1) sys.exit(-1)
pipe = subprocess.Popen(["git", "rev-parse", "--show-toplevel"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) pipe = subprocess.Popen(
["git", "rev-parse", "--show-toplevel"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
maindir = pipe.communicate()[0].decode().strip() maindir = pipe.communicate()[0].decode().strip()
scriptlocation = os.path.dirname(os.path.abspath(__file__)) scriptlocation = os.path.dirname(os.path.abspath(__file__))
print("repository: "+maindir) print(f"repository: {maindir}")
pipe = subprocess.Popen(["git", "describe", "--abbrev=0", "--tags"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) pipe = subprocess.Popen(
["git", "describe", "--abbrev=0", "--tags"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
versionresult = pipe.communicate()[0].decode().strip() versionresult = pipe.communicate()[0].decode().strip()
print("last version: "+versionresult ) print(f"last version: {versionresult}")
try: try:
currentv = extractnumbers(versionresult) currentv = extractnumbers(versionresult)
except: except:
currentv = [0,0,0] currentv = [0, 0, 0]
if(len(sys.argv) != 2): if len(sys.argv) != 2:
nextv = (currentv[0],currentv[1], currentv[2]+1) nextv = (currentv[0], currentv[1], currentv[2] + 1)
print ("please specify version number, e.g. "+toversionstring(*nextv)) print(f"please specify version number, e.g. {toversionstring(*nextv)}")
sys.exit(-1) sys.exit(-1)
try: try:
newversion = extractnumbers(sys.argv[1]) newversion = extractnumbers(sys.argv[1])
print(newversion) print(newversion)
except: except:
print("can't parse version number "+sys.argv[1]) print(f"can't parse version number {sys.argv[1]}")
sys.exit(-1) sys.exit(-1)
print("checking that new version is valid") print("checking that new version is valid")
if(newversion[0] != currentv[0]): if newversion[0] != currentv[0]:
assert newversion[0] == currentv[0] + 1 assert newversion[0] == currentv[0] + 1
assert newversion[1] == 0 assert newversion[1] == 0
assert newversion[2] == 0 assert newversion[2] == 0
elif (newversion[1] != currentv[1]): elif newversion[1] != currentv[1]:
assert newversion[1] == currentv[1] + 1 assert newversion[1] == currentv[1] + 1
assert newversion[2] == 0 assert newversion[2] == 0
else : else:
assert newversion[2] == currentv[2] + 1 assert newversion[2] == currentv[2] + 1
atleastminor= (currentv[0] != newversion[0]) or (currentv[1] != newversion[1]) atleastminor = (currentv[0] != newversion[0]) or (currentv[1] != newversion[1])
newmajorversionstring = str(newversion[0]) newmajorversionstring = str(newversion[0])
mewminorversionstring = str(newversion[1]) newminorversionstring = str(newversion[1])
newrevversionstring = str(newversion[2]) newpatchversionstring = str(newversion[2])
newversionstring = str(newversion[0]) + "." + str(newversion[1]) + "." + str(newversion[2]) newversionstring = f"{newversion[0]}.{newversion[1]}.{newversion[2]}"
cmakefile = maindir + os.sep + "CMakeLists.txt" cmakefile = f"{maindir}{os.sep}CMakeLists.txt"
for line in fileinput.input(cmakefile, inplace=1, backup=".bak"):
for line in fileinput.input(cmakefile, inplace=1, backup='.bak'): line = re.sub(
line = re.sub(r'project\(fast_float VERSION \d+\.\d+\.\d+ LANGUAGES CXX\)','project(fast_float VERSION '+newmajorversionstring+'.'+mewminorversionstring+'.'+newrevversionstring+" LANGUAGES CXX)", line.rstrip()) r"project\(fast_float VERSION \d+\.\d+\.\d+ LANGUAGES CXX\)",
f"project(fast_float VERSION {newversionstring} LANGUAGES CXX)",
line.rstrip(),
)
print(line) print(line)
print("modified "+cmakefile+", a backup was made") print(f"modified {cmakefile}, a backup was made")
versionfilerel = f"{os.sep}include{os.sep}fast_float{os.sep}float_common.h"
versionfile = f"{maindir}{versionfilerel}"
for line in fileinput.input(versionfile, inplace=1, backup=".bak"):
versionfilerel = os.sep + "include" + os.sep + "fast_float" + os.sep + "float_common.h" line = re.sub(
versionfile = maindir + versionfilerel r"#define FASTFLOAT_VERSION_MAJOR \d+",
f"#define FASTFLOAT_VERSION_MAJOR {newmajorversionstring}",
for line in fileinput.input(versionfile, inplace=1, backup='.bak'): line.rstrip(),
line = re.sub(r'#define FASTFLOAT_VERSION_MAJOR \d+','#define FASTFLOAT_VERSION_MAJOR '+newmajorversionstring, line.rstrip()) )
line = re.sub(r'#define FASTFLOAT_VERSION_MINOR \d+','#define FASTFLOAT_VERSION_MAJOR '+mewminorversionstring, line.rstrip()) line = re.sub(
line = re.sub(r'#define FASTFLOAT_VERSION_PATCH \d+','#define FASTFLOAT_VERSION_MAJOR '+newrevversionstring, line.rstrip()) r"#define FASTFLOAT_VERSION_MINOR \d+",
f"#define FASTFLOAT_VERSION_MINOR {newminorversionstring}",
line.rstrip(),
)
line = re.sub(
r"#define FASTFLOAT_VERSION_PATCH \d+",
f"#define FASTFLOAT_VERSION_PATCH {newpatchversionstring}",
line.rstrip(),
)
print(line) print(line)
print(versionfile + " modified") print(f"{versionfile} modified")
readmefile = f"{maindir}{os.sep}README.md"
readmefile = maindir + os.sep + "README.md" for line in fileinput.input(readmefile, inplace=1, backup=".bak"):
line = re.sub(
r"https://github.com/fastfloat/fast_float/releases/download/v(\d+\.\d+\.\d+)/fast_float.h",
for line in fileinput.input(readmefile, inplace=1, backup='.bak'): f"https://github.com/fastfloat/fast_float/releases/download/v{newversionstring}/fast_float.h",
line = re.sub(r'https://github.com/fastfloat/fast_float/releases/download/v(\d+\.\d+\.\d+)/fast_float.h','https://github.com/fastfloat/fast_float/releases/download/v'+newmajorversionstring+'.'+mewminorversionstring+'.'+newrevversionstring+'/fast_float.h', line.rstrip()) line.rstrip(),
)
line = re.sub(
r"GIT_TAG tags/v(\d+\.\d+\.\d+)",
f"GIT_TAG tags/v{newversionstring}",
line.rstrip(),
)
line = re.sub(
r"GIT_TAG v(\d+\.\d+\.\d+)\)", f"GIT_TAG v{newversionstring})", line.rstrip()
)
print(line) print(line)
print("modified "+readmefile+", a backup was made") print(f"modified {readmefile}, a backup was made")
print("running amalgamate.py") print("running amalgamate.py")
with open(maindir+ os.sep + 'fast_float.h', "w") as outfile: with open(f"{maindir}{os.sep}fast_float.h", "w") as outfile:
cp = subprocess.run(["python3", maindir+ os.sep + "script/amalgamate.py"], stdout=outfile) cp = subprocess.run(
[f"python3", f"{maindir}{os.sep}script{os.sep}amalgamate.py"], stdout=outfile
)
if(cp.returncode != 0): if cp.returncode != 0:
print("Failed to run amalgamate") print("Failed to run amalgamate")
else: else:
print("amalgamate.py ran successfully") print("amalgamate.py ran successfully")
print("You should upload "+ maindir+ os.sep + 'fast_float.h') print(f"You should upload {maindir}{os.sep}fast_float.h")
print("Please run the tests before issuing a release. \n")
print("to issue release, enter \n git commit -a && git push && git tag -a v"+toversionstring(*newversion)+" -m \"version "+toversionstring(*newversion)+"\" && git push --tags \n")
print("Please run the tests before issuing a release.\n")
print(
f'to issue release, enter\n git commit -a && git push && git tag -a v{toversionstring(*newversion)} -m "version {toversionstring(*newversion)}" && git push --tags\n'
)

View File

@ -1,14 +1,15 @@
def format(number): def format(number):
upper = number // (1<<64) upper = number // (1 << 64)
lower = number % (1<<64) lower = number % (1 << 64)
print(""+hex(upper)+","+hex(lower)+",") print("" + hex(upper) + "," + hex(lower) + ",")
for q in range(-342,0):
for q in range(-342, 0):
power5 = 5 ** -q power5 = 5 ** -q
z = 0 z = 0
while( (1<<z) < power5) : while (1 << z) < power5:
z += 1 z += 1
if(q >= -27): if q >= -27:
b = z + 127 b = z + 127
c = 2 ** b // power5 + 1 c = 2 ** b // power5 + 1
format(c) format(c)
@ -16,16 +17,16 @@ for q in range(-342,0):
b = 2 * z + 2 * 64 b = 2 * z + 2 * 64
c = 2 ** b // power5 + 1 c = 2 ** b // power5 + 1
# truncate # truncate
while(c >= (1<<128)): while c >= (1 << 128):
c //= 2 c //= 2
format(c) format(c)
for q in range(0,308+1): for q in range(0, 308 + 1):
power5 = 5 ** q power5 = 5 ** q
# move the most significant bit in position # move the most significant bit in position
while(power5 < (1<<127)): while power5 < (1 << 127):
power5 *= 2 power5 *= 2
# *truncate* # *truncate*
while(power5 >= (1<<128)): while power5 >= (1 << 128):
power5 //= 2 power5 //= 2
format(power5) format(power5)