diff options
author | Paul Ivanov <paul.ivanov@local> | 2009-12-28 20:49:52 +0000 |
---|---|---|
committer | Paul Ivanov <paul.ivanov@local> | 2009-12-28 20:49:52 +0000 |
commit | e4f233ecfedd2aafa258db2d3ae27e30604cc020 (patch) | |
tree | 6d32fbdd19b8dca00cd7cafd8df076bac55ddfd8 /numpy/core | |
parent | 5ba01996a9ab2fdfb7c120a5afae801f854a781a (diff) | |
download | numpy-e4f233ecfedd2aafa258db2d3ae27e30604cc020.tar.gz |
fixed a whole bunch of doctests
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/arrayprint.py | 5 | ||||
-rw-r--r-- | numpy/core/defchararray.py | 36 | ||||
-rw-r--r-- | numpy/core/fromnumeric.py | 7 | ||||
-rw-r--r-- | numpy/core/function_base.py | 10 | ||||
-rw-r--r-- | numpy/core/numeric.py | 50 | ||||
-rw-r--r-- | numpy/core/records.py | 2 |
6 files changed, 74 insertions, 36 deletions
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index c8d4e13f7..536b26f40 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -88,6 +88,11 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None, >>> x**2 - (x + eps)**2 array([-0., -0., 0., 0.]) + To put back the default options, you can use: + + >>> np.set_printoptions(edgeitems=3,infstr='Inf', + ... linewidth=75, nanstr='NaN', precision=8, + ... suppress=False, threshold=1000) """ global _summaryThreshold, _summaryEdgeItems, _float_output_precision, \ diff --git a/numpy/core/defchararray.py b/numpy/core/defchararray.py index b7ad2230e..4530bd5ad 100644 --- a/numpy/core/defchararray.py +++ b/numpy/core/defchararray.py @@ -457,7 +457,7 @@ def count(a, sub, start=0, end=None): -------- >>> c = np.array(['aAaAaA', ' aA ', 'abBABba']) >>> c - array(['aAaAaA', ' aA', 'abBABba'], + array(['aAaAaA', ' aA ', 'abBABba'], dtype='|S7') >>> np.char.count(c, 'A') array([3, 1, 1]) @@ -505,7 +505,7 @@ def decode(a, encoding=None, errors=None): -------- >>> c = np.array(['aAaAaA', ' aA ', 'abBABba']) >>> c - array(['aAaAaA', ' aA', 'abBABba'], + array(['aAaAaA', ' aA ', 'abBABba'], dtype='|S7') >>> np.char.encode(c, encoding='cp037') array(['\\x81\\xc1\\x81\\xc1\\x81\\xc1', '@@\\x81\\xc1@@', @@ -584,9 +584,9 @@ def endswith(a, suffix, start=0, end=None): >>> s array(['foo', 'bar'], dtype='|S3') - >>> np.charendswith(s, 'ar') + >>> np.char.endswith(s, 'ar') array([False, True], dtype=bool) - >>> s.endswith(s, 'a', start=1, end=2) + >>> np.char.endswith(s, 'a', start=1, end=2) array([False, True], dtype=bool) """ @@ -1010,15 +1010,17 @@ def lstrip(a, chars=None): -------- >>> c = np.array(['aAaAaA', ' aA ', 'abBABba']) >>> c - array(['aAaAaA', ' aA', 'abBABba'], + array(['aAaAaA', ' aA ', 'abBABba'], dtype='|S7') >>> np.char.lstrip(c, 'a') # 'a' unstripped from c[1] because whitespace leading - array(['AaAaA', ' aA', 'bBABba'], - dtype='|S6') + array(['AaAaA', ' aA ', 'bBABba'], + dtype='|S7') >>> np.char.lstrip(c, 'A') # leaves c unchanged - array(['aAaAaA', ' aA', 'abBABba'], + array(['aAaAaA', ' aA ', 'abBABba'], dtype='|S7') >>> (np.char.lstrip(c, ' ') == np.char.lstrip(c, '')).all() + ... # XXX: is this a regression? this line now returns False + ... # np.char.lstrip(c,'') does not modify c at all. True >>> (np.char.lstrip(c, ' ') == np.char.lstrip(c, None)).all() True @@ -1313,7 +1315,7 @@ def rstrip(a, chars=None): dtype='|S7') >>> np.char.rstrip(c, 'a') array(['aAaAaA', 'abBABb'], - dtype='|S6') + dtype='|S7') >>> np.char.rstrip(c, 'A') array(['aAaAa', 'abBABba'], dtype='|S7') @@ -1444,16 +1446,16 @@ def strip(a, chars=None): -------- >>> c = np.array(['aAaAaA', ' aA ', 'abBABba']) >>> c - array(['aAaAaA', ' aA', 'abBABba'], + array(['aAaAaA', ' aA ', 'abBABba'], dtype='|S7') >>> np.char.strip(c) array(['aAaAaA', 'aA', 'abBABba'], dtype='|S7') >>> np.char.strip(c, 'a') # 'a' unstripped from c[1] because whitespace leads - array(['AaAaA', ' aA', 'bBABb'], - dtype='|S6') + array(['AaAaA', ' aA ', 'bBABb'], + dtype='|S7') >>> np.char.strip(c, 'A') # 'A' unstripped from c[1] because (unprinted) ws trails - array(['aAaAa', ' aA', 'abBABba'], + array(['aAaAa', ' aA ', 'abBABba'], dtype='|S7') """ @@ -1523,7 +1525,7 @@ def title(a): array(['a1b c', '1b ca', 'b ca1', 'ca1b'], dtype='|S5') >>> np.char.title(c) - chararray(['A1B C', '1B Ca', 'B Ca1', 'Ca1B'], + array(['A1B C', '1B Ca', 'B Ca1', 'Ca1B'], dtype='|S5') """ a_arr = numpy.asarray(a) @@ -1590,7 +1592,7 @@ def upper(a): >>> c = np.array(['a1b c', '1bca', 'bca1']); c array(['a1b c', '1bca', 'bca1'], dtype='|S5') - >>> numpy.char.upper(c) + >>> np.char.upper(c) array(['A1B C', '1BCA', 'BCA1'], dtype='|S5') """ @@ -1742,7 +1744,7 @@ class chararray(ndarray): Examples -------- >>> charar = np.chararray((3, 3)) - >>> charar[:, :] = 'abc' + >>> charar[:] = 'a' >>> charar chararray([['a', 'a', 'a'], ['a', 'a', 'a'], @@ -1750,7 +1752,7 @@ class chararray(ndarray): dtype='|S1') >>> charar = np.chararray(charar.shape, itemsize=5) - >>> charar[:, :] = 'abc' + >>> charar[:] = 'abc' >>> charar chararray([['abc', 'abc', 'abc'], ['abc', 'abc', 'abc'], diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index 4b3970f00..b9265216f 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -1298,8 +1298,9 @@ def clip(a, a_min, a_max, out=None): array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.clip(a, 3, 6, out=a) array([3, 3, 3, 3, 4, 5, 6, 6, 6, 6]) + >>> a = np.arange(10) >>> a - array([3, 3, 3, 3, 4, 5, 6, 6, 6, 6]) + array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.clip(a, [3,4,1,1,1,4,4,4,4,4], 8) array([3, 4, 2, 3, 4, 5, 6, 7, 8, 8]) @@ -1500,7 +1501,7 @@ def any(a,axis=None, out=None): >>> # Check now that z is a reference to o >>> z is o True - >>> id(z), id(o) # identity of z and o + >>> id(z), id(o) # identity of z and o # doctest: +SKIP (191614240, 191614240) """ @@ -1563,7 +1564,7 @@ def all(a,axis=None, out=None): >>> o=np.array([False]) >>> z=np.all([-1, 4, 5], out=o) - >>> id(z), id(o), z + >>> id(z), id(o), z # doctest: +SKIP (28293632, 28293632, array([ True], dtype=bool)) """ diff --git a/numpy/core/function_base.py b/numpy/core/function_base.py index 33cf3a15b..b2f9dc70c 100644 --- a/numpy/core/function_base.py +++ b/numpy/core/function_base.py @@ -63,8 +63,11 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False): >>> x1 = np.linspace(0, 10, N, endpoint=True) >>> x2 = np.linspace(0, 10, N, endpoint=False) >>> plt.plot(x1, y, 'o') + [<matplotlib.lines.Line2D object at 0x...>] >>> plt.plot(x2, y + 0.5, 'o') + [<matplotlib.lines.Line2D object at 0x...>] >>> plt.ylim([-0.5, 1]) + (-0.5, 1) >>> plt.show() """ @@ -129,8 +132,10 @@ def logspace(start,stop,num=50,endpoint=True,base=10.0): ----- Logspace is equivalent to the code - >>> y = linspace(start, stop, num=num, endpoint=endpoint) + >>> y = np.linspace(start, stop, num=num, endpoint=endpoint) + ... # doctest: +SKIP >>> power(base, y) + ... # doctest: +SKIP Examples -------- @@ -149,8 +154,11 @@ def logspace(start,stop,num=50,endpoint=True,base=10.0): >>> x2 = np.logspace(0.1, 1, N, endpoint=False) >>> y = np.zeros(N) >>> plt.plot(x1, y, 'o') + [<matplotlib.lines.Line2D object at 0x...>] >>> plt.plot(x2, y + 0.5, 'o') + [<matplotlib.lines.Line2D object at 0x...>] >>> plt.ylim([-0.5, 1]) + (-0.5, 1) >>> plt.show() """ diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 8c4fa2980..176212d09 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -638,6 +638,7 @@ def correlate(a,v,mode='valid',old_behavior=True): This function is equivalent to >>> np.convolve(a, v[::-1], mode=mode) + ... #doctest: +SKIP where ``v[::-1]`` is the reverse of `v`. @@ -963,6 +964,9 @@ def tensordot(a, b, axes=2): [aaaaaaacccccccc, bbbbbbbdddddddd]]], dtype=object) >>> np.tensordot(a, A, 0) # "Left for reader" (result too long to incl.) + array([[[[[a, b], + [c, d]], + ... >>> np.tensordot(a, A, (0, 1)) array([[[abbbbb, cddddd], @@ -1413,7 +1417,7 @@ def array_str(a, max_line_width=None, precision=None, suppress_small=None): Examples -------- >>> np.array_str(np.arange(3)) - >>> '[0 1 2]' + '[0 1 2]' """ return array2string(a, max_line_width, precision, suppress_small, ' ', "", str) @@ -1465,7 +1469,7 @@ def indices(dimensions, dtype=int): -------- >>> grid = np.indices((2, 3)) >>> grid.shape - (2,2,3) + (2, 2, 3) >>> grid[0] # row indices array([[0, 0, 0], [1, 1, 1]]) @@ -1973,19 +1977,19 @@ def array_equiv(a1, a2): Examples -------- >>> np.array_equiv([1, 2], [1, 2]) - >>> True + True >>> np.array_equiv([1, 2], [1, 3]) - >>> False + False Showing the shape equivalence: >>> np.array_equiv([1, 2], [[1, 2], [1, 2]]) - >>> True + True >>> np.array_equiv([1, 2], [[1, 2, 1, 2], [1, 2, 1, 2]]) - >>> False + False >>> np.array_equiv([1, 2], [[1, 2], [1, 3]]) - >>> False + False """ try: @@ -2064,11 +2068,12 @@ def seterr(all=None, divide=None, over=None, under=None, invalid=None): Examples -------- + >>> old_settings = np.seterr(all='ignore') #seterr to known value >>> np.seterr(over='raise') {'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore', 'under': 'ignore'} >>> np.seterr(all='ignore') # reset to default - {'over': 'raise', 'divide': 'warn', 'invalid': 'warn', 'under': 'warn'} + {'over': 'raise', 'divide': 'ignore', 'invalid': 'ignore', 'under': 'ignore'} >>> np.int16(32000) * np.int16(3) 30464 @@ -2078,16 +2083,28 @@ def seterr(all=None, divide=None, over=None, under=None, invalid=None): File "<stdin>", line 1, in <module> FloatingPointError: overflow encountered in short_scalars - >>> np.seterr(all='print') + >>> old_settings = np.seterr(all='print') + >>> np.geterr() {'over': 'print', 'divide': 'print', 'invalid': 'print', 'under': 'print'} >>> np.int16(32000) * np.int16(3) Warning: overflow encountered in short_scalars 30464 Calling `seterr` with no arguments resets treatment for all floating-point - errors to the defaults. + errors to the defaults. XXX: lies!!! code doesn't do that + >>> np.geterr() + {'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore', 'under': 'ignore'} + >>> np.seterr(all='warn') + {'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore', 'under': 'ignore'} + >>> np.geterr() + {'over': 'warn', 'divide': 'warn', 'invalid': 'warn', 'under': 'warn'} + >>> np.seterr() # XXX: this should reset to defaults according to docstring above + {'over': 'warn', 'divide': 'warn', 'invalid': 'warn', 'under': 'warn'} + >>> np.geterr() # XXX: but clearly it doesn't + {'over': 'warn', 'divide': 'warn', 'invalid': 'warn', 'under': 'warn'} >>> old_settings = np.seterr() + >>> old_settings = np.seterr(all='ignore') >>> np.geterr() {'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore', 'under': 'ignore'} @@ -2234,7 +2251,7 @@ def seterrcall(func): Callback upon error: >>> def err_handler(type, flag): - print "Floating point error (%s), with flag %s" % (type, flag) + ... print "Floating point error (%s), with flag %s" % (type, flag) ... >>> saved_handler = np.seterrcall(err_handler) @@ -2245,13 +2262,15 @@ def seterrcall(func): array([ Inf, Inf, Inf]) >>> np.seterrcall(saved_handler) + <function err_handler at 0x...> >>> np.seterr(**save_err) + {'over': 'call', 'divide': 'call', 'invalid': 'call', 'under': 'call'} Log error message: >>> class Log(object): - def write(self, msg): - print "LOG: %s" % msg + ... def write(self, msg): + ... print "LOG: %s" % msg ... >>> log = Log() @@ -2260,9 +2279,13 @@ def seterrcall(func): >>> np.array([1, 2, 3]) / 0.0 LOG: Warning: divide by zero encountered in divide + <BLANKLINE> + array([ Inf, Inf, Inf]) >>> np.seterrcall(saved_handler) + <__main__.Log object at 0x...> >>> np.seterr(**save_err) + {'over': 'log', 'divide': 'log', 'invalid': 'log', 'under': 'log'} """ if func is not None and not callable(func): @@ -2371,7 +2394,6 @@ class errstate(object): nan >>> with np.errstate(invalid='raise'): ... np.sqrt(-1) - ... Traceback (most recent call last): File "<stdin>", line 2, in <module> FloatingPointError: invalid value encountered in sqrt diff --git a/numpy/core/records.py b/numpy/core/records.py index b1736e0cf..77c8968f1 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -126,7 +126,7 @@ class format_parser: >>> np.format_parser(['f8', 'i4', 'a5'], ['col1', 'col2', 'col3'], ... ['T1', 'T2', 'T3']).dtype dtype([(('T1', 'col1'), '<f8'), (('T2', 'col2'), '<i4'), - (('T3', 'col3'), '|S5')] + (('T3', 'col3'), '|S5')]) `names` and/or `titles` can be empty lists. If `titles` is an empty list, titles will simply not appear. If `names` is empty, default field names |