Popular recipes tagged "meta:loc=57"http://code.activestate.com/recipes/tags/meta:loc=57/2016-07-31T04:03:29-07:00ActiveState Code RecipesEmulating super() in Python 3.x using Python 2.7 (Python)
2016-07-31T04:03:29-07:00sunqingyaohttp://code.activestate.com/recipes/users/4194518/http://code.activestate.com/recipes/580694-emulating-super-in-python-3x-using-python-27/
<p style="color: grey">
Python
recipe 580694
by <a href="/recipes/users/4194518/">sunqingyao</a>
(<a href="/recipes/tags/beginner/">beginner</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/python2/">python2</a>).
</p>
<p>Depending on the name of the first argument, <code>self.__sup</code> or <code>cls.__sup</code> behaves like <code>super()</code> in Python 3, while this code is written in Python 2.7.</p>
<p>It works for both ordinary methods and class methods(static methods don't use <code>super()</code>). See my code for detailed examples:</p>
2D Discrete Fourier Transform (Python)
2015-01-03T03:39:34-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578997-2d-discrete-fourier-transform/
<p style="color: grey">
Python
recipe 578997
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/image_processing/">image_processing</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/signal_processing/">signal_processing</a>).
</p>
<p>2D Discrete Fourier Transform (DFT) and its inverse.</p>
<p>Calculates 2D DFT of an image and recreates the image using inverse 2D DFT.</p>
<p>Computation is slow so only suitable for thumbnail size images.</p>
Discrete Fourier Transform (Python)
2014-12-27T21:43:53-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578994-discrete-fourier-transform/
<p style="color: grey">
Python
recipe 578994
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/signal/">signal</a>, <a href="/recipes/tags/signal_processing/">signal_processing</a>).
</p>
<p>Discrete Fourier Transform and Inverse Discrete Fourier Transform</p>
<p>To test, it creates an input signal using a Sine wave that has known frequency, amplitude, phase.
Later it calculates DFT of the input signal and finds its frequency, amplitude, phase to compare.</p>
beep based alarm - command line utility (Python)
2014-10-25T22:29:10-07:00Tomas Nordinhttp://code.activestate.com/recipes/users/4189558/http://code.activestate.com/recipes/578953-beep-based-alarm-command-line-utility/
<p style="color: grey">
Python
recipe 578953
by <a href="/recipes/users/4189558/">Tomas Nordin</a>
(<a href="/recipes/tags/alarm/">alarm</a>, <a href="/recipes/tags/argparse/">argparse</a>, <a href="/recipes/tags/beep/">beep</a>, <a href="/recipes/tags/commandline/">commandline</a>).
Revision 2.
</p>
<p>An alarm beeping on you when the eggs are boiled.</p>
Simple Bash Text Mode Sine Curve Generator. (Bash)
2014-08-12T20:57:39-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578921-simple-bash-text-mode-sine-curve-generator/
<p style="color: grey">
Bash
recipe 578921
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/cygwin/">cygwin</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/text/">text</a>).
Revision 2.
</p>
<p>This bash script is a taster for a kids level, audio, text mode, sweep generator.
The code just creates a single cycle of a quantised sine curve inside an 80 x 24 bash terminal.
This will be the calculator for a sinewave sweep generator from about 50Hz the 12KHz...
The code tells you more and the display is in comments at the end...</p>
Music Downloader (Python)
2013-05-25T06:52:51-07:00Christian Careagahttp://code.activestate.com/recipes/users/4186639/http://code.activestate.com/recipes/578530-music-downloader/
<p style="color: grey">
Python
recipe 578530
by <a href="/recipes/users/4186639/">Christian Careaga</a>
(<a href="/recipes/tags/download/">download</a>, <a href="/recipes/tags/downloader/">downloader</a>, <a href="/recipes/tags/music/">music</a>, <a href="/recipes/tags/program/">program</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python_scripts/">python_scripts</a>, <a href="/recipes/tags/selenium/">selenium</a>, <a href="/recipes/tags/urllib/">urllib</a>, <a href="/recipes/tags/urllib2/">urllib2</a>).
</p>
<p>A Python Program i wrote that downloads music from the web</p>
Random Multi-Maze Generator (Python)
2012-12-14T11:23:29-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578378-random-multi-maze-generator/
<p style="color: grey">
Python
recipe 578378
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/ai/">ai</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/pil/">pil</a>).
</p>
<p>Randomly creates mazes which have multiple paths to solve.
Entry and exit points for each path can be assigned arbitrarily.
(I never seen this kind of maze anywhere before but I think calling them multi-maze should be okay.)</p>
GAE Matplotlib Demo (Python)
2012-12-31T17:39:14-08:00Dima Tisnekhttp://code.activestate.com/recipes/users/4068698/http://code.activestate.com/recipes/578393-gae-matplotlib-demo/
<p style="color: grey">
Python
recipe 578393
by <a href="/recipes/users/4068698/">Dima Tisnek</a>
(<a href="/recipes/tags/app_engine/">app_engine</a>, <a href="/recipes/tags/gae/">gae</a>, <a href="/recipes/tags/google_app_engine/">google_app_engine</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>Google App Engine python 2.7 runtime includes support for numpy and matplotlib since 13-Dec-2012, however, by default, matplotlib is not supported on the development server.
This workaround let you run both in developer mode and deployed on google app engine:</p>
Verify Username and Password Using a File for Storage (Python)
2013-01-31T02:11:24-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578446-verify-username-and-password-using-a-file-for-stor/
<p style="color: grey">
Python
recipe 578446
by <a href="/recipes/users/4184772/">Captain DeadBones</a>
(<a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/usernames/">usernames</a>).
</p>
<p>This script was written for an article I wrote to check user passwords. It stores data using SHA one-way function. For more information <a href="http://thelivingpearl.com/2013/01/29/authentication-of-users-and-passwords-in-python/">Authentication Of Users And Passwords In Python</a></p>
Knight's Tour Map using Warnsdorff's Algorithm (Python)
2012-12-18T01:05:51-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578386-knights-tour-map-using-warnsdorffs-algorithm/
<p style="color: grey">
Python
recipe 578386
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/pil/">pil</a>).
</p>
<p>Solves Knight's Tour Problem using Warnsdorff's Algorithm for every square on the chessboard of arbitrary size.</p>
<p>It colors each initial square with a color that depends on the final square coordinates.
(This is a method used to create fractals.)
Many different formulas maybe used for coloring (based on abs/rel x/y/dist/ang).</p>
<p>Calculating each tour in full creates a highly chaotic image (and takes hours).
So I added maxItPercent to cut-off tours earlier.
(Using %10 creates an image that has both ordered and chaotic regions.)</p>
Tweet Google+ posts (Python)
2012-10-31T10:29:32-07:00Timothy Makobuhttp://code.activestate.com/recipes/users/4165901/http://code.activestate.com/recipes/578297-tweet-google-posts/
<p style="color: grey">
Python
recipe 578297
by <a href="/recipes/users/4165901/">Timothy Makobu</a>
(<a href="/recipes/tags/google/">google</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/twitter/">twitter</a>).
Revision 2.
</p>
<p>Tweet public Google+ posts.</p>
<p>The SQLite table was created with: CREATE TABLE posts(post varchar(200) unique);</p>
Flexible datetime parsing (Python)
2012-08-21T07:35:34-07:00Glenn Hutchingshttp://code.activestate.com/recipes/users/4175415/http://code.activestate.com/recipes/578245-flexible-datetime-parsing/
<p style="color: grey">
Python
recipe 578245
by <a href="/recipes/users/4175415/">Glenn Hutchings</a>
(<a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/parsing/">parsing</a>).
</p>
<p>The strptime() method of datetime accepts a format string that you have to specify in advance. What if you want to be more flexible in the kinds of date your program accepts? Here's a recipe for a function that tries many different formats until it finds one that works.</p>
lazy DSU (Python)
2012-09-12T04:49:59-07:00Chris Smithhttp://code.activestate.com/recipes/users/2725752/http://code.activestate.com/recipes/578259-lazy-dsu/
<p style="color: grey">
Python
recipe 578259
by <a href="/recipes/users/2725752/">Chris Smith</a>
(<a href="/recipes/tags/dsu/">dsu</a>, <a href="/recipes/tags/sorting/">sorting</a>).
</p>
<p>sort a sequence by applying decoration only as needed</p>
Paster Run Script (Python)
2011-04-20T13:22:49-07:00Nick Holdenhttp://code.activestate.com/recipes/users/4177739/http://code.activestate.com/recipes/577669-paster-run-script/
<p style="color: grey">
Python
recipe 577669
by <a href="/recipes/users/4177739/">Nick Holden</a>
(<a href="/recipes/tags/script/">script</a>).
</p>
<p>If you are like me and your company has a custom piece of software for managing processes, you have your paster app installed in egg form in a virtualenv, then you might have a hard time running a paster server (turbogears 2) within those limits.</p>
<p>As I had so much trouble writing this I thought I would share. You can add this as a console script in your egg, then run it as --prod or --dev. When run as --prod it adds a signal handler so that if it is sigtermed by the "supervisor" program then it will kill off the rest of the paster processes (they usually don't get stopped if the main running process is killed).</p>
<p>The script is pretty rough and expects the production.ini to be in your package's main folder, with the development.ini being in the same directory as the setup.py (one directory above the production.ini). Make sure you change my_paster_package to be whatever your project is called, like "movies" from the turbogears example</p>
<p>Of course if someone has a better way of doing this that would be great too! (wsgi isn't an option for me, and it seems difficult to set it up when your project is installed as an egg)</p>
Palindrome count (Python)
2010-01-16T11:21:15-08:00David Lamberthttp://code.activestate.com/recipes/users/4169769/http://code.activestate.com/recipes/577010-palindrome-count/
<p style="color: grey">
Python
recipe 577010
by <a href="/recipes/users/4169769/">David Lambert</a>
(<a href="/recipes/tags/palindrome/">palindrome</a>).
</p>
<p>'abccdadba' contains 50 palindromes, not all unique. Examples are:
'a' at positions 0,-4, and -1.
'aa' at (0,4),(5,-1), and (5,-1)
The longest is 'abdadba'
This code counts the palindromes in the data variable.</p>
Simple threading decorator (Python)
2009-03-10T01:38:51-07:00david.gaarenstroomhttp://code.activestate.com/recipes/users/4168848/http://code.activestate.com/recipes/576684-simple-threading-decorator/
<p style="color: grey">
Python
recipe 576684
by <a href="/recipes/users/4168848/">david.gaarenstroom</a>
(<a href="/recipes/tags/async/">async</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/run_asynchronously/">run_asynchronously</a>, <a href="/recipes/tags/thread/">thread</a>, <a href="/recipes/tags/threading/">threading</a>).
Revision 4.
</p>
<p>When you're new at threading, using threads can be a bit daunting at first. If all you want is just to "run this function in parallel (= asynchronously) to the main program code", then this recipe can be of use. Simply use "@run_async" as a decorator for the function you want to run asynchronously. A call to that function will return immediately but the function itself will run in parallel.</p>
Utility Mill Support (Python)
2009-06-03T14:18:24-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/576791-utility-mill-support/
<p style="color: grey">
Python
recipe 576791
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/rpc/">rpc</a>, <a href="/recipes/tags/testing/">testing</a>, <a href="/recipes/tags/utility_mill/">utility_mill</a>).
Revision 2.
</p>
<p>Have you ever published a recipe to <a href="http://UtilityMill.com" rel="nofollow">UtilityMill.com</a> and then wished that you could remotely execute your program? Now that is easy to do with the following supporting library! Running the latest version of your program is easy with the "run_latest(name, query)" command and requires no knowledge of UM's API -- just supply the program's name and keyword arguments that it requires for execution. "utility_mill" is easy to use and can be customized with access to more specific commands available to you. You do not need to know the latest version number? Just use "get_results(name, version, query)" instead to save a call to the server!</p>
C struct decorator (Python)
2009-05-02T11:27:22-07:00geremy condrahttp://code.activestate.com/recipes/users/4170000/http://code.activestate.com/recipes/576734-c-struct-decorator/
<p style="color: grey">
Python
recipe 576734
by <a href="/recipes/users/4170000/">geremy condra</a>
(<a href="/recipes/tags/cpp/">cpp</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/struct/">struct</a>).
Revision 3.
</p>
<p>Like my earlier <a href="http://code.activestate.com/recipes/576731/">C function decorator</a>, this recipe is designed to make it easier to interact with C by removing some of the boilerplate code from defining C structs in Python.</p>
Fast prime generator (C++)
2008-11-30T00:40:23-08:00Florian Mayerhttp://code.activestate.com/recipes/users/4165843/http://code.activestate.com/recipes/576559-fast-prime-generator/
<p style="color: grey">
C++
recipe 576559
by <a href="/recipes/users/4165843/">Florian Mayer</a>
(<a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/prime_generator/">prime_generator</a>).
Revision 2.
</p>
<p>This is the sieve of Eratosthenes implemented in C++.</p>
Access Cairo surface from numpy and pygame (Python)
2006-11-21T08:22:51-08:00Simon Burtonhttp://code.activestate.com/recipes/users/1214028/http://code.activestate.com/recipes/498278-access-cairo-surface-from-numpy-and-pygame/
<p style="color: grey">
Python
recipe 498278
by <a href="/recipes/users/1214028/">Simon Burton</a>
(<a href="/recipes/tags/graphics/">graphics</a>).
</p>
<p>We alias the memory from a cairo surface so that we can manipulate the bits as a numpy array, or display/manipulate the image in pygame.</p>