From 30a3414a97f6bbcae73a936f658b639296fcdf98 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 31 Jan 2008 17:39:33 +0100 Subject: [PATCH] * Add file for misc tools, now containing make_iter to ensure something is iteratable. --- tools/misc.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tools/misc.py diff --git a/tools/misc.py b/tools/misc.py new file mode 100644 index 0000000..110c370 --- /dev/null +++ b/tools/misc.py @@ -0,0 +1,15 @@ +""" +Makes an iteratable object out of the given value. If value is already +iterable, it is simply returned. An exception is made for strings, which +are treated as non-iterable (It is assumed that you never want to have a +list of characters). For non iteratable values or strings, a list +containing only value is returned. +""" +def make_iter(value): + if (not isinstance(value, basestring)): + try: + iter(value) + return value + except TypeError: + pass + return [value] -- 2.30.2