Popular recipes tagged "meta:loc=54"http://code.activestate.com/recipes/tags/meta:loc=54/2015-12-06T21:23:48-08:00ActiveState Code RecipesValidate data easily with JSON Schema (Python) 2015-12-06T21:23:48-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579135-validate-data-easily-with-json-schema/ <p style="color: grey"> Python recipe 579135 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/jsonschema/">jsonschema</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>). </p> <p>This recipe shows how to use the jsonschema Python library, which implements the JSON Schema specification, to easily validate your Python data. It sends good output to stdout and bad output to stderr, for demo purposes.</p> <p>jsonschema Python library: <a href="https://pypi.python.org/pypi/jsonschema" rel="nofollow">https://pypi.python.org/pypi/jsonschema</a></p> <p>JSON Schema: <a href="http://json-schema.org/" rel="nofollow">http://json-schema.org/</a></p> how to solder (Text) 2015-04-11T00:43:28-07:00Danilo Marianohttp://code.activestate.com/recipes/users/4192007/http://code.activestate.com/recipes/579045-how-to-solder/ <p style="color: grey"> Text recipe 579045 by <a href="/recipes/users/4192007/">Danilo Mariano</a> . </p> <p>Tip and tricks on soldering.</p> Logistic Map Fractal (Python) 2013-01-19T00:27:29-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578426-logistic-map-fractal/ <p style="color: grey"> Python recipe 578426 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Logistic Map Fractal.</p> <p><a href="http://en.wikipedia.org/wiki/Logistic_map" rel="nofollow">http://en.wikipedia.org/wiki/Logistic_map</a></p> Movable button (Text) 2013-02-18T09:24:24-08:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578463-movable-button/ <p style="color: grey"> Text recipe 578463 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/button/">button</a>, <a href="/recipes/tags/ironpython/">ironpython</a>). </p> <p>Just for fun. This IronPython script shows how you can create a form with movable button.</p> Sorting a dict's items and keys (Python) 2012-02-04T04:23:01-08:00George V. Reillyhttp://code.activestate.com/recipes/users/4161272/http://code.activestate.com/recipes/578031-sorting-a-dicts-items-and-keys/ <p style="color: grey"> Python recipe 578031 by <a href="/recipes/users/4161272/">George V. Reilly</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/sorting/">sorting</a>). </p> <p><a href="https://github.com/collective/icalendar">icalendar</a> uses its own CaselessDict as the base of many classes. I needed to produce the keys and items in a canonical order, so that certain keys would appear first.</p> Word Wrap for Proportional Fonts (Python) 2011-11-11T21:29:21-08:00Michael Foglemanhttp://code.activestate.com/recipes/users/4171845/http://code.activestate.com/recipes/577946-word-wrap-for-proportional-fonts/ <p style="color: grey"> Python recipe 577946 by <a href="/recipes/users/4171845/">Michael Fogleman</a> (<a href="/recipes/tags/fixed/">fixed</a>, <a href="/recipes/tags/font/">font</a>, <a href="/recipes/tags/proportional/">proportional</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/word/">word</a>, <a href="/recipes/tags/word_wrap/">word_wrap</a>, <a href="/recipes/tags/wrap/">wrap</a>). </p> <p>Word wrap function / algorithm for wrapping text using proportional (versus fixed-width) fonts.</p> <p><code>text</code>: a string of text to wrap <code>width</code>: the width in pixels to wrap to <code>extent_func</code>: a function that returns a (w, h) tuple given any string, to specify the size (text extent) of the string when rendered. the algorithm only uses the width.</p> <p>Returns a list of strings, one for each line after wrapping.</p> BMI calculator (Python) 2011-10-14T16:20:29-07:00Alexander Thomas Cruzhttp://code.activestate.com/recipes/users/4179528/http://code.activestate.com/recipes/577897-bmi-calculator/ <p style="color: grey"> Python recipe 577897 by <a href="/recipes/users/4179528/">Alexander Thomas Cruz</a> (<a href="/recipes/tags/bmi/">bmi</a>, <a href="/recipes/tags/calculator/">calculator</a>). Revision 2. </p> <p>This code helps you figure out your BMI! Can perform three calculations. Also has a built in quit function. NEW! now can figure up to 10 calculations! I also have a metric version here: <a href="http://code.activestate.com/recipes/577907-bmi-calculator-metric/" rel="nofollow">http://code.activestate.com/recipes/577907-bmi-calculator-metric/</a></p> BMI calculator Metric (Python) 2011-10-14T16:20:04-07:00Alexander Thomas Cruzhttp://code.activestate.com/recipes/users/4179528/http://code.activestate.com/recipes/577907-bmi-calculator-metric/ <p style="color: grey"> Python recipe 577907 by <a href="/recipes/users/4179528/">Alexander Thomas Cruz</a> (<a href="/recipes/tags/bmi/">bmi</a>, <a href="/recipes/tags/calculator/">calculator</a>). </p> <p>This code helps you figure out your BMI! Can perform three calculations. Also has a built in quit function. NEW! now can figure up to 10 calculations! I also have a version with metric calculations.</p> Formatting numbers with a state machine (implementation of a regex pattern) (Python) 2011-03-22T03:40:45-07:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/577618-formatting-numbers-with-a-state-machine-implementa/ <p style="color: grey"> Python recipe 577618 by <a href="/recipes/users/4167757/">James Mills</a> (<a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/regular_expressions/">regular_expressions</a>). </p> <p>I was once asked to explain how the following regular expression works which formats any integer with commas for every thousand (or group of 3 digits):</p> <pre class="prettyprint"><code>(\d)(?=(\d{3})+$) </code></pre> <p>Example:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; import re &gt;&gt;&gt; re.sub("(\d)(?=(\d{3})+$)", "\\1,", "1234") '1,234' </code></pre> <p>So here is an implementation of the above regular expression (as best as I could over a lunch break) that will hopefully highlight how a regular expression engine and finite automa work.</p> <p>Comments and feedback welcome!</p> <p>--JamesMills / prologic</p> Download all lolcat images from iCanHasCheezburger.com (Python) 2011-03-10T08:49:14-08:00Rahul Anandhttp://code.activestate.com/recipes/users/4173646/http://code.activestate.com/recipes/577603-download-all-lolcat-images-from-icanhascheezburger/ <p style="color: grey"> Python recipe 577603 by <a href="/recipes/users/4173646/">Rahul Anand</a> (<a href="/recipes/tags/download/">download</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/lolcat/">lolcat</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/web/">web</a>). </p> <p>Running this python script will download all lolcat images from <a href="http://icanhascheezburger.com" rel="nofollow">icanhascheezburger.com</a> to the current folder. Download will start from the oldest image. Images are collected into subfolders lolcat0, lolcat1 etc, each containing 300 images. The script can be stopped and resumed at anytime. Make sure to create files <em>lolconfig.txt</em> and <em>log.txt</em> in the same folder before running the script. <em>lolconfig.txt</em> must have a string as follows in the beginning: <em>1496/1496/0</em>. log.txt is an empty file in the beginning</p> Auto Named Decriptors (Python) 2010-10-19T22:55:16-07:00Aloys Baillethttp://code.activestate.com/recipes/users/4175355/http://code.activestate.com/recipes/577426-auto-named-decriptors/ <p style="color: grey"> Python recipe 577426 by <a href="/recipes/users/4175355/">Aloys Baillet</a> (<a href="/recipes/tags/automatically/">automatically</a>, <a href="/recipes/tags/constants/">constants</a>, <a href="/recipes/tags/descriptors/">descriptors</a>, <a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/name/">name</a>, <a href="/recipes/tags/string/">string</a>). Revision 3. </p> <p>Using named Descriptors? Tired of duplicating the name of the instance in a string? A small metaclass can solve this.</p> Relative path from one directory to another without explicit string functions (unix only) (Python) 2011-04-11T13:02:32-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577356-relative-path-from-one-directory-to-another-withou/ <p style="color: grey"> Python recipe 577356 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/relative/">relative</a>, <a href="/recipes/tags/unix/">unix</a>). Revision 4. </p> <p>I saw a <a href="http://code.activestate.com/recipes/208993/">recipe 208993</a> messed up with os.sep and '../' and decide to write near-pure-Python version. os.sep used in string expressions only for testing for root directory.</p> <p>Function deal with Unix paths (root: "/"), Windows systems are not supported (root: "C:\").</p> Maclaurin's_series_cos² (x) (Python) 2010-08-03T12:21:22-07:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/577345-maclaurins_series_cos2-x/ <p style="color: grey"> Python recipe 577345 by <a href="/recipes/users/4155345/">Fouad Teniou</a> (<a href="/recipes/tags/mathematics/">mathematics</a>). Revision 4. </p> <p>C. Maclaurin. A Scottish mathematician gained his master degree at age 17, and his major mathematics' work arise from his special knowledge in Newton's ideas and the formulation of Newton's methods.</p> <p>However, C. Maclaurin also contributed to the astronomy science and helped to improve maps and invented some mechanical devices .</p> <p>My mathematics python's programs is a set of Maclaurin's series to compute some of the most important functions in calculus.</p> <p>Though, the computation of an infinite sum which give the value of a function in terms of the derivatives evaluated at a special case where x0 = 0,in contrast with Taylor series. </p> <p>There are several ways of finding Maclaurin’s series, and I used the multiplication and the division to develop my own maclaurin’s series for cos(2x) and cos²(x).</p> <p>Cos(2x) = 2cos²(x) - 1 and cos²(x) = (cos(2x)+1)/2</p> <p>However, integration and differentiation could also be used to find Maclaurin’s series. </p> Maybe pattern (Python) 2010-07-21T11:08:11-07:00Alan Franzonihttp://code.activestate.com/recipes/users/4169882/http://code.activestate.com/recipes/577248-maybe-pattern/ <p style="color: grey"> Python recipe 577248 by <a href="/recipes/users/4169882/">Alan Franzoni</a> (<a href="/recipes/tags/design/">design</a>, <a href="/recipes/tags/maybe/">maybe</a>, <a href="/recipes/tags/pattern/">pattern</a>). Revision 2. </p> <p>An example Maybe pattern implementation for Python.</p> A new method for computing the hypergeometric function 1F1(a,b,t) (Python) 2010-03-22T05:00:20-07:00Fernando Nieuwveldthttp://code.activestate.com/recipes/users/4172088/http://code.activestate.com/recipes/577133-a-new-method-for-computing-the-hypergeometric-func/ <p style="color: grey"> Python recipe 577133 by <a href="/recipes/users/4172088/">Fernando Nieuwveldt</a> (<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/hypergeometric/">hypergeometric</a>, <a href="/recipes/tags/laplace_inversion/">laplace_inversion</a>). Revision 3. </p> <p>I present a method of computing the 1F1(a,b,x) function using a contour integral. The method is based on a numerical inversion, basically the Laplace inversion. Integral is 1F1(a,b,x) = Gamma(b)/2\pi i \int_\rho exp(zx)z^(-b)(1+x/z)^(-a)dz, \rho is taken as a Talbot contour. The Talbot method is applied with the use of the midpoint rule for numerical integration. Here the user must give the number of function evaluations and this may vary from problem to problem. It is very easy to implement with only a few lines of code and it is very accurate even for large arguments.</p> Modified os.walk which return current directory depth (Python) 2009-11-06T16:16:42-08:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/576933-modified-oswalk-which-return-current-directory-dep/ <p style="color: grey"> Python recipe 576933 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/directory_depth/">directory_depth</a>, <a href="/recipes/tags/os_walk/">os_walk</a>, <a href="/recipes/tags/relative_path/">relative_path</a>). Revision 2. </p> <p>On some task I need to collect file names under specified directory with distance from it. Standard os.walk function do not return depth value.</p> <p>One solution -- find function which will calculate relative distance from top directory to file.</p> <p>Another [presented] solution -- modify os.walk so it returns depth level as fourth tuple's value.</p> format a number as an ordinal (Python) 2010-02-04T12:54:27-08:00Serdar Tumgorenhttp://code.activestate.com/recipes/users/4171495/http://code.activestate.com/recipes/576888-format-a-number-as-an-ordinal/ <p style="color: grey"> Python recipe 576888 by <a href="/recipes/users/4171495/">Serdar Tumgoren</a> (<a href="/recipes/tags/number/">number</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/numerals/">numerals</a>, <a href="/recipes/tags/ordinals/">ordinals</a>). Revision 10. </p> <p>This function converts 0 and <em>positive</em> integers (or their string representations) to their ordinal values. For example, it would convert 0 to "0th", 1 to "1st", 2 to "2nd", 3 to "3rd" and so on.</p> Tkinter Splash Screen (Python) 2009-11-09T04:26:52-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/576936-tkinter-splash-screen/ <p style="color: grey"> Python recipe 576936 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/splash/">splash</a>, <a href="/recipes/tags/tix/">tix</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 4. </p> <p>This program is <a href="http://code.activestate.com/recipes/534124/">recipe 534124</a> re-envisioned. The code is mostly the same but was written with a slightly different style. It has also been tested on Python 3.1.1 and appears to work well with GIF images. Usage is the same as can be found in the original recipe. Any further improvements are welcome, and all are encouraged to use as needed.</p> Dot-style nested lookups over dictionary based data structures (Python) 2008-12-14T14:32:59-08:00David Mosshttp://code.activestate.com/recipes/users/4124829/http://code.activestate.com/recipes/576586-dot-style-nested-lookups-over-dictionary-based-dat/ <p style="color: grey"> Python recipe 576586 by <a href="/recipes/users/4124829/">David Moss</a> (<a href="/recipes/tags/access/">access</a>, <a href="/recipes/tags/attribute/">attribute</a>, <a href="/recipes/tags/dictionaries/">dictionaries</a>). Revision 2. </p> <p>This recipe allows you to present dictionary based nested data stuctures in your Python code as objects with nested attributes.</p> <p>It provides dot ('.') based attribute lookups, like this :-</p> <pre class="prettyprint"><code>&gt;&gt;&gt; x = d.foo.bar.baz </code></pre> <p>instead of the usual (and longer) Python dictionary syntax lookups :-</p> <pre class="prettyprint"><code>&gt;&gt;&gt; x = d['foo']['bar']['baz'] </code></pre> <p>This recipe saves you <em>lot</em> of typing!</p> <p>For simplicity and readability this particular version show a read only lookup class.</p> Another use for lambdas and function references (Python) 2009-02-27T06:20:33-08:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576672-another-use-for-lambdas-and-function-references/ <p style="color: grey"> Python recipe 576672 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/function_reference/">function_reference</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/loop/">loop</a>). </p> <p>I was writing code to do bootstrapping on a set of data. I wanted a test case where if I asked for one bootstrap I would be returned the original data. lambdas and function references saved me from inefficient code.</p>