Skip to content

Commit 745b33e

Browse files
Ken KundertKen Kundert
authored andcommitted
added is_array()
1 parent f7ab858 commit 745b33e

File tree

8 files changed

+97
-10
lines changed

8 files changed

+97
-10
lines changed

doc/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ Inform Utilities
6464

6565
.. autofunction:: inform.is_iterable
6666

67+
.. autofunction:: inform.is_array
68+
6769
.. autofunction:: inform.is_mapping
6870

6971
.. autofunction:: inform.is_str

doc/releases.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Latest development release
99
| Version: 1.33
1010
| Released: 2024-12-11
1111
12+
- Added :func:`is_array`.
13+
1214

1315
1.33 (2024-12-11)
1416
-----------------

doc/user.rst

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -797,17 +797,25 @@ You can specify the output stream when creating an informant. If you do not,
797797
then the stream uses is under the control of *Inform's* *stream_policy*
798798
argument.
799799

800-
If *stream_policy* is set to 'termination', then all messages are sent to the
800+
If *stream_policy* is set to 'termination' then all messages are sent to the
801801
standard output except the final termination message, which is set to standard
802802
error. This is suitable for programs whose output largely consists of status
803803
messages rather than data, and so would be unlikely to be used in a pipeline.
804804

805-
If *stream_policy* is 'header'. then all messages with headers (those messages
805+
If *stream_policy* is 'header' then all messages with headers (those messages
806806
produced from informants with *severity*) are sent to the standard error stream
807807
and all other messages are sent to the standard output. This is more suitable
808808
for programs whose output largely consists of data and so would likely be used
809809
in a pipeline.
810810

811+
If *stream_policy* is 'errors' then all error messages are sent to the standard
812+
error stream and all other messages are sent to the standard output. This is
813+
also commonly used for programs that act s filters.
814+
815+
If *stream_policy* is 'all' stderr is used for all informants that do not
816+
explicitly set their stream. By default, no informants explicitly set their
817+
stream, but your can create new informants and explicitly set the stream.
818+
811819
It is also possible for *stream_policy* to be a function that takes three
812820
arguments, the informant and the standard output and error streams. It should
813821
return the desired stream.
@@ -1534,6 +1542,37 @@ is_iterable
15341542
True
15351543
15361544
1545+
.. _is_array desc:
1546+
1547+
is_array
1548+
""""""""
1549+
1550+
.. py:function:: is_array(obj)
1551+
:noindex:
1552+
1553+
:func:`is_array` returns *True* if its argument is a list or a tuple. This
1554+
includes other list-like objects.
1555+
1556+
.. code-block:: python
1557+
1558+
>>> from inform import is_array
1559+
1560+
>>> is_array('') # string
1561+
False
1562+
1563+
>>> is_array([]) # list
1564+
True
1565+
1566+
>>> is_array(()) # tuple
1567+
True
1568+
1569+
>>> is_array({}) # dictionary
1570+
False
1571+
1572+
>>> is_array(set()) # set
1573+
False
1574+
1575+
15371576
.. _is_mapping desc:
15381577

15391578
is_mapping
@@ -1542,8 +1581,8 @@ is_mapping
15421581
.. py:function:: is_mapping(obj)
15431582
:noindex:
15441583

1545-
:func:`is_collection` returns *True* if its argument is a mapping. This
1546-
includes dictionary and other dictionary-like objects.
1584+
:func:`is_mapping` returns *True* if its argument is a mapping. This includes
1585+
dictionary and other dictionary-like objects.
15471586

15481587
.. code-block:: python
15491588

inform/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .inform import (
22
# inform utility functions and classes
3-
cull, indent, is_collection, is_iterable, is_mapping, is_str, join,
3+
cull, indent, is_collection, is_iterable, is_array, is_mapping, is_str, join,
44
Color, Info, LoggingCache,
55

66
# user utility functions and classes

inform/inform.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,41 @@ def is_collection(obj):
227227
>>> is_collection({}) # dictionary
228228
True
229229
230+
>>> is_collection(set()) # set
231+
True
232+
230233
"""
231234
return is_iterable(obj) and not is_str(obj)
232235

236+
# is_array {{{2
237+
def is_array(obj):
238+
"""Identifies objects that are is_array (tuples and arrays).
239+
240+
Returns *True* if argument is a sequence (tuple or list).
241+
242+
**Example**::
243+
244+
>>> from inform import is_array
245+
>>> is_array('') # string
246+
False
247+
248+
>>> is_array([]) # list
249+
True
250+
251+
>>> is_array(()) # tuple
252+
True
253+
254+
>>> is_array({}) # dictionary
255+
False
256+
257+
>>> is_array(set()) # set
258+
False
259+
260+
"""
261+
from collections.abc import Sequence
262+
return isinstance(obj, Sequence) and not is_str(obj)
263+
264+
233265
# is_mapping {{{2
234266
def is_mapping(obj):
235267
"""Identifies objects that are mappings (are dictionary like).

inform/inform.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def is_str(arg: Any) -> bool: ...
2323
def is_iterable(obj: Any) -> bool: ...
2424
def is_collection(obj: Any) -> bool: ...
2525
def is_mapping(obj: Any) -> bool: ...
26+
def is_array(obj: Any) -> bool: ...
2627

2728
class Color:
2829
enable: bool

tests/test_utilities.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
from inform import (
44
Color, Error, Info, LoggingCache, columns, conjoin, did_you_mean, comment,
55
cull, dedent, display, done, error, fatal, fmt, full_stop, indent, Inform,
6-
is_collection, is_iterable, is_mapping, is_str, join, get_prog_name,
7-
get_informer, narrate, os_error, output, plural, render, terminate,
8-
title_case, tree, truth, warn, ddd, ppp, sss, vvv, ProgressBar, parse_range,
9-
format_range
6+
is_collection, is_iterable, is_mapping, is_array, is_str, join,
7+
get_prog_name, get_informer, narrate, os_error, output, plural, render,
8+
terminate, title_case, tree, truth, warn, ddd, ppp, sss, vvv, ProgressBar,
9+
parse_range, format_range
1010
)
1111
from textwrap import dedent as tw_dedent
1212
import sys
@@ -795,20 +795,31 @@ def test_is_iterable():
795795
assert is_iterable([]) == True
796796
assert is_iterable(()) == True
797797
assert is_iterable({}) == True
798+
assert is_iterable(set()) == True
798799

799800
def test_is_collection():
800801
assert is_collection(0) == False
801802
assert is_collection('') == False
802803
assert is_collection([]) == True
803804
assert is_collection(()) == True
804805
assert is_collection({}) == True
806+
assert is_collection(set()) == True
805807

806808
def test_is_mapping():
807809
assert is_mapping(0) == False
808810
assert is_mapping('') == False
809811
assert is_mapping([]) == False
810812
assert is_mapping(()) == False
811813
assert is_mapping({}) == True
814+
assert is_mapping(set()) == False
815+
816+
def test_is_array():
817+
assert is_array(0) == False
818+
assert is_array('') == False
819+
assert is_array([]) == True
820+
assert is_array(()) == True
821+
assert is_array({}) == False
822+
assert is_array(set()) == False
812823

813824
def test_color():
814825
assert Color('white', scheme='dark')('') == ''

tests/test_zdoctests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_inform():
2323
return
2424

2525
rv = doctest.testfile('../inform/inform.py', optionflags=doctest.ELLIPSIS)
26-
assert rv.attempted == 173
26+
assert rv.attempted == 181
2727
assert rv.failed == 0
2828

2929
def test_manual():

0 commit comments

Comments
 (0)