Skip to content

Commit

Permalink
Correct code style to make it compatible with flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignacio Avas committed May 3, 2017
1 parent 2545526 commit 08e61ac
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 44 deletions.
9 changes: 5 additions & 4 deletions py101/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from story.story import BaseStory
from story.translation import gettext as _

from . import (introduction, variables, lists, operators, formatting, strings, conditions, loops,
functions, classes, dictionaries, modules)
from . import (introduction, variables, lists, operators, formatting, strings,
conditions, loops, functions, classes, dictionaries, modules)


__author__ = """Sophilabs"""
Expand All @@ -14,5 +14,6 @@ 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)
adventures = (introduction, variables, lists, operators, formatting,
strings, conditions, loops, functions, classes, dictionaries,
modules)
6 changes: 3 additions & 3 deletions py101/boilerplate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import sys
import unittest
from story.adventures import AdventureVerificationError, BaseAdventure
from story.translation import gettext as _


class TestOutput(unittest.TestCase):
Expand All @@ -31,8 +30,9 @@ def tearDown(self):
def runTest(self):
"""Makes a simple test of the output"""

#code = compile(self.candidate_code, self.file_name, 'exec', optimize=0)
#exec(code)
# code = compile(self.candidate_code, self.file_name, 'exec',
# optimize=0)
# exec(code)
self.fail("Test not implemented")


Expand Down
5 changes: 3 additions & 2 deletions py101/classes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ def tearDown(self):
def runTest(self):
"""Makes a simple test of the output"""

#code = compile(self.candidate_code, self.file_name, 'exec', optimize=0)
#exec(code)
# code = compile(self.candidate_code, self.file_name, 'exec',
# optimize=0)
# exec(code)
self.fail("Test not implemented")


Expand Down
9 changes: 6 additions & 3 deletions py101/conditions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ def __init__(self, candidate_code, file_name='<inline>'):
super(TestOutput, self).__init__()
self.candidate_code = candidate_code
self.file_name = file_name
self.inmutable_code = ast.parse(self.inmutable_code_str, file_name, 'exec')
self.inmutable_code = ast.parse(self.inmutable_code_str,
file_name,
'exec'
)

def setUp(self):
self.__old_stdout = sys.stdout
Expand All @@ -50,7 +53,8 @@ def runTest(self):
# Looks if the code is the same as the provided code.
body_dump = ast.dump(body)
for node in self.inmutable_code.body:
self.assertTrue(body_dump.find(ast.dump(node)) >= 0, "Provided code should not be modified")
self.assertTrue(body_dump.find(ast.dump(node)) >= 0,
"Provided code should not be modified")

code = compile(self.candidate_code, self.file_name, 'exec')
exec(code)
Expand All @@ -59,7 +63,6 @@ def runTest(self):
'Output is not correct')



class Adventure(BaseAdventure):
"""Conditions Adventure"""
title = _('Conditions')
Expand Down
5 changes: 3 additions & 2 deletions py101/dictionaries/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ def tearDown(self):
def runTest(self):
"""Makes a simple test of the output"""

#code = compile(self.candidate_code, self.file_name, 'exec', optimize=0)
#exec(code)
# code = compile(self.candidate_code, self.file_name, 'exec',
# optimize=0)
# exec(code)
self.fail("Test not implemented")


Expand Down
6 changes: 5 additions & 1 deletion py101/formatting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ def runTest(self):
'{}' in node.value.s
]

self.assertTrue(len(format_nodes) > 0, "It should have at one format call with curly braces {}")
self.assertGreater(
len(format_nodes),
0,
"It should have at one format call with curly braces {}"
)

