Browse Source

Use list comperhansion instead of for loop

 * nicer functional programming style.

 [Python's wiki][1] states:

  You can think of map as a for moved into C code.
  The only restriction is that the "loop body" of map must be a function call.
  Besides the syntactic benefit of list comprehensions,
  they are often as fast or faster than equivalent use of map.

 [1]:https://wiki.python.org/moin/PythonSpeed/PerformanceTips
Oz N Tiram 9 years ago
parent
commit
65753f553a
1 changed files with 7 additions and 6 deletions
  1. 7 6
      tests/test.py

+ 7 - 6
tests/test.py

@@ -54,9 +54,10 @@ except OSError:
 shift_factors = map(lambda x: (x - 1) / 5 +1,   range(1,21))
 
 
-for i in range(1,21):
-    f = open((os.path.join(CONFIG['content_root'], 'post' + str(i) + '.md')),
-             'w')
-    d = {'number': i,
-         'tags': shift(tags, shift_factors[i-1])[:-1] }
-    f.write(post.format(**d))
+def write_file(i):
+    f = open((os.path.join(CONFIG['content_root'],
+                           'post{}.md'.format(i))), 'w')
+    f.write(post.format(**{'number': i,
+                           'tags': shift(tags, shift_factors[i-1])[:-1]}))
+
+[write_file(i) for i in range(1, 21)]