Skip to content

Commit

Permalink
More Python 3/PEP 8/PEP 257 compliance
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Aug 20, 2015
1 parent 5d20a01 commit af1092c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion context_managers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion coroutines.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 12 additions & 12 deletions decorators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions for_-_else.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ like this:
fruits = ['apple', 'banana', 'mango']
for fruit in fruits:
print fruit.capitalize()
print(fruit.capitalize())
# Output: Apple
# Banana
Expand Down Expand Up @@ -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
Expand All @@ -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')
4 changes: 2 additions & 2 deletions ternary_operators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit af1092c

Please sign in to comment.