Skip to content

Commit

Permalink
Merge pull request #25 from StevenMaude/fix-code-formatting
Browse files Browse the repository at this point in the history
Fix code formatting
  • Loading branch information
yasoob committed Aug 18, 2015
2 parents 4473bb3 + 31edea1 commit 482c759
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
10 changes: 5 additions & 5 deletions collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ major one is that unlike lists, **you can not change a value in a
tuple**. In order to access the value in a tuple you use integer indexes
like:

::
.. code:: python
man = ('Ali', 30)
print(man[0])
Expand All @@ -217,7 +217,7 @@ integer indexes for accessing members of a tuple. You can think of
namedtuples like dictionaries but unlike dictionaries they are
immutable.

::
.. code:: python
from collections import namedtuple
Expand All @@ -244,7 +244,7 @@ memory than regular tuples. This makes them faster than dictionaries.
However, do remember that as with tuples, **attributes in namedtuples
are immutable**. It means that this would not work:

::
.. code:: python
from collections import namedtuple
Expand All @@ -260,7 +260,7 @@ You should use named tuples to make your code self-documenting. **They
are backwards compatible with normal tuples**. It means that you can use
integer indexes with namedtuples as well:

::
.. code:: python
from collections import namedtuple
Expand All @@ -272,7 +272,7 @@ integer indexes with namedtuples as well:
Last but not the least, you can convert a namedtuple to a dictionary.
Like this:

::
.. code:: python
from collections import namedtuple
Expand Down
18 changes: 9 additions & 9 deletions context_managers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ the ``with`` statement. Suppose you have two related operations which
you’d like to execute as a pair, with a block of code in between.
Context managers allow you to do specifically that. For example:

::
.. code:: python
with open('some_file', 'w') as opened_file:
opened_file.write('Hola!')
Expand All @@ -16,7 +16,7 @@ The above code opens the file, writes some data to it and then closes
it. If an error occurs while writing the data to the file, it tries to
close it. The above code is equivalent to:

::
.. code:: python
file = open('some_file', 'w')
try:
Expand All @@ -42,7 +42,7 @@ At the very least a context manager has an ``__enter__`` and
``__exit__`` methods defined. Let's make our own file opening Context
Manager and learn the basics.

::
.. code:: python
class File(object):
def __init__(self, file_name, method):
Expand All @@ -55,7 +55,7 @@ Manager and learn the basics.
Just by defining ``__enter__`` and ``__exit__`` methods we can use it in
a ``with`` statement. Let's try:

::
.. code:: python
with File('demo.txt', 'w') as opened_file:
opened_file.write('Hola!')
Expand Down Expand Up @@ -87,7 +87,7 @@ What if our file object raises an exception? We might be trying to
access a method on the file object which it does not supports. For
instance:

::
.. code:: python
with File('demo.txt', 'w') as opened_file:
opened_file.undefined_function('Hola!')
Expand All @@ -107,15 +107,15 @@ In our case the ``__exit__`` method returns ``None`` (when no return
statement is encountered then the method returns ``None``). Therefore,
``with`` statement raises the exception.

::
.. code:: python
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
AttributeError: 'file' object has no attribute 'undefined_function'
Let's try handling the exception in the ``__exit__`` method:

::
.. code:: python
class File(object):
def __init__(self, file_name, method):
Expand Down Expand Up @@ -146,7 +146,7 @@ Python has a contextlib module for this very purpose. Instead of a
class, we can implement a Context Manager using a generator function.
Let's see a basic, useless example:

::
.. code:: python
from contextlib import contextmanager
Expand Down Expand Up @@ -177,7 +177,7 @@ Let's disect this method a little.
So now that we know all this, we can use the newly generated Context
Manager like this:

::
.. code:: python
with open_file('some_file') as f:
f.write('hola!')
Expand Down
16 changes: 8 additions & 8 deletions decorators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Everything in python is an object:

First of all let's understand functions in python:

::
.. code:: python
def hi(name="yasoob"):
return "hi " + name
Expand Down Expand Up @@ -48,7 +48,7 @@ So those are the basics when it comes to functions. Lets take your
knowledge one step further. In Python we can define functions inside
other functions:

::
.. code:: python
def hi(name="yasoob"):
print "now you are inside the hi() function"
Expand Down Expand Up @@ -86,7 +86,7 @@ Returning functions from within functions:
It is not necessary to execute a function within another function, we
can return it as an output as well:

::
.. code:: python
def hi(name="yasoob"):
def greet():
Expand Down Expand Up @@ -125,7 +125,7 @@ function will be returned. We can also do print ``hi()()`` which outputs
Giving a function as an argument to another function:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

::
.. code:: python
def hi():
return "hi yasoob!"
Expand All @@ -147,7 +147,7 @@ Writing your first decorator:
In the last example we actually made a decorator! Lets modify the
previous decorator and make a little bit more usable program:

::
.. code:: python
def a_new_decorator(a_func):
Expand Down Expand Up @@ -181,7 +181,7 @@ wondering that we did not use the @ anywhere in our code? That is just a
short way of making up a decorated function. Here is how we could have
run the previous code sample using @.

::
.. code:: python
@a_new_decorator
def a_function_requiring_decoration():
Expand All @@ -200,7 +200,7 @@ run the previous code sample using @.
I hope you now have a basic understanding of how decorators work in
Python. Now there is one problem with our code. If we run:

::
.. code:: python
print(a_function_requiring_decoration.__name__)
# Output: wrapTheFunction
Expand All @@ -212,7 +212,7 @@ Luckily Python provides us a simple function to solve this problem and
that is ``functools.wraps``. Let's modify our previous example to use
``functools.wraps``:

::
.. code:: python
from functools import wraps
Expand Down
4 changes: 2 additions & 2 deletions for_-_else.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ one.
Let's first start of by what we know. We know that we can use for loops
like this:

::
.. code:: python
fruits = ['apple', 'banana', 'mango']
for fruit in fruits:
Expand Down Expand Up @@ -67,7 +67,7 @@ It outputs the prime numbers between 2 to 10. Now for the fun part. We
can add an additional ``else`` block which catches the numbers which are
not prime and tells us so:

::
.. code:: python
for n in range(2, 10):
for x in range(2, n):
Expand Down

0 comments on commit 482c759

Please sign in to comment.