From 6b48cf96c36e5164391da536aff77593f32cd4af Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Wed, 5 Aug 2009 22:30:33 -0700 Subject: [PATCH] validation --- advanced-iterators.html | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/advanced-iterators.html b/advanced-iterators.html index 98eee7a..96f1314 100755 --- a/advanced-iterators.html +++ b/advanced-iterators.html @@ -51,7 +51,7 @@ import itertools def solve(puzzle): words = re.findall('[A-Z]+', puzzle.upper()) unique_characters = set(''.join(words)) - assert len(unique_characters) <= 10, 'Too many letters' + assert len(unique_characters) <= 10, 'Too many letters' first_letters = {word[0] for word in words} n = len(first_letters) sorted_characters = ''.join(first_letters) + \ @@ -164,11 +164,11 @@ if __name__ == '__main__': >>> assert 1 + 1 == 2 >>> assert 1 + 1 == 3 Traceback (most recent call last): - File "<stdin>", line 1, in + File "<stdin>", line 1, in <module> AssertionError >>> assert 2 + 2 == 5, "Only for very large values of 2" Traceback (most recent call last): - File "", line 1, in + File "", line 1, in <module> AssertionError: Only for very large values of 2
  1. The assert statement is followed by any valid Python expression. In this case, the expression 1 + 1 == 2 evaluates to True, so the assert statement does nothing. @@ -335,7 +335,7 @@ StopIteration

    What does this have to do with the itertools module? I’m glad you asked.

    -

    …continuing from the previous interactive shell… +…continuing from the previous interactive shell… >>> import itertools >>> groups = itertools.groupby(names, len) >>> groups @@ -582,8 +582,8 @@ NameError: name '__import__' is not defined >>> eval("__import__('subprocess').getoutput('rm -rf /')", ... {"__builtins__":None}, {}) Traceback (most recent call last): - File "", line 1, in - File "", line 1, in + File "<stdin>", line 1, in + File "<string>", line 1, in NameError: name '__import__' is not defined

    1. To evaluate untrusted expressions safely, you need to define a global namespace dictionary that maps "__builtins__" to None, the Python null value. Internally, the “built-in” functions are contained within a pseudo-module called "__builtins__". This pseudo-module (i.e. the set of built-in functions) is made available to evaluated expressions unless you explicitly override it.