Popular recipes tagged "meta:loc=62"http://code.activestate.com/recipes/tags/meta:loc=62/2016-05-05T23:47:49-07:00ActiveState Code RecipesFind Paper Format of based on Pixel Width and Height of a (PDF) Page (Python) 2016-05-05T23:47:49-07:00Harald Liederhttp://code.activestate.com/recipes/users/4191581/http://code.activestate.com/recipes/580659-find-paper-format-of-based-on-pixel-width-and-heig/ <p style="color: grey"> Python recipe 580659 by <a href="/recipes/users/4191581/">Harald Lieder</a> (<a href="/recipes/tags/a4/">a4</a>, <a href="/recipes/tags/format/">format</a>, <a href="/recipes/tags/letter/">letter</a>, <a href="/recipes/tags/paper/">paper</a>). </p> <p>A simple function to determine what format page a page in document has. Parameters provided are width and height in float or integer format.</p> <p>Return is a string like "A4-P" or "Letter-L" when an exact fit is found. If not, information is provided as a string like "width x height (other), closest &lt;format&gt; width x height". The closest format in this case is determined by minimizing the sum of absolute differences of width and height with a table of official formats.</p> Automated Stock Market Trading Simulation (Python) 2014-05-16T02:40:28-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578881-automated-stock-market-trading-simulation/ <p style="color: grey"> Python recipe 578881 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/simulation/">simulation</a>, <a href="/recipes/tags/stock/">stock</a>, <a href="/recipes/tags/stock_market/">stock_market</a>). </p> <p>It simulates an automated trading strategy against a simulated stock.</p> <p>I think the results are interesting. It seems number of wins are always higher than number of losses but average amount of loss is also always higher than average amount of win! </p> Calendar (HTML) 2013-10-23T15:55:05-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578695-calendar/ <p style="color: grey"> HTML recipe 578695 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/calendar/">calendar</a>, <a href="/recipes/tags/javascript/">javascript</a>). </p> <p>Current month calendar. The smallest realization, I suppose.</p> Is enabled.. (cookie, javascript & java)? (JavaScript) 2013-03-19T11:23:28-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578495-is-enabled-cookie-javascript-java/ <p style="color: grey"> JavaScript recipe 578495 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/cookie/">cookie</a>, <a href="/recipes/tags/java/">java</a>, <a href="/recipes/tags/javascript/">javascript</a>). </p> <p>I'm not sure that the following method is universal in all cases to detect is enabled or disabled cookie, javascript and java in user browser at current moment. This is just an example.</p> Evaluates string embedded expressions (rubstr module) (v 0.91) (Python) 2013-03-09T12:22:00-08:00SnarkTurnehttp://code.activestate.com/recipes/users/4185559/http://code.activestate.com/recipes/578487-evaluates-string-embedded-expressions-rubstr-modul/ <p style="color: grey"> Python recipe 578487 by <a href="/recipes/users/4185559/">SnarkTurne</a> . Revision 2. </p> <p>This recipe allow you to write :</p> <pre class="prettyprint"><code>&gt;&gt;&gt; a,b=5,6 &gt;&gt;&gt; rstr("If you add #{a} and #{b}, you'll get #{a+b}.") If you add 5 and 6, you'll get 11. </code></pre> <p>This is more readble, from my point of view, than :</p> <pre class="prettyprint"><code>"If you add {} and {}, you'll get {}.".format(a,b,a+b) </code></pre> <p>This recipe is inspired from the way Ruby evaluates strings :</p> <pre class="prettyprint"><code>irb&gt; a,b=5,6 irb&gt; "If you add #{a} and #{b}, you'll get #{a+b}." ==&gt; If you add 5 and 6, you'll get 11. </code></pre> A matrix generated by shifting the zeroth row (Python) 2012-06-05T12:38:36-07:00Andrej T.http://code.activestate.com/recipes/users/4182297/http://code.activestate.com/recipes/578149-a-matrix-generated-by-shifting-the-zeroth-row/ <p style="color: grey"> Python recipe 578149 by <a href="/recipes/users/4182297/">Andrej T.</a> (<a href="/recipes/tags/matrix/">matrix</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/shift/">shift</a>). Revision 7. </p> <p>ShiftMatrix is a class that generates matrix values by shifting the zeroth row left or right length_of_row times. For example: if you initialize it with sm = ShiftMatrix([1,2,3]), sm[1] will return [3,1,2]. Only the initial list is stored, the other values are generated at runtime.</p> Funny text generator (Python) 2011-12-25T11:14:39-08:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/577988-funny-text-generator/ <p style="color: grey"> Python recipe 577988 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/generation/">generation</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/text/">text</a>). Revision 4. </p> <p>This class takes a text (preferably long enough) and generates another text that "looks like" the original. It won't mean anything, or just by chance ;-)</p> <p>For example, taking Hamlet, Act I, the program generates things like :</p> <p>Hamlet</p> <blockquote> <p>And vanish'd from our watch; <br /> His further. Fare third nights of the flushing immortal as it draw you into the flushing thy complete steel <br /> 'Tis sweet and each new-hatch'd: <br /> A country's father; <br /> To business and is prodigal thee! <br /> Have of crowing more the should I have heaven, <br /> Forward, therefore as ourself in the business it, Horatio <br /> To what is't that your watch, bid this here! </p> </blockquote> <p>Usage :</p> <pre class="prettyprint"><code>generator = TextGenerator(txt) result = generator.random_text(3000) </code></pre> Validate ACNs (Australian Company Numbers) (Python) 2011-05-13T03:15:35-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577691-validate-acns-australian-company-numbers/ <p style="color: grey"> Python recipe 577691 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/acn/">acn</a>, <a href="/recipes/tags/validation/">validation</a>). </p> <p>This is a validation function for Australian Company Numbers (ACNs). It tests whether the integer or string is a valid ACN (but not whether it is a <em>legal</em> ACN, i.e. if it belongs to the company that is quoting it).</p> Decision Maker (Python) 2010-10-16T05:45:58-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577427-decision-maker/ <p style="color: grey"> Python recipe 577427 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/business/">business</a>, <a href="/recipes/tags/decision/">decision</a>, <a href="/recipes/tags/math/">math</a>). Revision 2. </p> <p>This is a simple decision maker. It chooses the best option between N items. Each item can have M constraint values (non-zero). It is assumed each constraint has equal importance (weight).</p> self-logging exceptions (Python) 2009-05-27T17:43:56-07:00geremy condrahttp://code.activestate.com/recipes/users/4170000/http://code.activestate.com/recipes/576781-self-logging-exceptions/ <p style="color: grey"> Python recipe 576781 by <a href="/recipes/users/4170000/">geremy condra</a> (<a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/logging/">logging</a>). Revision 3. </p> <p>Pretty frequently I find myself needing to log my exceptions, and rather than produce a tangled mess of boilerplate code, I went ahead and put together this snippet to act as a base class for all those exceptions.</p> Tallying of objects (hashables) (Python) 2007-05-21T03:01:31-07:00klausman-aspnhttp://code.activestate.com/recipes/users/1786323/http://code.activestate.com/recipes/520589-tallying-of-objects-hashables/ <p style="color: grey"> Python recipe 520589 by <a href="/recipes/users/1786323/">klausman-aspn</a> (<a href="/recipes/tags/search/">search</a>). </p> <p>This class can be used to tally objects, for example when analyzing log files. It has two separate ways of calculating the "score board", one quicker for Python &gt;=2.4 and one that works with older versions (though I doubt it works with anything &lt;2.0).</p> <p>As the class uses a dictionary, only hashables can be counted. Other than that, you can mix and match them however you want.</p> Get Foreign Exchange Rates (Python) 2006-09-23T06:15:56-07:00Matt Keranenhttp://code.activestate.com/recipes/users/38288/http://code.activestate.com/recipes/498127-get-foreign-exchange-rates/ <p style="color: grey"> Python recipe 498127 by <a href="/recipes/users/38288/">Matt Keranen</a> . Revision 2. </p> <p>Retrieve a list of the available exhange rates from the NY Federal Reserve, and create a list of US Dollar to currency multipliers.</p> ProtectUTF8 (Python) 2006-04-28T06:46:25-07:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/492223-protectutf8/ <p style="color: grey"> Python recipe 492223 by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>protect_utf8 is a function decorator that can prevent naive functions from breaking UTF-8.</p> a better better command line calculator (Python) 2006-01-27T07:49:02-08:00Eli Criffieldhttp://code.activestate.com/recipes/users/2754246/http://code.activestate.com/recipes/473796-a-better-better-command-line-calculator/ <p style="color: grey"> Python recipe 473796 by <a href="/recipes/users/2754246/">Eli Criffield</a> . </p> <p>Eli Criffield <a href="&#109;a&#105;&#108;&#116;&#x6f;&#x3a;&#112;&#x79;&#116;hon&#x40;z&#101;&#110;&#x64;&#111;&#x2e;&#x6e;&#x65;&#116;">&#112;&#x79;&#116;hon&#x40;z&#101;&#110;&#x64;&#111;&#x2e;&#x6e;&#x65;&#116;</a> avalible at <a href="http://eli.criffield.net/sum/" rel="nofollow">http://eli.criffield.net/sum/</a> a better better command line calculator,</p> <p>put this in a file, run it, cut and past a bunch of numbers (don't worry about the $ signs and commas) hit enter and it adds them up use / * - + like normal</p> <p>added features over just using python as a command line calculator:</p> <ul> <li>numbers can include ,s and $s or anything really and it still works this allows you to cut and paste things like $1,123.65 and it still works</li> <li>add by default! (unless you run with -x) cut and paste a bunch of numbers hit enter and it'll add them all up</li> <li>command line history and readline, up arrow recalls your history even between sessions</li> <li>_ is the last answer, just like in python</li> </ul> Fast select (Python) 2006-01-21T11:33:31-08:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/466330-fast-select/ <p style="color: grey"> Python recipe 466330 by <a href="/recipes/users/2403049/">bearophile -</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>To quickly select the n-th rank ordered element of a given sequence. (It modifies the ordering of the given sequence).</p> Executing modules inside packages with '-m' (Python) 2004-10-10T19:03:49-07:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/307772-executing-modules-inside-packages-with-m/ <p style="color: grey"> Python recipe 307772 by <a href="/recipes/users/2035254/">Nick Coghlan</a> . Revision 3. </p> <p>Python 2.4 provides the '-m' option to run a module as a script. However, "python -m &lt;script&gt;" will report an error if the specified script is inside a package.</p> <p>Putting the following code in a module called "execmodule.py" and placing it in a directory on sys.path allows scripts inside packages to be executed using "python -m execmodule &lt;script&gt;".</p> cgiproxy.py (Python) 2004-06-22T07:20:42-07:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/286160-cgiproxypy/ <p style="color: grey"> Python recipe 286160 by <a href="/recipes/users/1565518/">Michael Foord</a> . </p> <p>A very simple script that fetches a webpage for you. Useful for a cgiproxy in situations where web access is restricted - and to illustrate the basic case use of urllib2.</p> tree.py - graphically displays the directory structure of a specified path (Python) 2003-08-21T00:50:43-07:00Doug Dahmshttp://code.activestate.com/recipes/users/129202/http://code.activestate.com/recipes/217212-treepy-graphically-displays-the-directory-structur/ <p style="color: grey"> Python recipe 217212 by <a href="/recipes/users/129202/">Doug Dahms</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>The following program displays the directory structure of a specified path using ASCII characters. The program can optionally display files in addition to directories. This program functions similar to the windows 'tree' command.</p> Generator for permutations, combinations, selections of a sequence (Python) 2003-03-20T10:54:20-08:00Ulrich Hoffmannhttp://code.activestate.com/recipes/users/1056747/http://code.activestate.com/recipes/190465-generator-for-permutations-combinations-selections/ <p style="color: grey"> Python recipe 190465 by <a href="/recipes/users/1056747/">Ulrich Hoffmann</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>Permutations and combinations are often required in algorithms that do a complete search of the solution space. They are typically rather large so it's best not to compute them entirely but better to lazily generate them. This recipe uses Python 2.2 generators to create appropriate generator objects, that can be use for example as ranges in for loops.</p> Network Ping Pong using Twisted Prespective Broker (Python) 2003-02-21T16:28:31-08:00Simon Fosterhttp://code.activestate.com/recipes/users/1001891/http://code.activestate.com/recipes/181905-network-ping-pong-using-twisted-prespective-broker/ <p style="color: grey"> Python recipe 181905 by <a href="/recipes/users/1001891/">Simon Foster</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>A simple program using Twisted Perspective Broker and showing use of Twisted Deferreds and other callback mechanisms. Start the server without arguments and the client with the host name of the server.</p>