Top-rated recipes tagged "meta:license=gpl3"http://code.activestate.com/recipes/tags/meta:license=gpl3/top/2017-06-18T17:43:47-07:00ActiveState Code RecipesExtract images of a PDF - optionally by page using PyMuPDF / fitz (Python)
2016-09-28T12:03:59-07:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580703-extract-images-of-a-pdf-optionally-by-page-using-p/
<p style="color: grey">
Python
recipe 580703
by <a href="/recipes/users/4193772/">Jorj X. McKie</a>
(<a href="/recipes/tags/fitz/">fitz</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/png/">png</a>).
</p>
<p>Two small scripts to extract images contained in a PDF document as PNG files.
(1) Script 1 extracts <strong>all</strong> images
(2) Script 2 extracts only images that are referenced by a page</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>
Simple local cache and cache decorator (Python)
2010-12-08T09:06:34-08:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/577492-simple-local-cache-and-cache-decorator/
<p style="color: grey">
Python
recipe 577492
by <a href="/recipes/users/4176176/">Andrey Nikishaev</a>
(<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/memoization/">memoization</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>Simple local cache.
It saves local data in singleton dictionary with convenient interface</p>
<h4 id="examples-of-use">Examples of use:</h4>
<pre class="prettyprint"><code># Initialize
SimpleCache({'data':{'example':'example data'}})
# Getting instance
c = SimpleCache.getInstance()
c.set('re.reg_exp_compiled',re.compile(r'\W*'))
reg_exp = c.get('re.reg_exp_compiled',default=re.compile(r'\W*'))
# --------------------------------------------------------------
c = SimpleCache.getInstance()
reg_exp = c.getset('re.reg_exp_compiled',re.compile(r'\W*'))
# --------------------------------------------------------------
@scache
def func1():
return 'OK'
</code></pre>
Pyscanlogger - Python Port scan detector (Python)
2010-03-17T07:27:15-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/576690-pyscanlogger-python-port-scan-detector/
<p style="color: grey">
Python
recipe 576690
by <a href="/recipes/users/760763/">Anand</a>
(<a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/programs/">programs</a>, <a href="/recipes/tags/security/">security</a>).
Revision 5.
</p>
<p>A pure Python program to detect network port scanning attacks. Currently logs different TCP port scans. Can run in the background like a daemon and log attacks to a log file.</p>
How to Create a PDF with a Caustic Drawing (Python)
2017-06-18T17:43:47-07:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580806-how-to-create-a-pdf-with-a-caustic-drawing/
<p style="color: grey">
Python
recipe 580806
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/pymupdf/">pymupdf</a>).
</p>
<p>Just a little demo on how to create simple drawings with PyMuPDF.</p>
<p>This script simulates what you see looking into your coffee mug, early in the morning after a long night of programming ...</p>
Create Calendars on PDF with a few lines (Python)
2017-06-13T10:57:34-07:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580805-create-calendars-on-pdf-with-a-few-lines/
<p style="color: grey">
Python
recipe 580805
by <a href="/recipes/users/4193772/">Jorj X. McKie</a>
(<a href="/recipes/tags/calendar/">calendar</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/pymupdf/">pymupdf</a>).
Revision 2.
</p>
<p>PyMuPDF (fitz) provides easy to use ways to create PDF documents out of simple texts.</p>
<p>An example is the text output of Python's calendar module. Here we take a starting year as script parameter and output a 3-page (A4 landscape) document with calendars for this and the following two years - in less than 20 lines of code.</p>
Rotate a PDF page in 3 lines (Python)
2016-11-06T11:33:59-08:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580713-rotate-a-pdf-page-in-3-lines/
<p style="color: grey">
Python
recipe 580713
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/pymupdf/">pymupdf</a>).
Revision 2.
</p>
<p>PyMuPDF v1.9.3 now supports several new features for manipulating PDFs.</p>
<p>Here is an example to rotate a page with just a few lines of Python code.</p>
PDF wrapper for FileOptimizer (Python)
2016-10-29T20:22:27-07:00Harald Liederhttp://code.activestate.com/recipes/users/4191581/http://code.activestate.com/recipes/580711-pdf-wrapper-for-fileoptimizer/
<p style="color: grey">
Python
recipe 580711
by <a href="/recipes/users/4191581/">Harald Lieder</a>
(<a href="/recipes/tags/fileoptimizer/">fileoptimizer</a>, <a href="/recipes/tags/fitz/">fitz</a>, <a href="/recipes/tags/mupdf/">mupdf</a>, <a href="/recipes/tags/optimization/">optimization</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pymupdf/">pymupdf</a>).
</p>
<p>Among dozens of other filetypes, FileOptimizer also compresses PDFs - often significantly.
The issue is that the used plugin <em>smpdf</em> is free for non-commercial use only and it annoyingly <strong>also overwrites metadata</strong> information to state this.</p>
<p>The following tool remedies these metadata changes (but not the license situation!).</p>
lru_timestamp - cache entry aging for functools.lru_cache (Python)
2014-02-02T21:28:25-08:00Peter Santorohttp://code.activestate.com/recipes/users/4189027/http://code.activestate.com/recipes/578817-lru_timestamp-cache-entry-aging-for-functoolslru_c/
<p style="color: grey">
Python
recipe 578817
by <a href="/recipes/users/4189027/">Peter Santoro</a>
(<a href="/recipes/tags/age/">age</a>, <a href="/recipes/tags/lru_cache/">lru_cache</a>).
</p>
<p>Return a timestamp string for @lru_cache decorated functions.</p>
<p>The returned timestamp is used as the value of an extra parameter
to @lru_cache decorated functions, allowing for more control over
how often cache entries are refreshed. The lru_timestamp function
should be called with the same refresh_interval value for a given
@lru_cache decorated function. The returned timestamp is for the
benefit of the @lru_cache decorator and is normally not used by
the decorated function.</p>
<p>Positional arguments:
refresh_interval -- in minutes (default 60), values less than 1
are coerced to 1, values more than 1440 are
coerced to 1440</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>
bubblebabble (Python)
2012-11-03T09:28:41-07:00Space Hobohttp://code.activestate.com/recipes/users/4184146/http://code.activestate.com/recipes/578319-bubblebabble/
<p style="color: grey">
Python
recipe 578319
by <a href="/recipes/users/4184146/">Space Hobo</a>
(<a href="/recipes/tags/babble/">babble</a>, <a href="/recipes/tags/bubble/">bubble</a>, <a href="/recipes/tags/digest/">digest</a>, <a href="/recipes/tags/hash/">hash</a>, <a href="/recipes/tags/human/">human</a>, <a href="/recipes/tags/readable/">readable</a>).
</p>
<p>This module provides a bubblebabble function, which computes a (somewhat more) human readable format for message digests.</p>
Josephus problem (Python)
2011-08-13T23:17:27-07:00Giannis Fysakishttp://code.activestate.com/recipes/users/4174072/http://code.activestate.com/recipes/577840-josephus-problem/
<p style="color: grey">
Python
recipe 577840
by <a href="/recipes/users/4174072/">Giannis Fysakis</a>
.
</p>
<p>In computer science and mathematics, the Josephus problem (or Josephus permutation) is a theoretical problem related to a certain counting-out game.</p>
<p>There are people standing in a circle waiting to be executed. After the first person is executed, certain number of people are skipped and one person is executed. Then again, people are skipped and a person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom.</p>
<p>The task is to choose the place in the initial circle so that you are the last one remaining and so survive.
If 2 are the number of people that are skipped</p>
dict2xml (Python)
2011-07-19T12:32:57-07:00nuggetierhttp://code.activestate.com/recipes/users/4178225/http://code.activestate.com/recipes/577739-dict2xml/
<p style="color: grey">
Python
recipe 577739
by <a href="/recipes/users/4178225/">nuggetier</a>
(<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/xml/">xml</a>).
Revision 4.
</p>
<p>convert dictionary / list structures into xml structure</p>
Multiplication table (Python)
2011-04-21T09:12:26-07:00Boubakrhttp://code.activestate.com/recipes/users/4176416/http://code.activestate.com/recipes/577672-multiplication-table/
<p style="color: grey">
Python
recipe 577672
by <a href="/recipes/users/4176416/">Boubakr</a>
(<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/multiplication/">multiplication</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/table/">table</a>).
</p>
<p>Multiplication table (Python)</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>
Keyword Argument Injection with Python Decorators (Python)
2010-09-05T17:06:04-07:00Ahmet Emre Aladağhttp://code.activestate.com/recipes/users/4174877/http://code.activestate.com/recipes/577382-keyword-argument-injection-with-python-decorators/
<p style="color: grey">
Python
recipe 577382
by <a href="/recipes/users/4174877/">Ahmet Emre Aladağ</a>
(<a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/class_decorator/">class_decorator</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/injection/">injection</a>, <a href="/recipes/tags/variable/">variable</a>).
Revision 2.
</p>
<p>In most of the object oriented codes we write, we need to set class attributes to the given argument values and this is a very line-consuming thing. To get over these redundant lines, I found a solution using decorators. </p>
Random Passwords (Python)
2010-07-27T20:28:17-07:00Danillo Souzahttp://code.activestate.com/recipes/users/4174445/http://code.activestate.com/recipes/577339-random-passwords/
<p style="color: grey">
Python
recipe 577339
by <a href="/recipes/users/4174445/">Danillo Souza</a>
(<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/random/">random</a>).
</p>
<p>Generate strong random passwords of specified length.</p>
Decorator to expose local variables of a function after execution [alternative implementation] (Python)
2010-07-08T09:44:28-07:00Andrea Maffezzolihttp://code.activestate.com/recipes/users/4171157/http://code.activestate.com/recipes/577295-decorator-to-expose-local-variables-of-a-function-/
<p style="color: grey">
Python
recipe 577295
by <a href="/recipes/users/4171157/">Andrea Maffezzoli</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/inner/">inner</a>, <a href="/recipes/tags/local/">local</a>, <a href="/recipes/tags/scope/">scope</a>, <a href="/recipes/tags/variables/">variables</a>).
Revision 6.
</p>
<p>Please note that the present is a fork of the <a href="http://code.activestate.com/recipes/577283/">recipe 577283</a> "Decorator to expose local variables of a function after execution" of Pietro Berkes, available at <a href="http://code.activestate.com/recipes/577283-decorator-to-expose-local-variables-of-a-function-/" rel="nofollow">http://code.activestate.com/recipes/577283-decorator-to-expose-local-variables-of-a-function-/</a> , and aiming only to report the alternative implementation "persistent_locals2", whose I'm co-author with Pietro Berkes, and which was submitted together to the original recipe. Refer to the latter for an exhaustive description and discussion.</p>
Decorator to expose local variables of a function after execution (Python)
2010-07-07T22:01:23-07:00Pietro Berkeshttp://code.activestate.com/recipes/users/4174299/http://code.activestate.com/recipes/577283-decorator-to-expose-local-variables-of-a-function-/
<p style="color: grey">
Python
recipe 577283
by <a href="/recipes/users/4174299/">Pietro Berkes</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/inner/">inner</a>, <a href="/recipes/tags/local/">local</a>, <a href="/recipes/tags/scope/">scope</a>, <a href="/recipes/tags/variables/">variables</a>).
Revision 2.
</p>
<p>Decorator to expose the local variables defined in the inner scope of a function. At the exit of the decorated function (regular exit or exceptions), the local dictionary is copied to a read-only property, <code>locals</code>.</p>
<p>The main implementation is based on injecting bytecode into the original function, and requires the lightweight module <code>byteplay</code> (available <a href="http://code.google.com/p/byteplay/">here</a>). See below for an alternative implementation that only uses the standard library.</p>
Python code minifier (Python)
2014-05-25T16:23:55-07:00Dan McDougallhttp://code.activestate.com/recipes/users/4169722/http://code.activestate.com/recipes/576704-python-code-minifier/
<p style="color: grey">
Python
recipe 576704
by <a href="/recipes/users/4169722/">Dan McDougall</a>
(<a href="/recipes/tags/bz2/">bz2</a>, <a href="/recipes/tags/bzip2/">bzip2</a>, <a href="/recipes/tags/comments/">comments</a>, <a href="/recipes/tags/compression/">compression</a>, <a href="/recipes/tags/docstring/">docstring</a>, <a href="/recipes/tags/embedded/">embedded</a>, <a href="/recipes/tags/gzip/">gzip</a>, <a href="/recipes/tags/minify/">minify</a>, <a href="/recipes/tags/pack/">pack</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/zlib/">zlib</a>).
Revision 16.
</p>
<p><strong>Update 05/25/2014:</strong> Pyminifier 2.0 has been released and now lives on Github: <a href="https://github.com/liftoff/pyminifier" rel="nofollow">https://github.com/liftoff/pyminifier</a> (docs are here: <a href="http://liftoff.github.io/pyminifier/" rel="nofollow">http://liftoff.github.io/pyminifier/</a>). The code below is very out-of-date but will be left alone for historical purposes.</p>
<p>Python Minifier: Reduces the size of Python code for use on embedded platforms. Performs the following:</p>
<ol>
<li>Removes docstrings.</li>
<li>Removes comments.</li>
<li>Removes blank lines.</li>
<li>Minimizes code indentation.</li>
<li>Joins multiline pairs of parentheses, braces, and brackets (and removes extraneous whitespace within).</li>
<li>Preserves shebangs and encoding info (e.g. "# -<em>- coding: utf-8 -</em>-")</li>
<li><strong>NEW:</strong> Optionally, produces a bzip2 or gzip-compressed self-extracting python script containing the minified source for ultimate minification.</li>
</ol>
<p><strong>Update 09/23/2010:</strong> Version 1.4.1: Fixed an indentation bug when operators such as @ and open parens started a line.</p>
<p><strong>Update 09/18/2010:</strong> Version 1.4:</p>
<ul>
<li>Added some command line options to save the result to an output file.</li>
<li>Added the ability to save the result as a bzip2 or gzip-compressed self-extracting python script (which is kinda neat--try it!).</li>
<li>Updated some of the docstrings to provide more examples of what each function does.</li>
</ul>
<p><strong>Update 06/02/2010:</strong> Version 1.3: Rewrote several functions to use Python's built-in tokenizer module (which I just discovered despite being in Python since version 2.2). This negated the requirement for pyparsing and improved performance by an order of magnitude. It also fixed some pretty serious bugs with dedent() and reduce_operators().</p>
<p>PLEASE POST A COMMENT IF YOU ENCOUNTER A BUG!</p>