Popular recipes tagged "meta:loc=40"http://code.activestate.com/recipes/tags/meta:loc=40/2017-02-23T22:38:50-08:00ActiveState Code RecipesSimulating an unless (reverse if) statement in Python (Python) 2017-02-23T22:38:50-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580758-simulating-an-unless-reverse-if-statement-in-pytho/ <p style="color: grey"> Python recipe 580758 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/features/">features</a>, <a href="/recipes/tags/if/">if</a>, <a href="/recipes/tags/perl/">perl</a>, <a href="/recipes/tags/programming/">programming</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/trick/">trick</a>). </p> <p>This recipe shows how to simulate an unless statement (a sort of reverse if, like Perl has), in Python. It is just for fun and as an experiment, not meant for real use, because the effect of unless can easily be got by negating the sense of the condition in an if statement.</p> <p>More details and output here:</p> <p><a href="https://jugad2.blogspot.in/2017/02/perl-like-unless-reverse-if-feature-in.html" rel="nofollow">https://jugad2.blogspot.in/2017/02/perl-like-unless-reverse-if-feature-in.html</a></p> ASCII art Sphere with integer square root (Batch) 2016-06-06T10:09:38-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580673-ascii-art-sphere-with-integer-square-root/ <p style="color: grey"> Batch recipe 580673 by <a href="/recipes/users/4182514/">Antoni Gual</a> (<a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/graphics/">graphics</a>). Revision 2. </p> <p>The code draws a sphere in ASCII Art. Square root routine bt Aacini</p> Collection Pipeline in Python (Python) 2016-03-16T14:45:02-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/580625-collection-pipeline-in-python/ <p style="color: grey"> Python recipe 580625 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/map/">map</a>, <a href="/recipes/tags/pipe/">pipe</a>, <a href="/recipes/tags/pipline/">pipline</a>). </p> <p>A powerful functional programming technique is the use of pipelines of functions. If you have used shell scripting languages like <code>bash</code>, you will have used this technique. For instance, to count the number of files or directories, you might say: <code>ls | wc -l</code>. The output of the first command is piped into the input of the second, and the result returned.</p> <p>We can set up a similar pipeline using Lisp-like Map, Filter and Reduce special functions. Unlike the standard Python <code>map</code>, <code>filter</code> and <code>reduce</code>, these are designed to operate in a pipeline, using the same <code>|</code> syntax used by bash and other shell scripting languages:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; data = range(1000) &gt;&gt;&gt; data | Filter(lambda n: 20 &lt; n &lt; 30) | Map(float) | List [21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0] </code></pre> <p>The standard Python functional tools is much less attractive, as you have to write the functions in the opposite order to how they are applied to the data. This makes it harder to follow the logic of the expression.</p> <pre class="prettyprint"><code>&gt;&gt;&gt; list(map(float, filter(lambda n: 20 &lt; n &lt; 30, data))) [21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0] </code></pre> <p>We can also end the pipeline with a call to <code>Reduce</code> to collate the sequence into a single value. Here we take a string, extract all the digits, convert to ints, and multiply:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; from operator import mul &gt;&gt;&gt; "abcd12345xyz" | Filter(str.isdigit) | Map(int) | Reduce(mul) 120 </code></pre> Convert CSV to XML (Python) 2010-10-11T06:20:19-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577423-convert-csv-to-xml/ <p style="color: grey"> Python recipe 577423 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/csv/">csv</a>, <a href="/recipes/tags/xml/">xml</a>). </p> <p>Convert CSV to XML.</p> A UNIX-like "which" command for Python (Python) 2015-03-20T19:23:45-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579035-a-unix-like-which-command-for-python/ <p style="color: grey"> Python recipe 579035 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/commands/">commands</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/which/">which</a>). </p> <p>UNIX users are familiar with the which command. Given an argument called name, it checks the system PATH environment variable, to see whether that name exists (as a file) in any of the directories specified in the PATH. (The directories in the PATH are colon-separated on UNIX and semicolon-separated on Windows.)</p> <p>This recipe shows how to write a minimal which command in Python. It has been tested on Windows.</p> Wait for network service to appear (Python) 2014-11-06T07:29:12-08:00Mohammad Taha Jahangirhttp://code.activestate.com/recipes/users/4188847/http://code.activestate.com/recipes/578955-wait-for-network-service-to-appear/ <p style="color: grey"> Python recipe 578955 by <a href="/recipes/users/4188847/">Mohammad Taha Jahangir</a> (<a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/socket/">socket</a>). </p> <p>This script allows you to wait until specified port is opened on remote server. This can be useful in automation jobs - restarting server, wake on lan etc. It can also be used for monitoring distant service/site.</p> <p>The main problem that this script solves is that you need to handle two different timeouts when opening probing socket, and it is not described in python documentation. See <a href="http://bugs.python.org/issue5293" rel="nofollow">http://bugs.python.org/issue5293</a> for more information.</p> A Simple Timing Function (Python) 2013-12-01T01:39:44-08:00Mike Sweeneyhttp://code.activestate.com/recipes/users/4177990/http://code.activestate.com/recipes/578776-a-simple-timing-function/ <p style="color: grey"> Python recipe 578776 by <a href="/recipes/users/4177990/">Mike Sweeney</a> (<a href="/recipes/tags/performance/">performance</a>, <a href="/recipes/tags/timing/">timing</a>). Revision 2. </p> <p>This function prints out a message with the elapsed time from the previous call. It works with most Python 2.x platforms. The function uses a simple trick to store a persistent variable (clock) without using a global variable.</p> Knight's Tour using Warnsdorff Algorithm (Python) 2012-12-17T06:04:19-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578382-knights-tour-using-warnsdorff-algorithm/ <p style="color: grey"> Python recipe 578382 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Knight's Tour using Warnsdorff Algorithm.</p> Verify a Single User Name and Password in Python (Python) 2013-01-31T02:09:58-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578445-verify-a-single-user-name-and-password-in-python/ <p style="color: grey"> Python recipe 578445 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/usernames/">usernames</a>). </p> <p>This script was written for an article I wrote to check user passwords. It stores data using SHA one-way function. For more information <a href="http://thelivingpearl.com/2013/01/29/authentication-of-users-and-passwords-in-python/">Authentication Of Users And Passwords In Python</a></p> Convert CSV to XML (Python) 2012-12-17T16:16:04-08:00Tom Wissinghttp://code.activestate.com/recipes/users/4184629/http://code.activestate.com/recipes/578384-convert-csv-to-xml/ <p style="color: grey"> Python recipe 578384 by <a href="/recipes/users/4184629/">Tom Wissing</a> (<a href="/recipes/tags/csv/">csv</a>, <a href="/recipes/tags/xml/">xml</a>). </p> <p>Convert CSV to XML.</p> Get user's IP address even when they're behind a proxy (Python) 2011-07-15T21:19:17-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/577795-get-users-ip-address-even-when-theyre-behind-a-pro/ <p style="color: grey"> Python recipe 577795 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/address/">address</a>, <a href="/recipes/tags/cgi/">cgi</a>, <a href="/recipes/tags/ip/">ip</a>, <a href="/recipes/tags/web/">web</a>, <a href="/recipes/tags/webpy/">webpy</a>). </p> <p>Function to get the user's IP address in a web app or CGI script, even when they're behind a web proxy.</p> <p>We use web.py as our web framework, but change web.ctx.env and web.ctx.get('ip') to whatever the equivalents are for the CGI environment variables and REMOTE_ADDR are in your framework.</p> Secure Password Generator (Python) 2011-06-17T15:25:20-07:00amir naghavihttp://code.activestate.com/recipes/users/4177294/http://code.activestate.com/recipes/577759-secure-password-generator/ <p style="color: grey"> Python recipe 577759 by <a href="/recipes/users/4177294/">amir naghavi</a> (<a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/random/">random</a>). </p> <p>A password generator that uses OS facilities to generate none pseudo random numbers. the SystemRandom uses CryptGenRandom in Windows and /dev/random in linux and it is so better to use this to create random numbers in cryptography or any other security areas.</p> FSM 2.5 Reader (Python) 2011-04-06T03:02:57-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577639-fsm-25-reader/ <p style="color: grey"> Python recipe 577639 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/file_io/">file_io</a>, <a href="/recipes/tags/file_share/">file_share</a>, <a href="/recipes/tags/messenger/">messenger</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/workaround/">workaround</a>). </p> <p>For those who would want to search the message logs produced by <a href="http://code.activestate.com/recipes/577638/">recipe 577638</a>, this program provides a command-line solution to searching messages according to their authors. If this program is placed in the message directory, the program may be executed on the command-line with the author's name as an argument. If and when the program is executed without an argument, usage information is shown on the screen before exiting. If an author was not found, the author's name is printed stating that nothing could be found. If a matching file was found, all timestamps and messages will be displayed that could be decoded correctly.</p> <p>If there are any recommendation for this recipe or if anyone wishes to down-vote this recipe, please provide corrective criticism showing the the program's faults and give suggestions on how you would fix any problems that it might have.</p> Recurse copy file (Python) 2010-12-09T09:52:36-08:00boussardhttp://code.activestate.com/recipes/users/4176187/http://code.activestate.com/recipes/577493-recurse-copy-file/ <p style="color: grey"> Python recipe 577493 by <a href="/recipes/users/4176187/">boussard</a> (<a href="/recipes/tags/copy/">copy</a>, <a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/recurse/">recurse</a>). </p> <p>Automate copy and paste file with erase the content of file if exists on destination directory. Like shutil.copytree but when dir is exists on destination tree , don't fail, just pass.</p> Voronoi diagram (Python) 2010-08-11T20:03:28-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577353-voronoi-diagram/ <p style="color: grey"> Python recipe 577353 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>). </p> <p>Voronoi diagram.</p> Clean a .py file full of constant chr(x) calls (Python) 2010-04-02T08:20:50-07:00Marcelo Fernándezhttp://code.activestate.com/recipes/users/4173551/http://code.activestate.com/recipes/577175-clean-a-py-file-full-of-constant-chrx-calls/ <p style="color: grey"> Python recipe 577175 by <a href="/recipes/users/4173551/">Marcelo Fernández</a> (<a href="/recipes/tags/chr/">chr</a>, <a href="/recipes/tags/cleaner/">cleaner</a>, <a href="/recipes/tags/cleaning/">cleaning</a>, <a href="/recipes/tags/regex/">regex</a>). </p> <p>This script identifies every chr(xx) call in a script (being xx an integer) and replaces it with a constant byte string. For example: print chr(13) + chr(255) in the input .py file gets translated into '\n' + '\xff' on the output .py file, not breaking the input program, and maybe speeding it a little.</p> ExecSql.cgi returning JSON for SQL (Python) 2009-11-28T02:00:30-08:00Martchenkohttp://code.activestate.com/recipes/users/4172446/http://code.activestate.com/recipes/576971-execsqlcgi-returning-json-for-sql/ <p style="color: grey"> Python recipe 576971 by <a href="/recipes/users/4172446/">Martchenko</a> (<a href="/recipes/tags/cgi/">cgi</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/sql/">sql</a>). </p> <p>CGI script getting JSON for SQL request</p> the list template (C) 2009-07-30T22:59:20-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576864-the-list-template/ <p style="color: grey"> C recipe 576864 by <a href="/recipes/users/4170398/">J Y</a> (<a href="/recipes/tags/list/">list</a>). </p> <p>reference: <a href="http://www.makelinux.net/ldd3/chp-11-sect-5.shtml" rel="nofollow">http://www.makelinux.net/ldd3/chp-11-sect-5.shtml</a></p> Lancos Gamma Approximation (Python) 2009-05-19T05:19:15-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/576763-lancos-gamma-approximation/ <p style="color: grey"> Python recipe 576763 by <a href="/recipes/users/4166679/">alexander baker</a> (<a href="/recipes/tags/gamma/">gamma</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Lancos Gamme Approximation</p> util,maybe a lib (C) 2009-05-20T18:23:05-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576774-utilmaybe-a-lib/ <p style="color: grey"> C recipe 576774 by <a href="/recipes/users/4170398/">J Y</a> (<a href="/recipes/tags/util/">util</a>). </p> <p>keep on working to avoid dimentia</p>