Esempio n. 1
0
def load_solution_function(file, parameters=1):
    try:
        path, file = os.path.split(os.path.abspath(file))
        sys.path.append(path)
        if file.endswith('.py'):
            module_name = file[:-3]
        elif file.endswith('.pyc'):
            module_name = file[:-4]
        else:
            module_name = file
        module = importlib.import_module(module_name)
        function = inspect.getmembers(module, inspect.isfunction)[0][1]
        assert len(inspect.signature(function).parameters) == parameters
        return function
    except ImportError:
        raise AdventureVerificationError(
            _('I can\'t import a module from {}. Are you sure the file exists '
              'and it\'s a valid python file?').format(file))
    except IndexError:
        raise AdventureVerificationError(
            _('I can\'t find a function definition in {}. Read the adventure '
              'again and use the template proposed there.').format(file))
    except AssertionError:
        raise AdventureVerificationError(
            _('I found a function in your file but it doesn\'t accept the '
              'required number of parameters ({}). If you define several '
              'functions in your file, make sure the solution function is the '
              'first one.'.format(parameters)))
Esempio n. 2
0
 def test(self, file):
     function = load_solution_function(file)
     correct_argument = 'Python'
     if not function(correct_argument):
         raise AdventureVerificationError(
             _("Your function didn't return True when executed with a "
               "correct argument '{}'.".format(correct_argument)))
     wrong_argument = 'ython'
     if function(wrong_argument):
         raise AdventureVerificationError(
             _("Your function returned True when executed with a wrong "
               "argument '{}'.".format(wrong_argument)))
Esempio n. 3
0
 def test(self, file):
     function = load_solution_function(file)
     correct_argument = get_random_string(self.dictionary, 4, 6) + '^'
     if not function(correct_argument):
         raise AdventureVerificationError(
             _("Your function didn't return True when executed with a "
               "correct argument '{}'.".format(correct_argument)))
     wrong_argument = get_random_string(self.dictionary, 4, 6)
     if function(wrong_argument):
         raise AdventureVerificationError(
             _("Your function returned True when executed with a wrong "
               "argument '{}'.".format(wrong_argument)))
Esempio n. 4
0
 def test(self, file):
     function = load_solution_function(file)
     correct_argument = 'hello' * random.randint(1, 5) + random.choice(
         ['python', 'pyschool'])
     if not function(correct_argument):
         raise AdventureVerificationError(
             _("Your function didn't return True when executed with a "
               "correct argument '{}'.".format(correct_argument)))
     wrong_argument = random.choice(['python', 'pyschool'])
     if function(wrong_argument):
         raise AdventureVerificationError(
             _("Your function returned True when executed with a wrong "
               "argument '{}'.".format(wrong_argument)))
Esempio n. 5
0
class Story(BaseStory):
    """Python Essentials Adventure"""
    name = 'py101'
    title = _('Learn Python essentials using the command line')
    adventures = (introduction, variables, lists, operators, formatting,
                  strings, conditions, loops, functions, classes, dictionaries,
                  modules)
Esempio n. 6
0
 def test(self, file):
     old_stdout = sys.stdout
     sys.stdout = io.StringIO()
     eval(codecs.open(file).read())
     message = sys.stdout.getvalue()
     sys.stdout = old_stdout
     assert message == _('Hi Python\n')
Esempio n. 7
0
 def test(self, file):
     function = load_solution_function(file)
     correct_argument = '{}{}{}'.format(
         random.randint(0, 9),
         ' ' * random.randint(0, 5),
         random.randint(0, 9)
     )
     if not function(correct_argument):
         raise AdventureVerificationError(
             _("Your function didn't return True when executed with a "
               "correct argument '{}'.".format(correct_argument))
         )
     wrong_argument = str(random.randint(0, 9))
     if function(wrong_argument):
         raise AdventureVerificationError(
             _("Your function returned True when executed with a wrong "
               "argument '{}'.".format(wrong_argument)))
