Popular recipes tagged "meta:loc=96"http://code.activestate.com/recipes/tags/meta:loc=96/2015-12-18T18:15:18-08:00ActiveState Code RecipesSimple breadth-first, depth-first tree traversal (Python) 2015-12-18T18:15:18-08:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/579138-simple-breadth-first-depth-first-tree-traversal/ <p style="color: grey"> Python recipe 579138 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/tree/">tree</a>). </p> <p>When you want to avoid recursion with a tree, you read the tree nodes into a stack, which is organized either breadth-first or depth-first.</p> <p>Here are two dead simple routines for doing so. Most of the recipe is just a test bed for those functions.</p> Accordion Widget (Tkinter) (Python) 2014-07-18T11:39:51-07:00Peter Mojeikohttp://code.activestate.com/recipes/users/4190241/http://code.activestate.com/recipes/578911-accordion-widget-tkinter/ <p style="color: grey"> Python recipe 578911 by <a href="/recipes/users/4190241/">Peter Mojeiko</a> (<a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). </p> <p>An expanding/collapsing widget for Tkinter.</p> <p>usage:</p> <pre class="prettyprint"><code>from accordion import Accordion, Chord root = Tk() accordion = Accordion(root) accordion.pack() chord1 = Chord(accordion, title='Chord') # create widgets and give them chord1 as parent accordion.append_chords([chord1]) root.mainloop() </code></pre> <p>There's a more detailed example at the bottom of the file.</p> Convert Microsoft Word files to PDF with DOCXtoPDF (Python) 2013-12-24T23:03:43-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578795-convert-microsoft-word-files-to-pdf-with-docxtopdf/ <p style="color: grey"> Python recipe 578795 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/docx/">docx</a>, <a href="/recipes/tags/microsoft/">microsoft</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/word/">word</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>Yhis recipe shows how to convert Microsoft Word .DOCX files to PDF format, using the python-docx library and my xtopdf toolkit for PDF creation.</p> <p>Note: The recipe has some limitations. E.g. fonts, tables, etc. from the input DOCX file are not preserved in the output PDF file.</p> MultiListbox Tkinter widget (Python) 2014-01-03T19:23:48-08:00Juan Ramirezhttp://code.activestate.com/recipes/users/4174026/http://code.activestate.com/recipes/578805-multilistbox-tkinter-widget/ <p style="color: grey"> Python recipe 578805 by <a href="/recipes/users/4174026/">Juan Ramirez</a> (<a href="/recipes/tags/ui/">ui</a>). </p> <p>This is a compound widget that gangs multiple Tk Listboxes to a single scrollbar to achieve a simple multi-column scrolled listbox. Most of the Listbox API is mirrored to make it act like the normal Listbox but with multiple values per row.</p> A Shell, Binary To Hexadecimal To Decimal Demo... (Bash) 2013-01-11T18:31:03-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578413-a-shell-binary-to-hexadecimal-to-decimal-demo/ <p style="color: grey"> Bash recipe 578413 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/binary/">binary</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/hexadecimal/">hexadecimal</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/terminal/">terminal</a>). </p> <p>This little DEMO may be common knowledge to the big guns but not to amateurs like myself, so......</p> <p>This is an Apple Macbbok Shell/Terminal DEMO shell script to show how to:-</p> <p>1) Create a binary file... 2) Save it to your DEFAULT /directory/drwawer/folder/... 3) Display a hexadecimal dump of said binary file to prove that it is binary... 4) Select a single BYTE of that file and save it as an ASCII text decimal _number_, also to your DEFAULT /directory/drawer/folder/... 5) Read this ASCII text decimal number back in again... 6) Add this string representation to a number... 7) Stop...</p> <p>It was intended purely for OSX 10.7.5 and above using the default terminal and shell...</p> <p>It does work on many Linux flavours and shells/terminals also however.</p> <p>Written so the anyone can understand what is going on.</p> <p>The two files generated and saved in this DEMO to your DEFAULT /directory/drawer/folder/ are:-</p> <p>BinaryString.dat BinaryString.txt</p> <p>This WILL lead to something very unusual in the not too distant future...</p> <p>This is Public Domain and you may do with it as you wish...</p> <p>Enjoy finding simple solutions to often very difficult problems...</p> <p>Bazza, G0LCU...</p> Print Logger Internals (Python) 2012-12-24T04:11:35-08:00Dave Baileyhttp://code.activestate.com/recipes/users/4168479/http://code.activestate.com/recipes/578389-print-logger-internals/ <p style="color: grey"> Python recipe 578389 by <a href="/recipes/users/4168479/">Dave Bailey</a> (<a href="/recipes/tags/logging/">logging</a>). </p> <p>This helper function simplifies the difficult task of setting up and maintaining a logging system. Changes to a logging system can cause unanticipated consequences such as lost messages or duplicates. Debugging a logging hierarchy can be a tedious task. This function overrides the internal __repr__ functions of the internal classes and allows a print statement to generate the complete logger hierarchy with its associated internals. It allows easy debugging a logger and allows changes to be easily detected.</p> Simple linear regression (Python) 2012-05-12T13:36:55-07:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/578129-simple-linear-regression/ <p style="color: grey"> Python recipe 578129 by <a href="/recipes/users/4174477/">Thomas Lehmann</a> (<a href="/recipes/tags/decrease/">decrease</a>, <a href="/recipes/tags/increase/">increase</a>, <a href="/recipes/tags/linear/">linear</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/percental/">percental</a>, <a href="/recipes/tags/regression/">regression</a>, <a href="/recipes/tags/simple/">simple</a>). Revision 3. </p> <p><strong>What?</strong></p> <ul> <li>It's about forecasting.</li> <li>It's about calculating a linear function.</li> <li><em>Details I found</em>: <a href="http://www.faes.de/Basis/Basis-Statistik/Basis-Statistik-Korrelation-Re/basis-statistik-korrelation-re.html" rel="nofollow">http://www.faes.de/Basis/Basis-Statistik/Basis-Statistik-Korrelation-Re/basis-statistik-korrelation-re.html</a> (in german)</li> </ul> <p>Also I don't know how the formula have been created the practical part was very easy to me. I have verified one example (see code) using Open Office Calc (I've learned: you can display the formula for the trend line as well as the coefficient of correlation - great).</p> <p><strong>Why?</strong></p> <ul> <li>In <a href="http://code.activestate.com/recipes/578111/">recipe 578111</a> I'm printing out current error rate for different training sessions in mental arithmetic.</li> <li>Anyway I would like to be able to given information - approximately - about how many you have improved since you train yourself.</li> </ul> <p><strong>What has changed?</strong></p> <ul> <li><strong>Revision2</strong>: Didn't compile with Jython 2.5.3b1 because of not supported exception syntax. Now it does work without exception.</li> <li><strong>Revision3</strong>: Test data row for failure not removed.</li> </ul> IFS fractals using iteration method (Python) 2012-01-07T18:33:21-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578003-ifs-fractals-using-iteration-method/ <p style="color: grey"> Python recipe 578003 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/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>This method uses inverse IFS transformations (assume new x, new y are known and x, y are unknown and solve the equation), and unlike IFS algorithm, it does not use a random number generator and neither the probability coefficients for each IFS transformation. (Except when IFS algorithm is used in the beginning to find bounding rectangle of the fractal.) </p> Connect PyGTK object events to class methods automatically (Python) 2011-02-19T17:50:42-08:00Pavel Krchttp://code.activestate.com/recipes/users/4177047/http://code.activestate.com/recipes/577577-connect-pygtk-object-events-to-class-methods-autom/ <p style="color: grey"> Python recipe 577577 by <a href="/recipes/users/4177047/">Pavel Krc</a> (<a href="/recipes/tags/automate/">automate</a>, <a href="/recipes/tags/connect/">connect</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/event/">event</a>, <a href="/recipes/tags/pygtk/">pygtk</a>). Revision 2. </p> <p>A module that allows you to not to repeat yourself (DRY) while writing typical PyGTK constructions (an object containing PyGTK widgets with its methods servicing widget events) by calling connect() automatically. See docstring.</p> Concurrent buffer for generators (Python) 2010-05-05T22:47:33-07:00Javier Ruerehttp://code.activestate.com/recipes/users/4172765/http://code.activestate.com/recipes/576999-concurrent-buffer-for-generators/ <p style="color: grey"> Python recipe 576999 by <a href="/recipes/users/4172765/">Javier Ruere</a> (<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/threading/">threading</a>). Revision 2. </p> <p>A buffer that will partially consume an iterator in the background.</p> <p>Very useful for reading files and merging the data using the excellent <a href="http://code.activestate.com/recipes/491285/" rel="nofollow">http://code.activestate.com/recipes/491285/</a></p> Python HTTP Pipelining (Python) 2009-02-27T16:21:23-08:00Markus Jhttp://code.activestate.com/recipes/users/4169350/http://code.activestate.com/recipes/576673-python-http-pipelining/ <p style="color: grey"> Python recipe 576673 by <a href="/recipes/users/4169350/">Markus J</a> (<a href="/recipes/tags/http/">http</a>, <a href="/recipes/tags/pipelining/">pipelining</a>). Revision 5. </p> <p>Gets several pages in parallel, without threads. It exploits HTTP pipelining by resetting the state of HTTPConnection to trick it into sending the next request ahead of time.</p> <p>More information about HTTP pipelining can be found on Wikipedia: <a href="http://en.wikipedia.org/wiki/HTTP_pipelining">http://en.wikipedia.org/wiki/HTTP_pipelining</a></p> How to use twisted pb FilePager (Python) 2009-04-30T05:20:09-07:00alex lavoro.propiohttp://code.activestate.com/recipes/users/4170029/http://code.activestate.com/recipes/576735-how-to-use-twisted-pb-filepager/ <p style="color: grey"> Python recipe 576735 by <a href="/recipes/users/4170029/">alex lavoro.propio</a> (<a href="/recipes/tags/filepager/">filepager</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/pb/">pb</a>, <a href="/recipes/tags/twisted/">twisted</a>). Revision 3. </p> <p>Banana limits string objects to 640k so sending a large file requires to break it into many small chunks. Twisted provide a helper class to do this: twisted.spread.pb.FilePager. I modified on <a href="http://code.activestate.com/recipes/457670/">recipe 457670</a>: "How to use twisted pb pager" to demonstrate the usage of FilePager. getFile1() is similar to getIt() in <a href="http://code.activestate.com/recipes/457670/">recipe 457670</a>. getFile2() uses the utility function in twisted.spread.pb.util.</p> True Lieberman-style delegation in Python. (Python) 2007-05-14T11:14:33-07:00Martin Blaishttp://code.activestate.com/recipes/users/1643324/http://code.activestate.com/recipes/519639-true-lieberman-style-delegation-in-python/ <p style="color: grey"> Python recipe 519639 by <a href="/recipes/users/1643324/">Martin Blais</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>Proxies are usually implemented as objects that forward method calls to a "target" object. This approach has a major problem: forwarding makes the target object the receiver of the method call; this means that calls originating from the body of a method in the target will not go through the proxy (and thus their behavior cannot be modified by the proxy).</p> <p>For example, suppose we want a proxy to an instance of Target (shown below) that is "safe", i.e., does not do anything bad like firing missiles. We can just define a class that forwards calls to the safe methods, namely send_flowers() and hang_out(). This class can have its own version of fire_missiles() that does nothing. Now consider what happens when we call the proxy object's innocent-looking hang_out() method. The call is forwarded to the target object, which in turn calls the target object's (not the proxy's) fire_missiles() method, and BOOM! (The proxy's version of fire_missiles() is not called because forwarding has made the target object the receiver of the new method call.)</p> <p>By using delegation, one can implement proxies without the drawbacks of the method-forwarding approach. This recipe shows how Python's __getattr__ method can be used to implement the kind of delegation present in prototype-based languages like Self and Javascript, and how delegation can be used to implement better proxies.</p> Asynchronous port forwarding (Python) 2006-04-06T19:25:57-07:00Nicolas Lehuenhttp://code.activestate.com/recipes/users/1599156/http://code.activestate.com/recipes/483732-asynchronous-port-forwarding/ <p style="color: grey"> Python recipe 483732 by <a href="/recipes/users/1599156/">Nicolas Lehuen</a> (<a href="/recipes/tags/network/">network</a>). Revision 2. </p> <p>This forward the TCP traffic from your machine to another host, and back in the the other way. It uses asynchronous socket thanks to ye olde asyncore module, which was used by Zope up until recently (they integrated the Twisted reactor). As a consequence, it should be able to handle a great number of connections without crumbling under the weight of many threads.</p> Disk Dumper (Python) 2006-03-20T20:33:47-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/475157-disk-dumper/ <p style="color: grey"> Python recipe 475157 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/programs/">programs</a>). </p> <p>This recipe will display a hex dump of the disk specified on the command line. As the last two arguments, the program takes the first sector and last sector that should be displayed by this utility. The size of the sectors is stored in a variable created right after the imports executed by this script. The main feature of this recipe is its cross OS capabilities as demonstrated in get_data.</p> binary-converter1.0 (Python) 2006-06-04T15:51:33-07:00phinix bsshttp://code.activestate.com/recipes/users/2911482/http://code.activestate.com/recipes/496778-binary-converter10/ <p style="color: grey"> Python recipe 496778 by <a href="/recipes/users/2911482/">phinix bss</a> . </p> <p>This is a python program that can convert bianry number( binary is a language that computers use to comunicate with other) to a normal digital number and the other way around.</p> profiling threads (Python) 2006-01-08T14:01:16-08:00Maciej Obarskihttp://code.activestate.com/recipes/users/2559120/http://code.activestate.com/recipes/465831-profiling-threads/ <p style="color: grey"> Python recipe 465831 by <a href="/recipes/users/2559120/">Maciej Obarski</a> (<a href="/recipes/tags/threads/">threads</a>). Revision 8. </p> <p>This recipe shows how to profile threads in Python by using custom profiler function.</p> ARFF to SQL Database importer (Python) 2007-03-12T06:11:03-07:00Pradeep Kishore Gowdahttp://code.activestate.com/recipes/users/2230395/http://code.activestate.com/recipes/440533-arff-to-sql-database-importer/ <p style="color: grey"> Python recipe 440533 by <a href="/recipes/users/2230395/">Pradeep Kishore Gowda</a> (<a href="/recipes/tags/database/">database</a>). Revision 3. </p> <p>Python script to import ARFF data files (<a href="http://www.cs.waikato.ac.nz/%7Eml/weka/arff.html" rel="nofollow">http://www.cs.waikato.ac.nz/~ml/weka/arff.html</a>) into <em>any</em> database.</p> Set activity log on. (Tcl) 2005-10-18T00:43:31-07:00Patrick Finneganhttp://code.activestate.com/recipes/users/1220635/http://code.activestate.com/recipes/442422-set-activity-log-on/ <p style="color: grey"> Tcl recipe 442422 by <a href="/recipes/users/1220635/">Patrick Finnegan</a> (<a href="/recipes/tags/websphere/">websphere</a>). </p> <p>Set activity log options for appserver.</p> Encrypting A String (Python) 2005-10-02T19:12:29-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/440628-encrypting-a-string/ <p style="color: grey"> Python recipe 440628 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>The main purpose of these functions are to encrypt and decrypt a string (or change the string from one "language" into another). I'm guessing that Python already has something like this, but this is my implementation of the process. The code is simple, but it provides a simple solution to a well-known encryption scheme. As a recommendation, this may be better when used in conjunction with an encoding system (such as the base 256 to base 255 encoded that I submitted).</p>