Popular recipes tagged "meta:loc=41"http://code.activestate.com/recipes/tags/meta:loc=41/2016-10-29T20:22:27-07:00ActiveState Code RecipesPDF 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> Python-controlled Unix pipeline to generate PDF (Python) 2016-01-07T18:02:52-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579146-python-controlled-unix-pipeline-to-generate-pdf/ <p style="color: grey"> Python recipe 579146 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdf_generation/">pdf_generation</a>, <a href="/recipes/tags/pipe/">pipe</a>, <a href="/recipes/tags/pipelining/">pipelining</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/unix/">unix</a>). </p> <p>This recipe shows how to create a Unix pipeline that generates PDF output, under the control of a Python program. It is tested on Linux. It uses nl, a standard Linux command that adds line numbers to its input, and selpg, a custom Linux command-line utility, that selects only specified pages from its input, together in a pipeline (nl | selpg). The Python program sets up and starts that pipeline running, and then reads input from it and generates PDF output.</p> Obfuscation In Bash Shell. (Bash) 2014-12-19T20:01:30-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578986-obfuscation-in-bash-shell/ <p style="color: grey"> Bash recipe 578986 by <a href="/recipes/users/4177147/">Barry Walker</a> (<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/obfuscation/">obfuscation</a>). </p> <p>IMO, the immense power of the shell...</p> <p>Please let me know if there is any other human readable language that can do this...</p> <p>The DEMO code below was an idea I formed to see how to make a bash script very difficult to hack.</p> <p>Everything in it is made easy to read so as to see this idea working.</p> <p>It uses bash variables ONLY and although I have used bash loops to create the variables in this DEMO you could create your own set of variables and 'source' them to the the obfuscated code before running the main body of the code.</p> <p>It also goes without saying that you could obfuscate the changing of any or all the variable allocations at any time AFTER the code runs to make it even more obfuscated and as may times as you wish...</p> <p>I would be seriously difficult to actually write a lsrge bash app' using this method but boy oh boy would it be fun?!?</p> <p>Testbed:- Macbook Pro, OSX 10.7.x and above, using default bash terminal...</p> <p>LBNL, yeah I am aware of 'eval' but as it is obfuscated and can have as many obfuscated variables as I wish allocated to it then why worry... ;o)</p> <p>Enjoy finding simple solutions to often very difficult problems...</p> <p>Bazza...</p> Flattening an arbitrarily nested list in Python (Python) 2014-10-05T21:44:45-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578948-flattening-an-arbitrarily-nested-list-in-python/ <p style="color: grey"> Python recipe 578948 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This is a recipe to flatten a Python list which may have nested lists as items within it. It works for lists that have a maximum depth of nesting roughly equal to the recursion depth limit of Python, which is set to 1000 by default, though it can be increased with sys.setrecursionlimit().</p> Easy way to use Graph Facebook API without ad-hoc libraries (Ruby) 2012-11-20T14:14:27-08:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/578343-easy-way-to-use-graph-facebook-api-without-ad-hoc-/ <p style="color: grey"> Ruby recipe 578343 by <a href="/recipes/users/4174931/">Filippo Squillace</a> (<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/facebook/">facebook</a>, <a href="/recipes/tags/http/">http</a>, <a href="/recipes/tags/ruby/">ruby</a>). </p> <p>I was painfully lokking for a simple function that allow easily make GET or POST requests in Ruby without using complex libraries such as Koala for accessing to the Facebook Graph. At the end I gave up and did it by myself, so the function fb_api, based on net/http, is able to make GET or POST requests (depending if the request is for retrieving information of the graph or is for changing the graph such as post feed etc.). It returns a dict from a JSON data structure.</p> <p>This might be useful for your facebook app ;)</p> Finding complete loops in a map of connected nodes (Python) 2012-04-24T18:47:21-07:00Sachin Joglekarhttp://code.activestate.com/recipes/users/4181845/http://code.activestate.com/recipes/578110-finding-complete-loops-in-a-map-of-connected-nodes/ <p style="color: grey"> Python recipe 578110 by <a href="/recipes/users/4181845/">Sachin Joglekar</a> (<a href="/recipes/tags/circuit/">circuit</a>, <a href="/recipes/tags/connection/">connection</a>, <a href="/recipes/tags/loops/">loops</a>). </p> <p>This module finds loops in a given map.Input is a dictionary like</p> <p>d={1:[2,4,5,6],2:[1,3],3:[2,4,5,6],4:[1,3,5],5:[1,3,4],6:[1,3]}</p> <p>this means node 1 is connected to nodes 2,4,5 and 6 and so on..</p> <p>Output is a list of complete loops. for above examples,output is</p> <p>[[1, 4, 5, 1], [3, 4, 5, 3], [1, 2, 3, 4, 1], [1, 2, 3, 5, 1], [1, 2, 3, 6, 1], [1, 4, 3, 5, 1], [1, 4, 3, 6, 1], [1, 5, 3, 6, 1], [1, 2, 3, 4, 5, 1], [1, 4, 5, 3, 6, 1]]</p> Shannon Entropy Calculation (Python) 2010-11-29T08:37:34-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577476-shannon-entropy-calculation/ <p style="color: grey"> Python recipe 577476 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Shannon entropy of a string indicates minimum average number of bits per symbol required for encoding (compressing) the string.</p> Inner Join (Python) 2011-11-01T20:10:34-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577937-inner-join/ <p style="color: grey"> Python recipe 577937 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/join/">join</a>, <a href="/recipes/tags/sql/">sql</a>). Revision 2. </p> <p>Implemented an SQL style INNER JOIN for two lists of tuples to be joined on a common field.</p> Convert strings like '5d' and '60s' to timedelta objects (Python) 2011-10-06T14:06:54-07:00Dan McDougallhttp://code.activestate.com/recipes/users/4169722/http://code.activestate.com/recipes/577894-convert-strings-like-5d-and-60s-to-timedelta-objec/ <p style="color: grey"> Python recipe 577894 by <a href="/recipes/users/4169722/">Dan McDougall</a> (<a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/timedelta/">timedelta</a>). </p> <p>I wrote this little function for <code>[Gate One](http://vimeo.com/24857127)</code> (a web-based terminal emulator/SSH client)... It converts strings in the format of &lt;num&gt;&lt;character&gt; into timedelta objects. It's not rocket science but maybe it'll save someone a few keystrokes :). Besides that, it comes with a really nice Sphinx-ready (reStructuredText) docstring with complete doctests.</p> Context manager for a daemon pid file (Python) 2013-10-07T21:03:30-07:00Graham Poulterhttp://code.activestate.com/recipes/users/4172291/http://code.activestate.com/recipes/577911-context-manager-for-a-daemon-pid-file/ <p style="color: grey"> Python recipe 577911 by <a href="/recipes/users/4172291/">Graham Poulter</a> (<a href="/recipes/tags/daemon/">daemon</a>, <a href="/recipes/tags/pid/">pid</a>). Revision 3. </p> <p>Context manager for a pid (process id) file used to tell whether a daemon process is still running.</p> <p>On entry, it writes the pid of the current process to the path. On exit, it removes the file.</p> <p>Designed to work with python-daemon.</p> PythonTimer (Python) 2011-04-11T19:53:17-07:00Narendra Sisodiyahttp://code.activestate.com/recipes/users/4177605/http://code.activestate.com/recipes/577646-pythontimer/ <p style="color: grey"> Python recipe 577646 by <a href="/recipes/users/4177605/">Narendra Sisodiya</a> (<a href="/recipes/tags/timer/">timer</a>). Revision 2. </p> <p>import PythonTimer</p> <p>t = PythonTimer.TickTockTimer()</p> <p>t.StartTimer()</p> <p>t.GetTime()</p> <p>3.9744668006896973</p> <p>t.Pause()</p> <p>t.UnPause()</p> tryout regex (Python) 2010-12-17T17:28:26-08:00Lost Protocolhttp://code.activestate.com/recipes/users/4176279/http://code.activestate.com/recipes/577503-tryout-regex/ <p style="color: grey"> Python recipe 577503 by <a href="/recipes/users/4176279/">Lost Protocol</a> (<a href="/recipes/tags/regex/">regex</a>). Revision 2. </p> <p>Teaches you regex in python by trying it out. </p> <pre class="prettyprint"><code>&gt;&gt;&gt; only one command called compile allows you to compile a certain string &gt;&gt;&gt; any other input is treated as pattern to match </code></pre> Polynomial Interpolation using Lagrange Polynomial (Python) 2010-11-28T03:03:36-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577474-polynomial-interpolation-using-lagrange-polynomial/ <p style="color: grey"> Python recipe 577474 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Polynomial Interpolation (curve-fitting) using Lagrange Polynomial.</p> Recursive function to replace nested for loops (cartesian product) (Python) 2010-10-01T00:40:26-07:00Kieranhttp://code.activestate.com/recipes/users/4175132/http://code.activestate.com/recipes/577415-recursive-function-to-replace-nested-for-loops-car/ <p style="color: grey"> Python recipe 577415 by <a href="/recipes/users/4175132/">Kieran</a> (<a href="/recipes/tags/cartesian_product/">cartesian_product</a>, <a href="/recipes/tags/code/">code</a>, <a href="/recipes/tags/for_loop/">for_loop</a>, <a href="/recipes/tags/python/">python</a>). Revision 3. </p> <p>Same functionality as the itertools product method (<a href="http://docs.python.org/library/itertools.html#itertools.product" rel="nofollow">http://docs.python.org/library/itertools.html#itertools.product</a>) with one major difference: generators are executed as the loop executes. An itertools product causes all the variables to be collected before the loop actually starts looping.</p> Python program that show .class files from *.jar (Python) 2010-07-22T09:13:59-07:00Michal Niklashttp://code.activestate.com/recipes/users/186902/http://code.activestate.com/recipes/577328-python-program-that-show-class-files-from-jar/ <p style="color: grey"> Python recipe 577328 by <a href="/recipes/users/186902/">Michal Niklas</a> (<a href="/recipes/tags/jar/">jar</a>, <a href="/recipes/tags/java/">java</a>). </p> <p>This program iterate through .jar files and print .class files from it</p> Win Shortcuts wrapper. (Python) 2010-07-23T08:10:44-07:00Louis RIVIEREhttp://code.activestate.com/recipes/users/4035877/http://code.activestate.com/recipes/577321-win-shortcuts-wrapper/ <p style="color: grey"> Python recipe 577321 by <a href="/recipes/users/4035877/">Louis RIVIERE</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>, <a href="/recipes/tags/windows/">windows</a>, <a href="/recipes/tags/wrapper/">wrapper</a>). Revision 3. </p> <p>Reads or Creates Windows shortcuts (links).</p> A Profiling Tool (Python) 2011-03-04T05:41:04-08:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577323-a-profiling-tool/ <p style="color: grey"> Python recipe 577323 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/profiling/">profiling</a>, <a href="/recipes/tags/timing/">timing</a>). Revision 2. </p> <p>This is a decorator for doing the profiling of a function.</p> A server that waits through dropped connections (Python) 2010-05-19T18:37:10-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/577232-a-server-that-waits-through-dropped-connections/ <p style="color: grey"> Python recipe 577232 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>The code is a barebones example of how to write a loop for a server so that it gracefully detects when the client has dropped the connection and goes back to listening for a new client.</p> sieve-of-eratosthenes (python) (Python) 2010-05-15T18:53:11-07:00Rahul Rajhttp://code.activestate.com/recipes/users/4173983/http://code.activestate.com/recipes/577228-sieve-of-eratosthenes-python/ <p style="color: grey"> Python recipe 577228 by <a href="/recipes/users/4173983/">Rahul Raj</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/efficient/">efficient</a>, <a href="/recipes/tags/sieve/">sieve</a>). </p> <p>sieve-of-eratosthenes algorithm with efficient scaling for big numbers.</p> Reversed Ranges (Python) 2014-03-29T12:43:26-07:00Tal Einathttp://code.activestate.com/recipes/users/2892534/http://code.activestate.com/recipes/576801-reversed-ranges/ <p style="color: grey"> Python recipe 576801 by <a href="/recipes/users/2892534/">Tal Einat</a> (<a href="/recipes/tags/iteration/">iteration</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/reverse/">reverse</a>). Revision 16. </p> <p>A simple function for efficiently iterating over ranges in reverse.</p> <p>This is equivalent to reversed(range(...)) but somewhat more efficient.</p>