python_koans / python3 / koans / about_iteration.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from runner.koan import *

class AboutIteration(Koan):

    def test_iterators_are_a_type(self):
        it = iter(range(1,6))
        
        fib = 0
        
        for num in it:
            fib += num
            
        self.assertEqual(__ , fib)

    def test_iterating_with_next(self):
        stages = iter(['alpha','beta','gamma'])

        try:
            self.assertEqual(__, next(stages))
            next(stages)
            self.assertEqual(__, next(stages))
            next(stages)
        except StopIteration as ex:
            err_msg = 'Ran out of iterations'
            
        self.assertRegexpMatches(err_msg, __)         

    # ------------------------------------------------------------------

    def add_ten(self, item):
        return item + 10

    def test_map_transforms_elements_of_a_list(self):
        seq = [1, 2, 3]
        mapped_seq = list()
   
        mapping = map(self.add_ten, seq)
        
        self.assertNotEqual(list, type(mapping).__name__)
        self.assertEqual(__, type(mapping).__name__)
        # In Python 3 built in iterator funcs return iteratable view objects
        # instead of lists
   
        for item in mapping:
            mapped_seq.append(item)
            
        self.assertEqual(__, mapped_seq)
        
        # None, iterator methods actually return objects of iter type in
        # python 3. In python 2 map() would give you a list.
        
    def test_filter_selects_certain_items_from_a_list(self):
        def is_even(item):
            return (item % 2) == 0

        seq = [1, 2, 3, 4, 5, 6]
        even_numbers = list()
   
        for item in filter(is_even, seq):
            even_numbers.append(item)
            
        self.assertEqual(__, even_numbers)
    
    def test_just_return_first_item_found(self):
        def is_big_name(item):
            return len(item) > 4
        
        names = ["Jim", "Bill", "Clarence", "Doug", "Eli"]
        name = None

        iterator = filter(is_big_name, names)
        try:
            name = next(iterator)
        except StopIteration:
            msg = 'Ran out of big names'

        self.assertEqual(__, name)
        

    # ------------------------------------------------------------------

    def add(self,accum,item):
        return accum + item

    def multiply(self,accum,item):
        return accum * item
        
    def test_reduce_will_blow_your_mind(self):
        import functools
        # As of Python 3 reduce() has been demoted from a builtin function
        # to the functools module.
        
        result = functools.reduce(self.add, [2, 3, 4])
        self.assertEqual(__, type(result).__name__)
        # Reduce() syntax is same as Python 2
        
        self.assertEqual(__, result)
    
        result2 = functools.reduce(self.multiply, [2, 3, 4], 1) 
        self.assertEqual(__, result2)
    
        # Extra Credit:
        # Describe in your own words what reduce does.
    
    # ------------------------------------------------------------------

    def test_creating_lists_with_list_comprehensions(self):        
        feast = ['lambs', 'sloths', 'orangutans', 'breakfast cereals', 'fruit bats']
        
        comprehension = [delicacy.capitalize() for delicacy in feast]
        
        self.assertEqual(__, comprehension[0])
        self.assertEqual(__, comprehension[2])
        
    def test_use_pass_for_iterations_with_no_body(self):
        for num in range(1,5):
            pass
                
        self.assertEqual(__, num)

    # ------------------------------------------------------------------
        
    def test_all_iteration_methods_work_on_any_sequence_not_just_lists(self):
        # Ranges are an iteratable sequence
        result = map(self.add_ten, range(1,4))
        self.assertEqual(__, list(result))

        try:
            file = open("example_file.txt")

            try:
                def make_upcase(line):
                    return line.strip().upper()
                upcase_lines = map(make_upcase, file.readlines())
                self.assertEqual(__, list(upcase_lines))
            finally:
                # Arg, this is ugly.
                # We will figure out how to fix this later.
                file.close()
        except IOError:
            # should never happen
            self.fail()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.