Skip to content

Commit

Permalink
fixed doc instructions for decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
An4ik committed Nov 10, 2018
1 parent 1e3105c commit 5b6cca4
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 27 deletions.
8 changes: 6 additions & 2 deletions decorator/answers/stringify.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def inner(*args):
return inner


@stringify
def add(a, b):
"""
Decorated function which returns the sum of two numbers.
Expand All @@ -27,10 +26,15 @@ def add(a, b):
return a + b


@stringify
add = stringify(add)


def multiply(a, b):
"""
Decorated function which returns the multiplication of two numbers.
"""

return a * b


multiply = stringify(multiply)
16 changes: 12 additions & 4 deletions decorator/instruction.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
###Decorators

1. Simple decorator - stringify
2. Decorator with arguments - registration
#####Part-1. Simple decorator - stringify.

#### Stringify
**Problem**: *add, multiply* functions return int, but we want it to be str.

**Solution**: Implement *stringify* function which is changes return type of the given function to str type and wrap (decorate) *add, multiply* methods.

**To Do:**

1. Look at the file **stringify.py**.

Expand All @@ -21,8 +24,13 @@

6. Commit and push your changes

#####Part-2. Decorator with arguments - registration

**Problem**: We want to register/unregister subject. Know subject's status (is active or not). Print all active subjects.

**Solution**: Implement *register* function which set attribute *is_active* equals to the *is_active* param and add it to the *registered* set if is_active otherwise discard it from registered.

#### Registration
**To Do:**

1. Look at the file **registration.py**.

Expand Down
4 changes: 2 additions & 2 deletions decorator/stringify.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def inner(*args):


def add(a, b):
"""Decorated function which returns the sum of two numbers."""
"""Returns the sum of two numbers."""


def multiply(a, b):
"""Decorated function which returns the multiplication of two numbers."""
"""Returns the multiplication of two numbers."""
Binary file modified docs/build/doctrees/decorators.doctree
Binary file not shown.
Binary file modified docs/build/doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/build/doctrees/magic_functions.doctree
Binary file not shown.
22 changes: 18 additions & 4 deletions docs/build/html/_sources/decorators.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,15 @@ inner function and return the new function.



Stringify
-------------

Task-1. Simple decorator - stringify.
---------------------------------------

**Problem**: *add, multiply* functions return int type, but we want it to be str.

**Solution**: Implement *stringify* function which is changes return type of the given function to str type and wrap (decorate) *add, multiply* methods.

**To Do:**

1. Look at the file `stringify.py <https://github.com/An4ik/Python-TDD/blob/master/decorator/stringify.py/>`_

Expand Down Expand Up @@ -212,8 +219,15 @@ Obviously we can reset them within func_wrapper but Python provides a much nicer
print get_text.__module__ # __main__


Registration
-------------
Task-2. Decorator with arguments - Registration
------------------------------------------------------------

**Problem**: We want to register/unregister subject. Know subject's status (is active or not). Print all active subjects.

**Solution**: Implement *register* function which set attribute *is_active* equals to the *is_active* param and add it to the *registered* set if is_active otherwise discard it from registered.

**To Do:**


1. Look at the file `registration.py <https://github.com/An4ik/Python-TDD/blob/master/decorator/registration.py/>`_

Expand Down
1 change: 1 addition & 0 deletions docs/build/html/_sources/magic_functions.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Magic functions
**Magic function** is a function that you deal with, especially, when you are working with classes and objects. These functions are similar to ordinary functions, but the main difference is that they don't have to be called directly as other created methods.
Magic function's name starts and ends with double underscores (*__*). For example, __init__(), __str__(), etc.
These methods allow us to emulate built-in types or implement operator overloading. For example, when you want to add two objects of the same type, you can just implement magic function __add__() and use binary operator '+' to add them, instead of creating special method with arguments and call it.

**ToDo:** Implementing simple functions.

**Learn:** Magic functions.
Expand Down
18 changes: 12 additions & 6 deletions docs/build/html/decorators.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@
<li class="toctree-l3"><a class="reference internal" href="#inner-functions-have-access-to-the-enclosing-scope">Inner functions have access to the enclosing scope</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#stringify">Stringify</a></li>
<li class="toctree-l2"><a class="reference internal" href="#task-1-simple-decorator-stringify">Task-1. Simple decorator - stringify.</a></li>
<li class="toctree-l2"><a class="reference internal" href="#python-s-decorator-syntax">Python’s Decorator Syntax</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#example">Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#composition-of-decorators">Composition of Decorators</a></li>
<li class="toctree-l3"><a class="reference internal" href="#debugging-decorated-functions">Debugging decorated functions</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#registration">Registration</a></li>
<li class="toctree-l2"><a class="reference internal" href="#task-2-decorator-with-arguments-registration">Task-2. Decorator with arguments - Registration</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="context_manager.html">Context manager</a></li>
Expand Down Expand Up @@ -247,8 +247,11 @@ <h3>Inner functions have access to the enclosing scope<a class="headerlink" href
</div>
</div>
</div>
<div class="section" id="stringify">
<h2>Stringify<a class="headerlink" href="#stringify" title="Permalink to this headline"></a></h2>
<div class="section" id="task-1-simple-decorator-stringify">
<h2>Task-1. Simple decorator - stringify.<a class="headerlink" href="#task-1-simple-decorator-stringify" title="Permalink to this headline"></a></h2>
<p><strong>Problem</strong>: <em>add, multiply</em> functions return int type, but we want it to be str.</p>
<p><strong>Solution</strong>: Implement <em>stringify</em> function which is changes return type of the given function to str type and wrap (decorate) <em>add, multiply</em> methods.</p>
<p><strong>To Do:</strong></p>
<ol class="arabic">
<li><p class="first">Look at the file <a class="reference external" href="https://github.com/An4ik/Python-TDD/blob/master/decorator/stringify.py/">stringify.py</a></p>
</li>
Expand Down Expand Up @@ -357,8 +360,11 @@ <h3>Debugging decorated functions<a class="headerlink" href="#debugging-decorate
</div>
</div>
</div>
<div class="section" id="registration">
<h2>Registration<a class="headerlink" href="#registration" title="Permalink to this headline"></a></h2>
<div class="section" id="task-2-decorator-with-arguments-registration">
<h2>Task-2. Decorator with arguments - Registration<a class="headerlink" href="#task-2-decorator-with-arguments-registration" title="Permalink to this headline"></a></h2>
<p><strong>Problem</strong>: We want to register/unregister subject. Know subject’s status (is active or not). Print all active subjects.</p>
<p><strong>Solution</strong>: Implement <em>register</em> function which set attribute <em>is_active</em> equals to the <em>is_active</em> param and add it to the <em>registered</em> set if is_active otherwise discard it from registered.</p>
<p><strong>To Do:</strong></p>
<ol class="arabic">
<li><p class="first">Look at the file <a class="reference external" href="https://github.com/An4ik/Python-TDD/blob/master/decorator/registration.py/">registration.py</a></p>
</li>
Expand Down
4 changes: 2 additions & 2 deletions docs/build/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ <h1>Modulus<a class="headerlink" href="#modulus" title="Permalink to this headli
<li class="toctree-l1"><a class="reference internal" href="fibonacci_numbers.html">Fibonacci numbers</a></li>
<li class="toctree-l1"><a class="reference internal" href="decorators.html">Decorators</a><ul>
<li class="toctree-l2"><a class="reference internal" href="decorators.html#what-you-need-to-know-about-functions">What you need to know about functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="decorators.html#stringify">Stringify</a></li>
<li class="toctree-l2"><a class="reference internal" href="decorators.html#task-1-simple-decorator-stringify">Task-1. Simple decorator - stringify.</a></li>
<li class="toctree-l2"><a class="reference internal" href="decorators.html#python-s-decorator-syntax">Python’s Decorator Syntax</a></li>
<li class="toctree-l2"><a class="reference internal" href="decorators.html#registration">Registration</a></li>
<li class="toctree-l2"><a class="reference internal" href="decorators.html#task-2-decorator-with-arguments-registration">Task-2. Decorator with arguments - Registration</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="context_manager.html">Context manager</a><ul>
Expand Down
4 changes: 2 additions & 2 deletions docs/build/html/magic_functions.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@
<h1>Magic functions<a class="headerlink" href="#magic-functions" title="Permalink to this headline"></a></h1>
<p><strong>Magic function</strong> is a function that you deal with, especially, when you are working with classes and objects. These functions are similar to ordinary functions, but the main difference is that they don’t have to be called directly as other created methods.
Magic function’s name starts and ends with double underscores (<em>__</em>). For example, __init__(), __str__(), etc.
These methods allow us to emulate built-in types or implement operator overloading. For example, when you want to add two objects of the same type, you can just implement magic function __add__() and use binary operator ‘+’ to add them, instead of creating special method with arguments and call it.
<strong>ToDo:</strong> Implementing simple functions.</p>
These methods allow us to emulate built-in types or implement operator overloading. For example, when you want to add two objects of the same type, you can just implement magic function __add__() and use binary operator ‘+’ to add them, instead of creating special method with arguments and call it.</p>
<p><strong>ToDo:</strong> Implementing simple functions.</p>
<p><strong>Learn:</strong> Magic functions.</p>
<p><strong>Working directory:</strong> <a class="reference external" href="https://github.com/An4ik/Python-TDD/tree/master/magic_functions">**magic_functions**</a></p>
<div class="section" id="number">
Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/searchindex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5b6cca4

Please sign in to comment.