exec(code)
self.assertMultiLineEqual('Talk is cheap. Show me the code.\n',
Expand Down
10 changes: 8 additions & 2 deletions py101/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
class TestOutput(unittest.TestCase):
"""Adventure test"""

expected_output = '\n'.join([str(element) for element in range(2, 101, 2)]) + '\n'
expected_output = '\n'.join([
str(element) for element in range(2, 101, 2)
]) + '\n'

def __init__(self, candidate_code, file_name='<inline>'):
"""Init the test"""
Expand Down Expand Up @@ -48,7 +50,11 @@ def runTest(self):
if isinstance(node, ast.Call)
])

self.assertTrue(len(defined_functions & called_functions) > 0, "Should call at least one defined function")
self.assertGreater(
len(defined_functions & called_functions),
0,
"Should call at least one defined function"
)

code = compile(self.candidate_code, self.file_name, 'exec', optimize=0)
exec(code)
Expand Down
6 changes: 2 additions & 4 deletions py101/introduction/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class TestOutput(unittest.TestCase):
"Introduction Adventure test"
"""Introduction Adventure test"""
def __init__(self, candidate_code, file_name='<inline>'):
"""Init the test"""
super(TestOutput, self).__init__()
Expand All @@ -29,7 +29,7 @@ def tearDown(self):

@staticmethod
def mock_print(stringy):
"Mock function"
"""Mock function"""
pass

def runTest(self):
Expand All @@ -55,5 +55,3 @@ def test(cls, sourcefile):
result = unittest.TextTestRunner().run(suite)
if not result.wasSuccessful():
raise AdventureVerificationError()


4 changes: 3 additions & 1 deletion py101/lists/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def runTest(self):

code = compile(self.candidate_code, self.file_name, 'exec', optimize=0)

self.assertIn('languages', code.co_names, 'Should have defined languages variable')
self.assertIn('languages',
code.co_names,
'Should have defined languages variable')
exec(code)
lines = self.__mockstdout.getvalue().split('\n')
self.assertEqual([str(["ADA", "Pascal", "Fortran", "Smalltalk"]), ''],
Expand Down
5 changes: 3 additions & 2 deletions py101/loops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class TestOutput(unittest.TestCase):
['']
)


def __init__(self, candidate_code, file_name='<inline>'):
"""Init the test"""
super(TestOutput, self).__init__()
Expand All @@ -47,7 +46,9 @@ def runTest(self):
for node in ast.walk(body)
if isinstance(node, ast.If)
]
self.assertGreater(len(if_statements), 0, "Should have at least on if statement")
self.assertGreater(len(if_statements),
0,
"Should have at least on if statement")

