summaryrefslogtreecommitdiff
path: root/pokemontools/helpers.py
diff options
context:
space:
mode:
authorBryan Bishop <kanzure@gmail.com>2013-09-01 19:34:50 -0700
committerBryan Bishop <kanzure@gmail.com>2013-09-01 19:34:50 -0700
commit2e6d805aea27741bc0e6097bd52c6f2b6d176b74 (patch)
treebb91bace929310f2787b80424fdada70bba94e81 /pokemontools/helpers.py
parent7aa016fb528bcc8dcb30c6a887957851623eccc0 (diff)
parent52d4978974263f4f19985632442291c2d30c4b67 (diff)
Merge pull request #7 from kanzure/september-cleanup
python cleanup
Diffstat (limited to 'pokemontools/helpers.py')
-rw-r--r--pokemontools/helpers.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/pokemontools/helpers.py b/pokemontools/helpers.py
new file mode 100644
index 0000000..2ecb073
--- /dev/null
+++ b/pokemontools/helpers.py
@@ -0,0 +1,51 @@
+"""
+Generic functions that should be reusable anywhere in pokemontools.
+"""
+import os
+
+def index(seq, f):
+ """
+ return the index of the first item in seq
+ where f(item) == True.
+ """
+ return next((i for i in xrange(len(seq)) if f(seq[i])), None)
+
+def grouper(some_list, count=2):
+ """
+ splits a list into sublists
+
+ given: [1, 2, 3, 4]
+ returns: [[1, 2], [3, 4]]
+ """
+ return [some_list[i:i+count] for i in range(0, len(some_list), count)]
+
+def flattener(x):
+ """
+ flattens a list of sublists into just one list (generator)
+ """
+ try:
+ it = iter(x)
+ except TypeError:
+ yield x
+ else:
+ for i in it:
+ for j in flattener(i):
+ yield j
+
+def flatten(x):
+ """
+ flattens a list of sublists into just one list
+ """
+ return list(flattener(x))
+
+def mkdir_p(path):
+ """
+ Make a directory at a given path.
+ """
+ try:
+ os.makedirs(path)
+ except OSError as exc: # Python >2.5
+ if exc.errno == errno.EEXIST:
+ pass
+ else:
+ raise exc