Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 4f80136

Browse files
committed
t-28012: collections.abc
1 parent 5ec24db commit 4f80136

File tree

27 files changed

+296
-288
lines changed

27 files changed

+296
-288
lines changed

src/sage/arith/misc.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
"""
2+
r"""
33
Miscellaneous arithmetic functions
44
"""
55

@@ -13,16 +13,15 @@
1313
# https://www.gnu.org/licenses/
1414
# ****************************************************************************
1515

16-
from __future__ import absolute_import, print_function
17-
1816
import math
19-
import collections
17+
from collections.abc import Iterable
2018

2119
from sage.misc.misc import powerset
2220
from sage.misc.misc_c import prod
2321

2422
from sage.libs.pari.all import pari
25-
import sage.libs.flint.arith as flint_arith
23+
from sage.libs.flint.arith import (bernoulli_number as flint_bernoulli,
24+
dedekind_sum as flint_dedekind_sum)
2625

2726
from sage.structure.element import parent
2827
from sage.structure.coerce import py_scalar_to_element
@@ -34,16 +33,16 @@
3433
from sage.rings.real_mpfr import RealNumber
3534
from sage.rings.complex_number import ComplexNumber
3635

37-
import sage.rings.fast_arith as fast_arith
38-
prime_range = fast_arith.prime_range
36+
from sage.rings.fast_arith import arith_int, arith_llong, prime_range
3937

4038

4139
##################################################################
4240
# Elementary Arithmetic
4341
##################################################################
4442

4543

46-
def algdep(z, degree, known_bits=None, use_bits=None, known_digits=None, use_digits=None, height_bound=None, proof=False):
44+
def algdep(z, degree, known_bits=None, use_bits=None, known_digits=None,
45+
use_digits=None, height_bound=None, proof=False):
4746
"""
4847
Return an irreducible polynomial of degree at most `degree` which
4948
is approximately satisfied by the number `z`.
@@ -367,7 +366,7 @@ def bernoulli(n, algorithm='default', num_threads=1):
367366
if n >= 100000:
368367
from warnings import warn
369368
warn("flint is known to not be accurate for large Bernoulli numbers")
370-
return flint_arith.bernoulli_number(n)
369+
return flint_bernoulli(n)
371370
elif algorithm == 'pari':
372371
x = pari(n).bernfrac() # Use the PARI C library
373372
return Rational(x)
@@ -2108,9 +2107,9 @@ def get_gcd(order):
21082107
<function gcd at ...>
21092108
"""
21102109
if order <= 46340: # todo: don't hard code
2111-
return fast_arith.arith_int().gcd_int
2110+
return arith_int().gcd_int
21122111
elif order <= 2147483647: # todo: don't hard code
2113-
return fast_arith.arith_llong().gcd_longlong
2112+
return arith_llong().gcd_longlong
21142113
else:
21152114
return gcd
21162115

@@ -2130,9 +2129,9 @@ def get_inverse_mod(order):
21302129
<function inverse_mod at ...>
21312130
"""
21322131
if order <= 46340: # todo: don't hard code
2133-
return fast_arith.arith_int().inverse_mod_int
2132+
return arith_int().inverse_mod_int
21342133
elif order <= 2147483647: # todo: don't hard code
2135-
return fast_arith.arith_llong().inverse_mod_longlong
2134+
return arith_llong().inverse_mod_longlong
21362135
else:
21372136
return inverse_mod
21382137

@@ -3651,7 +3650,7 @@ def multinomial(*ks):
36513650
36523651
- Gabriel Ebner
36533652
"""
3654-
if isinstance(ks[0], collections.Iterable):
3653+
if isinstance(ks[0], Iterable):
36553654
if len(ks) > 1:
36563655
raise ValueError("multinomial takes only one iterable argument")
36573656
ks = ks[0]
@@ -5840,7 +5839,7 @@ def dedekind_sum(p, q, algorithm='default'):
58405839
- :wikipedia:`Dedekind\_sum`
58415840
"""
58425841
if algorithm == 'default' or algorithm == 'flint':
5843-
return flint_arith.dedekind_sum(p, q)
5842+
return flint_dedekind_sum(p, q)
58445843

58455844
if algorithm == 'pari':
58465845
import sage.interfaces.gp

