Popular recipes tagged "meta:loc=55"http://code.activestate.com/recipes/tags/meta:loc=55/2017-06-13T18:54:02-07:00ActiveState Code RecipesA simple text file pager in Python (Python) 2017-02-10T21:34:45-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580755-a-simple-text-file-pager-in-python/ <p style="color: grey"> Python recipe 580755 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/pagination/">pagination</a>, <a href="/recipes/tags/paging/">paging</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>This recipe shows how to create a simple text file pager in Python. It allows you to view text content a page at a time (with a user-definable number of lines per page). Like standard Unix utilities, it can either take a text file name as a command-line argument, or can read the text from its standard input, which can be redirected to come from a file, or to come from a pipe. The recipe is for Windows only, though, since it uses the msvcrt.getch() function, which is Windows-specific. However, the recipe can be modified to work on Unix by using things like tty, curses, termios, cbreak, etc.</p> <p>More details here:</p> <p><a href="https://jugad2.blogspot.in/2017/02/tp-simple-text-pager-in-python.html" rel="nofollow">https://jugad2.blogspot.in/2017/02/tp-simple-text-pager-in-python.html</a></p> Compute Memory footprint of an object and its contents (Python) 2012-11-23T23:57:31-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577504-compute-memory-footprint-of-an-object-and-its-cont/ <p style="color: grey"> Python recipe 577504 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sizeof/">sizeof</a>). Revision 3. </p> <p>Recursive version sys.getsizeof(). Extendable with custom handlers.</p> Using Tkinter's Invoke Method for testing (Python) 2017-06-13T18:54:02-07:00Stephen Rigdenhttp://code.activestate.com/recipes/users/4191139/http://code.activestate.com/recipes/578978-using-tkinters-invoke-method-for-testing/ <p style="color: grey"> Python recipe 578978 by <a href="/recipes/users/4191139/">Stephen Rigden</a> (<a href="/recipes/tags/invoke/">invoke</a>, <a href="/recipes/tags/mainloop/">mainloop</a>, <a href="/recipes/tags/tkinter/">tkinter</a>, <a href="/recipes/tags/ttk/">ttk</a>, <a href="/recipes/tags/unittest/">unittest</a>, <a href="/recipes/tags/unittests/">unittests</a>). </p> <p>This recipe shows a skeleton unittest fixture that calls a widget's command using <code>invoke</code> without starting tkinter's mainloop. (Tcl8.6.3/Tk8.6.3.)</p> Compute Memory footprint of an object and its contents (Python) 2014-05-15T13:44:15-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578880-compute-memory-footprint-of-an-object-and-its-cont/ <p style="color: grey"> Python recipe 578880 by <a href="/recipes/users/4184815/">yota</a> (<a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sizeof/">sizeof</a>). </p> <p>Recursive version sys.getsizeof(). Extendable with custom handlers.</p> music module (Python) 2013-08-02T02:37:01-07:00Andrew Wayne Teesdale Jr.http://code.activestate.com/recipes/users/4187305/http://code.activestate.com/recipes/578620-music-module/ <p style="color: grey"> Python recipe 578620 by <a href="/recipes/users/4187305/">Andrew Wayne Teesdale Jr.</a> (<a href="/recipes/tags/beep/">beep</a>, <a href="/recipes/tags/music/">music</a>). </p> <p>a module for musical beeps</p> easy user input (Python) 2013-09-13T06:34:08-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578552-easy-user-input/ <p style="color: grey"> Python recipe 578552 by <a href="/recipes/users/4184815/">yota</a> (<a href="/recipes/tags/input/">input</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/query/">query</a>, <a href="/recipes/tags/user/">user</a>). Revision 7. </p> <p>Improvement over <a href="http://code.activestate.com/recipes/577058/">Recipe 577058</a> and cie.</p> <p><code>easy_input()</code> function extends the built-in <code>input()</code> function. A question is prompted as well as some expected answers.</p> <p>The user input can be incomplete (ie. <code>y</code> or <code>ye</code> instead of <code>yes</code>)</p> <ul> <li>If no list of expected answer is provided, default will be "yes/no".</li> <li>If no default answer is provided, default will be the first expected answer.</li> </ul> <p>Try and see.</p> <p>Disclaimer: written in python3, meant for *nix shell, indented with tabs</p> <p><strong>Avoided caveat:</strong> If some expected <code>answer</code> have the same beginning, the user can not enter too few letters. Ex: <code>answer = ['continue', 'test', 'testicle']</code>, user can not input <code>t</code>, <code>te</code> or <code>tes</code> because it will be ambiguous. User can however input <code>test</code>, which is not.</p> python string concatenation (Python) 2012-11-18T21:49:51-08:00akhil jaggarwalhttp://code.activestate.com/recipes/users/4184305/http://code.activestate.com/recipes/578333-python-string-concatenation/ <p style="color: grey"> Python recipe 578333 by <a href="/recipes/users/4184305/">akhil jaggarwal</a> (<a href="/recipes/tags/efficient/">efficient</a>, <a href="/recipes/tags/strings/">strings</a>). </p> <p>tests efficiency of string concatenation operations in python</p> YouTube-like primary key generation (PHP) 2011-02-24T21:43:55-08:00Slava Yansonhttp://code.activestate.com/recipes/users/4176967/http://code.activestate.com/recipes/577571-youtube-like-primary-key-generation/ <p style="color: grey"> PHP recipe 577571 by <a href="/recipes/users/4176967/">Slava Yanson</a> (<a href="/recipes/tags/activerecord/">activerecord</a>, <a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/php/">php</a>, <a href="/recipes/tags/unique/">unique</a>). Revision 4. </p> <p>If you have more then 1 primary database servers - you have probably experienced the pain of primary key overlapping... Here is a super simple solution that will generate a hash of random letters and numbers.</p> <p>Included is a sample ActiveRecord class (MyTable.php).</p> Vectorize Operation (Python) 2011-02-05T05:36:19-08:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577563-vectorize-operation/ <p style="color: grey"> Python recipe 577563 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/vector/">vector</a>). Revision 2. </p> <p>This python submodule enable you to do the vectorize calculation, such as vectorize add, and etc.</p> Maclaurin's_series_ln(x) (Python) 2010-07-15T11:16:30-07:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/577300-maclaurins_series_lnx/ <p style="color: grey"> Python recipe 577300 by <a href="/recipes/users/4155345/">Fouad Teniou</a> (<a href="/recipes/tags/mathematics/">mathematics</a>). Revision 8. </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>The maclaurin's series approximation for ln(1+x) is used to approximate ln(x) by detucting the maclaurin's series for ln(1-x). ln(1+x) - ln(1-x) = ln(1+x/1-x)and by letting y = (1+x/1-x), y could take any positive value.</p> <p>y = 1+x/1-x, therefore x = y-1/y+1 </p> <p>-1 &lt; x &lt; 1 </p> Floating point range (Python) 2010-02-24T07:12:55-08:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577068-floating-point-range/ <p style="color: grey"> Python recipe 577068 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/range/">range</a>). </p> <p>Generator that produces floats, equivalent to range for integers, minimising rounding errors by using only a single multiplication and addition for each number, and no divisions.</p> <p>This generator takes an optional argument controlling whether it produces numbers from the open, closed, or half-open interval.</p> Sudoku solver in functional-programming style (Python) 2010-04-23T12:35:13-07:00Arnau Sanchezhttp://code.activestate.com/recipes/users/4173270/http://code.activestate.com/recipes/577188-sudoku-solver-in-functional-programming-style/ <p style="color: grey"> Python recipe 577188 by <a href="/recipes/users/4173270/">Arnau Sanchez</a> (<a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/sudoku/">sudoku</a>). Revision 10. </p> <p>A simple brute-force Sudoku solver written in functional-programming style. This code is not aimed for speed, the goal is to write a clear, compact and (hopefully) pedagogical functional solution.</p> Binary Matrix Fractals using iteration method (Python) 2010-03-18T22:00:21-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577126-binary-matrix-fractals-using-iteration-method/ <p style="color: grey"> Python recipe 577126 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>). </p> <p>Binary Matrix Fractals using iteration method. It can produce a fractal from any given binary matrix pattern.</p> RFC 822-style parser (Python) 2010-01-09T22:46:42-08:00Karl Dickmanhttp://code.activestate.com/recipes/users/4172760/http://code.activestate.com/recipes/576996-rfc-822-style-parser/ <p style="color: grey"> Python recipe 576996 by <a href="/recipes/users/4172760/">Karl Dickman</a> (<a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/formats/">formats</a>). Revision 7. </p> <p>In <em>The Art of Unix Programming</em>, Eric S. Raymond describes a data file metaformat based on RFC 822. [http://www.faqs.org/docs/artu/ch05s02.html#id2902039] This is a simple parser for that format.</p> pending/blocking a signal (C) 2009-05-25T22:42:30-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576775-pendingblocking-a-signal/ <p style="color: grey"> C recipe 576775 by <a href="/recipes/users/4170398/">J Y</a> (<a href="/recipes/tags/signal/">signal</a>). </p> <p><a href="http://www-h.eng.cam.ac.uk/help/tpl/unix/signals.html" rel="nofollow">http://www-h.eng.cam.ac.uk/help/tpl/unix/signals.html</a></p> <p>do you want certain signals to be ignored or blocked? The sigaction(), sigprocmask(), siginterrupt(), and sigsuspend() functions control the manipulation of the signal mask, which defines the set of signals currently blocked. The manual pages give details. The following code shows how the response to signals can be delayed. </p> Yet another enum for Python (Python) 2009-04-03T12:02:33-07:00Antonio Cunihttp://code.activestate.com/recipes/users/4169762/http://code.activestate.com/recipes/576711-yet-another-enum-for-python/ <p style="color: grey"> Python recipe 576711 by <a href="/recipes/users/4169762/">Antonio Cuni</a> (<a href="/recipes/tags/enum/">enum</a>). </p> <p>There are plenty of ways to declare C-like enums in Python. This one tries to be minimalistic and with a nice syntax, (ab)using decorators.</p> <p>The code is also available here: <a href="http://codespeak.net/svn/user/antocuni/hack/enum.py" rel="nofollow">http://codespeak.net/svn/user/antocuni/hack/enum.py</a></p> Y combinator (Python) 2008-07-31T23:57:07-07:00kay schluehrhttp://code.activestate.com/recipes/users/2398921/http://code.activestate.com/recipes/576366-y-combinator/ <p style="color: grey"> Python recipe 576366 by <a href="/recipes/users/2398921/">kay schluehr</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 4. </p> <p>The Y combinator is a famous higher order function used to implement recursion on anonymous functions i.e. lambda expressions. Y combinators have the fixed point property which is formally expressed by the relation Y(f) = f(Y(f)).</p> Ellipse (Python) 2009-02-14T04:09:28-08:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/576513-ellipse/ <p style="color: grey"> Python recipe 576513 by <a href="/recipes/users/4155345/">Fouad Teniou</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>). Revision 8. </p> <p>In early 1600 Johannas Kepler conclude that all the planets in the solar system move in elliptical orbits with the sun at a focus, thus, the importance of ellipse in science. Ellipse is a Python program for students and tutors at higher levels of education . There are two equations of an ellipse : x^2/a^2 + y^2/b^2 and: x^2/b^2 + y^2/a^2 the foci are on the x axis or y axis. From Ellipse program the student will be able to sketch the ellipse, by drawing a box with (a) and (-a) units on the major axis and ( b) and (-b) units on the minor axis. Since Ellipse program determine the values of a and b and the position of the foci c. c = Square-Root (a^2 – b^2)</p> Regular expression for python string literals (Python) 2006-03-10T20:52:36-08:00Edward Loperhttp://code.activestate.com/recipes/users/2637812/http://code.activestate.com/recipes/475109-regular-expression-for-python-string-literals/ <p style="color: grey"> Python recipe 475109 by <a href="/recipes/users/2637812/">Edward Loper</a> (<a href="/recipes/tags/programs/">programs</a>). </p> <p>A regular expression that matches Python string literals. Tripple-quoted, unicode, and raw strings are supported.</p> weakattr (Python) 2006-06-04T15:46:28-07:00tomer filibahttp://code.activestate.com/recipes/users/2520014/http://code.activestate.com/recipes/496771-weakattr/ <p style="color: grey"> Python recipe 496771 by <a href="/recipes/users/2520014/">tomer filiba</a> (<a href="/recipes/tags/extending/">extending</a>). Revision 2. </p> <p>Weak attributes - cyclic references made easy</p>