Popular recipes tagged "generator" but not "anim"http://code.activestate.com/recipes/tags/generator-anim/2016-03-18T19:11:48-07:00ActiveState Code RecipesPluggable Python generators (Python)
2016-03-18T19:11:48-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580628-pluggable-python-generators/
<p style="color: grey">
Python
recipe 580628
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>This is a simple recipe to show that Python generators are pluggable, i.e., they can be passed as arguments into functions, and then used inside those functions.</p>
Python Short URL Generator (Python)
2014-11-09T11:19:07-08:00Des Wagnerhttp://code.activestate.com/recipes/users/4191097/http://code.activestate.com/recipes/578961-python-short-url-generator/
<p style="color: grey">
Python
recipe 578961
by <a href="/recipes/users/4191097/">Des Wagner</a>
(<a href="/recipes/tags/deterministic/">deterministic</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/short/">short</a>, <a href="/recipes/tags/tiny/">tiny</a>, <a href="/recipes/tags/unique/">unique</a>, <a href="/recipes/tags/url/">url</a>).
</p>
<p>Python implementation for generating Tiny URL- and bit.ly-like URLs.</p>
Random Password Generation (Python)
2014-08-10T15:50:40-07:00Paul Wolfhttp://code.activestate.com/recipes/users/4190553/http://code.activestate.com/recipes/578920-random-password-generation/
<p style="color: grey">
Python
recipe 578920
by <a href="/recipes/users/4190553/">Paul Wolf</a>
(<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/strong/">strong</a>).
</p>
<p>Generate a password (or other secure token) using a pattern language similar to regular expressions. We'll use the <code>strgen</code> module that enables a user to generate test data, unique ids, passwords, vouchers or other randomized data very quickly using a template language. The template language is superficially similar to regular expressions but instead of defining how to match or capture strings, it defines how to generate randomized strings.</p>
Cycling a sequence (Python)
2014-09-20T19:20:46-07:00Tomas Nordinhttp://code.activestate.com/recipes/users/4189558/http://code.activestate.com/recipes/578942-cycling-a-sequence/
<p style="color: grey">
Python
recipe 578942
by <a href="/recipes/users/4189558/">Tomas Nordin</a>
(<a href="/recipes/tags/cyclic_iterator/">cyclic_iterator</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>A post-it function to cycle through some sequence. Better use itertools.cycle if for any iterable.</p>
create a unique session key (Python)
2014-07-03T16:32:53-07:00john stinsonhttp://code.activestate.com/recipes/users/4190325/http://code.activestate.com/recipes/578903-create-a-unique-session-key/
<p style="color: grey">
Python
recipe 578903
by <a href="/recipes/users/4190325/">john stinson</a>
(<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/sessions/">sessions</a>).
</p>
<p>A simple function that will generate a secure and unique session key.</p>
grep in Python (Python)
2014-03-05T19:47:50-08:00Andy Dustmanhttp://code.activestate.com/recipes/users/2435929/http://code.activestate.com/recipes/578845-grep-in-python/
<p style="color: grey">
Python
recipe 578845
by <a href="/recipes/users/2435929/">Andy Dustman</a>
(<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/search/">search</a>).
</p>
<p>The grep() function is inspired by UNIX grep, but is not limited to string patterns and files or streams.</p>
And yet another round-robin generator (Python)
2013-11-13T19:30:29-08:00Jan Müllerhttp://code.activestate.com/recipes/users/4183984/http://code.activestate.com/recipes/578768-and-yet-another-round-robin-generator/
<p style="color: grey">
Python
recipe 578768
by <a href="/recipes/users/4183984/">Jan Müller</a>
(<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/roundrobin/">roundrobin</a>).
</p>
<p>I know that there is a <a href="http://code.activestate.com/recipes/528936-roundrobin-generator/">nice recipe</a> already that even made it into the <a href="http://docs.python.org/2/library/itertools.html">Python documentation</a>, but this one is more concise and at the same time simpler.</p>
<pre class="prettyprint"><code>>>> list(roundrobin('ABC', 'D', 'EF'))
['A', 'D', 'E', 'B', 'F', 'C']
</code></pre>
Generator with lookahead (Python)
2013-09-24T10:44:56-07:00Rutger Saalminkhttp://code.activestate.com/recipes/users/4187940/http://code.activestate.com/recipes/578671-generator-with-lookahead/
<p style="color: grey">
Python
recipe 578671
by <a href="/recipes/users/4187940/">Rutger Saalmink</a>
(<a href="/recipes/tags/background/">background</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/lookahead/">lookahead</a>, <a href="/recipes/tags/performance/">performance</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>Python generators are a great way of reducing memory usage due to the lazy (on demand) generation of values. However, when the process-time of generating such a value is relatively high, we can improve performance even more by obtaining the next n values of the generator in a separate thread in the background. Hence, the BackgroundGenerator.</p>
Python Short URL Generator (Python)
2011-09-19T14:06:36-07:00Michael Foglemanhttp://code.activestate.com/recipes/users/4171845/http://code.activestate.com/recipes/576918-python-short-url-generator/
<p style="color: grey">
Python
recipe 576918
by <a href="/recipes/users/4171845/">Michael Fogleman</a>
(<a href="/recipes/tags/deterministic/">deterministic</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/short/">short</a>, <a href="/recipes/tags/tiny/">tiny</a>, <a href="/recipes/tags/unique/">unique</a>, <a href="/recipes/tags/url/">url</a>).
Revision 3.
</p>
<p>Python implementation for generating Tiny URL- and bit.ly-like URLs.</p>
Retry loop (Python)
2013-05-23T20:45:01-07:00Ryan Nowakowskihttp://code.activestate.com/recipes/users/4186624/http://code.activestate.com/recipes/578527-retry-loop/
<p style="color: grey">
Python
recipe 578527
by <a href="/recipes/users/4186624/">Ryan Nowakowski</a>
(<a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/loop/">loop</a>, <a href="/recipes/tags/retry/">retry</a>, <a href="/recipes/tags/timeout/">timeout</a>).
</p>
<p>Encapsulates the logic of a retry loop using a generator function.</p>
A SOX 1KHz Sinewave Generator Using A Windows, (TM), Batch File... (Batch)
2013-05-02T17:48:18-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578516-a-sox-1khz-sinewave-generator-using-a-windows-tm-b/
<p style="color: grey">
Batch
recipe 578516
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/sound/">sound</a>, <a href="/recipes/tags/sound_exchange/">sound_exchange</a>, <a href="/recipes/tags/sox/">sox</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>32 bit Windows to Windows 7...</p>
<p>This batch file generates a 65536 byte binary file to give 8 seconds of pure sinewave at the earphone/speaker output(s)...</p>
<p>It uses ONLY a default Windows 32 bit installation, to Windows 7, except for the installation of SOX...</p>
<p>Obtain SOX from here:-</p>
<p><a href="http://sox.sourceforge.net/" rel="nofollow">http://sox.sourceforge.net/</a></p>
<p>Notepad was the _main_ editor... ;o)</p>
<p>Hopefully the code section does NOT corrupt the binary part which is 8 bytes in size.
If it does then let me know on here and I will post a pointer from where to grab it...</p>
<p>Enjoy...</p>
Password Generator (mkpasswd) (Python)
2013-07-31T23:23:02-07:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/578468-password-generator-mkpasswd/
<p style="color: grey">
Python
recipe 578468
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/mkpasswd/">mkpasswd</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/secure/">secure</a>).
Revision 3.
</p>
<p>Since everyone is posting one of these, I thought I'd post mine. I wrote this many years ago and use it everywhere.</p>
Wrap any iterable context manager so it closes when consumed (Python)
2012-11-19T20:10:35-08:00Andrew Barnerthttp://code.activestate.com/recipes/users/4184316/http://code.activestate.com/recipes/578342-wrap-any-iterable-context-manager-so-it-closes-whe/
<p style="color: grey">
Python
recipe 578342
by <a href="/recipes/users/4184316/">Andrew Barnert</a>
(<a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/context_manager/">context_manager</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/with_statement/">with_statement</a>).
</p>
<p>There are a few types in Python—most notably, files—that are both iterators and context managers. For trivial cases, these features are easy to use together, but as soon as you need to use the iterator lazily or asynchronously, a with statement won't help. That's where this recipe comes in handy:</p>
<pre class="prettyprint"><code>send_async(with_iter(open(path, 'r')))
</code></pre>
<p>This also allows you to "forward" closing for a wrapped iterator, so closing the outer iterator also closes the inner one:</p>
<pre class="prettyprint"><code>sync_async(line.upper() for line in with_iter(open(path, 'r')))
</code></pre>
DEMO - Generate A Crude 1KHz Sinewave Using A BASH Script. (Bash)
2013-03-01T19:41:47-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578477-demo-generate-a-crude-1khz-sinewave-using-a-bash-s/
<p style="color: grey">
Bash
recipe 578477
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/sinewave/">sinewave</a>, <a href="/recipes/tags/sound/">sound</a>).
</p>
<p>A very simple crude sinewave generator using a BASH script inside a Linux Terminal.</p>
<p>The file required is generated inside the code and requires /dev/audio to work.</p>
<p>Ensure you have this device, if not the download oss-compat from your OS's repository...</p>
<p>It lasts for about 8 seconds before exiting and saves a 65536 byte file to your working directory/drawer/folder as sinewave.raw.</p>
<p>Use an oscilloscope to check the waveform generated...</p>
<p>It is entirely Public Domain and you may do with it as you please...</p>
<p>Enjoy finding simple solutions to often very difficult problems... ;o)</p>
<p>Bazza, G0LCU...</p>
Colour Bar And Greyscale Generator For Standard Text Mode Python. (Python)
2013-01-18T21:08:21-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578425-colour-bar-and-greyscale-generator-for-standard-te/
<p style="color: grey">
Python
recipe 578425
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/bar/">bar</a>, <a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/colour/">colour</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/gray/">gray</a>, <a href="/recipes/tags/grey/">grey</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/scale/">scale</a>).
</p>
<p>This is just a simple colour bar and combined greyscale generator for standard text mode Python...</p>
<p>It relies on the _magic_ of the ANSI Escape sequences to work and does mess with the terminal colours but restores the colours back to the defaults...</p>
<p>See the code for the machines tested on. It might need the colours adjusting for some terminals but I am sure that is not beyond the average coder...</p>
<p>Written so that anyone can see how it works.</p>
<p>To hide the cursor the command "tput" is assumed to be available, if not, try "setterm -cursor off" and "setterm -cursor on" instead...</p>
<p>Enjoy...</p>
<p>Bazza, G0LCU...</p>
Platform Independent White Noise Generator... (Python)
2012-11-25T10:10:45-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578350-platform-independent-white-noise-generator/
<p style="color: grey">
Python
recipe 578350
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/noise/">noise</a>, <a href="/recipes/tags/program/">program</a>, <a href="/recipes/tags/pyaudio/">pyaudio</a>, <a href="/recipes/tags/signal/">signal</a>, <a href="/recipes/tags/sound/">sound</a>, <a href="/recipes/tags/source/">source</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>This code is a derivative of the Pure Sinewave Generator and produces a continuous noise out of the speakers or headphone sockets.</p>
<p>It is for the hobbyist to be able to generate a pseudo-random noise signal for testing with.</p>
<p>It is issued as Public Domian and you may do with it as you please.</p>
<p>It is very easy to convert to Python 3.x.x but as OSX only has 2.7.x and lower ATM then these are what are used...</p>
<p>An installation of pyaudio IS needed for this to work; see the code for more information.</p>
<p>It is near platform independent but sadly the AMIGA is not included, but hey, I have already shown how generate sound for Classic AMIGAs.</p>
<p>Enjoy finding simple solutions to often very difficult problems...</p>
<p>Bazza, G0LCU...</p>
Platform Independent 1KHz Pure Audio Sinewave Generator... (Python)
2012-10-23T12:53:37-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578301-platform-independent-1khz-pure-audio-sinewave-gene/
<p style="color: grey">
Python
recipe 578301
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/program/">program</a>, <a href="/recipes/tags/pyaudio/">pyaudio</a>, <a href="/recipes/tags/signal/">signal</a>, <a href="/recipes/tags/sinewave/">sinewave</a>, <a href="/recipes/tags/sound/">sound</a>, <a href="/recipes/tags/source/">source</a>, <a href="/recipes/tags/windows/">windows</a>).
Revision 2.
</p>
<p>IKHz_SW_OSX.py</p>
<p>A mono _pure_ sinewave generator using standard text mode Python 2.6.7 to at least 2.7.3.</p>
<p>This DEMO kids level 1KHz generator is mainly for a MacBook Pro, (13 inch in my case),
OSX 10.7.5 and above. See below...</p>
<p>It is another simple piece of testgear for the young amateur electronics enthusiast and
uses pyaudio fully installed for it to work.</p>
<p>PyAudio can be obtained from here:- <a href="http://people.csail.mit.edu/hubert/pyaudio/" rel="nofollow">http://people.csail.mit.edu/hubert/pyaudio/</a></p>
<p>This was primarily for a MacBook Pro, but works on at least 2 Linux flavours and Windows Vista 32 bit...</p>
<p>The sinewave generated is near excellent...</p>
<p>Enjoy finding simple solutions to often very difficult problems... Bazza, G0LCU...</p>
Retry loop (Python)
2013-05-24T05:19:50-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578163-retry-loop/
<p style="color: grey">
Python
recipe 578163
by <a href="/recipes/users/2033964/">Oren Tirosh</a>
(<a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/loop/">loop</a>, <a href="/recipes/tags/retry/">retry</a>, <a href="/recipes/tags/timeout/">timeout</a>).
Revision 4.
</p>
<p>Encapsulates the logic of a retry loop using a generator function.</p>
Forwards Compatibility; Generating A Function On The Fly... (Python)
2012-06-06T17:16:27-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578160-forwards-compatibility-generating-a-function-on-th/
<p style="color: grey">
Python
recipe 578160
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/amiga/">amiga</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/e_uae/">e_uae</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/windows/">windows</a>, <a href="/recipes/tags/winuae/">winuae</a>).
</p>
<p>This code auto-generates a function that converts a string to bytes only in Python Versions 3.x.x.
The function will NOT be generated in Python Versions 1.4.0 to 2.7.x. See the code for all the versions
tested on. Inside the code are commented-out lines to show a practical usage for this and WILL be used
in future /dev/audio or /dev/dsp access that I may develop...</p>
<p>See the notes right at the bottom and read all the notes inside the code...</p>
<p>This is NOT Public Domain like all my other stuff but is MIT licenced...</p>
<p>Enjoy finding simple solutions to often very difficult problems...</p>
<p>Bazza, G0LCU...</p>
Recursively defined, Haskell-style infinite lists (Python)
2012-05-04T14:09:14-07:00John Crichtonhttp://code.activestate.com/recipes/users/4181975/http://code.activestate.com/recipes/578119-recursively-defined-haskell-style-infinite-lists/
<p style="color: grey">
Python
recipe 578119
by <a href="/recipes/users/4181975/">John Crichton</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/infinite/">infinite</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/lazy/">lazy</a>, <a href="/recipes/tags/recursive/">recursive</a>).
Revision 2.
</p>
<p>A decorator to simplify the creation of recursively defined, Haskell-style infinite lists -- ie. recursive generators -- inspired by Raymond Hettinger's "Technique for cyclical iteration" [*]. </p>
<p>[*] <a href="http://code.activestate.com/recipes/576961-technique-for-cyclical-iteration/" rel="nofollow">http://code.activestate.com/recipes/576961-technique-for-cyclical-iteration/</a> </p>