src/sage/categories/poor_man_map.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# -*- coding: utf-8 -*-
2-
"""
2+
r"""
33
Poor Man's map
44
"""
5-
#*****************************************************************************
5+
# ****************************************************************************
66
# Copyright (C) 2009 Nicolas M. Thiery <nthiery at users.sf.net>
77
# 2016 Julian Rüth <[email protected]>
88
#
99
# This program is free software: you can redistribute it and/or modify
1010
# it under the terms of the GNU General Public License as published by
1111
# the Free Software Foundation, either version 2 of the License, or
1212
# (at your option) any later version.
13-
# http://www.gnu.org/licenses/
14-
#*****************************************************************************
13+
# https://www.gnu.org/licenses/
14+
# ****************************************************************************
1515
import sage.structure.sage_object
1616

1717
class PoorManMap(sage.structure.sage_object.SageObject):
@@ -69,7 +69,7 @@ def __init__(self, function, domain = None, codomain = None, name = None):
6969
sage: TestSuite(f*g).run()
7070
7171
"""
72-
from collections import Iterable
72+
from collections.abc import Iterable
7373
if not isinstance(function, Iterable):
7474
function = (function,)
7575
self._functions = tuple(function)

src/sage/combinat/finite_state_machine.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -924,16 +924,17 @@
924924
# 2012--2015 Daniel Krenn <[email protected]>
925925
# 2012--2015 Sara Kropf <[email protected]>
926926
#
927-
# Distributed under the terms of the GNU General Public License (GPL)
928-
# as published by the Free Software Foundation; either version 2 of
929-
# the License, or (at your option) any later version.
930-
# https://www.gnu.org/licenses/
927+
# This program is free software: you can redistribute it and/or modify
928+
# it under the terms of the GNU General Public License as published by
929+
# the Free Software Foundation, either version 2 of the License, or
930+
# (at your option) any later version.
931+
# https://www.gnu.org/licenses/
931932
# ****************************************************************************
932-
from __future__ import print_function
933933

934934
from IPython.lib.pretty import pretty
935-
import collections
936935
import itertools
936+
from collections import defaultdict, deque, namedtuple, OrderedDict
937+
from collections.abc import Iterator
937938
from copy import copy, deepcopy
938939

939940
from sage.calculus.var import var
@@ -964,13 +965,13 @@ def full_group_by(l, key=lambda x: x):
964965
A list of pairs ``(k, elements)`` such that ``key(e)=k`` for all
965966
``e`` in ``elements``.
966967

967-
This is similar to ``itertools.groupby`` except that lists are
968+
This is similar to :func:`itertools.groupby` except that lists are
968969
returned instead of iterables and no prior sorting is required.
969970

970971
We do not require
971972

972973
- that the keys are sortable (in contrast to the
973-
approach via ``sorted`` and ``itertools.groupby``) and
974+
approach via :func:`sorted` and :func:`itertools.groupby`) and
974975
- that the keys are hashable (in contrast to the
975976
implementation proposed in `<https://stackoverflow.com/a/15250161>`_).
976977

@@ -998,13 +999,13 @@ def full_group_by(l, key=lambda x: x):
998999
1/x [1]
9991000
2/x [2]
10001001

1001-
Note that the behavior is different from ``itertools.groupby``
1002+
Note that the behavior is different from :func:`itertools.groupby`
10021003
because neither `1/x<2/x` nor `2/x<1/x` does hold.
10031004

10041005
Here, the result ``r`` has been sorted in order to guarantee a
10051006
consistent order for the doctest suite.
10061007
"""
1007-
elements = collections.defaultdict(list)
1008+
elements = defaultdict(list)
10081009
original_keys = {}
10091010
for item in l:
10101011
k = key(item)
@@ -4924,7 +4925,7 @@ def key_function(s):
49244925
# transitions have to be sorted anyway, the performance
49254926
# penalty should be bearable; nevertheless, this is only
49264927
# required for doctests.
4927-
adjacent = collections.OrderedDict(
4928+
adjacent = OrderedDict(
49284929
(pair, list(transitions))
49294930
for pair, transitions in
49304931
itertools.groupby(
@@ -13143,7 +13144,7 @@ def __init__(self, tape_cache_manager, tape, tape_ended,
1314313144

1314413145
self.tape_cache_manager = tape_cache_manager
1314513146
self.tape_cache_manager.append(self)
13146-
self.cache = tuple(collections.deque() for _ in self.tape)
13147+
self.cache = tuple(deque() for _ in self.tape)
1314713148

1314813149
def _repr_(self):
1314913150
"""
@@ -13927,8 +13928,7 @@ def is_FSMProcessIterator(PI):
1392713928
# ****************************************************************************
1392813929