self.assertMultiLineEqual(self.correct_output,
self.__mockstdout.getvalue(),
Expand Down
5 changes: 3 additions & 2 deletions py101/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ def tearDown(self):
def runTest(self):
"""Makes a simple test of the output"""

#code = compile(self.candidate_code, self.file_name, 'exec', optimize=0)
#exec(code)
# code = compile(self.candidate_code, self.file_name, 'exec',
# optimize=0)
# exec(code)
self.fail("Test not implemented")


Expand Down
15 changes: 11 additions & 4 deletions py101/operators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ def runTest(self):
body = ast.parse(self.candidate_code, self.file_name, 'exec')
code = compile(self.candidate_code, self.file_name, 'exec')

mult_instructions = [node for node in ast.walk(body) if isinstance(node, ast.Mult)]
self.assertTrue(len(mult_instructions) > 0, "It should have at least one duplication")

mult_instructions = [
node for node in ast.walk(body)
if isinstance(node, ast.Mult)
]
self.assertGreater(len(mult_instructions),
0,
"It should have at least one duplication"
)
exec(code)
self.assertMultiLineEqual('ka'*10+'\n', self.__mockstdout.getvalue(), "Should have printed ka 10 times")
self.assertMultiLineEqual('ka'*10+'\n',
self.__mockstdout.getvalue(),
"Should have printed ka 10 times")


class Adventure(BaseAdventure):
Expand Down
21 changes: 16 additions & 5 deletions py101/strings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,24 @@ def runTest(self):

code = compile(self.candidate_code, self.file_name, 'exec', optimize=0)

self.assertIn('len', code.co_names, 'Should have called the len function')
self.assertIn('split', code.co_names, 'Should have called the split function')
self.assertIn('upper', code.co_names, 'Should have called the upper function')
self.assertIn('lower', code.co_names, 'Should have called the lower function')
self.assertIn('len',
code.co_names,
'Should have called the len function')
self.assertIn('split',
code.co_names,
'Should have called the split function')
self.assertIn('upper',
code.co_names,
'Should have called the upper function')
self.assertIn('lower',
code.co_names,
'Should have called the lower function')
exec(code)
lines = self.__mockstdout.getvalue().split('\n')
self.assertEqual(self.expected_output, lines, 'Should have same output')
self.assertEqual(self.expected_output,
lines,
'Should have same output'
)


class Adventure(BaseAdventure):
Expand Down
22 changes: 17 additions & 5 deletions py101/variables/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class TestOutput(unittest.TestCase):
"Introduction Adventure test"
def __init__(self, candidate_code, file_name = '<inline>'):
def __init__(self, candidate_code, file_name='<inline>'):
"""Init the test"""
super(TestOutput, self).__init__()
self.candidate_code = candidate_code
Expand All @@ -32,12 +32,24 @@ def runTest(self):

code = compile(self.candidate_code, self.file_name, 'exec', optimize=0)

self.assertIn('myinteger', code.co_names, 'Should have defined myinteger variable')
self.assertIn('mystring', code.co_names, 'Should have defined mystring variable')
self.assertIn('myinteger',
code.co_names,
'Should have defined myinteger variable'
)
self.assertIn('mystring',
code.co_names,
'Should have defined mystring variable'
)
exec(code)
lines = [line.lower().strip() for line in self.__mockstdout.getvalue().split('\n')]
lines = [
line.lower().strip()
for line in self.__mockstdout.getvalue().split('\n')
]
self.assertEqual(3, len(lines), 'Should have printed two lines')
self.assertEqual(['4', 'python string here', ''], lines, 'Should have same output')
self.assertEqual(['4', 'python string here', ''],
lines,
'Should have same output'
)


class Adventure(BaseAdventure):
Expand Down
17 changes: 13 additions & 4 deletions tests/test_story.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,32 @@ def __init__(self, test_module, good_solution):
self.module = test_module
self.good_solution = good_solution


adventures = [
AdventureData(
py101.introduction,
"""print('Hello World')"""
),
AdventureData(
py101.variables,
"""myinteger = 4; mystring = 'Python String Here'; print(myinteger); print(mystring)"""
"""myinteger = 4;
mystring = 'Python String Here';
print(myinteger);
print(mystring)"""
),
AdventureData(
py101.lists,
"""languages = ["ADA", "Pascal", "Fortran", "Smalltalk"]; print(languages)"""
"""languages = ["ADA", "Pascal", "Fortran", "Smalltalk"];
print(languages)"""
),
AdventureData(
py101.operators,
"""print('ka'*10)"""
),
AdventureData(
py101.formatting,
"""s = 'Talk is {}. Show me the {}.'.format('cheap', 'code'); print(s)"""
"""s = 'Talk is {}. Show me the {}.'.format('cheap', 'code');
print(s)"""
),
AdventureData(
py101.strings,
Expand Down Expand Up @@ -87,7 +93,10 @@ def test_name(self):
self.assertEqual(py101.Story().name, 'py101', "name should be py101")

def test_content(self):
story_adventure_classes = [adventure.__class__ for adventure in py101.Story().adventures]
story_adventure_classes = [
adventure.__class__
for adventure in py101.Story().adventures
]

for adventure in adventures:
self.assertIn(adventure.module.Adventure, story_adventure_classes)
Expand Down

0 comments on commit 08e61ac

Please sign in to comment.