diff --git a/binary-array-set/binaryarrayset.py b/binary-array-set/binaryarrayset.py index 417bb31b..068af43b 100644 --- a/binary-array-set/binaryarrayset.py +++ b/binary-array-set/binaryarrayset.py @@ -48,8 +48,7 @@ def clear(self): def __iter__(self): for vals in self.values: if vals is not None: - for val in vals: - yield val + yield from vals # Runs in O((log n)^2) time diff --git a/bittorrent-bencode-format-tools/bencode.py b/bittorrent-bencode-format-tools/bencode.py index 18d1e8cf..e2799581 100644 --- a/bittorrent-bencode-format-tools/bencode.py +++ b/bittorrent-bencode-format-tools/bencode.py @@ -25,7 +25,6 @@ Bencode supports four types of values: * Integer, which is mapped to Python int. * Byte string, which is mapped to Python bytes or bytearray. - (Note that in Python 2, bytes is a synonym for str.) * List, which is mapped to Python list or tuple, such that every element is a bencode value. * Dictionary, which is mapped to Python dict, such that every key is a bytes object and every value is a bencode value.""" diff --git a/bittorrent-bencode-format-tools/decode-bencode-demo.py b/bittorrent-bencode-format-tools/decode-bencode-demo.py index 32df8c36..9e0285d9 100644 --- a/bittorrent-bencode-format-tools/decode-bencode-demo.py +++ b/bittorrent-bencode-format-tools/decode-bencode-demo.py @@ -21,7 +21,6 @@ # Software. # -from __future__ import print_function import os, sys import bencode diff --git a/btree-set/btreeset.py b/btree-set/btreeset.py index 41c1d715..bacc4fe7 100644 --- a/btree-set/btreeset.py +++ b/btree-set/btreeset.py @@ -167,8 +167,7 @@ def push_left_path(node): node, index = stack.pop() if node.is_leaf(): assert index == 0 - for obj in node.keys: - yield obj + yield from node.keys else: yield node.keys[index] index += 1 diff --git a/forcing-a-files-crc-to-any-value/forcecrc32.py b/forcing-a-files-crc-to-any-value/forcecrc32.py index 328c3bee..e4239484 100644 --- a/forcing-a-files-crc-to-any-value/forcecrc32.py +++ b/forcing-a-files-crc-to-any-value/forcecrc32.py @@ -58,7 +58,7 @@ def main(args): # ---- Main function ---- -# Public library function. path is str/unicode, offset is uint, newcrc is uint32, printstatus is bool. +# Public library function. path is str, offset is uint, newcrc is uint32, printstatus is bool. # Returns None. May raise IOError, ValueError, AssertionError. def modify_file_crc32(path, offset, newcrc, printstatus=False): with open(path, "r+b") as raf: @@ -107,7 +107,7 @@ def get_crc32(raf): while True: buffer = raf.read(128 * 1024) if len(buffer) == 0: - return reverse32(crc & MASK) + return reverse32(crc) crc = zlib.crc32(buffer, crc) diff --git a/free-small-fft-in-multiple-languages/fft-test.py b/free-small-fft-in-multiple-languages/fft-test.py index 37774638..dc22d9b9 100644 --- a/free-small-fft-in-multiple-languages/fft-test.py +++ b/free-small-fft-in-multiple-languages/fft-test.py @@ -111,7 +111,7 @@ def _naive_convolution(x, y): # ---- Utility functions ---- -_maxlogerr = float("-inf") +_maxlogerr = -math.inf def _log10_rms_err(x, y): global _maxlogerr diff --git a/gauss-jordan-elimination-over-any-field/fieldmath.py b/gauss-jordan-elimination-over-any-field/fieldmath.py index 932e34e4..ac1a474c 100644 --- a/gauss-jordan-elimination-over-any-field/fieldmath.py +++ b/gauss-jordan-elimination-over-any-field/fieldmath.py @@ -6,7 +6,7 @@ # https://www.nayuki.io/page/gauss-jordan-elimination-over-any-field # -import fractions +import fractions, math # ---- Field abstract class ---- @@ -366,7 +366,7 @@ def __init__(self, a, b, c, d): a = -a b = -b c = -c - gcd = fractions.gcd(fractions.gcd(a, b), c) + gcd = math.gcd(math.gcd(a, b), c) if gcd != 1: a //= gcd b //= gcd diff --git a/karatsuba-multiplication/karatsuba.py b/karatsuba-multiplication/karatsuba.py index 83e7ba7f..c42ca6e3 100644 --- a/karatsuba-multiplication/karatsuba.py +++ b/karatsuba-multiplication/karatsuba.py @@ -1,13 +1,11 @@ # # Karatsuba fast multiplication algorithm (Python) # -# Copyright (c) 2014 Project Nayuki +# Copyright (c) 2020 Project Nayuki # All rights reserved. Contact Nayuki for licensing. # https://www.nayuki.io/page/karatsuba-multiplication # -# Requires Python version >= 2.7 because of long.bit_length(). - # Requirement: _CUTOFF >= 64, or else there will be infinite recursion. _CUTOFF = 1536 diff --git a/montgomery-reduction-algorithm/montgomery-reducer.py b/montgomery-reduction-algorithm/montgomery-reducer.py index 43c83d83..62e3a865 100644 --- a/montgomery-reduction-algorithm/montgomery-reducer.py +++ b/montgomery-reduction-algorithm/montgomery-reducer.py @@ -6,7 +6,7 @@ # https://www.nayuki.io/page/montgomery-reduction-algorithm # -import fractions, random, unittest +import math, random, unittest class MontgomeryReducerTest(unittest.TestCase): @@ -48,7 +48,7 @@ def __init__(self, mod): self.reducerbits = (mod.bit_length() // 8 + 1) * 8 # This is a multiple of 8 self.reducer = 1 << self.reducerbits # This is a power of 256 self.mask = self.reducer - 1 - assert self.reducer > mod and fractions.gcd(self.reducer, mod) == 1 + assert self.reducer > mod and math.gcd(self.reducer, mod) == 1 # Other computed numbers self.reciprocal = MontgomeryReducer.reciprocal_mod(self.reducer % mod, mod) diff --git a/optimizing-brainfuck-compiler/bfc.py b/optimizing-brainfuck-compiler/bfc.py index a89943c9..fc834119 100644 --- a/optimizing-brainfuck-compiler/bfc.py +++ b/optimizing-brainfuck-compiler/bfc.py @@ -21,13 +21,13 @@ def main(args): inname = args[0] if not os.path.isfile(inname): - return inname + ": Not a file" + return f"{inname}: Not a file" outname = args[1] if outname.endswith(".c" ): outfunc = commands_to_c elif outname.endswith(".java"): outfunc = commands_to_java elif outname.endswith(".py" ): outfunc = commands_to_python - else: return outname + ": Unknown output type" + else: return f"{outname}: Unknown output type" # Read input with open(inname, "rt") as fin: diff --git a/windows-timestamp-accessor-library/wintimestamp-demo.py b/windows-timestamp-accessor-library/wintimestamp-demo.py index ac18a1e5..08d276cb 100644 --- a/windows-timestamp-accessor-library/wintimestamp-demo.py +++ b/windows-timestamp-accessor-library/wintimestamp-demo.py @@ -1,4 +1,3 @@ -# -*- coding: UTF-8 -*- # # Windows timestamp accessor demo (Python) # diff --git a/windows-timestamp-accessor-library/wintimestamp.py b/windows-timestamp-accessor-library/wintimestamp.py index 5612e9c9..7b69ce1d 100644 --- a/windows-timestamp-accessor-library/wintimestamp.py +++ b/windows-timestamp-accessor-library/wintimestamp.py @@ -39,7 +39,7 @@ def __exit__(self, type, value, traceback): self.close() - # Get-methods return the number of ticks as an int; path can be of type str or unicode + # Get-methods return the number of ticks as an int; path is of type str def get_creation_time(self, path): return self._get_some_time("Creation", path) @@ -59,7 +59,7 @@ def _get_some_time(self, type, path): return int(tokens[1]) - # Set-methods require the number of ticks to be an int; path can be of type str or unicode + # Set-methods require the number of ticks to be an int; path is of type str def set_creation_time(self, path, ticks): self._set_some_time("Creation", path, ticks)