1392913930

13930-
class FSMProcessIterator(SageObject,
13931-
collections.Iterator):
13931+
class FSMProcessIterator(SageObject, Iterator):
1393213932
"""
1393313933
This class takes an input, feeds it into a finite state machine
1393413934
(automaton or transducer, in particular), tests whether this was
@@ -14215,7 +14215,7 @@ def __repr__(self):
1421514215
return result
1421614216

1421714217

14218-
FinishedBranch = collections.namedtuple('Branch', 'accept, state, output')
14218+
FinishedBranch = namedtuple('Branch', 'accept, state, output')
1421914219
r"""
1422014220
A :func:`named tuple <collections.namedtuple>` representing the
1422114221
attributes of a branch, once
@@ -14311,7 +14311,7 @@ def __init__(self, fsm,
1431114311
self._finished_ = [] # contains (accept, state, output)
1431214312

1431314313

14314-
_branch_ = collections.namedtuple('Branch', 'tape_cache, outputs')
14314+
_branch_ = namedtuple('Branch', 'tape_cache, outputs')
1431514315
r"""
1431614316
A :func:`named tuple <collections.namedtuple>` representing the
1431714317
attributes of a branch at a particular state during processing.

src/sage/combinat/finite_state_machine_generators.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: utf-8 -*-
12
r"""
23
Common Automata and Transducers (Finite State Machines Generators)
34
@@ -76,19 +77,19 @@
7677
---------------------
7778
7879
"""
79-
#*****************************************************************************
80+
# ****************************************************************************
8081
# Copyright (C) 2014--2015 Clemens Heuberger <[email protected]>
8182
# 2014--2015 Daniel Krenn <[email protected]>
8283
# 2014 Sara Kropf <[email protected]>
8384
#
84-
# Distributed under the terms of the GNU General Public License (GPL)
85-
# as published by the Free Software Foundation; either version 2 of
86-
# the License, or (at your option) any later version.
87-
# http://www.gnu.org/licenses/
88-
#*****************************************************************************
89-
from __future__ import print_function
90-
91-
import collections
85+
# This program is free software: you can redistribute it and/or modify
86+
# it under the terms of the GNU General Public License as published by
87+
# the Free Software Foundation, either version 2 of the License, or
88+
# (at your option) any later version.
89+
# https://www.gnu.org/licenses/
90+
# ****************************************************************************
91+
92+
from collections import namedtuple
9293
import operator
9394

9495
from sage.combinat.finite_state_machine import Automaton, Transducer
@@ -1058,8 +1059,7 @@ def GrayCode(self):
10581059
with_final_word_out=[0])
10591060

10601061

1061-
RecursionRule = collections.namedtuple('RecursionRule',
1062-
['K', 'r', 'k', 's', 't'])
1062+
RecursionRule = namedtuple('RecursionRule', ['K', 'r', 'k', 's', 't'])
10631063

10641064

10651065
def _parse_recursion_equation_(self, equation, base, function, var,
@@ -1800,7 +1800,7 @@ def Recursion(self, recursions, base, function=None, var=None,
18001800

18011801
if is_zero is None:
18021802
is_zero = lambda x: not x
1803-
RuleRight = collections.namedtuple('Rule', ['k', 's', 't'])
1803+
RuleRight = namedtuple('Rule', ['k', 's', 't'])
18041804
initial_values = {}
18051805
rules = []
18061806
if input_alphabet is None and base in ZZ:

src/sage/combinat/ranker.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
"""
1+
# -*- coding: utf-8 -*-
2+
r"""
23
Rankers
34
"""
45
#*****************************************************************************
56
# Copyright (C) 2007 Mike Hansen <[email protected]>,
67
# Nicolas M. Thiery <nthiery at users.sf.net>
78
# Ported from MuPAD-Combinat (combinat::rankers)
89
#
9-
# Distributed under the terms of the GNU General Public License (GPL)
10-
#
11-
# This code is distributed in the hope that it will be useful,
12-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14-
# General Public License for more details.
15-
#
16-
# The full text of the GPL is available at:
17-
#
18-
# http://www.gnu.org/licenses/
19-
#*****************************************************************************
20-
21-
from collections import Iterable, Sequence
10+
# This program is free software: you can redistribute it and/or modify
11+
# it under the terms of the GNU General Public License as published by
12+
# the Free Software Foundation, either version 2 of the License, or
13+
# (at your option) any later version.
14+
# https://www.gnu.org/licenses/
15+
# ****************************************************************************
16+
17+
from collections.abc import Iterable, Sequence
2218
from sage.misc.cachefunc import cached_function
2319
from sage.misc.callable_dict import CallableDict
2420
from sage.structure.parent import Parent
@@ -188,14 +184,14 @@ def unrank(L, i):
188184
189185
The purpose of this utility is to give a uniform idiom to recover
190186
the `i`-th element of an object ``L``, whether ``L`` is a list,
191-
tuple (or more generally a :class:`collections.Sequence`), an
187+
tuple (or more generally a :class:`collections.abc.Sequence`), an
192188
enumerated set, some old parent of Sage still implementing
193189
unranking in the method ``__getitem__``, or an iterable (see
194-
:class:`collections.Iterable`). See :trac:`15919`.
190+
:class:`collections.abc.Iterable`). See :trac:`15919`.
195191
196192
EXAMPLES:
197193
198-
Lists, tuples, and other :class:`sequences <collections.Sequence>`::
194+
Lists, tuples, and other :class:`sequences <collections.abc.Sequence>`::
199195
200196
sage: from sage.combinat.ranker import unrank
201197
sage: unrank(['a','b','c'], 2)

