Skip to content

Commit 089d45c

Browse files
committed
-
1 parent 23c49a2 commit 089d45c

File tree

6 files changed

+62
-14
lines changed

6 files changed

+62
-14
lines changed

source_py2/python_toolbox/context_management/context_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def __exit_using_manage_context(self, exc_type, exc_value, exc_traceback):
104104
raise RuntimeError(
105105
"The generator didn't stop after the yield; possibly you "
106106
"have more than one `yield` in the generator function? "
107-
"The generator function must yield exactly one time.")
107+
"The generator function must `yield` exactly one time.")
108108
else:
109109
if exc_value is None:
110110
# Need to force instantiation so we can reliably
@@ -131,6 +131,6 @@ def __exit_using_manage_context(self, exc_type, exc_value, exc_traceback):
131131
raise RuntimeError(
132132
"The generator didn't stop after calling its `.throw()`; "
133133
"Possibly you have more than one `yield` in the generator "
134-
"function? The generator function must yield exactly one "
134+
"function? The generator function must `yield` exactly one "
135135
"time."
136136
)

source_py2/python_toolbox/math_tools/factorials.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
def factorial(x, start=1):
1616
'''
17-
Calculate a factorial
17+
Calculate a factorial.
1818
1919
This differs from the built-in `math.factorial` in that it allows a `start`
2020
argument. If one is given, the function returns `(x!)/(start!)`.
@@ -115,8 +115,7 @@ def to_factoradic(number, n_digits_pad=0):
115115
current_number = number
116116
for i in range(n_digits)[::-1]:
117117
unit = math.factorial(i)
118-
digits[n_digits - i - 1], current_number = \
119-
divmod(current_number, unit)
118+
digits[n_digits - i - 1], current_number = divmod(current_number, unit)
120119
result = tuple(digits)
121120
if (len(result) < n_digits_pad):
122121
return ((0,) * (n_digits_pad - len(result))) + result

source_py2/python_toolbox/math_tools/misc.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ def round_to_int(x, up=False):
7575
else:
7676
return rounded_down
7777

78-
7978
def ceil_div(x, y):
8079
'''Divide `x` by `y`, rounding up if there's a remainder.'''
8180
return cute_floor_div(x, y) + (1 if x % y else 0)
@@ -122,6 +121,12 @@ def restrict_number_to_range(number, low_cutoff=-infinity,
122121

123122

124123
def binomial(big, small):
124+
'''
125+
Get the binomial coefficient (big small).
126+
127+
This is used in combinatorical calculations. More information:
128+
http://en.wikipedia.org/wiki/Binomial_coefficient
129+
'''
125130
if big == small:
126131
return 1
127132
if big < small:
@@ -132,6 +137,19 @@ def binomial(big, small):
132137

133138

134139
def product(numbers):
140+
'''Get the product of all the numbers in `numbers`.'''
135141
from python_toolbox import misc_tools
136142
return misc_tools.general_product(numbers, start=1)
143+
144+
def is_integer(x):
145+
'''
146+
Is `x` an integer?
147+
148+
Does return `True` for things like 1.0 and `1+0j`.
149+
'''
150+
try:
151+
inted_x = int(x)
152+
except (TypeError, ValueError, OverflowError):
153+
return False
154+
return inted_x == x
137155

source_py2/python_toolbox/math_tools/sequences.py

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

4-
from __future__ import division
5-
64
import numbers
5+
import collections
6+
import itertools
77

88
infinity = float('inf')
99

1010

1111
_stirling_caches = []
1212
_n_highest_cache_completed = -1
1313
def stirling(n, k, skip_calculation=False):
14+
'''
15+
Calculate Stirling number of the second kind of `n` and `k`.
16+
17+
More information about these numbers:
18+
https://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind
19+
20+
Example:
21+
22+
>>> stirling(3, 2)
23+
-3
24+
25+
'''
1426
global _n_highest_cache_completed
1527
if k not in xrange(n + 1):
1628
return 0
@@ -51,5 +63,17 @@ def stirling(n, k, skip_calculation=False):
5163

5264

5365
def abs_stirling(n, k):
66+
'''
67+
Calculate Stirling number of the first kind of `n` and `k`.
68+
69+
More information about these numbers:
70+
https://en.wikipedia.org/wiki/Stirling_numbers_of_the_first_kind
71+
72+
Example:
73+
74+
>>> abs_stirling(3, 2)
75+
3
76+
77+
'''
5478
return abs(stirling(n, k))
5579

source_py2/python_toolbox/math_tools/types.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ class _PossiblyInfiniteIntegralType(abc.ABCMeta):
1414
def __instancecheck__(self, thing):
1515
return isinstance(thing, numbers.Integral) or (thing in infinities)
1616
class PossiblyInfiniteIntegral(numbers.Number):
17-
__metaclass__ = _PossiblyInfiniteIntegralType
17+
__metaclass__ =_PossiblyInfiniteIntegralType
18+
'''An integer or infinity (including negative infinity.)'''
1819

1920
class _PossiblyInfiniteRealType(abc.ABCMeta):
2021
def __instancecheck__(self, thing):
2122
return isinstance(thing, numbers.Real) or (thing in infinities)
2223
class PossiblyInfiniteReal(numbers.Number):
23-
__metaclass__ = _PossiblyInfiniteRealType
24-
24+
__metaclass__ =__PossiblyInfiniteRealType
25+
'''A real number or infinity (including negative infinity.)'''
2526

27+
class _NaturalType(abc.ABCMeta):
28+
def __instancecheck__(self, thing):
29+
return isinstance(thing, numbers.Integral) and thing >= 1
30+
class Natural(numbers.Number):
31+
__metaclass__ =__NaturalType
32+
'''A natural number, meaning a positive integer (0 not included.)'''

source_py2/python_toolbox/misc_tools/misc_tools.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
import sys
1515
import threading
1616

17-
from python_toolbox import decorator_tools
18-
from python_toolbox import cute_iter_tools
19-
2017

2118
_email_pattern = re.compile(
2219
r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*"
@@ -41,6 +38,7 @@ def is_subclass(candidate, base_class):
4138
not a type. (Python issue 10569.)
4239
'''
4340
# todo: disable ability to use nested iterables.
41+
from python_toolbox import cute_iter_tools
4442
if cute_iter_tools.is_iterable(base_class):
4543
return any(is_subclass(candidate, single_base_class) for
4644
single_base_class in base_class)
@@ -183,6 +181,8 @@ def find_clear_place_on_circle(circle_points, circle_size=1):
183181
interval connects to the start.)
184182
'''
185183

184+
from python_toolbox import cute_iter_tools
185+
186186
# Before starting, taking care of two edge cases:
187187
if not circle_points:
188188
# Edge case: No points at all

0 commit comments

Comments
 (0)