Popular recipes tagged "meta:loc=66"http://code.activestate.com/recipes/tags/meta:loc=66/2015-08-19T04:45:51-07:00ActiveState Code Recipesgetch()-like unbuffered character reading from stdin on both Windows and Unix (Python)
2015-08-19T04:45:51-07:00jwhite88http://code.activestate.com/recipes/users/4192711/http://code.activestate.com/recipes/579095-getch-like-unbuffered-character-reading-from-stdin/
<p style="color: grey">
Python
recipe 579095
by <a href="/recipes/users/4192711/">jwhite88</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
</p>
<p>A small utility class to read single characters from standard input, on both Windows and UNIX systems. It provides a getch() function-like instance.</p>
<p>This extends the original script by adding a timeout necessary for another project.</p>
Hex2Dec (JavaScript)
2013-07-04T07:41:53-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578506-hex2dec/
<p style="color: grey">
JavaScript
recipe 578506
by <a href="/recipes/users/4184115/">greg zakharov</a>
.
Revision 2.
</p>
<p>Web hex2dec sample without additional libraries.</p>
Use 'Google Maps Elevation Service' to get elevation data for any location on Earth. (Python)
2012-08-12T20:47:29-07:00Aristotelis Stamatiadishttp://code.activestate.com/recipes/users/4183191/http://code.activestate.com/recipes/578239-use-google-maps-elevation-service-to-get-elevation/
<p style="color: grey">
Python
recipe 578239
by <a href="/recipes/users/4183191/">Aristotelis Stamatiadis</a>
(<a href="/recipes/tags/elevation/">elevation</a>, <a href="/recipes/tags/gis/">gis</a>, <a href="/recipes/tags/google_maps/">google_maps</a>).
</p>
<p>The Google Maps Elevation Service provides a simple interface for getting elevation data for any location on Earth, including locations under the sea (where the elevation is negative). </p>
<p>The following function is based on the examples presented in the Elevation API web page: <code><a href="https://developers.google.com/maps/documentation/elevation/%3C/code%3E." rel="nofollow">https://developers.google.com/maps/documentation/elevation/</code>.</a></p>
<p>Before using the Elevation Service you must read the rather strict usage limits set by Google in the above web page, especially the term that stipulates that "the Elevation API may only be used in conjunction with displaying results on a Google map; using elevation data without displaying a map for which elevation data was requested is prohibited."</p>
Get parent process pid win32 (Python)
2012-05-15T18:01:22-07:00nuumiohttp://code.activestate.com/recipes/users/4182080/http://code.activestate.com/recipes/578133-get-parent-process-pid-win32/
<p style="color: grey">
Python
recipe 578133
by <a href="/recipes/users/4182080/">nuumio</a>
(<a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/process_management/">process_management</a>).
</p>
<p>Use ctypes + Win32 functions to enumerate processes and find parent process id.</p>
<p>Thanks to winterTTr Dong for the original recipe.</p>
Even faster prime generator (C++)
2011-11-27T21:48:25-08:00Sumudu Fernandohttp://code.activestate.com/recipes/users/4180103/http://code.activestate.com/recipes/577966-even-faster-prime-generator/
<p style="color: grey">
C++
recipe 577966
by <a href="/recipes/users/4180103/">Sumudu Fernando</a>
(<a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/prime_generator/">prime_generator</a>).
</p>
<p>A very quick (segmented) sieve of Eratosthenes.</p>
<p>Takes ~6s on a midrange machine to hit all 50 847 534 primes less than 1 billion, ending with 999999937</p>
<p>If you want to actually <em>do</em> anything with every prime (beyond counting them), there are three places to add a statement doing whatever is necessary with "lastP" -- one at the top (handles the special case '2'), one in the middle (handles "small" primes which are actually used to sieve), and one at the bottom (handles "large" primes which merely survive sieving).</p>
<p>In principle one can use a function object as parameter to allow generic operations on the primes, so add that if you want more general-purpose code (perhaps I'll do that later)</p>
<p>For higher limits you need to switch to wider types and follow the commented guidelines for the constants. For a fixed limit, changing <code>B_SIZE</code> may affect performance so if needed tune it (profile as you go, of course!). But this will get quite slow if you go to much higher numbers.</p>
Complex Polynomial Roots Fractal (Python)
2013-04-29T14:35:53-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577866-complex-polynomial-roots-fractal/
<p style="color: grey">
Python
recipe 577866
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/chaos/">chaos</a>, <a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>).
Revision 2.
</p>
<p>The code generates complex polynomials that has random real coefficients; each +1 or -1.
Later it plots the roots.</p>
<p>Warning: The calculation may take 15 minutes or so!</p>
Simple way to execute multiple process in parallel (Python)
2010-08-31T00:11:00-07:00Benjamin Sergeanthttp://code.activestate.com/recipes/users/4039626/http://code.activestate.com/recipes/577376-simple-way-to-execute-multiple-process-in-parallel/
<p style="color: grey">
Python
recipe 577376
by <a href="/recipes/users/4039626/">Benjamin Sergeant</a>
(<a href="/recipes/tags/multiprocessing/">multiprocessing</a>, <a href="/recipes/tags/process/">process</a>).
Revision 4.
</p>
<p>Does not require multiprocessing, easy to hack, maybe not optimal but did the job for a make like tool I wrote.</p>
Generate the partitions of a set, by index (Python)
2010-04-30T11:07:30-07:00Anton Vredegoorhttp://code.activestate.com/recipes/users/2667360/http://code.activestate.com/recipes/577211-generate-the-partitions-of-a-set-by-index/
<p style="color: grey">
Python
recipe 577211
by <a href="/recipes/users/2667360/">Anton Vredegoor</a>
(<a href="/recipes/tags/combinatorics/">combinatorics</a>, <a href="/recipes/tags/partition/">partition</a>).
</p>
<p>Sets of only a few items already have many ways they can be partitioned into subsets. Therefore it can be useful to generate these partitions by index, like the partition class were some large list where one can just access element "i". Of course one should not compute the whole list in advance but compute the partitions on the fly. This recipe was originally extracted form a book by Kreher and Stinson. Over the years I came back to my code from time to time creating a new and hopefully more pythonic version each time I understood it better. My current take on it is that the algorithm looks a lot like creating a Pascals triangle in order to compute combinations. One just tries to find a way down the triangle to a specific element, each time subtracting the amounts the different positions in the triangle account for. It is also similar to finding indexed permutations of a set with elements occurring more than once. One of these days I will perhaps understand how all of this fits together. Until then I'll post code solving specific situations. </p>
Docstring inheritance decorator (Python)
2009-07-28T13:42:32-07:00Shai Bergerhttp://code.activestate.com/recipes/users/2014324/http://code.activestate.com/recipes/576862-docstring-inheritance-decorator/
<p style="color: grey">
Python
recipe 576862
by <a href="/recipes/users/2014324/">Shai Berger</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/docstring/">docstring</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/metaprogramming/">metaprogramming</a>).
</p>
<p>In many cases, a subclass overrides a method in a parent class, just to change its implementation; in such cases, it would be nice to preserve the overridden method's docstring. The decorator below can be used to achieve this without explicit reference to the parent class. It does this by replacing the function with a descriptor, which accesses the parent class when the method is accessed as an attribute.</p>
Simultaneous topdown and bottomup variant of os.walk() (alt. title: "Delete .pyc files and empty directories recursively") (Python)
2010-04-21T20:16:09-07:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/577206-simultaneous-topdown-and-bottomup-variant-of-oswal/
<p style="color: grey">
Python
recipe 577206
by <a href="/recipes/users/2591466/">George Sakkis</a>
(<a href="/recipes/tags/os_walk/">os_walk</a>).
</p>
<p>The standard lib os.walk() function provides a topdown parameter that determines whether entries are yielded in a top-down or a bottom-up order. Sometimes though you may want each directory yielded twice; once before any of its children directories (and recursively their descendants) are yielded and once after they are all yielded. The walk2() function below does this by yielding 4-tuples; the first 3 elements are the same yielded by os.walk() and the 4th is True the first time (topdown) and False the second (bottomup).</p>
<p>An example is deleting all .pyc files and empty directories under some root dir, but excluding specific directories (e.g. VCS specific dirs). The exclusion check should be done topdown (we don't want to descend into any directory that must be excluded) but the check for empty directories has to be done bottom up, since a directory containing only .pyc files will be non-empty initially but empty after removing the files.</p>
Yet another Null object (Python)
2008-11-11T15:24:20-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/576562-yet-another-null-object/
<p style="color: grey">
Python
recipe 576562
by <a href="/recipes/users/2591466/">George Sakkis</a>
(<a href="/recipes/tags/design_pattern/">design_pattern</a>, <a href="/recipes/tags/dummy/">dummy</a>, <a href="/recipes/tags/null/">null</a>).
</p>
<p>This recipe builds on two previously posted recipes for a <a href="http://code.activestate.com/recipes/68205/">null</a> or <a href="http://code.activestate.com/recipes/576447/">dummy</a> object by modifying a few methods (e.g. as in SQL, <code>Null == Null</code> is Null, not True), supporting most (all?) special methods (e.g. int(Null)) and providing correct pickling/unpickling.</p>
Timing an external subprocess (Python)
2008-09-24T11:34:52-07:00Benjamin Hallhttp://code.activestate.com/recipes/users/4167286/http://code.activestate.com/recipes/576514-timing-an-external-subprocess/
<p style="color: grey">
Python
recipe 576514
by <a href="/recipes/users/4167286/">Benjamin Hall</a>
(<a href="/recipes/tags/running_external_programs/">running_external_programs</a>, <a href="/recipes/tags/subprocesses/">subprocesses</a>).
</p>
<p>Running separate processes with os.system() has a single major disadvantage for the work I'm doing, and that is the external program is prone to entering a state where it stops producing any output. The "wallclock" subroutine runs an external command and periodically checks to see if it has finished, killing it if it runs for too long.</p>
Dynamic function composition decorator (Python)
2007-09-06T02:36:53-07:00kay schluehrhttp://code.activestate.com/recipes/users/2398921/http://code.activestate.com/recipes/528929-dynamic-function-composition-decorator/
<p style="color: grey">
Python
recipe 528929
by <a href="/recipes/users/2398921/">kay schluehr</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>This pattern describes a way to add or remove behaviour to/from a function dynamically. This is not possible by just stacking decorators. Instead the callables used for the function composition will be hold independently by a Composer object.</p>
Skeleton for well behaved unix command line tools (Python)
2007-09-27T05:00:28-07:00Alfred Schilkenhttp://code.activestate.com/recipes/users/4085547/http://code.activestate.com/recipes/533114-skeleton-for-well-behaved-unix-command-line-tools/
<p style="color: grey">
Python
recipe 533114
by <a href="/recipes/users/4085547/">Alfred Schilken</a>
.
</p>
<p>This skeleton is a good start for well behaved unix command line tools.
The module optparse is used to get arguments and options and to print usage and help.
Like typical unix tools with one or two file arguments
it handles stdin and stdout if not enough arguments are given.</p>
Random Passwords (PHP)
2005-11-13T14:42:50-08:00Edward Williamshttp://code.activestate.com/recipes/users/2664647/http://code.activestate.com/recipes/444772-random-passwords/
<p style="color: grey">
PHP
recipe 444772
by <a href="/recipes/users/2664647/">Edward Williams</a>
.
</p>
<p>length = how long the password is
lconsonants = adds lower case consonants -- 1=on || 0=off
uconsonants = adds upper case consonants -- 1=on || 0=off
lvowels = adds lower case vowels -- 1=on || 0=off
uvowels = adds upper case vowels -- 1=on || 0=off
numbers = adds numbers -- 1=on || 0=off
specials = adds @#$%^ -- 1=on || 0=off</p>
Getting min/max in a sequence greater/less than some value (Python)
2005-06-01T05:41:42-07:00Edvard Majakarihttp://code.activestate.com/recipes/users/2450648/http://code.activestate.com/recipes/415233-getting-minmax-in-a-sequence-greaterless-than-some/
<p style="color: grey">
Python
recipe 415233
by <a href="/recipes/users/2450648/">Edvard Majakari</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 3.
</p>
<p>Sometimes it is useful to know what is the smallest value in a sequence
greater than (or equal to) some other value. Eg.</p>
<p>max_lt([2, 3, 5, 7, 11], 6) would be 5, because 5 is greatest value in the list
which is also less than 6. Following the same lines method call
min_gt([3, 5, 6, 10, 12], 6) would return 10, because 10 is the smallest value in the list greater than 6. The following four simple methods implement min_gt, min_le, max_gt and max_ge, but the input must be sorted for them to work.</p>
<p>However, Greg Jorgensen's suggestion is much more clever. No need for sorting, just use list comprehensions with min/max.</p>
button-tip for Tkinter buttons (Python)
2005-02-04T08:22:45-08:00Frank Jeworrek-Schmidthttp://code.activestate.com/recipes/users/2298608/http://code.activestate.com/recipes/365641-button-tip-for-tkinter-buttons/
<p style="color: grey">
Python
recipe 365641
by <a href="/recipes/users/2298608/">Frank Jeworrek-Schmidt</a>
(<a href="/recipes/tags/ui/">ui</a>).
Revision 6.
</p>
<p>This is an extension for the Button class of Tkinter. This class creates a tip-window when the mouse is entering a button widget.</p>
Docstring coverage. (Python)
2005-04-06T16:59:51-07:00James Harlowhttp://code.activestate.com/recipes/users/1894393/http://code.activestate.com/recipes/355731-docstring-coverage/
<p style="color: grey">
Python
recipe 355731
by <a href="/recipes/users/1894393/">James Harlow</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 2.
</p>
<p>Tool to examine lack of docstrings in a module.</p>
Update web to point to dynamic ip (Python)
2004-11-16T22:15:41-08:00Kevin Ryanhttp://code.activestate.com/recipes/users/1654599/http://code.activestate.com/recipes/335890-update-web-to-point-to-dynamic-ip/
<p style="color: grey">
Python
recipe 335890
by <a href="/recipes/users/1654599/">Kevin Ryan</a>
.
</p>
<p>Many users get a dynamic ip address when they sign on to the internet (e.g., cable modem). If you run a server on your home network, you probably know that you can't point a webpage address to that server with a dynamic ip b/c DNS servers need a static ip. However, if you get a home page with your ISP (e.g., users.someisp.com/myusername), this script will allow you to automatically update your home page with a web page that will redirect your users to your dynamic ip.</p>
<p>Run the code whenever you get a new ip address, and your users will always get to see your website. Best part of all - YOU CONTROL YOUR SERVER! You want to add PHP and MySQL support on your Linux server, go right ahead. The sky's the limit :)</p>
event driven programming (Python)
2004-06-16T16:06:39-07:00Elmar Bschorerhttp://code.activestate.com/recipes/users/1246619/http://code.activestate.com/recipes/286136-event-driven-programming/
<p style="color: grey">
Python
recipe 286136
by <a href="/recipes/users/1246619/">Elmar Bschorer</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 4.
</p>
<p>This small code demonstrates how state driven programming can be done in python.</p>