Popular recipes by ccpizza http://code.activestate.com/recipes/users/4170754/2016-04-28T13:24:15-07:00ActiveState Code RecipesRecursive find replace in files using regex (Python) 2016-04-28T13:24:15-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/580653-recursive-find-replace-in-files-using-regex/ <p style="color: grey"> Python recipe 580653 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/replace/">replace</a>, <a href="/recipes/tags/search/">search</a>). Revision 5. </p> <h4 id="recursively-find-and-replace-text-in-files-under-a-specific-folder-with-preview-of-changed-data-in-dry-run-mode">Recursively find and replace text in files under a specific folder with preview of changed data in dry-run mode</h4> <h5 id="example-usage">Example Usage</h5> <p><strong>See what is going to change (dry run):</strong></p> <pre class="prettyprint"><code>find_replace.py --dir project/myfolder --search-regex "\d{4}-\d{2}-\d{2}" --replace-regex "2012-12-12" --dryrun </code></pre> <p><strong>Do actual replacement:</strong></p> <pre class="prettyprint"><code>find_replace.py --dir project/myfolder --search-regex "\d{4}-\d{2}-\d{2}" --replace-regex "2012-12-12" </code></pre> <p><strong>Do actual replacement and create backup files:</strong></p> <pre class="prettyprint"><code>find_replace.py --dir project/myfolder --search-regex "\d{4}-\d{2}-\d{2}" --replace-regex "2012-12-12" --create-backup </code></pre> <p><strong>Same action as previous command with short-hand syntax:</strong></p> <pre class="prettyprint"><code>find_replace.py -d project/myfolder -s "\d{4}-\d{2}-\d{2}" -r "2012-12-12" -b </code></pre> <p>Output of <code>find_replace.py -h</code>:</p> <pre class="prettyprint"><code>DESCRIPTION: Find and replace recursively from the given folder using regular expressions optional arguments: -h, --help show this help message and exit --dir DIR, -d DIR folder to search in; by default current folder --search-regex SEARCH_REGEX, -s SEARCH_REGEX search regex --replace-regex REPLACE_REGEX, -r REPLACE_REGEX replacement regex --glob GLOB, -g GLOB glob pattern, i.e. *.html --dryrun, -dr don't replace anything just show what is going to be done --create-backup, -b Create backup files USAGE: find_replace.py -d [my_folder] -s &lt;search_regex&gt; -r &lt;replace_regex&gt; -g [glob_pattern] </code></pre> Enable Clear Type font smoothing on Windows (ctypes version) (Python) 2013-05-07T09:17:56-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/578500-enable-clear-type-font-smoothing-on-windows-ctypes/ <p style="color: grey"> Python recipe 578500 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/fontsmoothing/">fontsmoothing</a>, <a href="/recipes/tags/windows/">windows</a>). Revision 3. </p> <p>Running the script without parameters will enable Clear Type font smoothing. Pass 0, false, off, or disable to turn off Clear Type.</p> <p>This version requires the <code>ctypes</code> module.</p> <p>For a version that uses pywin32 see <a href="http://code.activestate.com/recipes/578499-enable-clear-type-font-smoothing-on-windows-pywin3/" rel="nofollow">http://code.activestate.com/recipes/578499-enable-clear-type-font-smoothing-on-windows-pywin3/</a> </p> Enable Clear Type font smoothing on Windows (pywin32 version) (Python) 2013-03-22T11:14:44-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/578499-enable-clear-type-font-smoothing-on-windows-pywin3/ <p style="color: grey"> Python recipe 578499 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/fontsmoothing/">fontsmoothing</a>, <a href="/recipes/tags/pywin32/">pywin32</a>, <a href="/recipes/tags/windows_api/">windows_api</a>). Revision 2. </p> <p>Running the script without parameters will enable Clear Type font smoothing. Pass <code>0</code>, <code>false</code>, <code>off</code>, or <code>disable</code> to turn off Clear Type.</p> <p>This version requires the <code>pywin32</code> module from <a href="http://sourceforge.net/projects/pywin32/files/pywin32/Build%2520218/" rel="nofollow">http://sourceforge.net/projects/pywin32/files/pywin32/Build%20218/</a></p> <p>For a <code>ctypes</code>-based version see: <a href="http://code.activestate.com/recipes/578500-enable-clear-type-font-smoothing-on-windows-ctypes/" rel="nofollow">http://code.activestate.com/recipes/578500-enable-clear-type-font-smoothing-on-windows-ctypes/</a></p> Recursive find and replace in all files in directory with regex (Python) 2013-03-21T12:33:44-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/578312-recursive-find-and-replace-in-all-files-in-directo/ <p style="color: grey"> Python recipe 578312 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/regular_expressions/">regular_expressions</a>, <a href="/recipes/tags/replace/">replace</a>). </p> <p>Search and replace files recursively in a given directory. Accepts regular expressions as search and replacement patterns.</p> <p><strong>Example usage</strong>:</p> <pre class="prettyprint"><code>python find_replace_regex.py ".", "&lt;span[^&gt;]*&gt;", "&lt;div&gt;", "*.html" backup </code></pre> <p><strong>Note</strong>: On win32 the <code>&lt;</code> and <code>&gt;</code> symbols are interpreted by the shell as input/output redirection even when they are surrounded with single or double quotes, and therefore need to be escaped with the caret character <code>^</code>.</p> Android Gmail: export messages from SQLite database blobs (Python) 2013-03-22T11:45:11-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/578106-android-gmail-export-messages-from-sqlite-database/ <p style="color: grey"> Python recipe 578106 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/android/">android</a>, <a href="/recipes/tags/sqlite/">sqlite</a>). Revision 5. </p> <p>This script will extract email message bodies from the SQLite database stored in an android phone.</p> <p>The gmail database is typically located on your phone under the following location:</p> <pre class="prettyprint"><code>`\data\data\com.google.android.gm\databases\mailstore.YOURUSERNAME@gmail.com.db` </code></pre> <p>To use the script, copy the file above from your phone to your machine and rename it to <code>gmail.db</code>.</p> <p><em>NOTE:</em> You need a rooted phone in order to get access to the folder above.</p> Dropbox file uploader via web interface using Python with urllib2 and mechanize (Python) 2016-04-28T13:21:43-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/578030-dropbox-file-uploader-via-web-interface-using-pyth/ <p style="color: grey"> Python recipe 578030 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/dropbox/">dropbox</a>, <a href="/recipes/tags/mechanize/">mechanize</a>). Revision 7. </p> <p>UPDATE: This is script is not maintained and does not anymore with the current version of Dropbox. For a proper command line interface to dropbox I recommend <code>dropbox_uploader</code>: <a href="https://github.com/andreafabrizi/Dropbox-Uploader" rel="nofollow">https://github.com/andreafabrizi/Dropbox-Uploader</a></p> <p>Originally inspired by the example at <a href="http://joncraton.org/blog/62/uploading-dropbox-python" rel="nofollow">http://joncraton.org/blog/62/uploading-dropbox-python</a>. </p> <p>The script uses mechanize to logon to the web page and upload the file(s) to the Dropbox root folder or to the folder supplied on the command line as <code>dir:/my_dropbox_path</code> (if present, this must be the first parameter).</p> <p>Multiple files and/or glob patterns names are accepted as script arguments.</p> <h5 id="example-usage">Example usage</h5> <pre class="prettyprint"><code>dropbox.py file1.txt # upload to root folder dropbox.py dir:/Backups/2012 file1.txt # upload to custom folder dropbox.py dir:/Backups/2012 *.txt # upload by file mask dropbox.py dir:/Backups/2020 * # upload all files in current dir </code></pre> <p>Limitations: only files in current folder are processed, subfolders are ignored.</p> <h5 id="note">NOTE</h5> <p>The script requires the <code>mechanize</code> module - use <code>pip install mechanize</code> or <code>easy_install mechanize</code> to add it to your site-packages.</p> <h5 id="note2">NOTE2</h5> <p>I have found a cleaner way to manage dropbox files from the console - see the <em>dropbox-uploade</em>r script at <a href="https://github.com/andreafabrizi/Dropbox-Uploader" rel="nofollow">https://github.com/andreafabrizi/Dropbox-Uploader</a> - it is a Bash script that works using the official Dropbox API rather than emulating a web browser.</p> Base64 encode-decode Five Liner (Python) 2013-04-26T09:37:21-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/577626-base64-encode-decode-five-liner/ <p style="color: grey"> Python recipe 577626 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/base64/">base64</a>). Revision 2. </p> <p>Encodes or decodes base64-encoded files. If the input or output file(s) are ommited standard input/output will be used.</p> <p>The encoding direction is detected from the file name: for decoding save the script e.g. as encode64.py. For decoding create a symlink with <code>decode</code> in the name.</p> Minimalistic PostgreSQL console shell (Python) 2011-11-30T13:43:28-08:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/577468-minimalistic-postgresql-console-shell/ <p style="color: grey"> Python recipe 577468 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/postgres/">postgres</a>, <a href="/recipes/tags/postgresql/">postgresql</a>, <a href="/recipes/tags/sql/">sql</a>). Revision 17. </p> <p>Primitive shell for running PostgreSQL quieries from console. Make sure you set the right connection settings in the script.</p> <p>All queries are executed in autocommit mode.</p> <p>Example command line usage:</p> <pre class="prettyprint"><code>pg.py select * from mytable where oidh=1 </code></pre> <p>The script recognizes a number of magic keywords:</p> <pre class="prettyprint"><code>pg.py show databases pg.py show schemas pg.py describe &lt;table_name&gt; </code></pre> Embed lyrics into MP3 files using mutagen (USLT tag), optionally set other ID3 tags (Python) 2011-05-17T15:56:56-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/577138-embed-lyrics-into-mp3-files-using-mutagen-uslt-tag/ <p style="color: grey"> Python recipe 577138 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/id3/">id3</a>, <a href="/recipes/tags/lyrics/">lyrics</a>, <a href="/recipes/tags/mp3/">mp3</a>, <a href="/recipes/tags/uslt/">uslt</a>). Revision 5. </p> <p>Quick and dirty script to embed unsynchronized lyrics or any other text into MP3 files. The text files with the lyrics are expected to be in the same folder: i.e. for MySong.mp3 the lyrics text should be in the file MySong.txt.</p> <p>The encoding of the text file will be probed in the following order: 'utf8','iso-8859-1','iso-8859-15','cp1252','cp1251','latin1'. If you need support for more encodings, a list is available at <a href="http://docs.python.org/release/2.5.2/lib/standard-encodings.html" rel="nofollow">http://docs.python.org/release/2.5.2/lib/standard-encodings.html</a></p> <p>To see the lyrics on an iPod (tested on 6G Classic) you need to press the middle button four times while a song is playing.</p> <p>The script can also be used to set other ID3 tags. By default SET_OTHER_ID3_TAGS is False so existing ID3 tags will NOT be overwritten.</p> <p>Usage: Running the file without arguments will process all MP3 files in the current directory.</p> <p>Alternatively the path to the folder with MP3's can be passed as the first argument.</p> Copy files over SSH using paramiko (Python) 2015-12-06T14:19:15-08:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/576810-copy-files-over-ssh-using-paramiko/ <p style="color: grey"> Python recipe 576810 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/paramiko/">paramiko</a>, <a href="/recipes/tags/scp/">scp</a>, <a href="/recipes/tags/ssh/">ssh</a>). Revision 20. </p> <p>This script copies files in unattended mode over SSH using a glob pattern. It uses the <a href="http://www.lag.net/paramiko/">paramiko</a> module behind the scenes. It operates as an actual SSH client, and does <em>not</em> rely on any command line utilities, such as <code>scp</code>.</p> <p>It first tries to connect using a key from a private key file or from an SSH agent. If RSA authentication fails, it will try to authenticate with a password if passwords are allowed on the SSH server. Assumes the <code>rsa_private_key</code> was generated with an empty passphrase.</p> <p>On most linux/unix-like systems paramiko can be installed with <code>sudo easy_install paramiko</code> or <code>pip install paramiko</code> or using the built-in system package manager.</p> <p>See also <a href="http://www.paramiko.org/installing.html" rel="nofollow">http://www.paramiko.org/installing.html</a></p> Remove ID3 tags from MP3 files (Python) 2010-03-29T10:55:27-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/577139-remove-id3-tags-from-mp3-files/ <p style="color: grey"> Python recipe 577139 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/id3/">id3</a>, <a href="/recipes/tags/mp3/">mp3</a>, <a href="/recipes/tags/mutagen/">mutagen</a>). Revision 3. </p> <p>Remove ID3 tags from all files in the current directory</p> Rename non-ASCII filenames to readable ASCII, i.e. replace accented characters, etc (Python) 2011-11-03T17:50:55-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/577226-rename-non-ascii-filenames-to-readable-ascii-ie-re/ <p style="color: grey"> Python recipe 577226 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/renaming/">renaming</a>, <a href="/recipes/tags/unicode/">unicode</a>). Revision 3. </p> <p>The script converts any accented characters in filenames to their ASCII equivalents. e.g.:</p> <p>Example:</p> <pre class="prettyprint"><code>â &gt; a ä &gt; a à &gt; a á &gt; a é &gt; e í &gt; i ó &gt; o ú &gt; u ñ &gt; n ü &gt; u ... </code></pre> <p>Before-and-after example:</p> <pre class="prettyprint"><code>01_Antonín_Dvořák_Allegro.mp3 &gt;&gt;&gt; 01_Antonin_Dvorak_Allegro.mp3 </code></pre> <p>Usage:</p> <pre class="prettyprint"><code>Running the script without arguments will rename all files in the current folder. !!!WARNING!!! ***No*** backups are created. </code></pre> Download chromium browser nightly builds for any OS (with proxy support) (Python) 2014-07-05T18:47:47-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/577162-download-chromium-browser-nightly-builds-for-any-o/ <p style="color: grey"> Python recipe 577162 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/chrome/">chrome</a>, <a href="/recipes/tags/chromium/">chromium</a>, <a href="/recipes/tags/download/">download</a>, <a href="/recipes/tags/unzip/">unzip</a>). Revision 19. </p> <p>Downloads the latest Chromium browser build from <a href="http://commondatastorage.googleapis.com/chromium-browser-continuous/" rel="nofollow">http://commondatastorage.googleapis.com/chromium-browser-continuous/</a> using urllib2 or wget (with Python versions below 2.5) and unzips the downloaded zip file to a predefined folder.</p> <p>To use a custom proxy define the <code>HTTP_PROXY</code> system variable.</p> <p>The script will figure out the OS but you can also pass the platform as the first parameter (one of <code>win32, linux, linux64, mac</code>).</p> <p><em>Prerequisites (!! only for Python versions below 2.5):</em></p> <ul> <li><p><code>wget</code> - usually already installed on most linux systems. Windows users can get it <a href="http://gnuwin32.sourceforge.net/packages/wget.htm">here</a>.</p></li> <li><p><code>unzip</code> - used for unpacking the archive; usually already installed on most linux systems. Windows users can get it <a href="http://gnuwin32.sourceforge.net/packages/unzip.htm">here</a>.</p></li> </ul> <p>Both <code>wget</code> and <code>unzip</code> should be available in PATH (for Python 2.5+ native Python modules are used).</p> <p>For most Linux distros this script does not make much sense since the built-in package managers do a better job of managing chromium builds and dependencies, but it still can be useful if you are using a stable Chromium build but would like to be able to test the nightly builds too.</p> <p>For OSX an additional installation step will be performed using the <code>install.sh</code> that is included in the OSX build. The OSX installer will copy the package to <code>~/Applications/Chromium</code>, and set some permissions that are required for Chromium to run. Running the unpacked zip without doing the installation will most likely will not work because of missing executable permissions on some files.</p> Convert PDF to plain text (Python) 2010-11-25T15:30:52-08:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/577095-convert-pdf-to-plain-text/ <p style="color: grey"> Python recipe 577095 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/pdf/">pdf</a>). </p> <p>This is a very raw PDF converter which has absolutely no idea of the page layout or text positioning.</p> <p>To install the required module try <code>easy_install pypdf</code> in a console.</p> Convert Trac wiki markup to HTML (Python) 2010-03-09T08:02:36-08:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/577094-convert-trac-wiki-markup-to-html/ <p style="color: grey"> Python recipe 577094 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/trac/">trac</a>). </p> <p>Convert trac wiki markup to HTML Usage: %s &lt;inputfile.wiki&gt; [outputfile.html] If outputfile.html is missing output is redirected to stdout in utf8 encoding.</p> Crop PDF File with pyPdf (Python) 2011-11-03T17:42:10-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/576837-crop-pdf-file-with-pypdf/ <p style="color: grey"> Python recipe 576837 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pypdf/">pypdf</a>). Revision 3. </p> <p>This recipe was originally posted by <code>sjvr767</code> on <a href="http://www.mobileread.com/forums/showthread.php?t=25565" rel="nofollow">http://www.mobileread.com/forums/showthread.php?t=25565</a> and I decided to also make it available here.</p> <p>It uses pypdf (<a href="http://pybrary.net/pyPdf/%29" rel="nofollow">http://pybrary.net/pyPdf/)</a></p> <p>The script is supposed to be run like this:</p> <p><code>pdf_crop.py" -m "120 50 120 180" -i mypdf.pdf</code></p> <p>where the margins are <code>left top right bottom</code></p> <p>To install pyPdf try <code>easy_install pypdf</code>.</p> Rename MP3 files from ID3 tags (does not require external ID3 modules) (Python) 2011-11-05T15:27:32-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/576811-rename-mp3-files-from-id3-tags-does-not-require-ex/ <p style="color: grey"> Python recipe 576811 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/id3/">id3</a>, <a href="/recipes/tags/mp3/">mp3</a>, <a href="/recipes/tags/rename/">rename</a>). Revision 5. </p> <p>Rename MP3 files in the current folder according to ID3 tags. This is based on Ned Batchelder's id3reader class. I only added the code in the <code>__main__</code> method.</p> <ul> <li>When run without arguments, the script renames the files in the current folder. The directory can be specified explicitly as the first argument.</li> <li>The files do not need to necessarily have the <code>MP3</code> extension.</li> <li>To move the files to directories based on album name use the <code>-d</code> switch.</li> <li>Does not work with other file types, such as OGG, M4A, MP4, etc.</li> <li>This is a quick and dirty script that works for me most of the times. If you need more power, try mutagen, picard, mp3tag or something similar.</li> </ul> Generate HTML syntax-highlighted listings for any file using pygments (Python) 2011-02-01T14:21:59-08:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/576812-generate-html-syntax-highlighted-listings-for-any-/ <p style="color: grey"> Python recipe 576812 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/pretty_printer/">pretty_printer</a>, <a href="/recipes/tags/pygments/">pygments</a>, <a href="/recipes/tags/syntax_highlighter/">syntax_highlighter</a>). Revision 6. </p> <p>Generates HTML highlighted code listings for source code files in any language known to pygments. For a list of supported formats see <a href="http://pygments.org/languages" rel="nofollow">http://pygments.org/languages</a></p> <p>Make sure you have <a href="http://pygments.org">pygments</a> is installed. Try <code>easy_install pygments</code>.</p> <p>Example usage:</p> <p><em>output to stdout:</em></p> <pre class="prettyprint"><code>python highlight.py my_source_file.java </code></pre> <p><em>output to file:</em></p> <pre class="prettyprint"><code>python highlight.py my_source_file.java &gt; my_source_file.html </code></pre>