Popular recipes tagged "meta:loc=83"http://code.activestate.com/recipes/tags/meta:loc=83/2017-01-24T20:23:45-08:00ActiveState Code RecipesTkinter remote debugging (Python) 2017-01-24T20:23:45-08:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580721-tkinter-remote-debugging/ <p style="color: grey"> Python recipe 580721 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/remote/">remote</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 6. </p> <p>This trick requires rpyc.</p> <p>You can install rpyc typing:</p> <blockquote> <p>pip install rpyc</p> </blockquote> <p>Run the code below and in another interpreter write:</p> <pre class="prettyprint"><code>import rpyc c = rpyc.classic.connect("localhost") c.execute("from Tkinter import Label; label=Label(app, text='a label')") c.execute("label.pack()") app = c.eval("app") app.responsive_button.invoke() </code></pre> Generate Sphinx table (Python) 2015-05-19T07:02:20-07:00Nicolas Laurancehttp://code.activestate.com/recipes/users/4192224/http://code.activestate.com/recipes/579054-generate-sphinx-table/ <p style="color: grey"> Python recipe 579054 by <a href="/recipes/users/4192224/">Nicolas Laurance</a> (<a href="/recipes/tags/rest/">rest</a>, <a href="/recipes/tags/sphinx/">sphinx</a>). </p> <p>This function will take a list of tuples and transform it into a list of string representing the data as a ReST Sphinx table.</p> Objectify of a XML node (Python) 2014-12-05T04:39:51-08:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/578976-objectify-of-a-xml-node/ <p style="color: grey"> Python recipe 578976 by <a href="/recipes/users/4174477/">Thomas Lehmann</a> (<a href="/recipes/tags/objectify/">objectify</a>, <a href="/recipes/tags/xml/">xml</a>). </p> <p>The script allows you to convert a XML node into an object instance that has the XML node attributes as fields with the given values. The values are converted (when possible):</p> <ul> <li>from string to int</li> <li>from string to float</li> <li>or remain as string</li> </ul> <p>You also initially can provide a dictionary of (key,value) to ensure existence of certain fields which might not be provided by the XML node but on the other hand those values might be overwritten by the XML node.</p> <p>Please have a look at the docstring for the example ...</p> Windows directory walk using ctypes (Python) 2013-08-09T00:17:00-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/578629-windows-directory-walk-using-ctypes/ <p style="color: grey"> Python recipe 578629 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/system/">system</a>). </p> <p>Windows shell/explorer has a limit size of full path, but both NTFS and ReFS can support full path longer than the limit; this is making os.walk on Windows bad if files are in deeply nested folders, and therefore this recipe.</p> String Matching using a Finit State Machine (Python) 2012-06-07T06:03:52-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/578161-string-matching-using-a-finit-state-machine/ <p style="color: grey"> Python recipe 578161 by <a href="/recipes/users/4174931/">Filippo Squillace</a> . </p> <p>This module executes the string matching between an input sequence T and a pattern P using a Finite State Machine. The complexity for building the transition function is O(m^3 x |A|) where A is the alphabet. Since the string matching function scan the input sequence only once, the total complexity is O(n + m^3 x |A|)</p> Dirt simple map/reduce (Python) 2011-05-15T16:46:55-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577676-dirt-simple-mapreduce/ <p style="color: grey"> Python recipe 577676 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/analysis/">analysis</a>, <a href="/recipes/tags/crosstab/">crosstab</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/map_reduce/">map_reduce</a>, <a href="/recipes/tags/pivot_table/">pivot_table</a>, <a href="/recipes/tags/statistics/">statistics</a>). Revision 9. </p> <p>Simple tool for analyzing datasets.</p> DistCC 'top' (Python) 2013-12-01T11:19:56-08:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/577933-distcc-top/ <p style="color: grey"> Python recipe 577933 by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a> (<a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/curses/">curses</a>, <a href="/recipes/tags/distcc/">distcc</a>, <a href="/recipes/tags/distributed/">distributed</a>, <a href="/recipes/tags/monitor/">monitor</a>, <a href="/recipes/tags/pack/">pack</a>, <a href="/recipes/tags/struct/">struct</a>, <a href="/recipes/tags/top/">top</a>, <a href="/recipes/tags/unpack/">unpack</a>). Revision 3. </p> <p>A small recipe for a curses based, 'top'-like monitor for DistCC. I know there is already distccmon-text, but I don't like it, and much prefer this sytle of monitoring. Note that I don't keep hosts around in the list like distccmon-gui/gnome. The screen is drawn for exactly what is currently in state. The terminal size is respected at initialization time, however resize events aren't handled. There is color designation of job types.</p> timeout decorator (with multiprocessing) (Python) 2011-08-23T05:25:09-07:00matt harrisonhttp://code.activestate.com/recipes/users/4179061/http://code.activestate.com/recipes/577853-timeout-decorator-with-multiprocessing/ <p style="color: grey"> Python recipe 577853 by <a href="/recipes/users/4179061/">matt harrison</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/multiprocessing/">multiprocessing</a>, <a href="/recipes/tags/timeout/">timeout</a>). </p> <p>Given that threads shouldn't be killed, multiprocessing is one mechanism for limiting time on a potentially long running computation.</p> And Now For Something COMPLETELY Different Using Text Mode Python... (Python) 2011-10-26T19:24:32-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/577924-and-now-for-something-completely-different-using-t/ <p style="color: grey"> Python recipe 577924 by <a href="/recipes/users/4177147/">Barry Walker</a> (<a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/sound/">sound</a>). </p> <p>Hi experts...</p> <p>A kids level project to do for yourselves...</p> <p>This is a Python DEMO to show the power of the sound card using Linux for a specific usage that I need(ed). It is a kids level project that uses the sound card as a variable DC Voltage reference for projects like stabilised linear PSUs. Why linear? Relatively clean RF wise...</p> <p>The TEST circuit is inside the code and is SOOO simple a dexterous 10 year old could make it in less than an hour and have it up and running to start using...</p> <p>My own circuit is just as simple, isolated, and, gives me MUCH more voltage range than the one inside the code... How did I do it? ;o)</p> <p>This is for Python 2.x.x, (probably even down to 1.5.2) but it would be just as easy to make it work on 3.x.x. I'll let the big guns do that...</p> <p>Enjoy finding simple solutions to often VERY difficult problems...</p> <p>Be aware of word wrapping etc...</p> <p>Bazza, G0LCU...</p> Faster prime generator (C++) 2011-10-08T23:26:24-07:00Mathijs Romanshttp://code.activestate.com/recipes/users/4179530/http://code.activestate.com/recipes/577899-faster-prime-generator/ <p style="color: grey"> C++ recipe 577899 by <a href="/recipes/users/4179530/">Mathijs Romans</a> (<a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/prime_generator/">prime_generator</a>). </p> <p>The sieve of Eratosthenes implemented in C++. I noticed <a href="http://code.activestate.com/recipes/576559-fast-prime-generator/">another recipe</a> that could be improved upon, making it faster and a bit prettier.</p> Auto Generate Simple SQL statements (Python) 2011-08-01T06:53:32-07:00MirGuesthttp://code.activestate.com/recipes/users/4176547/http://code.activestate.com/recipes/577605-auto-generate-simple-sql-statements/ <p style="color: grey"> Python recipe 577605 by <a href="/recipes/users/4176547/">MirGuest</a> . Revision 2. </p> <p>generate insert , update , and select</p> Using SQLite as an embedded database with C# instantly (Text) 2010-12-15T01:24:06-08:00Stephen Akikihttp://code.activestate.com/recipes/users/4172143/http://code.activestate.com/recipes/577500-using-sqlite-as-an-embedded-database-with-c-instan/ <p style="color: grey"> Text recipe 577500 by <a href="/recipes/users/4172143/">Stephen Akiki</a> (<a href="/recipes/tags/akiscode/">akiscode</a>, <a href="/recipes/tags/csharp/">csharp</a>, <a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/embedded/">embedded</a>, <a href="/recipes/tags/sqlite/">sqlite</a>). </p> <p><a href="http://akiscode.com/articles/sqlite-csharp.shtml" rel="nofollow">http://akiscode.com/articles/sqlite-csharp.shtml</a></p> <p>There are a few libaries that every programmer loves and knows because they either:</p> <ul> <li>Saves you a ton of time coding</li> <li>Does something that you don't know how to do yourself</li> </ul> <p>Examples include cURL, SQLite, or even the .NET library itself. Many of us have to create CRUD (Create, Read, Update, Delete) projects on a semi-regular routine. SQLite is nice in the fact that it handles all the CRUD-y stuff (bad pun I know) in a simple, small and standalone package. But because SQLite is so nice to use (when compared to say XML), the Gods have cursed SQLite to cause many headaches if you are a new to it on C# to balance the world karma (or something like that). Also note that i'm not going to explain what any of the code does as its pretty self explanatory. Anyway, here is a complete example on how to setup an embedded SQLite database, insert some data into it, and then update a listview with only certain columns from the database:</p> <p><strong>The Fun Stuff</strong></p> <ol> <li><p><em>*NOTE: I've made a demo project that you can download <a href="http://akiscode.com/articles/Akiscode-SQLiteTest.zip">here</a>. It includes the DLLs and everything that i've talked about below. *</em></p></li> <li><p>Download the setup from <a href="http://sqlite.phxsoftware.com/">these guys</a> and install it.</p></li> <li><p>Start a new Project in Visual C# Studio (I'm using version 2010 Express). Make it a Windows Forms Application. Save it anywhere. For this example, I will save it on the Desktop and call the project "Akiscode-SQLiteTest. At the time I was doing this, the SQLite dll only worked with a program targeted at .NET version 3.5. To learn how to change that go <a href="http://msdn.microsoft.com/en-us/library/bb772098(v=vs.90).aspx">here</a>.</p></li> <li><p>Go to the Solutions Explorer on the top right of your VC setup. Right click references and select "Add Reference". Go to the ".NET" tab and find the entry named "System.Data.SQLite". Not "System.Data.Sql", not "System.Data.SqlXml", NOT "MySql.Data". Just find "System.Data.SQLite". If you can't find it, sort the list by name. If you still can't find it, restart VC and try this process again. Still can't find it? Run the installer and do it all over again. Still not there? Well I can't really help you, hand in your programming badge on the way out.</p></li> <li><p>Once you added "System.Data.SQLite", right click it and select properties. Set "Copy Local" to true.</p></li> <li><p>Ok type at the top "using "using System.Data.SQLite;" without the quotes.</p></li> <li><p>Go back to the form designer. Add a listview component. In the properties window (bottom right) make sure the property view is set to "Details". Then right click on the listview and add columns. For this example I added two called "First Name" and "Last Name"</p></li> </ol> <p>8.Ok now for some copy and paste coding (my favorite): Double click the form to make a "Form1_Load" function appear and paste the following in it:</p> Embed lyrics into MP3 files using mutagen (USLT tag), optionally set other ID3 tags (Python) 2011-05-17T15:56:56-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/577138-embed-lyrics-into-mp3-files-using-mutagen-uslt-tag/ <p style="color: grey"> Python recipe 577138 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/id3/">id3</a>, <a href="/recipes/tags/lyrics/">lyrics</a>, <a href="/recipes/tags/mp3/">mp3</a>, <a href="/recipes/tags/uslt/">uslt</a>). Revision 5. </p> <p>Quick and dirty script to embed unsynchronized lyrics or any other text into MP3 files. The text files with the lyrics are expected to be in the same folder: i.e. for MySong.mp3 the lyrics text should be in the file MySong.txt.</p> <p>The encoding of the text file will be probed in the following order: 'utf8','iso-8859-1','iso-8859-15','cp1252','cp1251','latin1'. If you need support for more encodings, a list is available at <a href="http://docs.python.org/release/2.5.2/lib/standard-encodings.html" rel="nofollow">http://docs.python.org/release/2.5.2/lib/standard-encodings.html</a></p> <p>To see the lyrics on an iPod (tested on 6G Classic) you need to press the middle button four times while a song is playing.</p> <p>The script can also be used to set other ID3 tags. By default SET_OTHER_ID3_TAGS is False so existing ID3 tags will NOT be overwritten.</p> <p>Usage: Running the file without arguments will process all MP3 files in the current directory.</p> <p>Alternatively the path to the folder with MP3's can be passed as the first argument.</p> Apparent Infection Rate Calculation (Python) 2010-04-06T07:28:08-07:00donyo Ganchevhttp://code.activestate.com/recipes/users/4167704/http://code.activestate.com/recipes/577182-apparent-infection-rate-calculation/ <p style="color: grey"> Python recipe 577182 by <a href="/recipes/users/4167704/">donyo Ganchev</a> . </p> <p>calculate apparent infection rate</p> num2words (Python) 2012-11-28T19:41:07-08:00khttp://code.activestate.com/recipes/users/4168241/http://code.activestate.com/recipes/576577-num2words/ <p style="color: grey"> Python recipe 576577 by <a href="/recipes/users/4168241/">k</a> (<a href="/recipes/tags/num2words/">num2words</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/numerals/">numerals</a>). Revision 3. </p> <p>A simple python script that translates an integer to plain English.</p> Enhanced Complex Number Type (Python) 2008-01-05T17:45:16-08:00cesar oterohttp://code.activestate.com/recipes/users/2790742/http://code.activestate.com/recipes/541102-enhanced-complex-number-type/ <p style="color: grey"> Python recipe 541102 by <a href="/recipes/users/2790742/">cesar otero</a> . </p> <p>Python offers a powerful data type for complex numbers in Cartesian form. Unfortunately, python does not offer support for complex numbers in polar form. This recipe contains a class that supports complex numbers in both Cartesian and polar form, and allows for arithmetic that mixes both forms.</p> paper - rock - scissors game (Python) 2007-08-26T11:34:01-07:00dewi robertshttp://code.activestate.com/recipes/users/4078153/http://code.activestate.com/recipes/528898-paper-rock-scissors-game/ <p style="color: grey"> Python recipe 528898 by <a href="/recipes/users/4078153/">dewi roberts</a> . </p> <p>best of five against the computer</p> Qt Event Processing & Python Threads (Python) 2005-09-10T23:15:15-07:00Jonathan Kolyerhttp://code.activestate.com/recipes/users/452108/http://code.activestate.com/recipes/415311-qt-event-processing-python-threads/ <p style="color: grey"> Python recipe 415311 by <a href="/recipes/users/452108/">Jonathan Kolyer</a> (<a href="/recipes/tags/threads/">threads</a>). Revision 5. </p> <p>This class forms a bridge between the main Qt event loop and python-based threads. This was a thorny problem to figure out, so I'm posting for others to benefit. Basically, you need to invoke all Qt GUI calls and (ActiveX calls on Windows) from the main Qt thread. But if you're using Python threads, you need to manage the interaction yourself.</p> Mapping arbitrary objects to a PostgreSQL database with psycopg2 (Python) 2004-09-10T12:17:53-07:00Valentino Volonghihttp://code.activestate.com/recipes/users/199559/http://code.activestate.com/recipes/304223-mapping-arbitrary-objects-to-a-postgresql-database/ <p style="color: grey"> Python recipe 304223 by <a href="/recipes/users/199559/">Valentino Volonghi</a> (<a href="/recipes/tags/database/">database</a>). </p> <p>You need to store arbitrary objects in a PostgreSQL database without being intrusive for your classes (don't want inheritance from an 'Item' or 'Persistent' object).</p> Grab a document with images from the web (Python) 2004-08-31T02:39:59-07:00Yuriy Tkachenkohttp://code.activestate.com/recipes/users/2011468/http://code.activestate.com/recipes/302700-grab-a-document-with-images-from-the-web/ <p style="color: grey"> Python recipe 302700 by <a href="/recipes/users/2011468/">Yuriy Tkachenko</a> (<a href="/recipes/tags/web/">web</a>). </p> <p>This is a way to grab a web page containing images and save this page and selected images to the same directory.</p>