src/sage/combinat/shuffle.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,17 @@
4545
4646
- Jean-Baptiste Priez
4747
"""
48-
#*****************************************************************************
48+
# ****************************************************************************
4949
# Copyright (C) 2014 Jean-Baptiste Priez <[email protected]>
5050
#
51-
# Distributed under the terms of the GNU General Public License (GPL)
52-
#
53-
# The full text of the GPL is available at:
54-
#
55-
# http://www.gnu.org/licenses/
56-
#*****************************************************************************
57-
import collections
51+
# This program is free software: you can redistribute it and/or modify
52+
# it under the terms of the GNU General Public License as published by
53+
# the Free Software Foundation, either version 2 of the License, or
54+
# (at your option) any later version.
55+
# https://www.gnu.org/licenses/
56+
# ****************************************************************************
57+
58+
from collections.abc import Iterable
5859
import itertools
5960
import operator
6061

@@ -116,11 +117,11 @@ def __init__(self, l1, l2, element_constructor=None):
116117
[[2, 3, 4, 5], [2, 5, 3, 4], [5, 2, 3, 4], [2, 3, 5, 4], [1, 2, 3, 5], [1, 5, 2, 3],
117118
[5, 1, 2, 3], [1, 2, 5, 3]]
118119
"""
119-
assert(isinstance(l1, collections.Iterable) and
120-
isinstance(l2, collections.Iterable)
120+
assert(isinstance(l1, Iterable) and
121+
isinstance(l2, Iterable)
121122
)
122-
assert(all(isinstance(elem, collections.Iterable) for elem in l1))
123-
assert(all(isinstance(elem, collections.Iterable) for elem in l2))
123+
assert(all(isinstance(elem, Iterable) for elem in l1))
124+
assert(all(isinstance(elem, Iterable) for elem in l2))
124125
self._l1 = list(l1)
125126
self._l2 = list(l2)
126127

@@ -259,8 +260,8 @@ def __init__(self, l1, l2, element_constructor=None):
259260
word: bbbaa, word: bbaba, word: babba, word: abbba]
260261
261262
"""
262-
assert(isinstance(l1, collections.Iterable) and
263-
isinstance(l2, collections.Iterable)
263+
assert(isinstance(l1, Iterable) and
264+
isinstance(l2, Iterable)
264265
)
265266
self._l1 = list(l1)
266267
self._l2 = list(l2)

0 commit comments

Comments
 (0)