Esempio n. 8
0
class Adventure(BaseAdventure):

    title = _('Print method')

    def test(self, file):
        old_stdout = sys.stdout
        sys.stdout = io.StringIO()
        eval(codecs.open(file).read())
        message = sys.stdout.getvalue()
        sys.stdout = old_stdout
        assert message == _('Hi Python\n')
Esempio n. 9
0
class Adventure(BaseAdventure):
    title = _('Introduction')

    @classmethod
    def test(cls, sourcefile):
        """Test against the provided file"""
        suite = unittest.TestSuite()
        raw_program = codecs.open(sourcefile).read()
        suite.addTest(TestOutput(raw_program, sourcefile))
        result = unittest.TextTestRunner().run(suite)
        if not result.wasSuccessful():
            raise AdventureVerificationError()
Esempio n. 10
0
 def test(self, file):
     function = load_solution_function(file)
     prefix = get_random_string(self.dictionary, 1, 5) + ','
     correct_argument = '{}{},{}'.format(
         prefix,
         get_random_string(self.dictionary, 1, 5),
         get_random_string(self.dictionary, 1, 5),
     )
     result = function(correct_argument)
     if result != prefix:
         raise AdventureVerificationError(
             _("Your function didn't return the expected string '{}' when "
               "executed with '{}'. "
               "It returned '{}'.".format(prefix, correct_argument, result))
         )
Esempio n. 11
0
class Adventure(BaseAdventure):

    title = _('Anchors')
    dictionary = string.ascii_lowercase + string.digits

    def test(self, file):
        function = load_solution_function(file)
        correct_argument = get_random_string(self.dictionary, 4, 6) + '^'
        if not function(correct_argument):
            raise AdventureVerificationError(
                _("Your function didn't return True when executed with a "
                  "correct argument '{}'.".format(correct_argument)))
        wrong_argument = get_random_string(self.dictionary, 4, 6)
        if function(wrong_argument):
            raise AdventureVerificationError(
                _("Your function returned True when executed with a wrong "
                  "argument '{}'.".format(wrong_argument)))
Esempio n. 12
0
class Story(BaseStory):

    name = 'learnregex'
    title = _('Learn regular expressions with Python')
    adventures = [
        introduction,
        special_characters,
        character_classes,
        negated_character_classes,
        the_dot,
        quantifiers,
        greediness,
        anchors,
        alternation,
        groups,
        capturing,
    ]
Esempio n. 13
0
class Adventure(BaseAdventure):

    title = _('Character classes')
    dictionary = string.ascii_lowercase

    def test(self, file):
        function = load_solution_function(file)
        correct_argument = '{}{}'.format(
            random.randint(0, 9), get_random_string(self.dictionary, 1, 5))
        if not function(correct_argument):
            raise AdventureVerificationError(
                _("Your function didn't return True when executed with a "
                  "correct argument '{}'.".format(correct_argument)))
        wrong_argument = get_random_string(self.dictionary, 1, 5)
        if function(wrong_argument):
            raise AdventureVerificationError(
                _("Your function returned True when executed with a wrong "
                  "argument '{}'.".format(wrong_argument)))
Esempio n. 14
0
class Adventure(BaseAdventure):

    title = _('Alternation')
    choices = ['red', 'green', 'blue']

    def test(self, file):
        function = load_solution_function(file)
        correct_argument = random.choice(self.choices)
        if not function(correct_argument):
            raise AdventureVerificationError(
                _("Your function didn't return True when executed with a "
                  "correct argument '{}'.".format(correct_argument)))
        wrong_argument = (random.choice(self.choices) +
                          random.choice(self.choices))
        if function(wrong_argument):
            raise AdventureVerificationError(
                _("Your function returned True when executed with a wrong "
                  "argument '{}'.".format(wrong_argument)))
Esempio n. 15
0
class Story(BaseStory):

    name = 'hipyschool'
    title = _('Sample pyschool story.')
    adventures = [print_method]