Popular recipes tagged "meta:loc=48"http://code.activestate.com/recipes/tags/meta:loc=48/2017-05-17T21:15:26-07:00ActiveState Code RecipesInserting pages into a PDF with PyMuPDF (Python)
2017-05-17T21:15:26-07:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580802-inserting-pages-into-a-pdf-with-pymupdf/
<p style="color: grey">
Python
recipe 580802
by <a href="/recipes/users/4193772/">Jorj X. McKie</a>
(<a href="/recipes/tags/fitz/">fitz</a>, <a href="/recipes/tags/mupdf/">mupdf</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/text_conversion/">text_conversion</a>).
Revision 2.
</p>
<p>Version 1.11.0 of PyMuPDF allows creating new PDF pages, as well as inserting images into existing pages.</p>
<p>Here is a script that converts any textfile into a PDF.</p>
Python Mandelbrot Fractal with Tkinter (Python)
2015-05-06T09:57:27-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/579048-python-mandelbrot-fractal-with-tkinter/
<p style="color: grey">
Python
recipe 579048
by <a href="/recipes/users/4182514/">Antoni Gual</a>
(<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tkinter/">tkinter</a>).
Revision 9.
</p>
<p>Displays in a Tk window a pretty coloured 640x480 Mandelbrot set in 6 seconds. Calculates each one of the 300K pixels with a maximum of 256 iterations. <br>
Only Tkinter used. Tested with Python 3.4 <br>
I will test any contribution and add it to the code if worthy.</p>
ListPopulatedFeatures&Tables.py (Python)
2013-09-10T13:39:47-07:00kmoneyhttp://code.activestate.com/recipes/users/4187770/http://code.activestate.com/recipes/578654-listpopulatedfeaturestablespy/
<p style="color: grey">
Python
recipe 578654
by <a href="/recipes/users/4187770/">kmoney</a>
(<a href="/recipes/tags/arcgis/">arcgis</a>, <a href="/recipes/tags/catalog/">catalog</a>, <a href="/recipes/tags/features/">features</a>, <a href="/recipes/tags/geodatabase/">geodatabase</a>, <a href="/recipes/tags/tables/">tables</a>).
</p>
<p>Quick and dirty Pytyon 2.7.5 script that prints a catalog of non-empty features and tables within a file-gdb or folder of mdb's using ArcGIS 10.1's arcpy library.</p>
A FUN Bash Shell Bomb-Out Error Sound... (Bash)
2013-07-14T19:31:13-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578605-a-fun-bash-shell-bomb-out-error-sound/
<p style="color: grey">
Bash
recipe 578605
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/error_beep/">error_beep</a>, <a href="/recipes/tags/error_sound/">error_sound</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/sound/">sound</a>, <a href="/recipes/tags/sound_exchange/">sound_exchange</a>, <a href="/recipes/tags/sox/">sox</a>).
</p>
<p>Do you remember the Bomb-Out icon(s) that appeared on some computers' SW and HW many years ago...</p>
<p>Well this is a matching sound to go with it and can be used as a critical error sound.</p>
<p>It sounds like a bomb being dropped from an aeroplane and is purely a shell sript only.</p>
<p>It is set up to run SOund eXchange, SOX, but just by editing the code "/dev/dsp" can be used instead.</p>
<p>Read the code for more informastion.</p>
<p>Enjoy...</p>
<p>Bazza...</p>
Center (HTML)
2013-07-14T16:16:09-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578603-center/
<p style="color: grey">
HTML
recipe 578603
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/center/">center</a>).
</p>
<p>Place element to center in the document.</p>
System Password (Python)
2013-07-08T17:12:33-07:00superducktoxichttp://code.activestate.com/recipes/users/4187037/http://code.activestate.com/recipes/578595-system-password/
<p style="color: grey">
Python
recipe 578595
by <a href="/recipes/users/4187037/">superducktoxic</a>
.
</p>
<p>You enter a password and the program will tell you if the the password in strong or weak. The program will also tell you how many lower and higher case letters you have and how many digits you have.</p>
topological sorting again (Python)
2013-03-06T19:21:11-08:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578406-topological-sorting-again/
<p style="color: grey">
Python
recipe 578406
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/topological/">topological</a>).
Revision 9.
</p>
<p>Topological sorting is the answer to the following question : in a direct acyclic graph, how can I pick up nodes "in order", such as upstream nodes are always before downstream ones ? Many solutions may exists, many algorithms too.</p>
<p>Alas, it seems I'm too stupid to understand already proposed recipes on topological sorting. Hopefully I do grasp the "write once, read many" concept.</p>
<p>Here, you will find a plain algorithm, optimized only for code clarity, of a topological sorting for direct acyclic graphs, implemented in python from the pseudo code found on <a href="http://en.wikipedia.org/wiki/Topological_sorting">wikipedia</a>:</p>
<pre class="prettyprint"><code>L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
remove a node n from S
insert n into L
for each node m with an edge e from n to m do
remove edge e from the graph
if m has no other incoming edges then
insert m into S
if graph has edges then
return error (graph has at least one cycle)
else
return L (a topologically sorted order)
</code></pre>
<p>Only tested with python3.2, should work with other versions. Be careful, code indented with tabs, since space is evil è_é</p>
Linux to Windows (Python)
2012-07-04T01:18:50-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578192-linux-to-windows/
<p style="color: grey">
Python
recipe 578192
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/archive/">archive</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/old/">old</a>, <a href="/recipes/tags/utility/">utility</a>).
</p>
<p>When trying to work with Linux archive downloaded onto Windows, sometimes it is helpful to untar all found archives, give extension-less files the "txt" extension, and convert text files to Windows line endings (\r\n), and that is just what this program is designed to accomplish. This is committed for archival to be run under Python 2.5 or later versions.</p>
Silmultaneous linear equation solver(any order) (Python)
2012-04-24T12:29:30-07:00Sachin Joglekarhttp://code.activestate.com/recipes/users/4181845/http://code.activestate.com/recipes/578109-silmultaneous-linear-equation-solverany-order/
<p style="color: grey">
Python
recipe 578109
by <a href="/recipes/users/4181845/">Sachin Joglekar</a>
(<a href="/recipes/tags/algebra/">algebra</a>, <a href="/recipes/tags/crammer/">crammer</a>, <a href="/recipes/tags/determinants/">determinants</a>, <a href="/recipes/tags/equation/">equation</a>, <a href="/recipes/tags/linear/">linear</a>, <a href="/recipes/tags/simultaneous/">simultaneous</a>).
</p>
<p>Solves simultaneous linear equations of any order using Crammer's rule. Required input is two lists..one for coefficients, and other for constants
eg. 2x+3y=8
x+4y=6 will be written as
simul([[2,3],[1,4]],[8,6])</p>
Nautilus script to push files to S3 in python (Python)
2012-04-10T21:41:48-07:00coleifer@gmail.comhttp://code.activestate.com/recipes/users/4181659/http://code.activestate.com/recipes/578102-nautilus-script-to-push-files-to-s3-in-python/
<p style="color: grey">
Python
recipe 578102
by <a href="/recipes/users/4181659/">coleifer@gmail.com</a>
(<a href="/recipes/tags/boto/">boto</a>, <a href="/recipes/tags/nautilus/">nautilus</a>, <a href="/recipes/tags/s3/">s3</a>).
</p>
<p>A small script that allows you to push files and directories to S3 using a context menu in nautilus file browser.</p>
<p>Add this script to <code>~/.gnome2/nautilus-scripts/</code> and be sure it is executable. Requires <a href="http://boto.readthedocs.org/">boto</a>, the python aws library. Credentials by default are looked up from ~/.boto but can be supplied in the get_s3_conn() function.</p>
Run async code inline, nonblocking (Python)
2011-11-23T10:13:31-08:00PRITAM Khttp://code.activestate.com/recipes/users/4180048/http://code.activestate.com/recipes/577959-run-async-code-inline-nonblocking/
<p style="color: grey">
Python
recipe 577959
by <a href="/recipes/users/4180048/">PRITAM K</a>
(<a href="/recipes/tags/asynchronous/">asynchronous</a>, <a href="/recipes/tags/closure/">closure</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/threads/">threads</a>).
</p>
<p>A decorator, that makes it easy to switch between the mainthread and background threads.</p>
See What the Builtins Are (Python)
2011-11-04T22:44:09-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577888-see-what-the-builtins-are/
<p style="color: grey">
Python
recipe 577888
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/builtins/">builtins</a>).
Revision 3.
</p>
<p>The built-ins are all those functions, constants, and types that are there when you need them most, like <code>list</code>, <code>any</code>, and <code>None</code>. This recipe is a simple way of looking at the built-ins. Check below for a thorough explanation.</p>
Coloured / highlighted version of string (Python)
2014-06-03T17:54:33-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577818-coloured-highlighted-version-of-string/
<p style="color: grey">
Python
recipe 577818
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/highlighting/">highlighting</a>, <a href="/recipes/tags/shell/">shell</a>).
Revision 3.
</p>
<p>Ok, this is really simple but I think it worths a recipe as it's one of those things I always use in shell scripts and for debugging in general.</p>
Basic Text Editor (Tcl)
2011-03-21T23:10:10-07:00Jonathan Fenechhttp://code.activestate.com/recipes/users/4169413/http://code.activestate.com/recipes/577617-basic-text-editor/
<p style="color: grey">
Tcl
recipe 577617
by <a href="/recipes/users/4169413/">Jonathan Fenech</a>
(<a href="/recipes/tags/basic/">basic</a>, <a href="/recipes/tags/editor/">editor</a>, <a href="/recipes/tags/text/">text</a>).
</p>
<p>Text editor with copy,cut and paste functions.</p>
Watch directory and do periodic commits to Subversion repository. (Python)
2011-02-10T14:00:13-08:00Jiri Zahradilhttp://code.activestate.com/recipes/users/4170037/http://code.activestate.com/recipes/577570-watch-directory-and-do-periodic-commits-to-subvers/
<p style="color: grey">
Python
recipe 577570
by <a href="/recipes/users/4170037/">Jiri Zahradil</a>
(<a href="/recipes/tags/subversion/">subversion</a>, <a href="/recipes/tags/watch/">watch</a>).
</p>
<p>You run this script with directory as parameter (defaults to current directory) and scripts watch this directory for changes in files - creating new file, deleting some file and modification to any file. All these changes are periodically commited to subversion repository using external commands.</p>
<p>Not especially good for coding but great for tracking changes in your text files f.e. during essay writing etc.</p>
Immutable object(subclass) (Python)
2010-04-23T08:30:50-07:00Dmitryhttp://code.activestate.com/recipes/users/4173772/http://code.activestate.com/recipes/577207-immutable-objectsubclass/
<p style="color: grey">
Python
recipe 577207
by <a href="/recipes/users/4173772/">Dmitry</a>
(<a href="/recipes/tags/immutable/">immutable</a>, <a href="/recipes/tags/object/">object</a>).
Revision 2.
</p>
<p>Useful class and decorator for create immutable objects. Decorator mutablemethod used for define mutable methods.</p>
Yet another Python implementation of PEP 380 (yield from) (Python)
2010-04-25T23:08:07-07:00Arnau Sanchezhttp://code.activestate.com/recipes/users/4173270/http://code.activestate.com/recipes/577153-yet-another-python-implementation-of-pep-380-yield/
<p style="color: grey">
Python
recipe 577153
by <a href="/recipes/users/4173270/">Arnau Sanchez</a>
(<a href="/recipes/tags/coroutine/">coroutine</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/refactor/">refactor</a>).
Revision 3.
</p>
<p>Any Python programmer knows how extremely powerful <a href="http://www.python.org/dev/peps/pep-0255/">generators</a> are. Now (since version 2.5) Python generators can not only yield values but also receive them, so they can be used to build <a href="http://www.python.org/dev/peps/pep-0342/">coroutines</a>.</p>
<p>One drawback of the current implementation of generators is that you can only yield/receive values to/from the immediate caller. That means, basically, that you cannot easily refactor your code and write nested generators. <a href="http://www.python.org/dev/peps/pep-0380/">PEP-380</a> is the most serious effort to overcome this issue, but until it gets approved we can still play around with pure Python implementations of <em>yield from</em>. </p>
<p>This recipe follows terminology used by others in the past (<a href="http://code.activestate.com/recipes/576727">recipe566726</a>, <a href="http://code.activestate.com/recipes/576728">recipe576728</a>), but I've tried to simplify the code as much as possible.</p>
Run async code inline, nonblocking (Python)
2009-11-11T12:55:01-08:00Thomas Ahlehttp://code.activestate.com/recipes/users/4060075/http://code.activestate.com/recipes/576952-run-async-code-inline-nonblocking/
<p style="color: grey">
Python
recipe 576952
by <a href="/recipes/users/4060075/">Thomas Ahle</a>
(<a href="/recipes/tags/asynchronous/">asynchronous</a>, <a href="/recipes/tags/closure/">closure</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/threads/">threads</a>).
Revision 7.
</p>
<p>A decorator, that makes it easy to switch between the mainthread and background threads.</p>
Partition Problem and natural selection (Python)
2009-10-27T00:27:14-07:00Jai Vikram Singh Vermahttp://code.activestate.com/recipes/users/4171450/http://code.activestate.com/recipes/576937-partition-problem-and-natural-selection/
<p style="color: grey">
Python
recipe 576937
by <a href="/recipes/users/4171450/">Jai Vikram Singh Verma</a>
(<a href="/recipes/tags/approximate_solution/">approximate_solution</a>, <a href="/recipes/tags/complete/">complete</a>, <a href="/recipes/tags/crossover/">crossover</a>, <a href="/recipes/tags/easiest_hard_problem/">easiest_hard_problem</a>, <a href="/recipes/tags/generations/">generations</a>, <a href="/recipes/tags/genetic_algorithms/">genetic_algorithms</a>, <a href="/recipes/tags/natural_selection/">natural_selection</a>, <a href="/recipes/tags/np/">np</a>, <a href="/recipes/tags/np_complete/">np_complete</a>, <a href="/recipes/tags/np_hard/">np_hard</a>, <a href="/recipes/tags/optimal_solution/">optimal_solution</a>, <a href="/recipes/tags/partition_problem/">partition_problem</a>).
Revision 4.
</p>
<p>Partition problem
From Wikipedia, the free encyclopedia</p>
<p>In computer science, the partition problem is an NP-complete problem. The problem is to decide whether a given multiset of integers can be partitioned into two "halves" that have the same sum. More precisely, given a multiset S of integers, is there a way to partition S into two subsets S1 and S2 such that the sum of the numbers in S1 equals the sum of the numbers in S2? The subsets S1 and S2 must form a partition in the sense that they are disjoint and they cover S. The optimization version asks for the "best" partition, and can be stated as: Find a partition into two subsets S1,S2 such that max(sum(S_1), sum(S_2)) is minimized (sometimes with the additional constraint that the sizes of the two sets in the partition must be equal, or differ by at most 1).</p>
<p>The partition problem is equivalent to the following special case of the subset sum problem: given a set S of integers, is there a subset S1 of S that sums to exactly t /2 where t is the sum of all elements of S? (The equivalence can be seen by defining S2 to be the difference S − S1.) Therefore, the pseudo-polynomial time dynamic programming solution to subset sum applies to the partition problem as well.</p>
<p>Although the partition problem is NP-complete, there are heuristics that solve the problem in many instances, either optimally or approximately. For this reason, it has been called the "The Easiest Hard Problem" by Brian Hayes.</p>
subclass defaultdict to tell default_factory which key is missing (Python)
2009-09-27T12:15:27-07:00Mick Krippendorfhttp://code.activestate.com/recipes/users/4171813/http://code.activestate.com/recipes/576913-subclass-defaultdict-to-tell-default_factory-which/
<p style="color: grey">
Python
recipe 576913
by <a href="/recipes/users/4171813/">Mick Krippendorf</a>
(<a href="/recipes/tags/defaultdict/">defaultdict</a>).
Revision 5.
</p>
<p>In a little Prolog interpreter I'm writing I needed a simple and concise way to create Var-objects and also store them in a mapping <em>varname</em> -> <em>Var-object</em> representing the scope of the current Prolog rule. Also, anonymous Prolog variables ("_") should not go into the mapping and should be renamed to <em>_<unique-number></em>. I came up with this:</p>