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

Commit

Permalink
t-28012: collections.abc
Browse files Browse the repository at this point in the history
  • Loading branch information
slel committed Sep 10, 2020
1 parent 5ec24db commit 4f80136
Show file tree
Hide file tree
Showing 27 changed files with 296 additions and 288 deletions.
29 changes: 14 additions & 15 deletions src/sage/arith/misc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""
r"""
Miscellaneous arithmetic functions
"""

Expand All @@ -13,16 +13,15 @@
# https://www.gnu.org/licenses/
# ****************************************************************************

from __future__ import absolute_import, print_function

import math
import collections
from collections.abc import Iterable

from sage.misc.misc import powerset
from sage.misc.misc_c import prod

from sage.libs.pari.all import pari
import sage.libs.flint.arith as flint_arith
from sage.libs.flint.arith import (bernoulli_number as flint_bernoulli,
dedekind_sum as flint_dedekind_sum)

from sage.structure.element import parent
from sage.structure.coerce import py_scalar_to_element
Expand All @@ -34,16 +33,16 @@
from sage.rings.real_mpfr import RealNumber
from sage.rings.complex_number import ComplexNumber

import sage.rings.fast_arith as fast_arith
prime_range = fast_arith.prime_range
from sage.rings.fast_arith import arith_int, arith_llong, prime_range


##################################################################
# Elementary Arithmetic
##################################################################


def algdep(z, degree, known_bits=None, use_bits=None, known_digits=None, use_digits=None, height_bound=None, proof=False):
def algdep(z, degree, known_bits=None, use_bits=None, known_digits=None,
use_digits=None, height_bound=None, proof=False):
"""
Return an irreducible polynomial of degree at most `degree` which
is approximately satisfied by the number `z`.
Expand Down Expand Up @@ -367,7 +366,7 @@ def bernoulli(n, algorithm='default', num_threads=1):
if n >= 100000:
from warnings import warn
warn("flint is known to not be accurate for large Bernoulli numbers")
return flint_arith.bernoulli_number(n)
return flint_bernoulli(n)
elif algorithm == 'pari':
x = pari(n).bernfrac() # Use the PARI C library
return Rational(x)
Expand Down Expand Up @@ -2108,9 +2107,9 @@ def get_gcd(order):
<function gcd at ...>
"""
if order <= 46340: # todo: don't hard code
return fast_arith.arith_int().gcd_int
return arith_int().gcd_int
elif order <= 2147483647: # todo: don't hard code
return fast_arith.arith_llong().gcd_longlong
return arith_llong().gcd_longlong
else:
return gcd

Expand All @@ -2130,9 +2129,9 @@ def get_inverse_mod(order):
<function inverse_mod at ...>
"""
if order <= 46340: # todo: don't hard code
return fast_arith.arith_int().inverse_mod_int
return arith_int().inverse_mod_int
elif order <= 2147483647: # todo: don't hard code
return fast_arith.arith_llong().inverse_mod_longlong
return arith_llong().inverse_mod_longlong
else:
return inverse_mod

Expand Down Expand Up @@ -3651,7 +3650,7 @@ def multinomial(*ks):
- Gabriel Ebner
"""
if isinstance(ks[0], collections.Iterable):
if isinstance(ks[0], Iterable):
if len(ks) > 1:
raise ValueError("multinomial takes only one iterable argument")
ks = ks[0]
Expand Down Expand Up @@ -5840,7 +5839,7 @@ def dedekind_sum(p, q, algorithm='default'):
- :wikipedia:`Dedekind\_sum`
"""
if algorithm == 'default' or algorithm == 'flint':
return flint_arith.dedekind_sum(p, q)
return flint_dedekind_sum(p, q)

if algorithm == 'pari':
import sage.interfaces.gp
Expand Down
10 changes: 5 additions & 5 deletions src/sage/categories/poor_man_map.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# -*- coding: utf-8 -*-
"""
r"""
Poor Man's map
"""
#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2009 Nicolas M. Thiery <nthiery at users.sf.net>
# 2016 Julian Rüth <julian.rueth@fsfe.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
# https://www.gnu.org/licenses/
# ****************************************************************************
import sage.structure.sage_object

