Most viewed recipes tagged "meta:loc=21"http://code.activestate.com/recipes/tags/meta:loc=21/views/2017-04-17T11:25:02-07:00ActiveState Code RecipesGet the path of the currently executing python script using import. (Python)
2006-02-24T05:23:36-08:00Jitender Cheemahttp://code.activestate.com/recipes/users/2790517/http://code.activestate.com/recipes/474083-get-the-path-of-the-currently-executing-python-scr/
<p style="color: grey">
Python
recipe 474083
by <a href="/recipes/users/2790517/">Jitender Cheema</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
</p>
<h4>1. sys.arg has the script name</h4>
<h4>2. Although there can be many ways, e.g. os.cwd() but, there is another</h4>
<h4>trick to</h4>
<h4>obtain the Complete URI or Location of the current script.</h4>
<h4>You can argue, os.getcwd()</h4>
<h4>import can give you working directory of the current script</h4>
<h4>Third party Java code calling your script.... ;)</h4>
<p>#</p>
Looping through multiple lists (Python)
2001-06-29T16:53:42-07:00andy mckayhttp://code.activestate.com/recipes/users/92886/http://code.activestate.com/recipes/65285-looping-through-multiple-lists/
<p style="color: grey">
Python
recipe 65285
by <a href="/recipes/users/92886/">andy mckay</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 2.
</p>
<p>Often you need to loop through every item of multiple lists and compare them. This can be done without a using a counter.</p>
Frame with border color for Tkinter (Python)
2017-04-17T11:25:02-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580735-frame-with-border-color-for-tkinter/
<p style="color: grey">
Python
recipe 580735
by <a href="/recipes/users/4189907/">Miguel Martínez López</a>
(<a href="/recipes/tags/border/">border</a>, <a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/frame/">frame</a>, <a href="/recipes/tags/tkinter/">tkinter</a>).
</p>
<p>This trick show how to add a border color to frame. These are the important configurations:</p>
<pre class="prettyprint"><code> highlightbackground="your border color here"
highlightcolor="your border color here"
highlightthickness="the border width"
bd= 0
</code></pre>
Diff Two Dictionaries (Python)
2009-02-05T08:35:07-08:00Michael Shepanskihttp://code.activestate.com/recipes/users/4169100/http://code.activestate.com/recipes/576644-diff-two-dictionaries/
<p style="color: grey">
Python
recipe 576644
by <a href="/recipes/users/4169100/">Michael Shepanski</a>
(<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/diff/">diff</a>, <a href="/recipes/tags/difference/">difference</a>).
</p>
<p>Diff two dictionaries returning just the differences. If an item is not found, it is represented by the string "<KEYNOTFOUND>". If there is a better way, please share. :)</p>
Check that a string represents an integer number (Python)
2004-09-05T06:50:04-07:00Francesco Ricciardihttp://code.activestate.com/recipes/users/2020988/http://code.activestate.com/recipes/303495-check-that-a-string-represents-an-integer-number/
<p style="color: grey">
Python
recipe 303495
by <a href="/recipes/users/2020988/">Francesco Ricciardi</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>Many times I needed to check if a string represented a number or not. This is a very short recipe that uses Python int() function to do the check, instead of looping on the single characters.</p>
Storing binary data in SQLite (Python)
2003-12-13T20:37:16-08:00John Barhamhttp://code.activestate.com/recipes/users/305009/http://code.activestate.com/recipes/252531-storing-binary-data-in-sqlite/
<p style="color: grey">
Python
recipe 252531
by <a href="/recipes/users/305009/">John Barham</a>
(<a href="/recipes/tags/database/">database</a>).
</p>
<p>This script demonstrates how to store binary data (aka BLOB or BINARY fields) in SQLite using the PySQLite extension.</p>
Generate SQL for insertation into table from dictionary object (Python)
2005-11-26T18:28:53-08:00Carson Reynoldshttp://code.activestate.com/recipes/users/2680336/http://code.activestate.com/recipes/457661-generate-sql-for-insertation-into-table-from-dicti/
<p style="color: grey">
Python
recipe 457661
by <a href="/recipes/users/2680336/">Carson Reynolds</a>
(<a href="/recipes/tags/database/">database</a>).
Revision 2.
</p>
<p>This recipe shows you how you might generate SQL code to insert the key-value pairs in a dictionary. The parameter table corresponds to an existing SQL table and dictionary keys correspond to the table's SQL column names. The SQL generated can then be inserted into a database (in this case MySQL) in the manner shown in exampleOfUse().</p>
Generate a salt (PHP)
2012-07-10T19:52:17-07:00Xavier L.http://code.activestate.com/recipes/users/4171602/http://code.activestate.com/recipes/576894-generate-a-salt/
<p style="color: grey">
PHP
recipe 576894
by <a href="/recipes/users/4171602/">Xavier L.</a>
(<a href="/recipes/tags/crypt/">crypt</a>, <a href="/recipes/tags/generation/">generation</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/hash/">hash</a>, <a href="/recipes/tags/pass/">pass</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/salt/">salt</a>, <a href="/recipes/tags/secure/">secure</a>, <a href="/recipes/tags/security/">security</a>, <a href="/recipes/tags/string/">string</a>).
Revision 6.
</p>
<p>This function will generate a salt for use with passwords ranging using characters in range a to z, A to Z, 0 to 9 and !@#$%&<em>?. The characters are sorted in a random value and can appear more than one time in the string. This way, this function is more powerful than the *shuffle()</em> function. This means that the salt could also be longer than the character list.</p>
Just For Fun: Quicksort in 3 Lines (Python)
2001-08-07T20:58:18-07:00Nathan Grayhttp://code.activestate.com/recipes/users/123436/http://code.activestate.com/recipes/66473-just-for-fun-quicksort-in-3-lines/
<p style="color: grey">
Python
recipe 66473
by <a href="/recipes/users/123436/">Nathan Gray</a>
(<a href="/recipes/tags/search/">search</a>).
Revision 4.
</p>
<p>OK, 4 lines if you count wrapped lines. :^) This is a rather naive implementation of quicksort that illustrates the expressive power of list comprehensions. DO NOT USE THIS IN REAL CODE!</p>
<p>NOTE: Due to an annoyance in the ASPN submission filters you must manually remove the space after the '\' character in the third line if you intend to use the code. Otherwise you will get a syntax error.</p>
Function Timeout (Python)
2007-10-19T22:33:18-07:00Symon Polleyhttp://code.activestate.com/recipes/users/4056524/http://code.activestate.com/recipes/534115-function-timeout/
<p style="color: grey">
Python
recipe 534115
by <a href="/recipes/users/4056524/">Symon Polley</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>a simple and useful script that uses signal to time out any function.</p>
<p>EDIT: use Jim Carrolls solution below</p>
min/max "peaks" with Numeric (Python)
2006-03-27T02:12:32-08:00Maxim Krikunhttp://code.activestate.com/recipes/users/1085177/http://code.activestate.com/recipes/475188-minmax-peaks-with-numeric/
<p style="color: grey">
Python
recipe 475188
by <a href="/recipes/users/1085177/">Maxim Krikun</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>Given a large one-dimensional array, break it into blocks of contstant length and compute min and max for each block, the so-called "peaks data".</p>
Bifurcation diagram of logistic map (Python)
2010-07-24T02:40:54-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577332-bifurcation-diagram-of-logistic-map/
<p style="color: grey">
Python
recipe 577332
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>).
Revision 2.
</p>
<p>Bifurcation diagram of the logistic map.</p>
various Windows Script Host (WSH) examples converted to Python (Python)
2001-06-08T10:49:34-07:00Kevin Altishttp://code.activestate.com/recipes/users/98071/http://code.activestate.com/recipes/65108-various-windows-script-host-wsh-examples-converted/
<p style="color: grey">
Python
recipe 65108
by <a href="/recipes/users/98071/">Kevin Altis</a>
.
</p>
<p>various Windows Script Host (WSH) examples converted to Python. The originals are at:
<a href="http://msdn.microsoft.com/scripting/default.htm?/scripting/windowshost/doc/wsMthRun.htm" rel="nofollow">http://msdn.microsoft.com/scripting/default.htm?/scripting/windowshost/doc/wsMthRun.htm</a>
<a href="http://msdn.microsoft.com/scripting/windowshost/doc/wsproenvironment.htm" rel="nofollow">http://msdn.microsoft.com/scripting/windowshost/doc/wsproenvironment.htm</a>
<a href="http://msdn.microsoft.com/scripting/default.htm?/scripting/windowshost/doc/wsMthSendKeys.htm" rel="nofollow">http://msdn.microsoft.com/scripting/default.htm?/scripting/windowshost/doc/wsMthSendKeys.htm</a></p>
<p>Note that this script is a Python program that utilizes WSH, so use a .py extension rather than .pys.</p>
Creating array into batch (Batch)
2012-11-01T18:35:05-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578314-creating-array-into-batch/
<p style="color: grey">
Batch
recipe 578314
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/array/">array</a>).
Revision 2.
</p>
<p>Command language supports creation array. For example:</p>
Prime Number Generator (Python)
2009-02-03T15:47:06-08:00dthhttp://code.activestate.com/recipes/users/4169078/http://code.activestate.com/recipes/576640-prime-number-generator/
<p style="color: grey">
Python
recipe 576640
by <a href="/recipes/users/4169078/">dth</a>
(<a href="/recipes/tags/eratosthene/">eratosthene</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/number/">number</a>, <a href="/recipes/tags/of/">of</a>, <a href="/recipes/tags/prime/">prime</a>, <a href="/recipes/tags/sieve/">sieve</a>).
Revision 7.
</p>
<p>Generate all prime numbers up to n.</p>
Farey Sequence (Python)
2006-06-24T12:38:55-07:00James Kassemihttp://code.activestate.com/recipes/users/2916378/http://code.activestate.com/recipes/496821-farey-sequence/
<p style="color: grey">
Python
recipe 496821
by <a href="/recipes/users/2916378/">James Kassemi</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>Function provides farey sequence, F(n), for any integer n.</p>
Android Gmail: export messages from SQLite database blobs (Python)
2013-03-22T11:45:11-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/578106-android-gmail-export-messages-from-sqlite-database/
<p style="color: grey">
Python
recipe 578106
by <a href="/recipes/users/4170754/">ccpizza</a>
(<a href="/recipes/tags/android/">android</a>, <a href="/recipes/tags/sqlite/">sqlite</a>).
Revision 5.
</p>
<p>This script will extract email message bodies from the SQLite database stored in an android phone.</p>
<p>The gmail database is typically located on your phone under the following location:</p>
<pre class="prettyprint"><code>`\data\data\com.google.android.gm\databases\mailstore.YOURUSERNAME@gmail.com.db`
</code></pre>
<p>To use the script, copy the file above from your phone to your machine and rename it to <code>gmail.db</code>.</p>
<p><em>NOTE:</em> You need a rooted phone in order to get access to the folder above.</p>
XML Directory Tree (Python)
2004-09-28T19:12:23-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/305313-xml-directory-tree/
<p style="color: grey">
Python
recipe 305313
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 4.
</p>
<p>Builds a directory tree in XML format. Demonstrates how to use xml.dom.minidom to rapidly construct nested XML with multiple element types and attributes. Serves as a model for building model complex documents.</p>
getShortPathName (Python)
2004-06-30T14:11:00-07:00Ruben Marquezhttp://code.activestate.com/recipes/users/1901354/http://code.activestate.com/recipes/286179-getshortpathname/
<p style="color: grey">
Python
recipe 286179
by <a href="/recipes/users/1901354/">Ruben Marquez</a>
.
</p>
<p>In Windows it is often useful to get the "Short Name" version (8.3) of a file or directory path. The Windows api does not provide a function that one can call for this.</p>
Wrap a string in a file-like object that calls a user callback whenever read() is called on the stream (Python)
2013-09-22T21:48:03-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/578669-wrap-a-string-in-a-file-like-object-that-calls-a-u/
<p style="color: grey">
Python
recipe 578669
by <a href="/recipes/users/4170919/">Ben Hoyt</a>
(<a href="/recipes/tags/callback/">callback</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/progress/">progress</a>, <a href="/recipes/tags/upload/">upload</a>).
</p>
<p>Wraps a string in a read-only file-like object, but also calls a user callback with the number of bytes read whenever <code>read()</code> is called on the stream. Used for tracking upload progress, for example for a progress bar in a UI application.</p>