Skip to content

Commit a8c48d3

Browse files
committed
-
1 parent 16b99ad commit a8c48d3

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2009-2015 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
import math
5+
6+
from python_toolbox import math_tools
7+
from python_toolbox import cute_iter_tools
8+
from python_toolbox import misc_tools
9+
10+
infinity = float('inf')
11+
12+
13+
class MISSING_ELEMENT:
14+
'''A placeholder for a missing element used in internal calculations.'''
15+
16+
17+
@misc_tools.limit_positional_arguments(1)
18+
def get_short_factorial_string(number, minus_one=False):
19+
'''
20+
Get a short description of the factorial of `number`.
21+
22+
If the number is long, just uses factorial notation.
23+
24+
Examples:
25+
26+
>>> get_short_factorial_string(4)
27+
'24'
28+
>>> get_short_factorial_string(14)
29+
'14!'
30+
31+
'''
32+
assert number >= 0 and \
33+
isinstance(number, math_tools.PossiblyInfiniteIntegral)
34+
if number == infinity:
35+
return "float('inf')"
36+
elif number <= 10:
37+
return str(math.factorial(number) - int(minus_one))
38+
else:
39+
assert number > 10
40+
return '%s!%s' % (number, ' - 1' if minus_one else '')
41+
42+
43+

source_py2/python_toolbox/combi/perming/comb_space.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# Copyright 2009-2015 Ram Rachum.
22
# This program is distributed under the MIT license.
33

4+
from python_toolbox import misc_tools
5+
46
from .perm_space import PermSpace
57

8+
69
class CombSpace(PermSpace):
710
'''
811
A space of combinations.
@@ -29,7 +32,8 @@ class CombSpace(PermSpace):
2932
extra functionality. (See documentation of `Comb` and `Perm` for more
3033
info.)
3134
'''
32-
def __init__(self, iterable_or_length, n_elements, *, slice_=None,
35+
@misc_tools.limit_positional_arguments(3)
36+
def __init__(self, iterable_or_length, n_elements, slice_=None,
3337
perm_type=None, _domain_for_checking=None,
3438
_degrees_for_checking=None):
3539
PermSpace.__init__(

source_py2/python_toolbox/combi/perming/perm.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
infinity = float('inf')
1919

2020

21-
class _BasePermView(metaclass=abc.ABCMeta):
21+
class _BasePermView(object):
2222
'''
2323
Abstract base class for viewers on Perm.
2424
'''
25+
__metaclass__ = abc.ABCMeta
2526
def __init__(self, perm):
2627
self.perm = perm
2728
__repr__ = lambda self: '<%s: %s>' % (type(self).__name__, self.perm)
@@ -69,8 +70,7 @@ def __call__(cls, item, perm_space=None):
6970

7071

7172
@functools.total_ordering
72-
class Perm(sequence_tools.CuteSequenceMixin, collections.Sequence,
73-
metaclass=PermType):
73+
class Perm(sequence_tools.CuteSequenceMixin, collections.Sequence):
7474
'''
7575
A permutation of items from a `PermSpace`
7676
@@ -87,6 +87,7 @@ class Perm(sequence_tools.CuteSequenceMixin, collections.Sequence,
8787
23
8888
8989
'''
90+
__metaclass__ = PermType
9091

9192
@classmethod
9293
def coerce(cls, item, perm_space=None):
@@ -384,7 +385,8 @@ def n_cycles(self):
384385
return n_cycles
385386

386387

387-
def get_neighbors(self, *, degrees=(1,), perm_space=None):
388+
@misc_tools.limit_positional_arguments(1)
389+
def get_neighbors(self, degrees=(1,), perm_space=None):
388390
'''
389391
Get the neighbor permutations of this permutation.
390392

source_py2/python_toolbox/combi/perming/perm_space.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from python_toolbox import nifty_collections
1717
from python_toolbox import dict_tools
1818
from python_toolbox import misc_tools
19+
from python_toolbox.third_party import funcsigs
1920

2021
from .. import misc
2122
from . import variations
@@ -54,7 +55,7 @@ def __call__(cls, *args, **kwargs):
5455
_degrees_for_checking=arguments.get('degrees', None),
5556
)
5657
else:
57-
return super().__call__(*args, **kwargs)
58+
return super(PermSpaceType, cls).__call__(*args, **kwargs)
5859

5960

6061
class PermSpace(_VariationRemovingMixin, _VariationAddingMixin,
@@ -393,7 +394,7 @@ def __init__(self, iterable_or_length, n_elements=None, domain=None,
393394
if not self.is_typed:
394395
self.untyped = self
395396

396-
__init__.signature = inspect.signature(__init__)
397+
__init__.signature = funcsigs.signature(__init__)
397398

398399
@caching.CachedProperty
399400
def _unsliced_length(self):

0 commit comments

Comments
 (0)