class PoorManMap(sage.structure.sage_object.SageObject):
Expand Down Expand Up @@ -69,7 +69,7 @@ def __init__(self, function, domain = None, codomain = None, name = None):
sage: TestSuite(f*g).run()
"""
from collections import Iterable
from collections.abc import Iterable
if not isinstance(function, Iterable):
function = (function,)
self._functions = tuple(function)
Expand Down
32 changes: 16 additions & 16 deletions src/sage/combinat/finite_state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,16 +924,17 @@
# 2012--2015 Daniel Krenn <dev@danielkrenn.at>
# 2012--2015 Sara Kropf <sara.kropf@aau.at>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# https://www.gnu.org/licenses/
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************
from __future__ import print_function

from IPython.lib.pretty import pretty
import collections
import itertools
from collections import defaultdict, deque, namedtuple, OrderedDict
from collections.abc import Iterator
from copy import copy, deepcopy

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

This is similar to ``itertools.groupby`` except that lists are
This is similar to :func:`itertools.groupby` except that lists are
returned instead of iterables and no prior sorting is required.

We do not require

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

Expand Down Expand Up @@ -998,13 +999,13 @@ def full_group_by(l, key=lambda x: x):
1/x [1]
2/x [2]

Note that the behavior is different from ``itertools.groupby``
Note that the behavior is different from :func:`itertools.groupby`
because neither `1/x<2/x` nor `2/x<1/x` does hold.

Here, the result ``r`` has been sorted in order to guarantee a
consistent order for the doctest suite.
"""
elements = collections.defaultdict(list)
elements = defaultdict(list)
original_keys = {}
for item in l:
k = key(item)
Expand Down Expand Up @@ -4924,7 +4925,7 @@ def key_function(s):
# transitions have to be sorted anyway, the performance
# penalty should be bearable; nevertheless, this is only
# required for doctests.
adjacent = collections.OrderedDict(
adjacent = OrderedDict(
(pair, list(transitions))
for pair, transitions in
itertools.groupby(
Expand Down Expand Up @@ -13143,7 +13144,7 @@ def __init__(self, tape_cache_manager, tape, tape_ended,

self.tape_cache_manager = tape_cache_manager
self.tape_cache_manager.append(self)
self.cache = tuple(collections.deque() for _ in self.tape)
self.cache = tuple(deque() for _ in self.tape)

def _repr_(self):
"""
Expand Down Expand Up @@ -13927,8 +13928,7 @@ def is_FSMProcessIterator(PI):
# ****************************************************************************


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


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


_branch_ = collections.namedtuple('Branch', 'tape_cache, outputs')
_branch_ = namedtuple('Branch', 'tape_cache, outputs')
r"""
A :func:`named tuple <collections.namedtuple>` representing the
attributes of a branch at a particular state during processing.
Expand Down
24 changes: 12 additions & 12 deletions src/sage/combinat/finite_state_machine_generators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
r"""
Common Automata and Transducers (Finite State Machines Generators)
Expand Down Expand Up @@ -76,19 +77,19 @@
---------------------
"""
#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2014--2015 Clemens Heuberger <clemens.heuberger@aau.at>
# 2014--2015 Daniel Krenn <dev@danielkrenn.at>
# 2014 Sara Kropf <sara.kropf@aau.at>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
from __future__ import print_function

import collections
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************

from collections import namedtuple
import operator

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


RecursionRule = collections.namedtuple('RecursionRule',
['K', 'r', 'k', 's', 't'])
RecursionRule = namedtuple('RecursionRule', ['K', 'r', 'k', 's', 't'])


def _parse_recursion_equation_(self, equation, base, function, var,
Expand Down Expand Up @@ -1800,7 +1800,7 @@ def Recursion(self, recursions, base, function=None, var=None,

if is_zero is None:
is_zero = lambda x: not x
RuleRight = collections.namedtuple('Rule', ['k', 's', 't'])
RuleRight = namedtuple('Rule', ['k', 's', 't'])
initial_values = {}
rules = []
if input_alphabet is None and base in ZZ:
Expand Down
30 changes: 13 additions & 17 deletions src/sage/combinat/ranker.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
"""
# -*- coding: utf-8 -*-
r"""
Rankers
"""
#*****************************************************************************
# Copyright (C) 2007 Mike Hansen <mhansen@gmail.com>,
# Nicolas M. Thiery <nthiery at users.sf.net>
# Ported from MuPAD-Combinat (combinat::rankers)
#
# Distributed under the terms of the GNU General Public License (GPL)
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# The full text of the GPL is available at:
#
# http://www.gnu.org/licenses/
#*****************************************************************************

from collections import Iterable, Sequence
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************

from collections.abc import Iterable, Sequence
from sage.misc.cachefunc import cached_function
from sage.misc.callable_dict import CallableDict
from sage.structure.parent import Parent
Expand Down Expand Up @@ -188,14 +184,14 @@ def unrank(L, i):
The purpose of this utility is to give a uniform idiom to recover
the `i`-th element of an object ``L``, whether ``L`` is a list,
tuple (or more generally a :class:`collections.Sequence`), an
tuple (or more generally a :class:`collections.abc.Sequence`), an
enumerated set, some old parent of Sage still implementing
unranking in the method ``__getitem__``, or an iterable (see
:class:`collections.Iterable`). See :trac:`15919`.
:class:`collections.abc.Iterable`). See :trac:`15919`.
EXAMPLES:
Lists, tuples, and other :class:`sequences <collections.Sequence>`::
Lists, tuples, and other :class:`sequences <collections.abc.Sequence>`::
sage: from sage.combinat.ranker import unrank
sage: unrank(['a','b','c'], 2)
Expand Down
29 changes: 15 additions & 14 deletions src/sage/combinat/shuffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@
- Jean-Baptiste Priez
"""
#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2014 Jean-Baptiste Priez <jbp@kerios.fr>
#
# Distributed under the terms of the GNU General Public License (GPL)
#
# The full text of the GPL is available at:
#
# http://www.gnu.org/licenses/
#*****************************************************************************
import collections
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************

from collections.abc import Iterable
import itertools
import operator

Expand Down Expand Up @@ -116,11 +117,11 @@ def __init__(self, l1, l2, element_constructor=None):
[[2, 3, 4, 5], [2, 5, 3, 4], [5, 2, 3, 4], [2, 3, 5, 4], [1, 2, 3, 5], [1, 5, 2, 3],
[5, 1, 2, 3], [1, 2, 5, 3]]
"""
assert(isinstance(l1, collections.Iterable) and
isinstance(l2, collections.Iterable)
assert(isinstance(l1, Iterable) and
isinstance(l2, Iterable)
)
assert(all(isinstance(elem, collections.Iterable) for elem in l1))
assert(all(isinstance(elem, collections.Iterable) for elem in l2))
assert(all(isinstance(elem, Iterable) for elem in l1))
assert(all(isinstance(elem, Iterable) for elem in l2))
self._l1 = list(l1)
self._l2 = list(l2)

Expand Down Expand Up @@ -259,8 +260,8 @@ def __init__(self, l1, l2, element_constructor=None):
word: bbbaa, word: bbaba, word: babba, word: abbba]
"""
assert(isinstance(l1, collections.Iterable) and
isinstance(l2, collections.Iterable)
assert(isinstance(l1, Iterable) and
isinstance(l2, Iterable)
)
self._l1 = list(l1)
self._l2 = list(l2)
Expand Down

0 comments on commit 4f80136

Please sign in to comment.