blob: 5c9b31cee2da8a9050d8f256f6b5636319fbd26c [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.transforms.resources &mdash; Apache Beam 2.38.0 documentation</title>
<script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
<script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></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/language_data.js"></script>
<script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="../../../_static/js/theme.js"></script>
<link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
</head>
<body class="wy-body-for-nav">
<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 class="version">
2.38.0
</div>
<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.dataframe.html">apache_beam.dataframe 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.ml.html">apache_beam.ml 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.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>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" 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.transforms.resources</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.transforms.resources</h1><div class="highlight"><pre>
<span></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;A module for defining resource requirements for execution of transforms.</span>
<span class="sd">Pipeline authors can use resource hints to provide additional information to</span>
<span class="sd">runners about the desired aspects of the execution environment.</span>
<span class="sd">Resource hints can be specified on a transform level for parts of the pipeline,</span>
<span class="sd">or globally via --resource_hint pipeline option.</span>
<span class="sd">See also: PTransforms.with_resource_hints().</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">TYPE_CHECKING</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Any</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Dict</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Optional</span>
<span class="kn">from</span> <span class="nn">apache_beam.options.pipeline_options</span> <span class="kn">import</span> <span class="n">StandardOptions</span>
<span class="kn">from</span> <span class="nn">apache_beam.portability.common_urns</span> <span class="kn">import</span> <span class="n">resource_hints</span>
<span class="k">if</span> <span class="n">TYPE_CHECKING</span><span class="p">:</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Mapping</span>
<span class="kn">from</span> <span class="nn">apache_beam.options.pipeline_options</span> <span class="kn">import</span> <span class="n">PipelineOptions</span>
<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
<span class="s1">&#39;ResourceHint&#39;</span><span class="p">,</span>
<span class="s1">&#39;AcceleratorHint&#39;</span><span class="p">,</span>
<span class="s1">&#39;MinRamHint&#39;</span><span class="p">,</span>
<span class="s1">&#39;merge_resource_hints&#39;</span><span class="p">,</span>
<span class="s1">&#39;parse_resource_hints&#39;</span><span class="p">,</span>
<span class="s1">&#39;resource_hints_from_options&#39;</span><span class="p">,</span>
<span class="p">]</span>
<div class="viewcode-block" id="ResourceHint"><a class="viewcode-back" href="../../../apache_beam.transforms.resources.html#apache_beam.transforms.resources.ResourceHint">[docs]</a><span class="k">class</span> <span class="nc">ResourceHint</span><span class="p">:</span>
<span class="sd">&quot;&quot;&quot;A superclass to define resource hints.&quot;&quot;&quot;</span>
<span class="c1"># A unique URN, one per Resource Hint class.</span>
<span class="n">urn</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># type: Optional[str]</span>
<span class="n">_urn_to_known_hints</span> <span class="o">=</span> <span class="p">{}</span> <span class="c1"># type: Dict[str, type]</span>
<span class="n">_name_to_known_hints</span> <span class="o">=</span> <span class="p">{}</span> <span class="c1"># type: Dict[str, type]</span>
<div class="viewcode-block" id="ResourceHint.parse"><a class="viewcode-back" href="../../../apache_beam.transforms.resources.html#apache_beam.transforms.resources.ResourceHint.parse">[docs]</a> <span class="nd">@classmethod</span>
<span class="k">def</span> <span class="nf">parse</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span> <span class="c1"># type: (str) -&gt; Dict[str, bytes]</span>
<span class="sd">&quot;&quot;&quot;Describes how to parse the hint.</span>
<span class="sd"> Override to specify a custom parsing logic.&quot;&quot;&quot;</span>
<span class="k">assert</span> <span class="bp">cls</span><span class="o">.</span><span class="n">urn</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
<span class="c1"># Override this method to have a custom parsing logic.</span>
<span class="k">return</span> <span class="p">{</span><span class="bp">cls</span><span class="o">.</span><span class="n">urn</span><span class="p">:</span> <span class="n">ResourceHint</span><span class="o">.</span><span class="n">_parse_str</span><span class="p">(</span><span class="n">value</span><span class="p">)}</span></div>
<div class="viewcode-block" id="ResourceHint.get_merged_value"><a class="viewcode-back" href="../../../apache_beam.transforms.resources.html#apache_beam.transforms.resources.ResourceHint.get_merged_value">[docs]</a> <span class="nd">@classmethod</span>
<span class="k">def</span> <span class="nf">get_merged_value</span><span class="p">(</span>
<span class="bp">cls</span><span class="p">,</span> <span class="n">outer_value</span><span class="p">,</span> <span class="n">inner_value</span><span class="p">):</span> <span class="c1"># type: (bytes, bytes) -&gt; bytes</span>
<span class="sd">&quot;&quot;&quot;Reconciles values of a hint when the hint specified on a transform is</span>
<span class="sd"> also defined in an outer context, for example on a composite transform, or</span>
<span class="sd"> specified in the transform&#39;s execution environment.</span>
<span class="sd"> Override to specify a custom merging logic.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c1"># Defaults to the inner value as it is the most specific one.</span>
<span class="k">return</span> <span class="n">inner_value</span></div>
<div class="viewcode-block" id="ResourceHint.get_by_urn"><a class="viewcode-back" href="../../../apache_beam.transforms.resources.html#apache_beam.transforms.resources.ResourceHint.get_by_urn">[docs]</a> <span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">get_by_urn</span><span class="p">(</span><span class="n">urn</span><span class="p">):</span>
<span class="k">return</span> <span class="n">ResourceHint</span><span class="o">.</span><span class="n">_urn_to_known_hints</span><span class="p">[</span><span class="n">urn</span><span class="p">]</span></div>
<div class="viewcode-block" id="ResourceHint.get_by_name"><a class="viewcode-back" href="../../../apache_beam.transforms.resources.html#apache_beam.transforms.resources.ResourceHint.get_by_name">[docs]</a> <span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">get_by_name</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="k">return</span> <span class="n">ResourceHint</span><span class="o">.</span><span class="n">_name_to_known_hints</span><span class="p">[</span><span class="n">name</span><span class="p">]</span></div>
<div class="viewcode-block" id="ResourceHint.is_registered"><a class="viewcode-back" href="../../../apache_beam.transforms.resources.html#apache_beam.transforms.resources.ResourceHint.is_registered">[docs]</a> <span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">is_registered</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="k">return</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">ResourceHint</span><span class="o">.</span><span class="n">_name_to_known_hints</span></div>
<div class="viewcode-block" id="ResourceHint.register_resource_hint"><a class="viewcode-back" href="../../../apache_beam.transforms.resources.html#apache_beam.transforms.resources.ResourceHint.register_resource_hint">[docs]</a> <span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">register_resource_hint</span><span class="p">(</span>
<span class="n">hint_name</span><span class="p">,</span> <span class="n">hint_class</span><span class="p">):</span> <span class="c1"># type: (str, type) -&gt; None</span>
<span class="k">assert</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">hint_class</span><span class="p">,</span> <span class="n">ResourceHint</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">hint_class</span><span class="o">.</span><span class="n">urn</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
<span class="n">ResourceHint</span><span class="o">.</span><span class="n">_name_to_known_hints</span><span class="p">[</span><span class="n">hint_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">hint_class</span>
<span class="n">ResourceHint</span><span class="o">.</span><span class="n">_urn_to_known_hints</span><span class="p">[</span><span class="n">hint_class</span><span class="o">.</span><span class="n">urn</span><span class="p">]</span> <span class="o">=</span> <span class="n">hint_class</span></div>
<span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">_parse_str</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Input must be a string.&quot;</span><span class="p">)</span>
<span class="k">return</span> <span class="n">value</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">)</span>
<span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">_parse_int</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
<span class="n">value</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Input must be an integer.&quot;</span><span class="p">)</span>
<span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">)</span>
<span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">_parse_storage_size_str</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Parses a human-friendly storage size string into a number of bytes.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
<span class="k">return</span> <span class="n">ResourceHint</span><span class="o">.</span><span class="n">_parse_int</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Input must be a string or integer.&quot;</span><span class="p">)</span>
<span class="n">value</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot; &quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">)</span>
<span class="n">units</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">&#39;PiB&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="o">**</span><span class="mi">50</span><span class="p">,</span>
<span class="s1">&#39;TiB&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="o">**</span><span class="mi">40</span><span class="p">,</span>
<span class="s1">&#39;GiB&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="o">**</span><span class="mi">30</span><span class="p">,</span>
<span class="s1">&#39;MiB&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="o">**</span><span class="mi">20</span><span class="p">,</span>
<span class="s1">&#39;KiB&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="o">**</span><span class="mi">10</span><span class="p">,</span>
<span class="s1">&#39;PB&#39;</span><span class="p">:</span> <span class="mi">10</span><span class="o">**</span><span class="mi">15</span><span class="p">,</span>
<span class="s1">&#39;TB&#39;</span><span class="p">:</span> <span class="mi">10</span><span class="o">**</span><span class="mi">12</span><span class="p">,</span>
<span class="s1">&#39;GB&#39;</span><span class="p">:</span> <span class="mi">10</span><span class="o">**</span><span class="mi">9</span><span class="p">,</span>
<span class="s1">&#39;MB&#39;</span><span class="p">:</span> <span class="mi">10</span><span class="o">**</span><span class="mi">6</span><span class="p">,</span>
<span class="s1">&#39;KB&#39;</span><span class="p">:</span> <span class="mi">10</span><span class="o">**</span><span class="mi">3</span><span class="p">,</span>
<span class="s1">&#39;B&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
<span class="p">}</span>
<span class="n">match</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;.*?(\D+)$&#39;</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">match</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unrecognized value pattern.&quot;</span><span class="p">)</span>
<span class="n">suffix</span> <span class="o">=</span> <span class="n">match</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="k">if</span> <span class="n">suffix</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">units</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unrecognized unit.&quot;</span><span class="p">)</span>
<span class="n">multiplier</span> <span class="o">=</span> <span class="n">units</span><span class="p">[</span><span class="n">suffix</span><span class="p">]</span>
<span class="n">value</span> <span class="o">=</span> <span class="n">value</span><span class="p">[:</span><span class="o">-</span><span class="nb">len</span><span class="p">(</span><span class="n">suffix</span><span class="p">)]</span>
<span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="o">*</span> <span class="n">multiplier</span><span class="p">))</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">)</span>
<span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">_use_max</span><span class="p">(</span><span class="n">v1</span><span class="p">,</span> <span class="n">v2</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">v1</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">v2</span><span class="p">)))</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">)</span></div>
<div class="viewcode-block" id="AcceleratorHint"><a class="viewcode-back" href="../../../apache_beam.transforms.resources.html#apache_beam.transforms.resources.AcceleratorHint">[docs]</a><span class="k">class</span> <span class="nc">AcceleratorHint</span><span class="p">(</span><span class="n">ResourceHint</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Describes desired hardware accelerators in execution environment.&quot;&quot;&quot;</span>
<span class="n">urn</span> <span class="o">=</span> <span class="n">resource_hints</span><span class="o">.</span><span class="n">ACCELERATOR</span><span class="o">.</span><span class="n">urn</span></div>
<span class="n">ResourceHint</span><span class="o">.</span><span class="n">register_resource_hint</span><span class="p">(</span><span class="s1">&#39;accelerator&#39;</span><span class="p">,</span> <span class="n">AcceleratorHint</span><span class="p">)</span>
<div class="viewcode-block" id="MinRamHint"><a class="viewcode-back" href="../../../apache_beam.transforms.resources.html#apache_beam.transforms.resources.MinRamHint">[docs]</a><span class="k">class</span> <span class="nc">MinRamHint</span><span class="p">(</span><span class="n">ResourceHint</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Describes min RAM requirements for transform&#39;s execution environment.&quot;&quot;&quot;</span>
<span class="n">urn</span> <span class="o">=</span> <span class="n">resource_hints</span><span class="o">.</span><span class="n">MIN_RAM_BYTES</span><span class="o">.</span><span class="n">urn</span>
<div class="viewcode-block" id="MinRamHint.parse"><a class="viewcode-back" href="../../../apache_beam.transforms.resources.html#apache_beam.transforms.resources.MinRamHint.parse">[docs]</a> <span class="nd">@classmethod</span>
<span class="k">def</span> <span class="nf">parse</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span> <span class="c1"># type: (str) -&gt; Dict[str, bytes]</span>
<span class="k">return</span> <span class="p">{</span><span class="bp">cls</span><span class="o">.</span><span class="n">urn</span><span class="p">:</span> <span class="n">ResourceHint</span><span class="o">.</span><span class="n">_parse_storage_size_str</span><span class="p">(</span><span class="n">value</span><span class="p">)}</span></div>
<div class="viewcode-block" id="MinRamHint.get_merged_value"><a class="viewcode-back" href="../../../apache_beam.transforms.resources.html#apache_beam.transforms.resources.MinRamHint.get_merged_value">[docs]</a> <span class="nd">@classmethod</span>
<span class="k">def</span> <span class="nf">get_merged_value</span><span class="p">(</span>
<span class="bp">cls</span><span class="p">,</span> <span class="n">outer_value</span><span class="p">,</span> <span class="n">inner_value</span><span class="p">):</span> <span class="c1"># type: (bytes, bytes) -&gt; bytes</span>
<span class="k">return</span> <span class="n">ResourceHint</span><span class="o">.</span><span class="n">_use_max</span><span class="p">(</span><span class="n">outer_value</span><span class="p">,</span> <span class="n">inner_value</span><span class="p">)</span></div></div>
<span class="n">ResourceHint</span><span class="o">.</span><span class="n">register_resource_hint</span><span class="p">(</span><span class="s1">&#39;min_ram&#39;</span><span class="p">,</span> <span class="n">MinRamHint</span><span class="p">)</span>
<span class="c1"># Alias for interoperability with SDKs preferring camelCase.</span>
<span class="n">ResourceHint</span><span class="o">.</span><span class="n">register_resource_hint</span><span class="p">(</span><span class="s1">&#39;minRam&#39;</span><span class="p">,</span> <span class="n">MinRamHint</span><span class="p">)</span>
<div class="viewcode-block" id="parse_resource_hints"><a class="viewcode-back" href="../../../apache_beam.transforms.resources.html#apache_beam.transforms.resources.parse_resource_hints">[docs]</a><span class="k">def</span> <span class="nf">parse_resource_hints</span><span class="p">(</span><span class="n">hints</span><span class="p">):</span> <span class="c1"># type: (Dict[Any, Any]) -&gt; Dict[str, bytes]</span>
<span class="n">parsed_hints</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">hint</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">hints</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">hint_cls</span> <span class="o">=</span> <span class="n">ResourceHint</span><span class="o">.</span><span class="n">get_by_name</span><span class="p">(</span><span class="n">hint</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">parsed_hints</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">hint_cls</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">value</span><span class="p">))</span>
<span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Resource hint </span><span class="si">{</span><span class="n">hint</span><span class="si">}</span><span class="s2"> has invalid value </span><span class="si">{</span><span class="n">value</span><span class="si">}</span><span class="s2">.&quot;</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Unknown resource hint: </span><span class="si">{</span><span class="n">hint</span><span class="si">}</span><span class="s2">.&quot;</span><span class="p">)</span>
<span class="k">return</span> <span class="n">parsed_hints</span></div>
<div class="viewcode-block" id="resource_hints_from_options"><a class="viewcode-back" href="../../../apache_beam.transforms.resources.html#apache_beam.transforms.resources.resource_hints_from_options">[docs]</a><span class="k">def</span> <span class="nf">resource_hints_from_options</span><span class="p">(</span><span class="n">options</span><span class="p">):</span>
<span class="c1"># type: (Optional[PipelineOptions]) -&gt; Dict[str, bytes]</span>
<span class="k">if</span> <span class="n">options</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="p">{}</span>
<span class="n">hints</span> <span class="o">=</span> <span class="p">{}</span>
<span class="n">option_specified_hints</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">view_as</span><span class="p">(</span><span class="n">StandardOptions</span><span class="p">)</span><span class="o">.</span><span class="n">resource_hints</span>
<span class="k">for</span> <span class="n">hint</span> <span class="ow">in</span> <span class="n">option_specified_hints</span><span class="p">:</span>
<span class="k">if</span> <span class="s1">&#39;=&#39;</span> <span class="ow">in</span> <span class="n">hint</span><span class="p">:</span>
<span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="o">=</span> <span class="n">hint</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;=&#39;</span><span class="p">,</span> <span class="n">maxsplit</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">hints</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">hints</span><span class="p">[</span><span class="n">hint</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">return</span> <span class="n">parse_resource_hints</span><span class="p">(</span><span class="n">hints</span><span class="p">)</span></div>
<div class="viewcode-block" id="merge_resource_hints"><a class="viewcode-back" href="../../../apache_beam.transforms.resources.html#apache_beam.transforms.resources.merge_resource_hints">[docs]</a><span class="k">def</span> <span class="nf">merge_resource_hints</span><span class="p">(</span>
<span class="n">outer_hints</span><span class="p">,</span> <span class="n">inner_hints</span>
<span class="p">):</span> <span class="c1"># type: (Mapping[str, bytes], Mapping[str, bytes]) -&gt; Dict[str, bytes]</span>
<span class="n">merged_hints</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">inner_hints</span><span class="p">)</span>
<span class="k">for</span> <span class="n">urn</span><span class="p">,</span> <span class="n">outer_value</span> <span class="ow">in</span> <span class="n">outer_hints</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="k">if</span> <span class="n">urn</span> <span class="ow">in</span> <span class="n">inner_hints</span><span class="p">:</span>
<span class="n">merged_value</span> <span class="o">=</span> <span class="n">ResourceHint</span><span class="o">.</span><span class="n">get_by_urn</span><span class="p">(</span><span class="n">urn</span><span class="p">)</span><span class="o">.</span><span class="n">get_merged_value</span><span class="p">(</span>
<span class="n">outer_value</span><span class="o">=</span><span class="n">outer_value</span><span class="p">,</span> <span class="n">inner_value</span><span class="o">=</span><span class="n">inner_hints</span><span class="p">[</span><span class="n">urn</span><span class="p">])</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">merged_value</span> <span class="o">=</span> <span class="n">outer_value</span>
<span class="n">merged_hints</span><span class="p">[</span><span class="n">urn</span><span class="p">]</span> <span class="o">=</span> <span class="n">merged_value</span>
<span class="k">return</span> <span class="n">merged_hints</span></div>
</pre></div>
</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/rtfd/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">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>