Popular recipes tagged "meta:loc=76"http://code.activestate.com/recipes/tags/meta:loc=76/2016-06-29T20:09:44-07:00ActiveState Code Recipesconvert hierarchy of data files into a convenient Python dictionary (Python) 2016-06-29T20:09:44-07:00Brian Fiedlerhttp://code.activestate.com/recipes/users/4194196/http://code.activestate.com/recipes/580687-convert-hierarchy-of-data-files-into-a-convenient-/ <p style="color: grey"> Python recipe 580687 by <a href="/recipes/users/4194196/">Brian Fiedler</a> (<a href="/recipes/tags/pickle/">pickle</a>). Revision 2. </p> <p>Converts the CRUTEM text data files into a convenient Python dictionary. The data files are at: <a href="http://www.metoffice.gov.uk/hadobs/crutem4/data/station_files/CRUTEM.4.4.0.0.station_files.zip" rel="nofollow">http://www.metoffice.gov.uk/hadobs/crutem4/data/station_files/CRUTEM.4.4.0.0.station_files.zip</a></p> Temperature Conversation Application in Python (Python) 2014-01-03T01:29:28-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578804-temperature-conversation-application-in-python/ <p style="color: grey"> Python recipe 578804 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/temperature/">temperature</a>, <a href="/recipes/tags/units/">units</a>). </p> <p>This is a basic program in python that can cover between units of temperature. This was written for an article on <a href="http://thelivingpearl.com/2014/01/02/temperature-conversation-application-in-python/">How to use modules in Python</a>. </p> Simple caching decorator (Python) 2010-12-01T00:43:01-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577479-simple-caching-decorator/ <p style="color: grey"> Python recipe 577479 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/memoization/">memoization</a>). </p> <p>Memoizing decorator. Has the same API as the functools.lru_cache() in Py3.2 but without the LRU feature, so it takes less memory, runs faster, and doesn't need locks to keep the dictionary in a consistent state.</p> Convert a youtube transcript in srt subtitle (Python) 2010-11-12T01:59:53-08:00Ginkohttp://code.activestate.com/recipes/users/4175855/http://code.activestate.com/recipes/577459-convert-a-youtube-transcript-in-srt-subtitle/ <p style="color: grey"> Python recipe 577459 by <a href="/recipes/users/4175855/">Ginko</a> (<a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/srt/">srt</a>, <a href="/recipes/tags/transcript/">transcript</a>, <a href="/recipes/tags/youtube/">youtube</a>). </p> <p>A quick and dirty script to convert youtube's transcripts (xml format) to .srt subtitle files.</p> <p>To download youtube's transcript, use this url: <a href="http://video.google.com/timedtext?lang=en&amp;v=VIDEO_ID" rel="nofollow">http://video.google.com/timedtext?lang=en&amp;v=VIDEO_ID</a> (replace "VIDEO_ID" by the ID which is in the video URL).</p> <p>You can easily use this converter in a script which could download the transcript by importing it and then call the main function.</p> converting numbers to their alphabetical style (Python) 2011-03-17T06:57:06-07:00amir naghavihttp://code.activestate.com/recipes/users/4177294/http://code.activestate.com/recipes/577607-converting-numbers-to-their-alphabetical-style/ <p style="color: grey"> Python recipe 577607 by <a href="/recipes/users/4177294/">amir naghavi</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/string/">string</a>). Revision 4. </p> <p>converts numbers to alphabetical numbers </p> Filtering CSV data by fields (cut for csv) (Python) 2011-02-02T21:39:45-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/577560-filtering-csv-data-by-fields-cut-for-csv/ <p style="color: grey"> Python recipe 577560 by <a href="/recipes/users/4167757/">James Mills</a> (<a href="/recipes/tags/csv/">csv</a>, <a href="/recipes/tags/cut/">cut</a>, <a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Ever wanted to take a CSV file as input, cut it up and only extract the fields that you want ?</p> <p>Here's how!</p> <p>$ cat cars.csv Year,Make,Model,Length 1997,Ford,E350,2.34 2000,Mercury,Cougar,2.38</p> <p>$ csvcut.py -f 0 -f -1 - &lt; cars.csv Year,Length 1997,2.34 2000,2.38</p> <p>--JamesMills (prologic)</p> Subprocess As Another User (Python) 2010-12-23T00:10:08-08:00Eric Pruitthttp://code.activestate.com/recipes/users/4170757/http://code.activestate.com/recipes/577495-subprocess-as-another-user/ <p style="color: grey"> Python recipe 577495 by <a href="/recipes/users/4170757/">Eric Pruitt</a> (<a href="/recipes/tags/login/">login</a>, <a href="/recipes/tags/subprocess/">subprocess</a>, <a href="/recipes/tags/user/">user</a>, <a href="/recipes/tags/windows/">windows</a>). Revision 3. </p> <p>Modifies the subprocess module and supplies a new class, LoginSTARTUPINFO, to launch processes as another user on Windows. Requires the pywin32 libraries, but the system ../lib/subprocess.py does not need to be modified.</p> <pre class="prettyprint"><code>&gt;&gt;&gt; import subprocesswin32 as subprocess &gt;&gt;&gt; sysuser = LoginSTARTUPINFO("username", "machine", "passwd123") &gt;&gt;&gt; stdout, stderr = subprocess.Popen("cmd.exe", stdout=subprocess.PIPE, ... startupinfo=sysuser).communicate() </code></pre> Creating a tar archive (without using the tarfile module) (Python) 2010-10-11T06:18:42-07:00Benjamin Sergeanthttp://code.activestate.com/recipes/users/4039626/http://code.activestate.com/recipes/577422-creating-a-tar-archive-without-using-the-tarfile-m/ <p style="color: grey"> Python recipe 577422 by <a href="/recipes/users/4039626/">Benjamin Sergeant</a> (<a href="/recipes/tags/compression/">compression</a>, <a href="/recipes/tags/tar/">tar</a>, <a href="/recipes/tags/unix/">unix</a>). </p> <p>Creating a tar file is easy if you read the spec (you can look it up on wikipedia). Not every kind of files are supported (it support regular files, folders ans symlinks) and it's generating archives for the original tar file format (path length are limited to 100 chars, no extended attributes, ...). It wasn't tested very much but it was a fun hack :) ... I cheated just a little by looking at the python tarfile code from the stdlib for the checksum computation.</p> <p>A tar file is very simple, it's a list of header/payload for each entry (file|folder|symlink) you want to archive. There's only a payload for file contents. The header is 512 bytes long and can be written in ascii. Numbers (attributes) needs to be written in octal. The files themselves needs to be written in chunks of 512 bytes, which mean you have to fill the last chunk with zeros when the file size is not a multiple of 512 bytes.</p> <p>Use it like that: </p> <pre class="prettyprint"><code>python batar.py /tmp/foo.tar `find .` &amp;&amp; tar tf /tmp/foo.tar # or xf if you want to extract it </code></pre> Lazy, recursive generator function (Python) 2008-12-14T10:04:29-08:00Don Sawatzkyhttp://code.activestate.com/recipes/users/4168423/http://code.activestate.com/recipes/576585-lazy-recursive-generator-function/ <p style="color: grey"> Python recipe 576585 by <a href="/recipes/users/4168423/">Don Sawatzky</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/lazy_evaluation/">lazy_evaluation</a>, <a href="/recipes/tags/recursion/">recursion</a>). Revision 4. </p> <p>This procedure was proposed as a challenge to Python and other languages as the most concise coding. See Icon programming on the web. This is a lazy, recursive generator. It can be implemented several ways in Python with lists, iteration, and recursion. However, the lists increase in size exponentially with each iteration and recursion plus they are saved in every recursion. This recipe develops a lazy generator function.</p> How to Use Custom Dictionaries to Modify Attribute Lookup (Python) 2006-01-25T19:26:50-08:00Wolfgang Lipphttp://code.activestate.com/recipes/users/772967/http://code.activestate.com/recipes/473784-how-to-use-custom-dictionaries-to-modify-attribute/ <p style="color: grey"> Python recipe 473784 by <a href="/recipes/users/772967/">Wolfgang Lipp</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>some while ago, i posted a recipe with linked dictionaries (see &lt;a href='http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/465748'&gt;#465748&lt;/a&gt;) to this site. yesterday a use case came up where that kind of delegational name/value lookup would be a nice thing if it worked for instance attributes.</p> <p>basically, using a customized dictionary as an instance &lt;tt&gt;__dict__&lt;/tt&gt; means modifying the standard lookup-by-inheritance-only behavior; in other words, part of the state of an instance can be kept in another instance.</p> <p>this way, an instance that holds, say user configuration data, &lt;tt&gt;userCfg&lt;/tt&gt; can be linked, at runtime, to a &lt;tt&gt;defaultCfg&lt;/tt&gt; instance, and all the &lt;tt&gt;userCfg.frobfactor&lt;/tt&gt; stuff that has not been defined by the user but for the default configuration would automatically be available.</p> <p>unfortunately, the naïve approach (setting &lt;tt&gt;self.__dict__=MyDict()&lt;/tt&gt; in &lt;tt&gt;__init__&lt;/tt&gt;) fails to work. an inquiry to comp.lang.python was swiftly answered by bengt richter, who suggested to use a property for &lt;tt&gt;__dict__&lt;/tt&gt;. i slightly improved on his suggestion by adding the capability to do item assignment as well.</p> <p>note that since we are using &lt;tt&gt;__getattr__&lt;/tt&gt; to redirect attribute lookup, delegation only happens &lt;i&gt;after&lt;/i&gt; lookup in the method resolution order (mro) has failed (inheritance overrides delegation).</p> File Selection Wrapper (Python) 2005-07-12T20:35:10-07:00Jarrod Robersonhttp://code.activestate.com/recipes/users/2516864/http://code.activestate.com/recipes/436507-file-selection-wrapper/ <p style="color: grey"> Python recipe 436507 by <a href="/recipes/users/2516864/">Jarrod Roberson</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>This is a simple Object Oriented way use regular expressions to with a simple include/exclude metaphore to select files on the filesystem.</p> Automatic indentation of output based on frame stack (Python) 2005-04-26T13:27:42-07:00Lonnie Princehousehttp://code.activestate.com/recipes/users/2071987/http://code.activestate.com/recipes/411791-automatic-indentation-of-output-based-on-frame-sta/ <p style="color: grey"> Python recipe 411791 by <a href="/recipes/users/2071987/">Lonnie Princehouse</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>Output stream wrapper; possibly useful for debugging code with print statements.</p> <p>When write() is called, it makes a note of the calling frame. The indentation level is equal to the number of frames in the call stack which have been previously noted. See example.</p> Error reporting via decorator (Python) 2005-03-10T16:13:58-08:00Scott David Danielshttp://code.activestate.com/recipes/users/98131/http://code.activestate.com/recipes/391249-error-reporting-via-decorator/ <p style="color: grey"> Python recipe 391249 by <a href="/recipes/users/98131/">Scott David Daniels</a> . </p> <p>A Python 2.4 (or later) decorator can be used to watch for unhandled exceptions, even in hybrid environments like wxPython. You place the decorator around methods that will be invoked by the wxWidgets code. The only big trick is a dance that may be necessary to provide access to values only available after the wxPthon app is started.</p> Format database field names in Excel (Python) 2004-10-29T19:57:50-07:00Matt Keranenhttp://code.activestate.com/recipes/users/38288/http://code.activestate.com/recipes/325735-format-database-field-names-in-excel/ <p style="color: grey"> Python recipe 325735 by <a href="/recipes/users/38288/">Matt Keranen</a> (<a href="/recipes/tags/database/">database</a>). Revision 2. </p> <p>Often I get data to import in MS Excel files, and the column headers are not very useful for column names. They contain spaces, punctuation, and special characters that make a simple import difficult.</p> <p>This script opens the file in Excel, and applies some simple formatting rules to the first row and addresses duplicate names. Then when the file is imported (SQL Server DTS in my case), the column names are somewhat usable.</p> skeletal compiler.visitor.ASTVisitor generator (Python) 2004-10-19T19:11:51-07:00Lonnie Princehousehttp://code.activestate.com/recipes/users/2071987/http://code.activestate.com/recipes/310790-skeletal-compilervisitorastvisitor-generator/ <p style="color: grey"> Python recipe 310790 by <a href="/recipes/users/2071987/">Lonnie Princehouse</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>Parse the online documentation for compiler.ast and generate code for a skeletal ASTVisitor class that includes stubs for all of the various visitNODE functions you might need in a visitor, along with comments in each function that list that node type's attributes.</p> Tuples with named elements - using metaclasses (Python) 2004-09-05T02:30:29-07:00Andrew Durdinhttp://code.activestate.com/recipes/users/2019895/http://code.activestate.com/recipes/303481-tuples-with-named-elements-using-metaclasses/ <p style="color: grey"> Python recipe 303481 by <a href="/recipes/users/2019895/">Andrew Durdin</a> . </p> <p>NamedTupleMetaclass is a metaclass for creating tuples with named elements that can be accessed by index and by name.</p> <p>NamedTuple is a class factory for NamedTupleMetaclass instances.</p> <p>This is an improved version of recipe #303439</p> A quick and dirty Client and Server Socket class (Python) 2003-05-21T03:07:24-07:00Amey Pathakhttp://code.activestate.com/recipes/users/1171264/http://code.activestate.com/recipes/200946-a-quick-and-dirty-client-and-server-socket-class/ <p style="color: grey"> Python recipe 200946 by <a href="/recipes/users/1171264/">Amey Pathak</a> (<a href="/recipes/tags/network/">network</a>). </p>