Top-rated recipes tagged "meta:loc=21"http://code.activestate.com/recipes/tags/meta:loc=21/top/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>
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>
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>
ctypes CDLL with automatic errno checking (Python)
2017-01-03T10:31:26-08:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/580741-ctypes-cdll-with-automatic-errno-checking/
<p style="color: grey">
Python
recipe 580741
by <a href="/recipes/users/2033964/">Oren Tirosh</a>
.
</p>
<p>This class extends ctypes.CDLL with automatic checking of errno and automatically raises an exception when set by the function.</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>
subprocess.terminate() not always implemented (Python)
2009-02-25T10:45:01-08:00Gui Rhttp://code.activestate.com/recipes/users/4166241/http://code.activestate.com/recipes/576667-subprocessterminate-not-always-implemented/
<p style="color: grey">
Python
recipe 576667
by <a href="/recipes/users/4166241/">Gui R</a>
(<a href="/recipes/tags/process_management/">process_management</a>, <a href="/recipes/tags/subprocess/">subprocess</a>, <a href="/recipes/tags/terminate/">terminate</a>).
Revision 2.
</p>
<p>The new subprocess module brings clarity and simplicity over the popenXX() functions and os.spawnXX() functions, but some implementations don't have the subprocess.terminate() method, which is crucial for killing a spawned process. This workaround works on both POSIX and Windows.</p>
String representation of a dict (sorted by key) (Python)
2008-06-16T13:37:45-07:00Jacob Oscarsonhttp://code.activestate.com/recipes/users/1355144/http://code.activestate.com/recipes/573453-string-representation-of-a-dict-sorted-by-key/
<p style="color: grey">
Python
recipe 573453
by <a href="/recipes/users/1355144/">Jacob Oscarson</a>
.
</p>
<p>I quite often find a need to create a 'key-value' pair representation of a
dictionary as a string, mainly using generators and the .join method of
string objects</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>
Prototype pattern (Python)
2001-11-03T06:51:26-08:00Andres Tuellshttp://code.activestate.com/recipes/users/139364/http://code.activestate.com/recipes/86651-prototype-pattern/
<p style="color: grey">
Python
recipe 86651
by <a href="/recipes/users/139364/">Andres Tuells</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>This is an implementation of the prototype pattern. In the prototype pattern you create a new instance cloning from other.</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>
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>
A minimal debugger (Tcl)
2001-06-21T16:47:46-07:00Jeff Hobbshttp://code.activestate.com/recipes/users/98167/http://code.activestate.com/recipes/65431-a-minimal-debugger/
<p style="color: grey">
Tcl
recipe 65431
by <a href="/recipes/users/98167/">Jeff Hobbs</a>
(<a href="/recipes/tags/debugging/">debugging</a>).
</p>
<p>Of course, Tcl's most minimal debugger is puts. But here is a cute
little piece of code that offers some more debugging functionality (if
you have stdin and stdout available - so not for wish on Windows)</p>
Using ctypes to call C code from Python (Python)
2016-05-09T18:00:44-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580660-using-ctypes-to-call-c-code-from-python/
<p style="color: grey">
Python
recipe 580660
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/ffi/">ffi</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/time/">time</a>).
</p>
<p>This recipe shows basic usage of the ctypes module to call C code from Python code.</p>
Recursively dumping the structure of an HTML5 document (Python)
2015-02-09T20:09:30-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579022-recursively-dumping-the-structure-of-an-html5-docu/
<p style="color: grey">
Python
recipe 579022
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
.
</p>
<p>This recipe shows how to dump the structure of an HTML5 document, using the html5lib Python library with recursion.</p>
Numbers as Ranges - iterable integers (C++)
2014-10-02T12:13:52-07:00elazarhttp://code.activestate.com/recipes/users/4187847/http://code.activestate.com/recipes/578945-numbers-as-ranges-iterable-integers/
<p style="color: grey">
C++
recipe 578945
by <a href="/recipes/users/4187847/">elazar</a>
(<a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/range/">range</a>).
Revision 2.
</p>
<p>This header file makes simple integers iterable.
Note that it works only on C++11 or above.</p>
<p>Usage:</p>
<pre class="prettyprint"><code>#include "num_range.h"
for (int i : 3) cout << i << endl;
</code></pre>
<p>Output:
0
1
2</p>
<p>Implementation note:
This code is far too generic. We only need <code>DerefableInt</code>.
The templates are there for no practical reason.</p>
<p>Cons:
pollutes namespace std;
nonstandard idiom;</p>
Prime Factorization (Python)
2014-09-19T08:11:54-07:00juanhttp://code.activestate.com/recipes/users/4190606/http://code.activestate.com/recipes/578938-prime-factorization/
<p style="color: grey">
Python
recipe 578938
by <a href="/recipes/users/4190606/">juan</a>
.
</p>
<p>Calculates the prime factorization from a number.</p>
Dice (Python)
2014-06-12T22:03:16-07:00Drew Stanly Kolukhttp://code.activestate.com/recipes/users/4190188/http://code.activestate.com/recipes/578890-dice/
<p style="color: grey">
Python
recipe 578890
by <a href="/recipes/users/4190188/">Drew Stanly Koluk</a>
(<a href="/recipes/tags/dice/">dice</a>).
Revision 2.
</p>
<p>this is a small program i wrote. it has 2 dice that go from 1 to 12 if you would like another program with only 1 die or the dice only go from 1 to 6 anything like that feel free to email me or just change it yourself(if u can)</p>
<p>email:shogundrew@hotmail.com </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>
Shuffle (Python)
2013-02-19T11:01:01-08:00Narayana Chikkamhttp://code.activestate.com/recipes/users/4174427/http://code.activestate.com/recipes/578466-shuffle/
<p style="color: grey">
Python
recipe 578466
by <a href="/recipes/users/4174427/">Narayana Chikkam</a>
(<a href="/recipes/tags/shuffle/">shuffle</a>).
</p>
<p>Python function to shuffle a deck of cards</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>