Popular recipes tagged "meta:loc=97"http://code.activestate.com/recipes/tags/meta:loc=97/2017-04-12T23:25:49-07:00ActiveState Code RecipesPDF Text Extraction using fitz / MuPDF (PyMuPDF) (Python) 2016-03-17T12:00:06-07:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580626-pdf-text-extraction-using-fitz-mupdf-pymupdf/ <p style="color: grey"> Python recipe 580626 by <a href="/recipes/users/4193772/">Jorj X. McKie</a> (<a href="/recipes/tags/cbz/">cbz</a>, <a href="/recipes/tags/epub/">epub</a>, <a href="/recipes/tags/mupdf/">mupdf</a>, <a href="/recipes/tags/openxps/">openxps</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pymupdf/">pymupdf</a>, <a href="/recipes/tags/text_extraction/">text_extraction</a>, <a href="/recipes/tags/xps/">xps</a>). </p> <p>Extract all the text of a PDF (or other supported container types) at very high speed. In general, text pieces of a PDF page are not arranged in natural reading order, but in the order they were entered during PDF creation. This script re-arranges text blocks according to their pixel coordinates to achieve a more readable output, i.e. top-down, left-right.</p> Metro Checkbuttons and Radiobuttons (Python) 2017-04-12T23:25:49-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580783-metro-checkbuttons-and-radiobuttons/ <p style="color: grey"> Python recipe 580783 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/checkbutton/">checkbutton</a>, <a href="/recipes/tags/radiobutton/">radiobutton</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 2. </p> <p>This recipe shows how to create custom checkbuttons and radiobuttons</p> Simple way to find number of perfect square numbers in a range. (Python) 2015-03-05T15:59:30-08:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/579031-simple-way-to-find-number-of-perfect-square-number/ <p style="color: grey"> Python recipe 579031 by <a href="/recipes/users/4166679/">alexander baker</a> (<a href="/recipes/tags/complex/">complex</a>, <a href="/recipes/tags/fun/">fun</a>, <a href="/recipes/tags/interview/">interview</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/range/">range</a>). </p> <p>The strategy here is not to iterate through the set of possible integer values and check for is_perfect_square() each time but to translate the upper and lower values to either complex or real space of square numbers.</p> <pre class="prettyprint"><code> # O(n) complexity len([x for x in range(0, 100) if x!= 0 and float(math.sqrt(x)).is_integer()]) so if a and b positive then we count the number of integer values between upper and lower sqrt() values if either a or b are negative then we need to use the complex number space for the sqrt() results. In this case we are still counting integer values either along the imaginary or real axis, the result is then a simple sum if both a and b are negative we need to make sure that when walking down the same imaginary axis we dont count the same inteters twice, in this case we can take the min(a,b) to get max distinct set of integer values. </code></pre> language decorator (Python) 2013-10-02T08:00:29-07:00Dr. Trigonhttp://code.activestate.com/recipes/users/4188033/http://code.activestate.com/recipes/578677-language-decorator/ <p style="color: grey"> Python recipe 578677 by <a href="/recipes/users/4188033/">Dr. Trigon</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/embedded_language/">embedded_language</a>). </p> <p>Use other languages like lua, C++ or simple bash from python. Instead of writing code as a string and then send it to some eval or execute function, we introduce here decorators that allow to write regular python functions containing the external code as docstring.</p> <p>That way the external code needs minimal adoptions only and becomes better read- and maintainable. Write your function in your favourite language and just place the right decorator in front of it - it simple like that.</p> <p>At the current state this recipe is a DRAFT and needs to be further extended and tested.</p> DEMO Kids Level Python RF Attenuator... (Python) 2013-02-09T10:21:39-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578451-demo-kids-level-python-rf-attenuator/ <p style="color: grey"> Python recipe 578451 by <a href="/recipes/users/4177147/">Barry Walker</a> (<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/attenuator/">attenuator</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/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/rf/">rf</a>, <a href="/recipes/tags/sound/">sound</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>This code is a DEMO RF Attenuator for SW listeners.</p> <p>(The code is to show how to generate the same waveform in each channel but in antiphase, as bridge output. The RF Attenuator is just a byproduct...)</p> <p>The circuit has an insertion loss of about 10dB at full gain but that doesn't matter as it IS an attenuator after all.</p> <p>It is another learning tool for kids to build and code for themselves.</p> <p>The only common user I/O on desktops, netbooks, laptops and current MacBooks is/are the Audio Mic and Ear sockets...</p> <p>This goes for "mobiles" too...</p> <p>The circuit inside the code is so easy a 10 year old could have a working unit inisde 2 hours to experiment with...</p> <p>PyAudio IS needed for this to work and although originally designed around a MacBook Pro 13 inch OSX 10.7.5 and Python 2.6.7 has shown itself to be near platform independent...</p> <p>Read the code for much more information...</p> <p>Enjoy finding simple solutions to often very difficult problems... ;o)</p> <p>Bazza, G0LCU...</p> Human readable format for a given time delta (Python) 2012-04-26T10:37:10-07:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/578113-human-readable-format-for-a-given-time-delta/ <p style="color: grey"> Python recipe 578113 by <a href="/recipes/users/4174477/">Thomas Lehmann</a> (<a href="/recipes/tags/back/">back</a>, <a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/human/">human</a>, <a href="/recipes/tags/readable/">readable</a>, <a href="/recipes/tags/time/">time</a>). </p> <p><strong>What is it about?</strong></p> <ul> <li>I need to say someting like <em>1 day ago</em>, <em>5 days ago</em>, <em>2 weeks ago</em>, ...</li> <li>I can control to have it with/without milliseconds and microseconds.</li> <li>I can use it automatically with current date and time or with a provide one.</li> </ul> <p><strong>Why?</strong></p> <ul> <li>I need it for next revision of my <a href="http://code.activestate.com/recipes/578111/">recipe 578111</a>.</li> <li>I found recipes here and there but often it is always assumed that a month has 30 days and that a year has 365 days; this is not true. That's why I've left away months and years.</li> </ul> Pluralize word -- convert singular word to its plural form (Python) 2011-07-06T21:37:19-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/577781-pluralize-word-convert-singular-word-to-its-plural/ <p style="color: grey"> Python recipe 577781 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/plural/">plural</a>, <a href="/recipes/tags/pluralize/">pluralize</a>, <a href="/recipes/tags/singular/">singular</a>, <a href="/recipes/tags/word/">word</a>). </p> <p>The pluralize() function returns the plural form of the given lowercase singular word (English only). Based on ActiveState recipe <a href="http://code.activestate.com/recipes/413172/" rel="nofollow">http://code.activestate.com/recipes/413172/</a></p> indefinite currying decorator with greedy call and up front args checking (Python) 2011-10-28T22:05:55-07:00Przemyslaw Podczasihttp://code.activestate.com/recipes/users/4179716/http://code.activestate.com/recipes/577928-indefinite-currying-decorator-with-greedy-call-and/ <p style="color: grey"> Python recipe 577928 by <a href="/recipes/users/4179716/">Przemyslaw Podczasi</a> (<a href="/recipes/tags/curry/">curry</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/functional/">functional</a>). Revision 2. </p> <p>Decorator makes function currying as long as there are more correct arguments to take and fires it as soon as there is enough to call, also checks arguments up front for errors.</p> Populate SQL tables from CSV data files (Python) 2011-02-02T21:21:49-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/577559-populate-sql-tables-from-csv-data-files/ <p style="color: grey"> Python recipe 577559 by <a href="/recipes/users/4167757/">James Mills</a> (<a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/csv/">csv</a>, <a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sql/">sql</a>). </p> <p>Just a quick recipe I developed a few years ago that I thought might be useful to others. Basically it takes as input a data file with comma separated values (CSV) and translates this into a series of SQL "INSERT" statements allowing you to then feed this into MySQL, SQLite, or any other database.</p> <p>Example Usage:</p> <p>$ cat cars.csv Year,Make,Model,Length 1997,Ford,E350,2.34 2000,Mercury,Cougar,2.38</p> <p>$ sqlite3 cars.db "CREATE TABLE cars (Year, Make, Model, Length)"</p> <p>$ ./csv2sql.py cars.csv | sqlite3 cars.db </p> <p>$ sqlite3 cars.db "SELECT * FROM cars" 1997|Ford|E350|2.34 2000|Mercury|Cougar|2.38</p> <p>Enjoy! Feedback welcome!</p> <p>cheers James Mills / prologic</p> Calculate distance from .kmz files (Python) 2009-05-31T23:59:34-07:00Michal Niklashttp://code.activestate.com/recipes/users/186902/http://code.activestate.com/recipes/576782-calculate-distance-from-kmz-files/ <p style="color: grey"> Python recipe 576782 by <a href="/recipes/users/186902/">Michal Niklas</a> (<a href="/recipes/tags/distance/">distance</a>, <a href="/recipes/tags/geocoding/">geocoding</a>, <a href="/recipes/tags/google_earth/">google_earth</a>, <a href="/recipes/tags/kmz/">kmz</a>, <a href="/recipes/tags/points/">points</a>). Revision 2. </p> <p>Calculates distance in kilometers from points saved in .kmz file. Such files can be created by Google Earth</p> tensor and transpose (Python) 2006-09-21T15:25:44-07:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/498123-tensor-and-transpose/ <p style="color: grey"> Python recipe 498123 by <a href="/recipes/users/2403049/">bearophile -</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>Two functions useful when you don't use a numerical library. The first one creates a tensor, hopefully in the correct way, avoiding the mutability trap. The second one transposes a 2D matrix keeping the type of the lists/tuples used.</p> simplest useful (I hope!) thread pool example (Python) 2004-09-08T13:53:48-07:00John Nielsenhttp://code.activestate.com/recipes/users/135654/http://code.activestate.com/recipes/302746-simplest-useful-i-hope-thread-pool-example/ <p style="color: grey"> Python recipe 302746 by <a href="/recipes/users/135654/">John Nielsen</a> . Revision 6. </p> <p>You can find examples on how to do threading, but they do not show off a thread pool. My goal was to get as small and simple as possible working thread pool example to show off the basic ideas without having extraneous things to understand. To show off the thread pool, I want stopping and starting of the threads to be explicit. This means the pool won't start until you are ready and will run forever until you are ready for it to stop. The main thread puts into the input queue and removes data from the output queue. The thread pool simply does the converse. Errors are also managed with another queue, so there is a clean distinction between errors and successful results.</p> Progress Bar form WX in Child Thread of Main Python Code. (Python) 2006-04-14T09:30:59-07:00cheeng shu chinhttp://code.activestate.com/recipes/users/163970/http://code.activestate.com/recipes/491258-progress-bar-form-wx-in-child-thread-of-main-pytho/ <p style="color: grey"> Python recipe 491258 by <a href="/recipes/users/163970/">cheeng shu chin</a> (<a href="/recipes/tags/threads/">threads</a>). Revision 2. </p> <p>I come across some cases, where I need to hook a progress bar from WX to my current running python project/script to indicate the current process progress. Some how if you use MainLoop() in child thread will cause the WX session hang and EVT handle will failed. After some study, found that WXAPP class instance and MainLoop have to same namespace. I manage to redesign it and make it work now. Just mail/vote me, if you feel this is useful for your project. E-Mail : <a href="mailto:cheengshuchin@gmail.com">cheengshuchin@gmail.com</a></p> Bounded Buffer Example (2) (Python) 2006-03-29T10:11:15-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/475217-bounded-buffer-example-2/ <p style="color: grey"> Python recipe 475217 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/threads/">threads</a>). </p> <p>This is the second example solution to the bounded buffer problem. By looking at the code, you may notice that it has several features that exapand on what is demonstrated in the first example. First of all, it accepts several new command line arguments that allow customization of the operation of this recipe (including an optional seed argument). Furthermore, this recipe features a fourth thread that takes care of printing for the producer and consumer threads. Of all the improvements in this example, one of the nicest involves improved functions for the threads being executed.</p> Trivia Template (Python) 2005-12-15T07:02:23-08:00Petko Petkovhttp://code.activestate.com/recipes/users/2704694/http://code.activestate.com/recipes/464766-trivia-template/ <p style="color: grey"> Python recipe 464766 by <a href="/recipes/users/2704694/">Petko Petkov</a> (<a href="/recipes/tags/web/">web</a>). </p> <p>Trivia is simple but powerful Python Template. It was designed for making nice and easy HTML templates.</p> A very simple session handling example (Python) 2004-10-28T10:38:30-07:00Jonas Galvezhttp://code.activestate.com/recipes/users/2122857/http://code.activestate.com/recipes/325484-a-very-simple-session-handling-example/ <p style="color: grey"> Python recipe 325484 by <a href="/recipes/users/2122857/">Jonas Galvez</a> (<a href="/recipes/tags/cgi/">cgi</a>). Revision 4. </p> <p>This is a very simple session handling example that uses plain Python CGI (tested only under Python 2.2+). Its goal is to show how cookies are set via HTTP and how easily they can be used for session management.</p> Timing out function (Python) 2004-10-14T11:55:55-07:00chris wrighthttp://code.activestate.com/recipes/users/2032785/http://code.activestate.com/recipes/307871-timing-out-function/ <p style="color: grey"> Python recipe 307871 by <a href="/recipes/users/2032785/">chris wright</a> . Revision 3. </p> <p>This recipe presents two ways to time out the execution of a callable. It relies on signal.SIGALRM; I've only tested in on MacOSX. One way (TimedOutFn) works on Python 2.3.4, and the second uses the decorator syntax introduced in 2.4a. In this version, I've used the code from John Speno's page (<a href="http://www.pycs.net/users/0000231/" rel="nofollow">http://www.pycs.net/users/0000231/</a>). He's done a better job of handling the signals, and I like the try/finally expression.</p> a better assertRaises() for unittest.py (Python) 2008-07-30T10:39:19-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/307970-a-better-assertraises-for-unittestpy/ <p style="color: grey"> Python recipe 307970 by <a href="/recipes/users/4173505/">Trent Mick</a> . Revision 2. </p> <p>When writing unit tests for Python using the standard unittest.py system the assertRaises() (aka failUnlessRaises()) method is used to test that a particular call raises the given exception. This recipe if for assertRaisesEx() that adds three things: (1) the ability to assert the raised exception's args; (2) the ability to test that the stringified exception matches a given regular expression; and (3) much better failure messages.</p> Rendering arbitrary objects with Nevow (Python) 2004-07-22T20:09:38-07:00Valentino Volonghihttp://code.activestate.com/recipes/users/199559/http://code.activestate.com/recipes/286260-rendering-arbitrary-objects-with-nevow/ <p style="color: grey"> Python recipe 286260 by <a href="/recipes/users/199559/">Valentino Volonghi</a> (<a href="/recipes/tags/web/">web</a>). Revision 4. </p> <p>This example is a simplified version of the irenderer example inside Nevow distrib by Matt Goodall (who deserves most of the glory for this example). It makes use of interfaces and adapters to render objects on a web page.</p> Safe Expression Evaluation (Python) 2004-06-14T09:56:27-07:00Sami Hangaslammihttp://code.activestate.com/recipes/users/118800/http://code.activestate.com/recipes/286134-safe-expression-evaluation/ <p style="color: grey"> Python recipe 286134 by <a href="/recipes/users/118800/">Sami Hangaslammi</a> (<a href="/recipes/tags/programs/">programs</a>). Revision 3. </p> <p>Often, we might want to let (untrusted) users input simple Python expressions and evaluate them, but the eval-function in Python is unsafe. The restricted execution model in the rexec module is deprecated, so we need another way ensure only "safe" expressions will be evaluted: analyzing bytecodes.</p>