<div class="section" id="indentation">
<span id="index-0"></span><span id="id1"></span>
<p>The text examples allow indentation. In python indentation
affects the logical meaning of the program.</p>
<p>However we can use python <strong>with</strong> statement to provide the indentation.</p>
<p>Here is the previous example using the <strong>with</strong> statement:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c">#!/usr/bin/env python2.7</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">ecflow</span>
<span class="kkn">print<>import</span> <span class="s">"Creating suite definition"<nn">sys</span>
<span class="n">version</span>
<span class="ko">with<>=</span> <span class="n">ecflow<>sys</span><span class="o">.</span><span class="n">Defs<>version_info</span><span class="p">();</span>
<span class="k">as<>if</span> <span class="n">defs<>version</span><span class="p">:[</span>
<spanspan><span class="mi">1</span><span class="kp">with<>]</span> <span class="n">defs</span><span class="o">.<</span><spanspan> <span class="nmi">add_suite</span><span>7</span> <span class="p">(:</span><spanspan>
<span class="s"k">print</span> <span class="s">"testThis example requires python version 2.7, but found : "</span><spanspan> <span class="po">)+</span> <span class="knb">as<>str</span> <spanspan><span class="p">(</span><span class="n">suite<>version</span><span class="p">:)</span>
<span class="n">suite<<span class="nb">exit</span><span class="op">.(</span><span class="nmi">add_variable<>0</span><span class="p">()</span><spanspan>
<span class="k">print</span> <span class="s">"ECF_INCLUDECreating suite definition"</span><spanspan>
<span class="pk">,<>with</span> <span class="n">os<>ecflow</span><span class="o">.</span><span class="n">getenv<>Defs</span><span class="p">(</span><span class="s">"HOME"</span><span class="p">)</span> <span class="ok">+<>as</span> <span class="s">"/course"</n">defs</span><span class="p">):</span>
<span class="nk">suite<>with</span><span span> <span class="n">defs</span><span class="o">.</span><span class="n">add_variable<suite</span><span class="p">(</span><span class="s">"ECF_HOMEtest"</span><span class="p">,)</span> <span class="k">as</span> <span class="n">os<>suite</span><span class="op">.:</span><spanspan>
<span class="n">getenv<>suite</span><span class="po">(.</span><span class="n">add_variable</span><span class="p">(</span><span class="s">"HOMEECF_INCLUDE"</span><span class="p">),</span> <span class="n">os</span><span class="o">+</span> <span.</span><span class="n">getenv</span><span class="p">(</span><span class="s">"/courseHOME"</span><span class="p">)</span>
<span class="o">+</span> <span class="s">"/course"</span><span class="p">)</span>
<span class="k">with</span> <span class="n">suite</span><span class="o">.</span><span class="n">add_family<variable</span><span class="p">(</span><span class="s">"f1ECF_HOME"</span><span class="p">),</span> <span class="k">as</span> <span class="n">f1</span><span class="p">:</span>
<span class="n">f1<>os</span><span class="o">.</span><span class="n">add_variable<>getenv</span><span class="p">(</span><span class="s">"SLEEPHOME"</span><span class="p">,)</span> <span class="mio">20</span><span>+</span> <span class="ps">"/course"</span><span class="p">)</span>
<span class="k">with</span> <span class="n">f1<>suite</span><span class="o">.</span><span class="n">add_task<family</span><span class="p">(</span><span class="s">"t1f1"</span><span class="p">)</span><spanspan> <span class="ok">.</span><span>as</span> <span class="n">add_meter<>f1</span><span class="p">(:</span><span span>
<span class="s">"progress"<n">f1</span><span class="po">,.</span>span><span <span class="min">1<>add_variable</span><span class="p">,(</span> <spanspan><span class="mi">100<s">"SLEEP"</span><span class="p">,</span> <span class="mi">90<>20</span><span class="p">)</span>
<span class="n">f1</span><span class="o">.</span><span class="n">add_task</span><span class="p">(</span><span class="s">"t2t1"</span><span class="p">)</span><span class="o">.</span><span class="n">add_trigger<meter</span><span class="p">(</span><span class="s">"t1 eq completeprogress"</span><span class="p">),</span><span class="o">.</span><spanspan> <span class="nmi">add_event<>1</span><span class="p">(,</span><spanspan> <span class="s">"a"<mi">100</span><span class="p">),</span><spanspan> <span class="omi">.<>90</span><span class="n">add_event</span><span class="p">(</span><span class="s">"b"</span><span class="p">)</p">)</span>
<span class="n">f1</span><span class="o">.</span><span class="n">add_task</span><span class="p">(</span><span class="s">"t3t2"</span><span class="p">)</span><span class="o">.</span><span class="n">add_trigger</span><span class="p">(</span><span class="s">"t2:at1 eq complete"</span><span class="p">)</span>
<span class="n">f1</span><span class="o">.</span><span class="n">add_task<span><span class="o">.</span><span class="n">add_event</span><span class="p">(</span><span class="s">"t4a"</span><span class="p">)</span><span class="o">.</span><span class="n">add_trigger<event</span><span class="p">(</span><span class="s">"t2 eq completeb"</span><span class="p">)</span><spanspan>
<span class="n">f1</span><span class="o">.</span><span class="n">add_complete<task</span><span class="p">(</span><span class="s">"t2:bt3"</span><span class="p">)</span>
<span class="n">f1</span><span class="o">.</span><span class="n">add_task<trigger</span><span class="p">(</span><span class="s">"t5t2:a"</span><span class="p">)</span><span class="o">.</span><span class="n">add_trigger</span><span class="p">(</span><span class="s">"t1:progress ge 30"</span><span class="p">)</span>
span>
<span class="n">f1</span><span class="o">.</span><span class="n">add_task</span><span class="p">(</span><span class="s">"t6t4"</span><span class="p">)</span><span class="o">.</span><span class="n">add_trigger</span><span class="p">(</span><span class="s">"t1:progress ge 60t2 eq complete"</span><span class="p">)</span>
<span class="n">f1</span><span class="o">.</span><span class="n">add_task<complete</span><span class="p">(</span><span class="s">"t7t2:b"</span><span class="p">)</span>
<span class="n">f1</span><span class="o">.</span><span class="n">add_trigger<task</span><span class="p">(</span><span class="s">"t1:progress ge 90t5"</span><span class="p">)</span>
<span class="k">with</span> <span class="n">suite</span><span class="o">.</span><span class="n">add_family<trigger</span><span class="p">(</span><span class="s">"f2t1:progress ge 30"</span><span class="p">)</span> <span class="k">as</span> <span class="n">f2</span><span class="p">:</span>
<span class="n">f2<>f1</span><span class="o">.</span><span class="n">add_variable<task</span><span class="p">(</span><span class="s">"SLEEPt6"</span><span class="p">,)</span>span><span <span class="mio">20<>.</span><span class="p">)</span>n">add_trigger</span><span class="p">(</span><span class="s">"t1:progress ge 60"</span><span class="p">)</span>
<span class="n">f2<>f1</span><span class="o">.</span><span class="n">add_task</span><span class="p">(</span><span class="s">"t1t7"</span><span class="p">)</span><span class="o">.</span><span class="n">add_time<trigger</span><span class="p">(</span> <spanspan><span class="s">"00t1:30 23:30 00:30progress ge 90"</span> <spanspan><span class="p">)</span>
<span class="k">with</span> <span class="n">f2<>suite</span><span class="o">.</span><span class="n">add_task<family</span><span class="p">(</span><span class="s">"t2f2"</span><span class="p">)</span><spanspan> <span class="ok">.</span><span>as</span> <span class="n">add_day<>f2</span><span class="p">(:</span> <span class="s">"sunday"</span> <span class="p">)</span>
<span class="n">f2</span><span class="o">.</span><span class="n">add_task<variable</span><span class="p">(</span><span class="s">"t3SLEEP"</span><span class="p">),</span><span class="o">.</span><spanspan> <span class="nmi">add_date<>20</span><span class="p">()</span><span class="mi">1</span><span class="p">,</span> <spanspan>
<span class="n">f2</span><span class="o">.</span><span class="min">0<>add_task</span><span class="p">,(</span> <spanspan><span class="mi">0<s">"t1"</span><span class="p">)</span><span class="o">.</span><span class="n">add_time</span><span class="p">(</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">0<s">"00:30 23:30 00:30"</span> <span class="p">)</span>
<span class="n">f2</span><span class="o">.</span><span class="n">add_task</span><span class="p">(</span><span class="s">"t4t2"</span><span class="p">)</span><span class="o">.</span><span class="n">add_time<day</span><span class="p">(</span> <span class="mi">0</span><spans">"sunday"</span> <span class="p">,)</span>
<span class="mi">2</span><span class="p">,</span> <span class="bp">True</span> <span class="p">)</span>
<span class="n">f2</span><span class="o">.</span><span class="n">add_task</span><span class="p">(</span><span class="s">"t5t3"</span><span class="p">)</span><span class="o">.</span><span class="n">add_time<date</span><span class="p">(</span> <spanspan><span class="mi">0<>1</span><span class="p">,</span> <span class="mi">2<>0</span> <spanspan><span class="p">),</span>
<span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">add_time</span><span class="p">(</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">0</span> <span class="p">)</span>
<span class="n">f2</span><span class="o">.</span><span class="n">add_task</span><span class="p">(</span><span class="s">"t4"</span><span class="p">)</span><span class="o">.</span><span class="n">add_time</span><span class="p">(</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="bp">True</span> <span class="p">)</span>
<span class="n">f2</span><span class="o">.</span><span class="n">add_task</span><span class="p">(</span><span class="s">"t5"</span><span class="p">)</span><span class="o">.</span><span class="n">add_time</span><span class="p">(</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">2</span> <span class="p">)</span>
<span class="k">print</span> <span class="n">defs</span>
<span class="k">print</span> <span class="s">"Checking job creation: .ecf -> .job0"</span>
<span class="k">print</span> <span class="n">defs</span><span class="o">.</span><span class="n">check_job_creation</span><span class="p">()</span>
<span class="k">print</span> <span class="s">"Checking job creation: .ecf -> .job0&trigger expressions"</span>
<span class="k">print</span> <span class="n">defs</span><span class="o">.</span><span class="n">check_job_creation<>check</span><span class="p">()</span>
<span class="k">print</span> <span class="s">"Saving definition to file 'test.def'"</span>
<span class="n">defs</span><span class="o">.</span><span class="n">save_as_defs</span><span class="p">(</span><span class="s">"test.def"</span><span class="p">)</span>
</pre></div>
</div>
</div>
|