Popular recipes tagged "meta:loc=94"http://code.activestate.com/recipes/tags/meta:loc=94/2016-03-23T14:14:26-07:00ActiveState Code RecipesSafely and atomically write to a file (Python)
2016-03-23T14:14:26-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/579097-safely-and-atomically-write-to-a-file/
<p style="color: grey">
Python
recipe 579097
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/atomic/">atomic</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/save/">save</a>).
Revision 3.
</p>
<p>Saving the user's data is risky. If you write to a file directly, and an error occurs during the write, you may corrupt the file and lose the user's data. One approach to prevent this is to write to a temporary file, then only when you know the file has been written successfully, over-write the original. This function returns a context manager which can make this more convenient.</p>
<p>Update: this now uses <code>os.replace</code> when available, and hopefully will work better on Windows.</p>
Convert doc and docx files to pdf (Python)
2014-03-31T18:39:16-07:00Fabian Mayerhttp://code.activestate.com/recipes/users/4189629/http://code.activestate.com/recipes/578858-convert-doc-and-docx-files-to-pdf/
<p style="color: grey">
Python
recipe 578858
by <a href="/recipes/users/4189629/">Fabian Mayer</a>
(<a href="/recipes/tags/doc/">doc</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/win32com/">win32com</a>).
Revision 2.
</p>
<p>The Script converts all doc and docx files in a specified folder to pdf files. It checks whether the provided absolute path does actually exist and whether the specified folder contains any doc and docx files. It does not travers the directory recursively. The script is not portable and runs only a Windows machine. Based on the experience I made, I recommend closing MS Word before running the script.</p>
Numerical Inversion of the Laplace Transform with mpmath (Python)
2013-12-27T05:10:12-08:00Pawel Olejnikhttp://code.activestate.com/recipes/users/4188852/http://code.activestate.com/recipes/578799-numerical-inversion-of-the-laplace-transform-with-/
<p style="color: grey">
Python
recipe 578799
by <a href="/recipes/users/4188852/">Pawel Olejnik</a>
(<a href="/recipes/tags/analysis/">analysis</a>, <a href="/recipes/tags/mpmath/">mpmath</a>, <a href="/recipes/tags/numerical/">numerical</a>).
</p>
<p>This Recipe is a variant of <a href="http://code.activestate.com/recipes/576934/">recipe 576934</a>: Numerical Inversion of the Laplace Transform using the Talbot method by Fernando Damian Nieuwveldt adapted to high precision mpmath</p>
A Building Block, Bash Binary File Manipulation... (Bash)
2013-01-29T22:07:57-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578441-a-building-block-bash-binary-file-manipulation/
<p style="color: grey">
Bash
recipe 578441
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/binary/">binary</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/script/">script</a>, <a href="/recipes/tags/shell/">shell</a>).
Revision 2.
</p>
<p>Apologies for any typos, and IF this has been done before...</p>
<p>The code generates a 256 byte binary file of _characters_ 0x00 to 0xFF for general usage and generates another binary file manipulated in a basic way.</p>
<p>The for loops in the code are purely for DEMO purposes only.</p>
<p>This is Public Domain and you may do with it as you please. I have uploaded it elsewhere too...</p>
<p>Watch for wordwrapping, etc and read the code for more information...</p>
<p>Enjoy finding simple solutions to often very difficult problems...</p>
<p>Bazza, G0LCU...</p>
xdiff in python (Python)
2012-11-12T20:59:54-08:00Hirohttp://code.activestate.com/recipes/users/4184239/http://code.activestate.com/recipes/578324-xdiff-in-python/
<p style="color: grey">
Python
recipe 578324
by <a href="/recipes/users/4184239/">Hiro</a>
(<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/process/">process</a>).
Revision 2.
</p>
<p>xdiff.py compares and output difference of file1 and file2. it finds out the lines that are unique in file1 and files2, the finding is written to file3 and file4 respectively.</p>
Set windows 7 to lock itself (upon timeout) if no internet connection found - security measure (Python)
2012-07-09T20:56:22-07:00commentator8http://code.activestate.com/recipes/users/4182761/http://code.activestate.com/recipes/578200-set-windows-7-to-lock-itself-upon-timeout-if-no-in/
<p style="color: grey">
Python
recipe 578200
by <a href="/recipes/users/4182761/">commentator8</a>
(<a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/windows/">windows</a>, <a href="/recipes/tags/windows_registry/">windows_registry</a>).
Revision 2.
</p>
<p>This script (or exe if using web2py with """setup(windows=['locker2.py']))""") can be run as a task in windows every x minutes and will test for the presence of an internet connection and depending on whether it is found will set windows to lock after a given timeout without user activity.</p>
<p>This was made with help from random code snippets from around the web.</p>
<p>Tested only on windows 7.</p>
Directory Size (Console) (Python)
2011-02-09T13:29:56-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577566-directory-size-console/
<p style="color: grey">
Python
recipe 577566
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/sizeof/">sizeof</a>, <a href="/recipes/tags/tree/">tree</a>).
</p>
<p>Have you ever wanted to find out how much room a particular directory was taking up on your hard drive? A roommate of mine in college was having trouble keeping track of where his hard drive space is going, so the following program provided a solution that allows a brief overview of a directory's size along with all of its children. A tree view is printed out in ASCII characters showing the relationship of each directory along with its size in a format easily readable to humans. The output can always be redirected to a text file if it needs to be saved for viewing later on.</p>
Running Median (Python)
2010-02-20T08:22:00-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577059-running-median/
<p style="color: grey">
Python
recipe 577059
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/median/">median</a>).
</p>
<p>Maintains sorted data as new elements are added and old one removed as a sliding window advances over a stream of data. Running time per median calculation is proportional to the square-root of the window size.</p>
Numerical Inversion of the Laplace Transform with mpmath (Python)
2009-10-27T01:48:22-07:00Dieter Kadelkahttp://code.activestate.com/recipes/users/4172107/http://code.activestate.com/recipes/576938-numerical-inversion-of-the-laplace-transform-with-/
<p style="color: grey">
Python
recipe 576938
by <a href="/recipes/users/4172107/">Dieter Kadelka</a>
(<a href="/recipes/tags/analysis/">analysis</a>, <a href="/recipes/tags/mpmath/">mpmath</a>, <a href="/recipes/tags/numerical/">numerical</a>).
</p>
<p>This Recipe is a variant of <a href="http://code.activestate.com/recipes/576934/">recipe 576934</a>: Numerical Inversion of the Laplace Transform using the Talbot method by Fernando Damian Nieuwveldt adapted to high precision mpmath</p>
Thread Pool (Python)
2008-11-30T05:42:59-08:00Louis RIVIEREhttp://code.activestate.com/recipes/users/4035877/http://code.activestate.com/recipes/576576-thread-pool/
<p style="color: grey">
Python
recipe 576576
by <a href="/recipes/users/4035877/">Louis RIVIERE</a>
(<a href="/recipes/tags/design_pattern/">design_pattern</a>, <a href="/recipes/tags/pool/">pool</a>, <a href="/recipes/tags/threads/">threads</a>, <a href="/recipes/tags/thread_pool/">thread_pool</a>).
Revision 4.
</p>
<p>Easy to use Thread Pool with a dynamically adjustable pool size.</p>
Pickle the interactive interpreter state (Python)
2008-05-20T10:36:50-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/572213-pickle-the-interactive-interpreter-state/
<p style="color: grey">
Python
recipe 572213
by <a href="/recipes/users/2033964/">Oren Tirosh</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
Revision 2.
</p>
<p>This code extends the pickle module to enable pickling of functions and classes defined interactively at the command prompt. You can save the interpreter state by pickling the __main__ module and restore it later.</p>
reduce as a generator (Python)
2007-03-25T03:23:31-07:00kay schluehrhttp://code.activestate.com/recipes/users/2398921/http://code.activestate.com/recipes/498264-reduce-as-a-generator/
<p style="color: grey">
Python
recipe 498264
by <a href="/recipes/users/2398921/">kay schluehr</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 4.
</p>
<p>In this recipe the builtin function reduce is generalized to a Python 2.5 style generator called greduce. The generator never stops and can be used to create an infinite stream of values by means of the generators send() method.</p>
Compose HTML Mail with embedded images from URL or local file (Python)
2006-02-06T12:10:55-08:00Catalin Constantinhttp://code.activestate.com/recipes/users/2766393/http://code.activestate.com/recipes/473851-compose-html-mail-with-embedded-images-from-url-or/
<p style="color: grey">
Python
recipe 473851
by <a href="/recipes/users/2766393/">Catalin Constantin</a>
.
</p>
<p>The thing about this class is to "build" a mail msg object, automatic, from an URL or a local html file WITH all images included.
The class takes care of the image parsing / downloading / embedding + "cid: ID-here" replacements. The return is a valid MIMEMultipart("related") msg object which can be used to send valid HTML mail.</p>
Permutation and Combination Enumerator (Python)
2006-03-20T23:23:53-08:00Guang Shen Huanghttp://code.activestate.com/recipes/users/2804722/http://code.activestate.com/recipes/474124-permutation-and-combination-enumerator/
<p style="color: grey">
Python
recipe 474124
by <a href="/recipes/users/2804722/">Guang Shen Huang</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>The usaual way to solve these problem is to inflate a huge list, thus it need a lot of memory. These two enumerator take a different approach. It use a linklist to imitate permutations, a binary tree to imitate combination. Thus the program can spit out the result one by one.</p>
A general purpose file iterator class (Python)
2005-04-06T06:29:14-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/408995-a-general-purpose-file-iterator-class/
<p style="color: grey">
Python
recipe 408995
by <a href="/recipes/users/760763/">Anand</a>
(<a href="/recipes/tags/files/">files</a>).
</p>
<p>This recipe presents a general purpose file object iterator cum file object proxy class.
It provides a class that gives several iterator functions to read a text file by characters,
words, lines, paragraphs or blocks. It also acts as a proxy for the wrapped file object.</p>
passwd-file-compatible ($1$) MD5 crypt (Python)
2004-10-26T13:58:15-07:00Mark Johnstonhttp://code.activestate.com/recipes/users/2119626/http://code.activestate.com/recipes/325204-passwd-file-compatible-1-md5-crypt/
<p style="color: grey">
Python
recipe 325204
by <a href="/recipes/users/2119626/">Mark Johnston</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
</p>
<p>A port of Poul-Henning Kamp's MD5 password hash routine, as initially found in FreeBSD 2. It is also used in Cisco routers, Apache htpasswd files, and other places that you find "$1$" at the beginning of password hashes.</p>
Parsing a UK postcode (Python)
2004-05-10T11:54:01-07:00Simon Brunninghttp://code.activestate.com/recipes/users/98010/http://code.activestate.com/recipes/279004-parsing-a-uk-postcode/
<p style="color: grey">
Python
recipe 279004
by <a href="/recipes/users/98010/">Simon Brunning</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 3.
</p>
<p>This function will split and (optionally) validate UK postcodes.</p>
A fun litle tree class.. (Python)
2003-01-10T19:38:17-08:00Alex Naanouhttp://code.activestate.com/recipes/users/104183/http://code.activestate.com/recipes/173217-a-fun-litle-tree-class/
<p style="color: grey">
Python
recipe 173217
by <a href="/recipes/users/104183/">Alex Naanou</a>
.
Revision 2.
</p>
<p>this recipe describes a tree backbone class that supports two modes of iteration and topological comparison.
and it has a nice node access inteface.</p>
try ... catch ... finally (Tcl)
2001-09-11T03:13:07-07:00Donal Fellowshttp://code.activestate.com/recipes/users/99497/http://code.activestate.com/recipes/68396-try-catch-finally/
<p style="color: grey">
Tcl
recipe 68396
by <a href="/recipes/users/99497/">Donal Fellows</a>
.
</p>
<p>The Java try ... catch ... finally construct is really nice for those cases when you want to guarantee to always release a resource (particularly a file handle) even if the code generates an exception. It is also very useful to be able to use different types of error handling depending on the error that occurred. This code implements such a facility in pure Tcl.</p>