blob: f41a22108d111fd259e95bb4ce3016daa577124b [file] [log] [blame]
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>apache_beam.utils.annotations &mdash; Apache Beam documentation</title>
<link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
<link rel="index" title="Index"
href="../../../genindex.html"/>
<link rel="search" title="Search" href="../../../search.html"/>
<link rel="top" title="Apache Beam documentation" href="../../../index.html"/>
<link rel="up" title="Module code" href="../../index.html"/>
<script src="../../../_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="../../../index.html" class="icon icon-home"> Apache Beam
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.coders.html">apache_beam.coders package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.internal.html">apache_beam.internal package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.io.html">apache_beam.io package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.metrics.html">apache_beam.metrics package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.options.html">apache_beam.options package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.portability.html">apache_beam.portability package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.runners.html">apache_beam.runners package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.testing.html">apache_beam.testing package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.tools.html">apache_beam.tools package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.transforms.html">apache_beam.transforms package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.typehints.html">apache_beam.typehints package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.utils.html">apache_beam.utils package</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.error.html">apache_beam.error module</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.pipeline.html">apache_beam.pipeline module</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.pvalue.html">apache_beam.pvalue module</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.version.html">apache_beam.version module</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../../../index.html">Apache Beam</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../../../index.html">Docs</a> &raquo;</li>
<li><a href="../../index.html">Module code</a> &raquo;</li>
<li>apache_beam.utils.annotations</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<h1>Source code for apache_beam.utils.annotations</h1><div class="highlight"><pre>
<span></span><span class="c1">#</span>
<span class="c1"># Licensed to the Apache Software Foundation (ASF) under one or more</span>
<span class="c1"># contributor license agreements. See the NOTICE file distributed with</span>
<span class="c1"># this work for additional information regarding copyright ownership.</span>
<span class="c1"># The ASF licenses this file to You under the Apache License, Version 2.0</span>
<span class="c1"># (the &quot;License&quot;); you may not use this file except in compliance with</span>
<span class="c1"># the License. You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1">#</span>
<span class="sd">&quot;&quot;&quot;Deprecated and experimental annotations.</span>
<span class="sd">Experimental: Signifies that a public API (public class, method or field) is</span>
<span class="sd">subject to incompatible changes, or even removal, in a future release. Note that</span>
<span class="sd">the presence of this annotation implies nothing about the quality or performance</span>
<span class="sd">of the API in question, only the fact that the API or behavior may change in any</span>
<span class="sd">way.</span>
<span class="sd">Deprecated: Signifies that users are discouraged from using a public API</span>
<span class="sd">typically because a better alternative exists, and the current form might be</span>
<span class="sd">removed in a future version.</span>
<span class="sd">Usage:</span>
<span class="sd">For internal use only; no backwards-compatibility guarantees.</span>
<span class="sd">Annotations come in two flavors: deprecated and experimental</span>
<span class="sd">The &#39;deprecated&#39; annotation requires a &#39;since&quot; parameter to specify</span>
<span class="sd">what version deprecated it.</span>
<span class="sd">Both &#39;deprecated&#39; and &#39;experimental&#39; annotations can specify the</span>
<span class="sd">current recommended version to use by means of a &#39;current&#39; parameter.</span>
<span class="sd">The following example illustrates how to annotate coexisting versions of the</span>
<span class="sd">same function &#39;multiply&#39;.::</span>
<span class="sd"> def multiply(arg1, arg2):</span>
<span class="sd"> print(arg1, &#39;*&#39;, arg2, &#39;=&#39;, end=&#39; &#39;)</span>
<span class="sd"> return arg1*arg2</span>
<span class="sd"># This annotation marks &#39;old_multiply&#39; as deprecated since &#39;v.1&#39; and suggests</span>
<span class="sd"># using &#39;multiply&#39; instead.::</span>
<span class="sd"> @deprecated(since=&#39;v.1&#39;, current=&#39;multiply&#39;)</span>
<span class="sd"> def old_multiply(arg1, arg2):</span>
<span class="sd"> result = 0</span>
<span class="sd"> for i in xrange(arg1):</span>
<span class="sd"> result += arg2</span>
<span class="sd"> print(arg1, &#39;*&#39;, arg2, &#39;(the old way)=&#39;, end=&#39; &#39;)</span>
<span class="sd"> return result</span>
<span class="sd"># This annotation marks &#39;exp_multiply&#39; as experimental and suggests</span>
<span class="sd"># using &#39;multiply&#39; instead.::</span>
<span class="sd"> @experimental(since=&#39;v.1&#39;, current=&#39;multiply&#39;)</span>
<span class="sd"> def exp_multiply(arg1, arg2):</span>
<span class="sd"> print(arg1, &#39;*&#39;, arg2, &#39;(the experimental way)=&#39;, end=&#39; &#39;)</span>
<span class="sd"> return (arg1*arg2)*(arg1/arg2)*(arg2/arg1)</span>
<span class="sd"># Set a warning filter to control how often warnings are produced.::</span>
<span class="sd"> warnings.simplefilter(&quot;always&quot;)</span>
<span class="sd"> print(multiply(5, 6))</span>
<span class="sd"> print(old_multiply(5,6))</span>
<span class="sd"> print(exp_multiply(5,6))</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">absolute_import</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">partial</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">wraps</span>
<span class="c1"># Produce only the first occurrence of matching warnings regardless of</span>
<span class="c1"># location per line of execution. Since the number of lines of execution</span>
<span class="c1"># depends on the concrete runner, the number of warnings produced will</span>
<span class="c1"># vary depending on the runner.</span>
<span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s2">&quot;once&quot;</span><span class="p">)</span>
<div class="viewcode-block" id="annotate"><a class="viewcode-back" href="../../../apache_beam.utils.annotations.html#apache_beam.utils.annotations.annotate">[docs]</a><span class="k">def</span> <span class="nf">annotate</span><span class="p">(</span><span class="n">label</span><span class="p">,</span> <span class="n">since</span><span class="p">,</span> <span class="n">current</span><span class="p">,</span> <span class="n">extra_message</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Decorates a function with a deprecated or experimental annotation.</span>
<span class="sd"> Args:</span>
<span class="sd"> label: the kind of annotation (&#39;deprecated&#39; or &#39;experimental&#39;).</span>
<span class="sd"> since: the version that causes the annotation.</span>
<span class="sd"> current: the suggested replacement function.</span>
<span class="sd"> extra_message: an optional additional message.</span>
<span class="sd"> Returns:</span>
<span class="sd"> The decorator for the function.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">_annotate</span><span class="p">(</span><span class="n">fnc</span><span class="p">):</span>
<span class="nd">@wraps</span><span class="p">(</span><span class="n">fnc</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">inner</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="n">label</span> <span class="o">==</span> <span class="s1">&#39;deprecated&#39;</span><span class="p">:</span>
<span class="n">warning_type</span> <span class="o">=</span> <span class="ne">DeprecationWarning</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">warning_type</span> <span class="o">=</span> <span class="ne">FutureWarning</span>
<span class="n">message</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> is </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">fnc</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">label</span><span class="p">)</span>
<span class="k">if</span> <span class="n">label</span> <span class="o">==</span> <span class="s1">&#39;deprecated&#39;</span><span class="p">:</span>
<span class="n">message</span> <span class="o">+=</span> <span class="s1">&#39; since </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">since</span>
<span class="n">message</span> <span class="o">+=</span> <span class="s1">&#39;. Use </span><span class="si">%s</span><span class="s1"> instead.&#39;</span> <span class="o">%</span> <span class="n">current</span> <span class="k">if</span> <span class="n">current</span> <span class="k">else</span> <span class="s1">&#39;.&#39;</span>
<span class="k">if</span> <span class="n">extra_message</span><span class="p">:</span>
<span class="n">message</span> <span class="o">+=</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="n">extra_message</span>
<span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="n">warning_type</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="k">return</span> <span class="n">fnc</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="k">return</span> <span class="n">inner</span>
<span class="k">return</span> <span class="n">_annotate</span></div>
<span class="c1"># Use partial application to customize each annotation.</span>
<span class="c1"># &#39;current&#39; will be optional in both deprecated and experimental</span>
<span class="c1"># while &#39;since&#39; will be mandatory for deprecated.</span>
<span class="n">deprecated</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="n">annotate</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;deprecated&#39;</span><span class="p">,</span>
<span class="n">current</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">extra_message</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<span class="n">experimental</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="n">annotate</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;experimental&#39;</span><span class="p">,</span>
<span class="n">current</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">since</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">extra_message</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
</pre></div>
</div>
<div class="articleComments">
</div>
</div>
<footer>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright .
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'../../../',
VERSION:'',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="../../../_static/jquery.js"></script>
<script type="text/javascript" src="../../../_static/underscore.js"></script>
<script type="text/javascript" src="../../../_static/doctools.js"></script>
<script type="text/javascript" src="../../../_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>