Popular recipes tagged "with" but not "with_statement"http://code.activestate.com/recipes/tags/with-with_statement/2017-06-22T11:57:20-07:00ActiveState Code RecipesVariable Abbreviations (Python) 2017-06-22T11:57:20-07:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/580807-variable-abbreviations/ <p style="color: grey"> Python recipe 580807 by <a href="/recipes/users/4182236/">Alfe</a> (<a href="/recipes/tags/abbreviations/">abbreviations</a>, <a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/variables/">variables</a>, <a href="/recipes/tags/with/">with</a>). </p> <p>One sometimes has nice long speaking names vor variables, maybe things like <code>buildingList[foundIndex].height</code>, but would like to address these in a shorter fashion to be used within a formula or similar where lots of longs names tend to confuse any reader trying to understand the formula. Physicists use one-letter names for a reason.</p> <p>For this I wrote a small context provider which allows using short names instead of long ones:</p> <pre class="prettyprint"><code>with Abbr(h=buildingList[foundIndex].height, g=gravitationalConstant): fallTime = sqrt(2 * h / g) endSpeed = sqrt(2 * h * g) print("Fall time:", fallTime) print("End speed:", endSpeed) </code></pre> <p>For longer formulas this can reduce ugly multi-line expressions to clearly readable one-liners.</p> <p>One could use this:</p> <pre class="prettyprint"><code>h = buildingList[foundIndex].height g = gravitationalConstant fallTime = sqrt(2 * h / g) endSpeed = sqrt(2 * h * g) del g, h print("Fall time:", fallTime) print("End speed:", endSpeed) </code></pre> <p>to achieve the same result, but</p> <ul> <li>it would not look as clean and</li> <li>the context provider solves the typical issues like cleanup on exception etc.</li> </ul> <p>Just using local variables without cleanup (like above without the <code>del</code> statement) also is an option of course, but that would clutter the variable name space unnecessarily.</p> <p>CAVEATS: The implementation of <code>Abbr()</code> is a hack. If used as intended and described here, it should work just fine, though. But the hackish nature forces me to mention some things: Since at compile time the compiler decides that the <code>h</code> and <code>g</code> in the example must be global variables (because they aren't assigned in the function), it produces byte code accessing global variables. The context provider changes the global variable structure to fill the needs. (Overridden already existing global variables of the same name get restored properly at context exit.) This means some things:</p> <ul> <li>One cannot have a local variable of the same name in the frame surrounding the context manager.</li> <li>Existing global variables are changed during the time of the context manager; so using names like <code>sys</code> or <code>os</code> for abbreviations might be a bad idea due to side-effects.</li> </ul> POSIX context manager to temporarily silence, or filter lines from stdout (Python) 2010-03-03T06:17:23-08:00pwallerhttp://code.activestate.com/recipes/users/4173218/http://code.activestate.com/recipes/577083-posix-context-manager-to-temporarily-silence-or-fi/ <p style="color: grey"> Python recipe 577083 by <a href="/recipes/users/4173218/">pwaller</a> (<a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/freopen/">freopen</a>, <a href="/recipes/tags/silence/">silence</a>, <a href="/recipes/tags/stdout/">stdout</a>, <a href="/recipes/tags/with/">with</a>). Revision 2. </p> <p>Fed up with libraries you don't have control over emitting text into your precious stdout?</p> <p>If they use stdout through python, then you can just change sys.stdout to be something else. If they are printing directly to stdout through a C module, or some other means, then you are stuck.</p> <p>.. at least until you discover the <code>with silence():</code> block!</p> <p>Caveats: Non-portable, tested only on 2.6 under Linux, uses threading.</p> <p>Example output:</p> <pre class="prettyprint"><code>$ python silence_file.py Before with block.. Sensible stuff! After the silence block </code></pre>