From 77bab1f07fa1a8fa7cefdbd7747bc747bc4ad805 Mon Sep 17 00:00:00 2001 From: Prodesire Date: Thu, 2 Nov 2017 22:49:26 +0800 Subject: [PATCH] add itercompat.is_iterable for checking iterables --- pydu/itercompat.py | 8 ++++++++ tests/test_itercompat.py | 9 +++++++++ 2 files changed, 17 insertions(+) create mode 100644 pydu/itercompat.py create mode 100644 tests/test_itercompat.py diff --git a/pydu/itercompat.py b/pydu/itercompat.py new file mode 100644 index 0000000..9895e3f --- /dev/null +++ b/pydu/itercompat.py @@ -0,0 +1,8 @@ +def is_iterable(x): + "An implementation independent way of checking for iterables" + try: + iter(x) + except TypeError: + return False + else: + return True diff --git a/tests/test_itercompat.py b/tests/test_itercompat.py new file mode 100644 index 0000000..ede16a8 --- /dev/null +++ b/tests/test_itercompat.py @@ -0,0 +1,9 @@ +from pydu.itercompat import is_iterable + + +def test_is_iterable(): + assert is_iterable(list()) + assert is_iterable(tuple()) + assert is_iterable(dict()) + assert is_iterable(set()) + assert not is_iterable(1)