From af1092c89ac19c635ec5c040be4364d76d4d9947 Mon Sep 17 00:00:00 2001 From: Chris Warrick Date: Thu, 20 Aug 2015 20:51:20 +0200 Subject: [PATCH] More Python 3/PEP 8/PEP 257 compliance Signed-off-by: Chris Warrick --- context_managers.rst | 2 +- coroutines.rst | 2 +- decorators.rst | 24 ++++++++++++------------ for_-_else.rst | 9 ++++----- ternary_operators.rst | 4 ++-- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/context_managers.rst b/context_managers.rst index f144967..45b2b8d 100644 --- a/context_managers.rst +++ b/context_managers.rst @@ -123,7 +123,7 @@ Let's try handling the exception in the ``__exit__`` method: def __enter__(self): return self.file_obj def __exit__(self, type, value, traceback): - print "Exception has been handled" + print("Exception has been handled") self.file_obj.close() return True diff --git a/coroutines.rst b/coroutines.rst index 4763c0f..8615e13 100644 --- a/coroutines.rst +++ b/coroutines.rst @@ -34,7 +34,7 @@ example would be a ``grep`` alternative in Python: .. code:: python def grep(pattern): - print "Searching for %s" % pattern + print("Searching for", pattern) while True: line = (yield) if pattern in line: diff --git a/decorators.rst b/decorators.rst index 1eddce9..1fd294c 100644 --- a/decorators.rst +++ b/decorators.rst @@ -30,7 +30,7 @@ First of all let's understand functions in python: # We are not using parentheses here because we are not calling the function hi # instead we are just putting it into the greet variable. Let's try to run this - print greet() + print(greet()) # output: 'hi yasoob' # Let's see what happens if we delete the old hi function! @@ -51,7 +51,7 @@ other functions: .. code:: python def hi(name="yasoob"): - print "now you are inside the hi() function" + print("now you are inside the hi() function") def greet(): return "now you are in the greet() function" @@ -131,11 +131,11 @@ Giving a function as an argument to another function: return "hi yasoob!" def doSomethingBeforeHi(func): - print("I am doing some boring work before executing hi()") + print("I am doing some boring work before executing hi()") print(func()) doSomethingBeforeHi(hi) - #outputs:I am doing some boring work before executing hi() + #outputs:I am doing some boring work before executing hi() # hi yasoob! Now you have all the required knowledge to learn what decorators really @@ -152,7 +152,7 @@ previous decorator and make a little bit more usable program: def a_new_decorator(a_func): def wrapTheFunction(): - print "I am doing some boring work before executing a_func()" + print("I am doing some boring work before executing a_func()") a_func() @@ -170,7 +170,7 @@ previous decorator and make a little bit more usable program: #now a_function_requiring_decoration is wrapped by wrapTheFunction() a_function_requiring_decoration() - #outputs:I am doing some boring work before executing a_function_requiring_decoration() + #outputs:I am doing some boring work before executing a_function_requiring_decoration() # I am the function which needs some decoration to remove my foul smell # I am doing some boring work after executing a_function_requiring_decoration() @@ -185,12 +185,12 @@ run the previous code sample using @. @a_new_decorator def a_function_requiring_decoration(): - """Hey yo! Decorate me!""" + """Hey you! Decorate me!""" print("I am the function which needs some decoration to " - " remove my foul smell") + "remove my foul smell") a_function_requiring_decoration() - #outputs: I am doing some boring work before executing a_function_requiring_decoration() + #outputs: I am doing some boring work before executing a_function_requiring_decoration() # I am the function which needs some decoration to remove my foul smell # I am doing some boring work after executing a_function_requiring_decoration() @@ -219,7 +219,7 @@ that is ``functools.wraps``. Let's modify our previous example to use def a_new_decorator(a_func): @wraps(a_func) def wrapTheFunction(): - print("I am doing some boring work before executing a_func()") + print("I am doing some boring work before executing a_func()") a_func() print("I am doing some boring work after executing a_func()") return wrapTheFunction @@ -236,7 +236,7 @@ that is ``functools.wraps``. Let's modify our previous example to use Now that is much better. Let's move on and learn some use-cases of decorators. -**Blueprint :** +**Blueprint:** .. code:: python @@ -313,7 +313,7 @@ Logging is another area where the decorators shine. Here is an example: @logit def addition_func(x): - """does some math""" + """Do some math.""" return x + x diff --git a/for_-_else.rst b/for_-_else.rst index ce59d13..335cb65 100644 --- a/for_-_else.rst +++ b/for_-_else.rst @@ -13,7 +13,7 @@ like this: fruits = ['apple', 'banana', 'mango'] for fruit in fruits: - print fruit.capitalize() + print(fruit.capitalize()) # Output: Apple # Banana @@ -60,7 +60,7 @@ documentation: for n in range(2, 10): for x in range(2, n): if n % x == 0: - print n, 'equals', x, '*', n/x + print(n, 'equals', x, '*', n/x) break It finds factors for numbers between 2 to 10. Now for the fun part. We @@ -72,9 +72,8 @@ prime and tells us so: for n in range(2, 10): for x in range(2, n): if n % x == 0: - print n, 'equals', x, '*', n/x + prin( n, 'equals', x, '*', n/x) break else: # loop fell through without finding a factor - print n, 'is a prime number' - + print(n, 'is a prime number') diff --git a/ternary_operators.rst b/ternary_operators.rst index 47e6336..0a077fd 100644 --- a/ternary_operators.rst +++ b/ternary_operators.rst @@ -39,8 +39,8 @@ is some sample code: .. code:: python fat = True - fitness = ("skinny","fat")[fat] - print("Ali is " + fitness) + fitness = ("skinny", "fat")[fat] + print("Ali is", fitness) # Output: Ali is fat The above example is not widely used and is generally disliked by