Popular recipes tagged "meta:loc=81"http://code.activestate.com/recipes/tags/meta:loc=81/2017-01-07T12:21:39-08:00ActiveState Code RecipesCSV export / import of PDF bookmarks (table of contents) (Python)
2017-01-07T12:21:39-08:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580743-csv-export-import-of-pdf-bookmarks-table-of-conten/
<p style="color: grey">
Python
recipe 580743
by <a href="/recipes/users/4193772/">Jorj X. McKie</a>
(<a href="/recipes/tags/bookmarks/">bookmarks</a>, <a href="/recipes/tags/csv/">csv</a>, <a href="/recipes/tags/fitz/">fitz</a>, <a href="/recipes/tags/mupdf/">mupdf</a>, <a href="/recipes/tags/pdf/">pdf</a>).
</p>
<p>Two little utilities to export or import a PDF's table of contents from / to a standard CSV file.
Typical usecase would be:</p>
<ol>
<li>export TOC to CSV file</li>
<li>edit CSV file</li>
<li>import TOC from CSV file</li>
</ol>
Use PyAudio to play a list of WAV files (Python)
2015-10-22T18:24:08-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579116-use-pyaudio-to-play-a-list-of-wav-files/
<p style="color: grey">
Python
recipe 579116
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/music/">music</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/sound/">sound</a>).
</p>
<p>This recipe shows how to use PyAudio, a 3rd-party Python audio toolkit, to play a list of WAV files on your computer. This is an enhanced version of a basic WAV code example on the PyAudio site. You can specify either one WAV filename on the command line, like this:</p>
<p>py pyaudio_play_wav.py chimes.wav</p>
<p>or specify a text file containing names of WAV files to play, like this:</p>
<p>py pyaudio_play_wav.py -f wav_fil_list.txt</p>
<p>The only dependency is PyAudio, which you can install with pip.</p>
Printing an ASCII table to PDF (Python)
2015-04-02T18:38:04-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579043-printing-an-ascii-table-to-pdf/
<p style="color: grey">
Python
recipe 579043
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdf_generation/">pdf_generation</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>).
</p>
<p>This recipe shows how to print part of an ASCII table to PDF. It shows it for the first 32 ASCII characters, the control characters, i.e. the characters with ASCII codes 0 to 31. The same logic can be extended to print the remaining ASCII characters, either upto code 127 or 255, depending on your definition of ASCII, and also on whether your PDF font supports printing all those characters.</p>
Continued fractions (Python)
2013-08-22T07:15:47-07:00Glenn Hutchingshttp://code.activestate.com/recipes/users/4175415/http://code.activestate.com/recipes/578647-continued-fractions/
<p style="color: grey">
Python
recipe 578647
by <a href="/recipes/users/4175415/">Glenn Hutchings</a>
(<a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/fractions/">fractions</a>).
Revision 2.
</p>
<p>A class representing a <a href="https://en.wikipedia.org/wiki/Continued_fraction">continued fraction</a>.</p>
Observer Pattern (Python)
2013-03-09T10:03:10-08:00Mauro B. Bianchttp://code.activestate.com/recipes/users/4185493/http://code.activestate.com/recipes/578484-observer-pattern/
<p style="color: grey">
Python
recipe 578484
by <a href="/recipes/users/4185493/">Mauro B. Bianc</a>
(<a href="/recipes/tags/cascade/">cascade</a>, <a href="/recipes/tags/observer/">observer</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/pattern/">pattern</a>, <a href="/recipes/tags/patterns/">patterns</a>, <a href="/recipes/tags/setattr/">setattr</a>, <a href="/recipes/tags/__setattr__/">__setattr__</a>).
Revision 2.
</p>
<p>This is a Python implementation of the observer pattern described by Gamma et. al.
It defines a one-to many dependency between objects so that when one object changes state,
all its dependents (i.e. observers) are notified and updated automatically.</p>
<p>My adaptation gets rid of the need to use specific functions to set the data (and to call Notify)
and allows you to be notified for ANY attribute you set.
It is possible to specify a list of attributes which should not trigger a notification.
In case you need the opposite, it is very easy to invert the behavior of the code.</p>
<p>The example should output:
Creating data1 without notification for attrs name & surname <br />
Creating data2 without notification for attr age <br />
Setting data1.name=Heather - Notification unnecessary <br />
Setting data1.num=333 - Notification expected <br />
Observer1: Subject Heather has updated attr num to 333 <br />
Setting data2.name=Molly - Notification expected <br />
Observer2: Subject Molly has updated attr name to Molly <br />
Setting data2.age=28 - Notification unnecessary <br />
Setting data2.eyecolor=blue - Notification expected <br />
Observer2: Subject Molly has updated attr eyecolor to blue </p>
ChainedList and ChainedListView: Exposing Multiple Lists as a Single Sequence (Python)
2012-07-03T21:00:02-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/578190-chainedlist-and-chainedlistview-exposing-multiple-/
<p style="color: grey">
Python
recipe 578190
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
</p>
<p>Handy for composing lists without losing the underlying indepedence. The "lists" don't actually have to be lists, though for ChainedList they must be mutable.</p>
Spring-Mass System Simulation (Python)
2011-05-02T01:59:45-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577681-spring-mass-system-simulation/
<p style="color: grey">
Python
recipe 577681
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/chaos/">chaos</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/physics/">physics</a>, <a href="/recipes/tags/simulation/">simulation</a>).
</p>
<p>It simulates a damped spring-mass system driven by sinusoidal force.</p>
Python Progressbar (Python)
2011-09-18T18:57:48-07:00Anler Hernández Peralhttp://code.activestate.com/recipes/users/4176323/http://code.activestate.com/recipes/577871-python-progressbar/
<p style="color: grey">
Python
recipe 577871
by <a href="/recipes/users/4176323/">Anler Hernández Peral</a>
(<a href="/recipes/tags/progress/">progress</a>).
</p>
<p>A progressbar utility for command line programs, very easy to use.
The original code is hosted in github: <a href="https://github.com/ikame/progressbar" rel="nofollow">https://github.com/ikame/progressbar</a></p>
Socket Broadcast Help (Python)
2011-12-04T22:18:13-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577950-socket-broadcast-help/
<p style="color: grey">
Python
recipe 577950
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/broadcast/">broadcast</a>, <a href="/recipes/tags/socket/">socket</a>, <a href="/recipes/tags/udp/">udp</a>).
Revision 6.
</p>
<p>The classes in this module are stepping stones for building discoverable
services on a network. Server replies are to be handled by the importer.</p>
Youtap - Downloading YouTube video through Python command line script (Python)
2011-07-26T21:47:24-07:00Sundar Srinivasanhttp://code.activestate.com/recipes/users/4177884/http://code.activestate.com/recipes/577807-youtap-downloading-youtube-video-through-python-co/
<p style="color: grey">
Python
recipe 577807
by <a href="/recipes/users/4177884/">Sundar Srinivasan</a>
(<a href="/recipes/tags/flash/">flash</a>, <a href="/recipes/tags/mime/">mime</a>, <a href="/recipes/tags/youtube/">youtube</a>, <a href="/recipes/tags/youtube_downloader/">youtube_downloader</a>).
Revision 3.
</p>
<p>Python program to download YouTube video from command line. Originally posted in: <a href="https://github.com/krishnasun82/youtap" rel="nofollow">https://github.com/krishnasun82/youtap</a></p>
<p>Usage: python youtap.py "<youtube-link>"</p>
<p>The reason for giving the link in double-quotes is that sometimes the link contain '&'(ampersand), which the UNIX interprets as "run the program in background"</p>
Inherit Method Docstrings Using Only Function Decorators (Python)
2011-06-26T02:28:55-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577746-inherit-method-docstrings-using-only-function-deco/
<p style="color: grey">
Python
recipe 577746
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/docstring/">docstring</a>).
Revision 3.
</p>
<p>This recipe provides a descriptor and a decorator. The decorator will be used on any method in your class to indicate that you want that method to inherit its docstring.</p>
<p>This is useful when you are using abstract bases classes and want a method to have the same docstring as the abstract method it implements.</p>
<p>This recipe uses <a href="http://code.activestate.com/recipes/577745/">recipe #577745</a>, the deferred_binder module.</p>
Maclaurin's_series_ln(x)_decimal_module_version (Python)
2011-01-20T12:09:24-08:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/577307-maclaurins_series_lnx_decimal_module_version/
<p style="color: grey">
Python
recipe 577307
by <a href="/recipes/users/4155345/">Fouad Teniou</a>
(<a href="/recipes/tags/mathematics/">mathematics</a>).
Revision 5.
</p>
<p>C. Maclaurin. A Scottish mathematician gained his master degree at age 17, and his major mathematics' work arise from his special knowledge in Newton's ideas and the formulation of Newton's methods.</p>
<p>However, C. Maclaurin also contributed to the astronomy science and helped to improve maps and invented some mechanical devices .</p>
<p>My mathematics python's programs is a set of Maclaurin's series to compute some of the most important functions in calculus.</p>
<p>Though, the computation of an infinite sum which give the value of a function in terms of the derivatives evaluated at a special case where x0 = 0,in contrast with Taylor series. </p>
<p>The maclaurin's series approximation for ln(1+x) is used to approximate ln(x) by detucting the maclaurin's series for ln(1-x). ln(1+x) - ln(1-x) = ln(1+x/1-x)and by letting y = (1+x/1-x), y could take any positive value.</p>
<p>y = 1+x/1-x, therefore x = y-1/y+1 </p>
<p>-1 < x < 1</p>
Python word frequency count using sets and lists (Python)
2009-03-26T23:00:54-07:00nickhttp://code.activestate.com/recipes/users/4169647/http://code.activestate.com/recipes/576699-python-word-frequency-count-using-sets-and-lists/
<p style="color: grey">
Python
recipe 576699
by <a href="/recipes/users/4169647/">nick</a>
(<a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/unique_words/">unique_words</a>, <a href="/recipes/tags/word_frequency/">word_frequency</a>).
</p>
<p>This lists unique words and word frequencies occurring in a Python string. You can ignore or take account of letter case in distinguishing words, and you can pass it your own inclusion list of characters allowed in words (e.g. is "import123" the kind of word you want to list, or not? It might be if you're a programmer.) By default only alpha chars are allowed in words.</p>
<p>At first glance having the whole piece of text, and intermediate results, in memory at once is a problem for large files. But it's zippy: it found 1600 unique words in a 7M SQL script (472,000 words in original) in 20 seconds, and hardly notices a 4000-word document cut and pasted across from a word processor.</p>
<p>With a bit of extra work, the algorithm could be fed a very large file in chunks. Anyone?</p>
Liturgical Calendar Year class (Python)
2008-09-30T16:57:29-07:00Martin Diershttp://code.activestate.com/recipes/users/4167352/http://code.activestate.com/recipes/576518-liturgical-calendar-year-class/
<p style="color: grey">
Python
recipe 576518
by <a href="/recipes/users/4167352/">Martin Diers</a>
(<a href="/recipes/tags/calendar/">calendar</a>, <a href="/recipes/tags/church/">church</a>).
</p>
<p>When initialized with a <code>year</code>, returns a class with a number of date attributes corresponding to common feasts of the western liturgical calendar. Number of Sundays after Epiphany and Trinity / Pentecost are also calculated, and stored in attributes. If the optional parameter <code>v2</code> is set to True, the calendar will return a post-Vatican II calendar which changes the date of Transfiguration Sunday, and the number of Sundays after Epiphany. In either case, the pre-Lent Sundays and Sundays after Pentecost will be set.</p>
<p><strong>Note:</strong> The <code>year</code> parameter is the year of the First Sunday in Advent - the beginning of the church year, and the resulting calendar thus ends in the following year.</p>
gzip pipe for asynchronous use (Python)
2008-03-24T21:47:17-07:00Raphaël Jolivethttp://code.activestate.com/recipes/users/4135673/http://code.activestate.com/recipes/551784-gzip-pipe-for-asynchronous-use/
<p style="color: grey">
Python
recipe 551784
by <a href="/recipes/users/4135673/">Raphaël Jolivet</a>
(<a href="/recipes/tags/files/">files</a>).
Revision 2.
</p>
<p>I've written a small class to handle a gzip pipe that won't read the whole source file at once, but will deliver small chunks of data on demand.</p>
ffmpeg ppm.so vhook plugin (Python)
2008-05-26T09:41:05-07:00Konrads Smelkovshttp://code.activestate.com/recipes/users/2852558/http://code.activestate.com/recipes/572220-ffmpeg-ppmso-vhook-plugin/
<p style="color: grey">
Python
recipe 572220
by <a href="/recipes/users/2852558/">Konrads Smelkovs</a>
(<a href="/recipes/tags/graphics/">graphics</a>).
</p>
<p>This recipe shows how to read a continuous stream of PPM frames as output by ffmpeg ppm.so vhook. In this example, the image is flipped and output back.</p>
Scripting iTunes (for Windows) with Python (Python)
2006-11-03T01:23:57-08:00Fabien C.http://code.activestate.com/recipes/users/4010550/http://code.activestate.com/recipes/498241-scripting-itunes-for-windows-with-python/
<p style="color: grey">
Python
recipe 498241
by <a href="/recipes/users/4010550/">Fabien C.</a>
.
</p>
<p>Here are two examples showing how to script iTunes for Windows with Python.
See below under "Discussion" for additional comments and explainations.</p>
decorator: suppressable (Python)
2007-02-11T01:16:47-08:00Andrew Moffathttp://code.activestate.com/recipes/users/2979564/http://code.activestate.com/recipes/502212-decorator-suppressable/
<p style="color: grey">
Python
recipe 502212
by <a href="/recipes/users/2979564/">Andrew Moffat</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 3.
</p>
<p>useful for decorating functions/methods that you'd like to disable from the main loop or commandline via optparse. an example would be running certain verifications or checks that you deem unnecessary in certain scenarios</p>
Lazy Traversal of Directed Graphs (Python)
2006-11-06T20:34:39-08:00Vincent Kraeutlerhttp://code.activestate.com/recipes/users/4011188/http://code.activestate.com/recipes/498247-lazy-traversal-of-directed-graphs/
<p style="color: grey">
Python
recipe 498247
by <a href="/recipes/users/4011188/">Vincent Kraeutler</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>The os.path.walk routine that ships with the python standard library is limited to traversing the file system tree. A generic traversal for arbitrary (directed) graphs with support for recursion limits and other accumulated partial results seems useful.</p>
thread pool example #2 -- easy_pool class (Python)
2004-09-08T13:48:11-07:00John Nielsenhttp://code.activestate.com/recipes/users/135654/http://code.activestate.com/recipes/303108-thread-pool-example-2-easy_pool-class/
<p style="color: grey">
Python
recipe 303108
by <a href="/recipes/users/135654/">John Nielsen</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 3.
</p>
<p>I am trying to show how to have a thread pool
building on the recipe in <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302746" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302746</a>.
This is a python class that essentially makes a thread pool for a function you define. Like the earlier example, I want to show off the power of having a thread pool that you can stop and start at will. Interestingly, you can mimic more standard thread use with the pool -- which I show off in as little as 3 lines of simple code.</p>