Popular recipes tagged "meta:loc=6"http://code.activestate.com/recipes/tags/meta:loc=6/2016-11-06T11:33:59-08:00ActiveState Code RecipesRotate 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> Merge unique items from multiple lists into a new list (Python) 2016-04-05T18:11:49-07:00Johannes Shttp://code.activestate.com/recipes/users/4193888/http://code.activestate.com/recipes/580634-merge-unique-items-from-multiple-lists-into-a-new-/ <p style="color: grey"> Python recipe 580634 by <a href="/recipes/users/4193888/">Johannes S</a> (<a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/merge/">merge</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/set/">set</a>). Revision 2. </p> <p>Suppose you have multiple lists. You want to print all unique items from the list. Now, what you could do is merge the lists into one_big_list (e.g., a + b +c), and then iterate over each item in one_big_list, etc. The solution proposed here gets this done faster and in one line of code. How? By using <strong>a python set</strong>. A python set is a dictionary that contains only keys (and no values). And dictionary keys are, by definition, unique. Hence, duplicate items are weeded out automatically. Once you have the set, you can easily convert it back into a list. As easy as that!</p> Set the logging level to every logger (Python) 2015-10-28T23:16:26-07:00Tim McNamarahttp://code.activestate.com/recipes/users/4193044/http://code.activestate.com/recipes/579119-set-the-logging-level-to-every-logger/ <p style="color: grey"> Python recipe 579119 by <a href="/recipes/users/4193044/">Tim McNamara</a> (<a href="/recipes/tags/configuration/">configuration</a>, <a href="/recipes/tags/logging/">logging</a>). </p> <p>Python's logging allocates a name to every logger. That makes it hard to do something like, setting everything to <code>logging.ERROR</code>. Here's one way you might go about that:</p> Add function to Python's __builtin__ module through C API - makefile (Text) 2015-10-16T12:11:36-07:00airweenhttp://code.activestate.com/recipes/users/4192997/http://code.activestate.com/recipes/579111-add-function-to-pythons-__builtin__-module-through/ <p style="color: grey"> Text recipe 579111 by <a href="/recipes/users/4192997/">airween</a> (<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/c/">c</a>, <a href="/recipes/tags/makefile/">makefile</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Makefile for add function to __builtin__ module through C API.</p> <p>C source is here:</p> <p><a href="https://code.activestate.com/recipes/579110-add-function-to-__builtin__-module-through-c-api/" rel="nofollow">https://code.activestate.com/recipes/579110-add-function-to-__builtin__-module-through-c-api/</a></p> <p>Python script is here:</p> <p><a href="https://code.activestate.com/recipes/579112-add-function-to-__builtin__-module-through-c-api-c/" rel="nofollow">https://code.activestate.com/recipes/579112-add-function-to-__builtin__-module-through-c-api-c/</a></p> Drop a minimal, valid Windows executable file to disk, for testing (Python) 2015-03-03T10:47:32-08:00Simon Harrisonhttp://code.activestate.com/recipes/users/4191738/http://code.activestate.com/recipes/579029-drop-a-minimal-valid-windows-executable-file-to-di/ <p style="color: grey"> Python recipe 579029 by <a href="/recipes/users/4191738/">Simon Harrison</a> (<a href="/recipes/tags/executable/">executable</a>, <a href="/recipes/tags/pe/">pe</a>, <a href="/recipes/tags/windows/">windows</a>). Revision 3. </p> <p>Sometimes I need to create a valid windows executable file from a Python script for the sake of running a test.</p> smart copy (Python) 2015-02-06T09:45:12-08:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/579020-smart-copy/ <p style="color: grey"> Python recipe 579020 by <a href="/recipes/users/4184815/">yota</a> (<a href="/recipes/tags/shutil/">shutil</a>). </p> <p>take a glob expression, a source directory and a destination directory to copy each files matching the glob in the appropriate directory</p> <pre class="prettyprint"><code>glob = */*.txt src_dir = ./a/b dst_dir = /z/x/y </code></pre> <p>if the glob match a file <code>./a/b/c/foo.txt</code>, it will copy it in <code>/z/x/y/c/foo.txt</code> (and create the missing directory if needed)</p> <p>Require Python3.4, code tab indented</p> create a unique session key (Python) 2014-07-03T16:32:53-07:00john stinsonhttp://code.activestate.com/recipes/users/4190325/http://code.activestate.com/recipes/578903-create-a-unique-session-key/ <p style="color: grey"> Python recipe 578903 by <a href="/recipes/users/4190325/">john stinson</a> (<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/sessions/">sessions</a>). </p> <p>A simple function that will generate a secure and unique session key.</p> Embedding media content in an HTML page with PyEmbed and Python (Python) 2014-02-15T21:21:30-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578833-embedding-media-content-in-an-html-page-with-pyemb/ <p style="color: grey"> Python recipe 578833 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/multimedia/">multimedia</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/video/">video</a>). </p> <p>This recipe shows how to embed multimedia content such as a YouTube video in a web page, using the PyEmbed library for Python.</p> A Simple Clock, Well Maybe Not That Simple... (Python) 2013-08-09T18:15:09-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578633-a-simple-clock-well-maybe-not-that-simple/ <p style="color: grey"> Python recipe 578633 by <a href="/recipes/users/4177147/">Barry Walker</a> (<a href="/recipes/tags/amiga/">amiga</a>, <a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/big/">big</a>, <a href="/recipes/tags/clock/">clock</a>, <a href="/recipes/tags/digits/">digits</a>, <a href="/recipes/tags/e_uae/">e_uae</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/winuae/">winuae</a>). </p> <p>BIG_TIME.py</p> <p>Remember this?</p> <p><a href="http://code.activestate.com/recipes/578610-a-simple-clock-well-maybe-not-that-simple/" rel="nofollow">http://code.activestate.com/recipes/578610-a-simple-clock-well-maybe-not-that-simple/</a></p> <p>And this?</p> <p><a href="http://code.activestate.com/recipes/578079-pure-fun-for-text-mode-python/?in=user-4177147" rel="nofollow">http://code.activestate.com/recipes/578079-pure-fun-for-text-mode-python/?in=user-4177147</a></p> <p>Well the Python version of the top URL has been uploaded to AMINET on 01-08-2013...</p> <p>Now the big gun that complained about my fun Python upload and voted it down can see that this was a serious attempt ae viewing "Hello World!" in extra large xharacters.</p> <p>This is the result in Python...</p> <p>It is ONLY uploaded to AMINET and that is where it will stay.</p> <p>If you want it then use the pointer inside code section.</p> <p>Remember to change the BIG_TIME.py.txt file on AMINET to BIG_TIME.py...</p> <p>Designed around a Macbook Pro OSX 10.7.5 for the Classic AMIGA platforms and works on Debian 6.0.x and PCLiuxOS 2009 using Python versions 1.4.0 to 3.3.2 inclusive...</p> <p>Although the description in the AMINET readme below is for an AMIGA environment it does not take a genius to adapt it to the other platforms...</p> <p>Boy was this difficult to get working in a single Python script...</p> <p>ENJOY...</p> <p>This is the AMINET readme file:-</p> <p>Short: A Big Digital Clock Python Script. Author: <a href="mailto:wisecracker@tesco.net">wisecracker@tesco.net</a> (Barry Walker) Uploader: <a href="mailto:wisecracker@tesco.net">wisecracker@tesco.net</a> (Barry Walker) Type: dev/src Version: 1.00.00 Architecture: generic; m68k-amigaos</p> <p>Distribution: Public Domain, Emailware.</p> <p>============================================================================</p> <h5 id="architectures-classic-amigas-e-uae-and-winuae">Architectures: Classic AMIGAs, E-UAE and WinUAE.</h5> <p>MINIMUM Requirements Are:-</p> <hr /> <pre class="prettyprint"><code> 68EC020 CPU and 2MB RAM total, example, a standard A1200(HD). WinUAE Version 1.5.3 minimum. Standard OS3.0x install or better. Python Version 1.4.0 or higher. (Also, OSX 10.7.5, Debian Linux 6.0.x and PCLinuxOS 2009.) </code></pre> <hr /> <h5 id="history">History.</h5> <p>01-08-2013. Version 1.00.00:- Original upload to AMINET...</p> <p>This is dedicated entirely to the AMIGA community and AMINET and will not be uploaded elsewhere.</p> <hr /> <p>To install just download the "BIG_TIME.py.txt" file and rename it to "BIG_TIME.py" without the quotes. Drag this file into your AMIGA PYTHON: volume and...</p> <p>From a minimum of Python Version 1.4.0 prompt:-</p> <pre class="prettyprint"><code>&gt;&gt;&gt; exec(open("PYTHON:BIG_TIME.py").read())&lt;CR&gt; </code></pre> <p>And away you go...</p> <p>This code is/was designed to work in all versions of Python from 1.4.0 to 3.3.2 on at least the platforms shown in the Architectures: section.</p> <p>It was developed entirely on a Macbook Pro OSX 10.7.5 and finalised using MEmacs for the AMIGA.</p> <hr /> <p>This is just a simple DEMO to show how to generate large digits in Python and put them to use as an _AT_A_GLANCE_ digital clock.</p> <p>It starts up by generating all of the characters available for the DEMO for five seconds then goes into the clock proper.</p> <p>As this code is multi platform AND multi Python version then it is issued as GPL2...</p> <p>Enjoy finding simple solutions to often very difficult problems...</p> <hr /> <pre class="prettyprint"><code> IMPORTANT:- ----------- The Legal Stuff:- ----------------- This Python script is issued under the GPL2 licence conditions. The author is not responsible for any damage to, or loss of, or failure of equipment or data caused in any way by the use of this script. There is NO warranty with the use of this software release and YOU USE IT AT YOUR OWN RISK. </code></pre> <hr /> <pre class="prettyprint"><code>Testing Evaluation:- -------------------- All WinUAE, E-UAE and Classic AMIGA test conditions were/are running standard OS3.0x/OS3.1x and using standard ~topaz 8~ fonts throughout. I have no idea what strange configuration setups will create so refer to the ~The Legal Stuff~ above. </code></pre> <hr /> <p>Contact:-</p> <hr /> <p>Mr Barry Walker, G0LCU.</p> <p>Email:- <a href="mailto:wisecracker@tesco.net">wisecracker@tesco.net</a></p> <p>Author of the ~TestGear?~ projects in the ~docs/hard~ drawer of AMINET.</p> <hr /> <p>A very useful HardWare related site, (C) Anthony Hoffman, for modifications, schematics, repairs and the like is:-</p> <pre class="prettyprint"><code> <a href="http://amiga.serveftp.net/">http://amiga.serveftp.net/</a> </code></pre> <p>============================================================================</p> Get external IP (Perl) 2013-07-01T05:48:51-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578581-get-external-ip/ <p style="color: grey"> Perl recipe 578581 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/ipv4/">ipv4</a>). Revision 2. </p> <p>Checks IPv4</p> warm welcome (Python) 2013-02-03T06:24:46-08:00Rayhttp://code.activestate.com/recipes/users/4185136/http://code.activestate.com/recipes/578448-warm-welcome/ <p style="color: grey"> Python recipe 578448 by <a href="/recipes/users/4185136/">Ray</a> (<a href="/recipes/tags/python_2_5/">python_2_5</a>). </p> <p>just a warm welcome.</p> Play a Sound File in Python using PyGame (Python) 2013-01-08T18:08:55-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578410-play-a-sound-file-in-python-using-pygame/ <p style="color: grey"> Python recipe 578410 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/pygame/">pygame</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sound/">sound</a>). </p> <p>This is how you can play an ogg sound file in python using the pygame library. This is part of an article about <a href="http://thelivingpearl.com/2013/01/08/morse-code-and-dictionaries-in-python-with-sound/">Morse Code and Dictionaries in Python</a></p> Correctly reading CSV files in arbitrary encodings (Python) 2011-07-25T07:29:08-07:00Devin Jeanpierrehttp://code.activestate.com/recipes/users/4178508/http://code.activestate.com/recipes/577778-correctly-reading-csv-files-in-arbitrary-encodings/ <p style="color: grey"> Python recipe 577778 by <a href="/recipes/users/4178508/">Devin Jeanpierre</a> (<a href="/recipes/tags/csv/">csv</a>, <a href="/recipes/tags/unicode/">unicode</a>). Revision 4. </p> <p>Recipe for using unicode files (i.e. files opened with <code>codecs.open</code>) with the csv module.</p> scale rectangle while keeping aspect ratio (Python) 2011-02-20T22:36:39-08:00Romain Dartigueshttp://code.activestate.com/recipes/users/4167472/http://code.activestate.com/recipes/577575-scale-rectangle-while-keeping-aspect-ratio/ <p style="color: grey"> Python recipe 577575 by <a href="/recipes/users/4167472/">Romain Dartigues</a> (<a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/resize/">resize</a>, <a href="/recipes/tags/simple/">simple</a>). Revision 2. </p> <p>Resize a rectangle (ie.: an image) while keeping the aspect ratio, without float.</p> <p>You can scale up and down the proportions so the edges will be either <strong>at most</strong> or <strong>at least</strong> the given one.</p> Counting decorator (Python) 2011-01-07T11:22:55-08:00Noufal Ibrahimhttp://code.activestate.com/recipes/users/4173873/http://code.activestate.com/recipes/577534-counting-decorator/ <p style="color: grey"> Python recipe 577534 by <a href="/recipes/users/4173873/">Noufal Ibrahim</a> (<a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/testing/">testing</a>). </p> <p>To be used as a decorator for a function that will maintain the number of times it was called. Here is an example use. </p> <pre class="prettyprint"><code>&gt;&gt;&gt; def test(): ... print "Hello" ... &gt;&gt;&gt; test = counter(test) &gt;&gt;&gt; test() Hello &gt;&gt;&gt; test() Hello &gt;&gt;&gt; test() Hello &gt;&gt;&gt; test() Hello &gt;&gt;&gt; test() Hello &gt;&gt;&gt; test.invocations 5 &gt;&gt;&gt; test() Hello &gt;&gt;&gt; test.invocations 6 &gt;&gt;&gt; </code></pre> Open a python module given it's name in Emacs (Bash) 2010-07-19T10:25:06-07:00Noufal Ibrahimhttp://code.activestate.com/recipes/users/4173873/http://code.activestate.com/recipes/577317-open-a-python-module-given-its-name-in-emacs/ <p style="color: grey"> Bash recipe 577317 by <a href="/recipes/users/4173873/">Noufal Ibrahim</a> (<a href="/recipes/tags/emacs/">emacs</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python_developer_tools/">python_developer_tools</a>, <a href="/recipes/tags/zsh/">zsh</a>). </p> <p>A tiny little <code>zsh</code> function to open a python module in <code>Emacs</code>. Assumes that an <code>Emacs</code> server process is running. </p> Access grep from python (Python) 2010-02-24T08:30:25-08:00Daniel Cohnhttp://code.activestate.com/recipes/users/4172918/http://code.activestate.com/recipes/577069-access-grep-from-python/ <p style="color: grey"> Python recipe 577069 by <a href="/recipes/users/4172918/">Daniel Cohn</a> (<a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/linux/">linux</a>). </p> <p>The recipe below passes a filename and an argument to grep, returning the stdout and stderr. Each line in the stdout will have its line number prepended.</p> Design Pattern -- Inherit from an instance (Python) 2010-02-22T06:04:16-08:00Daniel Cohnhttp://code.activestate.com/recipes/users/4172918/http://code.activestate.com/recipes/577026-design-pattern-inherit-from-an-instance/ <p style="color: grey"> Python recipe 577026 by <a href="/recipes/users/4172918/">Daniel Cohn</a> (<a href="/recipes/tags/design_pattern/">design_pattern</a>, <a href="/recipes/tags/factory_class/">factory_class</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/instance/">instance</a>). Revision 4. </p> <p>Take an instance (call it foo) and create a factory class (call it InstanceFactory) that produces foo's. Then inherit from InstanceFactory.</p> Komodo JS Macro - select the current word under the cursor (JavaScript) 2010-01-14T12:30:17-08:00Todd Whitemanhttp://code.activestate.com/recipes/users/2666241/http://code.activestate.com/recipes/577007-komodo-js-macro-select-the-current-word-under-the-/ <p style="color: grey"> JavaScript recipe 577007 by <a href="/recipes/users/2666241/">Todd Whiteman</a> (<a href="/recipes/tags/cursor/">cursor</a>, <a href="/recipes/tags/javascript/">javascript</a>, <a href="/recipes/tags/komodo/">komodo</a>, <a href="/recipes/tags/macro/">macro</a>, <a href="/recipes/tags/toddw/">toddw</a>, <a href="/recipes/tags/word/">word</a>). </p> <p>A <a href="http://www.activestate.com/komodo">Komodo</a> JavaScript macro that can be used to select the current word under the cursor position.</p> <p>Python version here: <a href="http://code.activestate.com/recipes/577006/">Recipe 577006</a>. </p> Altering immutable string object. (Python) 2010-01-11T06:11:05-08:00Pepe Aracilhttp://code.activestate.com/recipes/users/4171769/http://code.activestate.com/recipes/577000-altering-immutable-string-object/ <p style="color: grey"> Python recipe 577000 by <a href="/recipes/users/4171769/">Pepe Aracil</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/immutable/">immutable</a>). Revision 3. </p> <p>"Useless" alteration of immutable string object in 100% pure python. :) </p>