Top-rated recipes tagged "variable"http://code.activestate.com/recipes/tags/variable/top/2017-06-22T10:24:05-07:00ActiveState Code Recipes Keyword Argument Injection with Python Decorators (Python) 2010-09-05T17:06:04-07:00Ahmet Emre Aladağhttp://code.activestate.com/recipes/users/4174877/http://code.activestate.com/recipes/577382-keyword-argument-injection-with-python-decorators/ <p style="color: grey"> Python recipe 577382 by <a href="/recipes/users/4174877/">Ahmet Emre Aladağ</a> (<a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/class_decorator/">class_decorator</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/injection/">injection</a>, <a href="/recipes/tags/variable/">variable</a>). Revision 2. </p> <p>In most of the object oriented codes we write, we need to set class attributes to the given argument values and this is a very line-consuming thing. To get over these redundant lines, I found a solution using decorators. </p> Stack Environment (Python) 2017-06-22T10:24:05-07:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/578147-stack-environment/ <p style="color: grey"> Python recipe 578147 by <a href="/recipes/users/4182236/">Alfe</a> (<a href="/recipes/tags/environment/">environment</a>, <a href="/recipes/tags/global/">global</a>, <a href="/recipes/tags/stack/">stack</a>, <a href="/recipes/tags/stackenv/">stackenv</a>, <a href="/recipes/tags/variable/">variable</a>). Revision 3. </p> <p>The environment variables of processes get inherited by their children who can modify them and pass them on to their children. The tree of processes is similar to the call tree of a running Python process, but a similar mechanism like the environment variables is missing. I'm offering a solution for this; each stack frame takes the place of a process, hence I call it StackEnv. It comes in handy if you want to pass information from one frame to a frame far below without patching all the calls in between to pass the data (which might belong to a framework you don't want to change).</p> <p>Usecases:</p> <ol> <li><p>You call a framework which calls back your code before returning (e. g. in passed objects you provide). You want to pass some information to your code without relying on global variables or similar constructs which weren't thread-safe nor re-entrant.</p></li> <li><p>You want to pass pseudo-global but in fact situation-related information (e. g. the verbosity based on the situation the call comes from) without handing the information down in each and every call which can happen below yours.</p></li> <li><p>You want to give called methods the option to override the decision of the caller method regarding this information. (E. g. the caller decides that verbosity should be True, but the called method then calls another method and decides that the verbosity in this case should be False.)</p></li> </ol> <p>Alike processes, called frames cannot influence the values for calling frames (but of course mutable values <em>can</em> be used to pass information upwards; this normal kind of "abuse" isn't prevented).</p> <p>Importing:</p> <pre class="prettyprint"><code>from stackEnv import stackEnv </code></pre> <p>Setting a stackEnv variable:</p> <pre class="prettyprint"><code>stackEnv.verbose = True </code></pre> <p>Using the value of a stackEnv variable:</p> <pre class="prettyprint"><code>if stackEnv.verbose: print "being verbose now" </code></pre> <p>Testing a stackEnv variable:</p> <pre class="prettyprint"><code>if 'verbose' in stackEnv: print "having information on verbosity" </code></pre> <p>Overriding a stackEnv variable's value for the rest of this frame and its calls:</p> <pre class="prettyprint"><code>stackEnv.verbose = False </code></pre> <p>Removing a stackEnv variable for the rest of this frame and its calls:</p> <pre class="prettyprint"><code>del stackEnv.verbose </code></pre> <p>Some more useful API of this class can be found in the unit test included.</p> Adding the directory of the python executable to the system PATH under windows (Python) 2010-05-19T19:01:31-07:00Anthon van der Neuthttp://code.activestate.com/recipes/users/2403822/http://code.activestate.com/recipes/577233-adding-the-directory-of-the-python-executable-to-t/ <p style="color: grey"> Python recipe 577233 by <a href="/recipes/users/2403822/">Anthon van der Neut</a> (<a href="/recipes/tags/environment/">environment</a>, <a href="/recipes/tags/executable/">executable</a>, <a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/variable/">variable</a>). </p> <p>If you install python under windows and then open a command shell (DOS-prompt, you normally get an error message if you type "python" at the prompt. This is because the directory of the python executable is not in the PATH environment variable. If you know where you installed python, you can add this via Control Panel -> System -> Advanced -> Environment Variables but this is not very user friendly way of doing things and error prone.</p> <p>This program, if run by double clicking the file or by dragging the file to a command shell, will add the directory of the executable associated with the .py extension to the PATH env. var (if it is not already in there). It will notify other programs of this change, but unfortunately <a href="http://command.com" rel="nofollow">command.com</a> is not smart enough to understand that. You have to open a new command shell after running the program in order to be able to run "python" at the dos prompt.</p> <p>If run with the optional command line parameter 'remove' the directory will be removed from the PATH.</p>