|
|
@ -1096,6 +1096,47 @@ Julio |
|
|
|
>>> # retrieve |
|
|
|
>>> # retrieve |
|
|
|
</code></pre> |
|
|
|
</code></pre> |
|
|
|
</section> |
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
|
|
|
<h5>Decorators mais comuns:</h5> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<ul> |
|
|
|
|
|
|
|
<li><code>@property</code></li> |
|
|
|
|
|
|
|
<li><code>@staticmethod</code></li> |
|
|
|
|
|
|
|
</ul> |
|
|
|
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
|
|
|
<h5><code>@property</code></h5> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<pre><code class='hljs'> |
|
|
|
|
|
|
|
>>> class CheckConn(object): |
|
|
|
|
|
|
|
>>> def __init__(self, func): |
|
|
|
|
|
|
|
>>> self._func = func |
|
|
|
|
|
|
|
>>> |
|
|
|
|
|
|
|
>>> @property |
|
|
|
|
|
|
|
>>> def func(self): |
|
|
|
|
|
|
|
>>> return self._func |
|
|
|
|
|
|
|
>>> |
|
|
|
|
|
|
|
>>> @func.setter |
|
|
|
|
|
|
|
>>> def func(self, value): |
|
|
|
|
|
|
|
>>> self._func = func |
|
|
|
|
|
|
|
</code></pre> |
|
|
|
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
|
|
|
<h5><code>@staticmethod</code></h5> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<pre><code class='hljs'> |
|
|
|
|
|
|
|
>>> class CheckConn(object): |
|
|
|
|
|
|
|
>>> def __init__(self, func): |
|
|
|
|
|
|
|
>>> self._func = func |
|
|
|
|
|
|
|
>>> |
|
|
|
|
|
|
|
>>> @staticmethod |
|
|
|
|
|
|
|
>>> def from_text(self, text): |
|
|
|
|
|
|
|
>>> return CheckConn(getattr(self, text)) |
|
|
|
|
|
|
|
</code></pre> |
|
|
|
|
|
|
|
</section> |
|
|
|
</section> |
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
<section> |
|
|
@ -1196,6 +1237,57 @@ Julio |
|
|
|
</section> |
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
<section> |
|
|
|
|
|
|
|
<section> |
|
|
|
|
|
|
|
<h3>Context Managers</h3> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<p> |
|
|
|
|
|
|
|
<i>Context Managers</i> são usados como "marcadores" |
|
|
|
|
|
|
|
para entrada e saída de pontos específicos. |
|
|
|
|
|
|
|
</p> |
|
|
|
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
|
|
|
<h3>Context Managers</h3> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
|
|
|
>>> class Connection(object): |
|
|
|
|
|
|
|
>>> def __init__(self): |
|
|
|
|
|
|
|
>>> self._conn = None |
|
|
|
|
|
|
|
>>> |
|
|
|
|
|
|
|
>>> def __enter__(self): |
|
|
|
|
|
|
|
>>> self._conn = self._make_connection() |
|
|
|
|
|
|
|
>>> return self._conn |
|
|
|
|
|
|
|
>>> |
|
|
|
|
|
|
|
>>> def __exit__(self, exc_type, exc_value, traceback): |
|
|
|
|
|
|
|
>>> self._conn.close() |
|
|
|
|
|
|
|
>>> if exc_type: # then exc_value and traceback |
|
|
|
|
|
|
|
>>> print "Exception!", exc_type, exc_value |
|
|
|
|
|
|
|
>>> print traceback |
|
|
|
|
|
|
|
</code></pre> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
|
|
|
>>> with Connection() as connection: |
|
|
|
|
|
|
|
>>> connection.request('Value') |
|
|
|
|
|
|
|
</code></pre> |
|
|
|
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
|
|
|
<h3>Context Managers vs Exceptions</h3> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
|
|
|
>>> try: |
|
|
|
|
|
|
|
>>> conn = self._make_connection() |
|
|
|
|
|
|
|
>>> conn.request('value') |
|
|
|
|
|
|
|
>>> finally: |
|
|
|
|
|
|
|
>>> conn.close() |
|
|
|
|
|
|
|
>>> except Exception as exc: # Bare exceptions are BAAAADDD! |
|
|
|
|
|
|
|
>>> print 'Exception!', exc |
|
|
|
|
|
|
|
</code></pre> |
|
|
|
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
<section> |
|
|
|
<section> |
|
|
|
<h3>Docstrings</h3> |
|
|
|
<h3>Docstrings</h3> |
|
|
|
|
|
|
|
|
|
|
|