• danzibar

    I would be very grateful if someone could help explain the reason why I'm struggling with the importing of modules...

    I'm using Pythonista to learn OOP and I'm working through a tutorial which centres around the creation of a roulette simulator.

    The tutorial emphasis the use of unit testing.

    Here is my unit test code which is causing me the problem:

    
    import unittest
    from outcome import Outcome
    from wheel import Wheel
    from non_random import NonRandom
    
    
    class WheelBuilderTestCase(unittest.TestCase):
        
        def setUp(self):
            self.five = Outcome("00-0-1-2-3", 6)
            self.zero = Outcome("0", 35)
            self.zerozero = Outcome("00", 35)
            rng = NonRandom(0, 1)
            rng.set_seed(5);
            self.wheel = Wheel(rng)
                
        def testWheelConstruction(self):
            pass
    
    if __name__ == '__main__':
        unittest.main()
    
    

    Here is the outcome module:

    class Outcome (object):
        
        def __init__(self, name, odds):
            self.name = name
            self.odds = odds
            
        def winAmount(self, amount):
            return ( self.odds * amount )
            
        def __eq__(self, other):
            return ( self.name == other.name )
            
        def __ne__(self, other):
            return not self.__eq__(other)
            
        def __str__(self):
            return "%s (%d:1)" % ( self.name, self.odds )
    
    

    Here is the bin module:

    
    class Bin (object):
            
        def __init__(self, *outcomes):
            self.outcomes = frozenset(outcomes)
                
        def add(self, outcome):
            self.outcomes |= set( [outcome] )
                
        def __str__(self):
            return ', '.join( map(str, self.outcomes) )
            
    

    and here is the wheel module:

    
    from bin import Bin
    
    class Wheel (object):
        
        def __init__(self, rng):
            self.rng = rng
            self.bins = tuple(Bin() for i in range(38))
            
        def addOutcome(self, number, outcome):
            self.bins[number].add(outcome)
            
        def next(self):
            return rng.choice(self.bins)
            
        def get(self, bin):
            return self.bins[bin]
            
    

    However when I run the unit test wheel_test.py I get the following error:

    E
    ======================================================================
    ERROR: testWheelConstruction (__main__.WheelBuilderTestCase)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/var/mobile/Applications/B3FDE480-BD57-442B-BAA6-D0F3CF220D07/Documents/roulette/wheel_test.py", line 21, in testWheelConstruction
        self.wheel = Wheel(2)
      File "./wheel.py", line 7, in __init__
        self.rng = rng
      File "./wheel.py", line 7, in <genexpr>
        self.rng = rng
    NameError: global name 'Bin' is not defined
    
    ----------------------------------------------------------------------
    Ran 1 test in 0.001s
    
    FAILED (errors=1)
    EXIT (1)
    

    It appears that the Bin class isn't defined in the WheelBuilderTestCase even though I imported the Wheel class from the wheel module which does include the import of the Bin class via the bin module.

    The bizarre thing is that this works fine in IDLE.

    Any thoughts..?

    posted in Editorial read more

Internal error.

Oops! Looks like something went wrong!