Popular recipes tagged "meta:loc=9"http://code.activestate.com/recipes/tags/meta:loc=9/2016-02-18T19:34:17-08:00ActiveState Code RecipesGenerate a set of random integers (Python)
2016-02-18T19:34:17-08:00Lance Spencehttp://code.activestate.com/recipes/users/4193647/http://code.activestate.com/recipes/580613-generate-a-set-of-random-integers/
<p style="color: grey">
Python
recipe 580613
by <a href="/recipes/users/4193647/">Lance Spence</a>
(<a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/random/">random</a>).
</p>
<p>This is just a short and simple script that generates a set of random numbers. You enter how many random numbers you want and the program will generate them.</p>
<p>The num is multiplied by 10 in the upper limit within the for loop to give you larger generated integers. Adjust it as needed.</p>
Detect what browser your website is being accessed from using php (PHP)
2013-06-19T20:09:55-07:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578571-detect-what-browser-your-website-is-being-accessed/
<p style="color: grey">
PHP
recipe 578571
by <a href="/recipes/users/4184772/">Captain DeadBones</a>
(<a href="/recipes/tags/browser/">browser</a>, <a href="/recipes/tags/php/">php</a>, <a href="/recipes/tags/server/">server</a>).
</p>
<p>This is a short hack on how to figure out what OS you site is being accessed from. This is useful if you want to redirect to different versions of your site through php. </p>
Wrap any iterable context manager so it closes when consumed (Python)
2012-11-19T20:10:35-08:00Andrew Barnerthttp://code.activestate.com/recipes/users/4184316/http://code.activestate.com/recipes/578342-wrap-any-iterable-context-manager-so-it-closes-whe/
<p style="color: grey">
Python
recipe 578342
by <a href="/recipes/users/4184316/">Andrew Barnert</a>
(<a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/context_manager/">context_manager</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/with_statement/">with_statement</a>).
</p>
<p>There are a few types in Python—most notably, files—that are both iterators and context managers. For trivial cases, these features are easy to use together, but as soon as you need to use the iterator lazily or asynchronously, a with statement won't help. That's where this recipe comes in handy:</p>
<pre class="prettyprint"><code>send_async(with_iter(open(path, 'r')))
</code></pre>
<p>This also allows you to "forward" closing for a wrapped iterator, so closing the outer iterator also closes the inner one:</p>
<pre class="prettyprint"><code>sync_async(line.upper() for line in with_iter(open(path, 'r')))
</code></pre>
Small random number generator (C)
2012-05-15T19:49:34-07:00Sachin Joglekarhttp://code.activestate.com/recipes/users/4181845/http://code.activestate.com/recipes/578134-small-random-number-generator/
<p style="color: grey">
C
recipe 578134
by <a href="/recipes/users/4181845/">Sachin Joglekar</a>
(<a href="/recipes/tags/generate/">generate</a>, <a href="/recipes/tags/malloc/">malloc</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/stdlib/">stdlib</a>).
</p>
<p>Generates random numbers in a given range using malloc function in stdlib.h. Based on the observation that the memory address allocated during malloc is usually 'random' (for humans). takes in two arguments- start of range, end of range.</p>
Python Multidimensional List Searcher (Python)
2011-10-29T22:21:39-07:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577929-python-multidimensional-list-searcher/
<p style="color: grey">
Python
recipe 577929
by <a href="/recipes/users/4179768/">Alexander James Wallar</a>
(<a href="/recipes/tags/element/">element</a>, <a href="/recipes/tags/elements/">elements</a>, <a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/finder/">finder</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/search/">search</a>).
</p>
<p>This module/function lets you find a 2 dimensional list of indices for elements you are looking for in a super list.
Example:</p>
<p>find([1,1,1,2,1,2,3,3],[1,2,3])</p>
<p>returns: [[0, 1, 2, 4], [3, 5], [6, 7]]</p>
Remove UTF-8 Byte Order Mark (BOM) from text files (Bash)
2011-10-18T06:37:52-07:00Graham Poulterhttp://code.activestate.com/recipes/users/4172291/http://code.activestate.com/recipes/577912-remove-utf-8-byte-order-mark-bom-from-text-files/
<p style="color: grey">
Bash
recipe 577912
by <a href="/recipes/users/4172291/">Graham Poulter</a>
(<a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/unicode/">unicode</a>, <a href="/recipes/tags/utf8/">utf8</a>).
</p>
<p>Shell script to removes UTF-8 Byte Order Mark (BOM) from text files, where present.</p>
Python Easily Packetize / Slice / Chunk Text (Python)
2011-09-30T05:34:57-07:00__nerohttp://code.activestate.com/recipes/users/4177968/http://code.activestate.com/recipes/577885-python-easily-packetize-slice-chunk-text/
<p style="color: grey">
Python
recipe 577885
by <a href="/recipes/users/4177968/">__nero</a>
(<a href="/recipes/tags/chunk/">chunk</a>, <a href="/recipes/tags/packetize/">packetize</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/split/">split</a>, <a href="/recipes/tags/udp/">udp</a>).
</p>
<p>I needed to chunk up some text to send over UDP and didn't want to have messy for loops with an if condition for size and then the little bit left over. All that struck me as very messy. I then thought of the re module and came up with a very simple solution to chunk up data.</p>
Make a Class's Name Available in its Definition Body (Python)
2011-08-04T21:38:40-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577813-make-a-classs-name-available-in-its-definition-bod/
<p style="color: grey">
Python
recipe 577813
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/classes/">classes</a>).
</p>
<p>Since a class object is created <em>after</em> the body is executed, it can't be available to the class body. Even the name is unavailable, at least by default. However, you can use the <code>__prepare__()</code> method in a metaclass to stick it in there. This recipe is a simple demonstration of how.</p>
Pick random elements from a predefined list of choices (Python)
2011-06-14T17:46:41-07:00Patrick Dobbshttp://code.activestate.com/recipes/users/4177984/http://code.activestate.com/recipes/577754-pick-random-elements-from-a-predefined-list-of-cho/
<p style="color: grey">
Python
recipe 577754
by <a href="/recipes/users/4177984/">Patrick Dobbs</a>
(<a href="/recipes/tags/choice/">choice</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/selection/">selection</a>).
</p>
<p>This (quick but strangely satisfying) recipe combines the use of random.choice with functools.partial from the standard library. It is a factory function returning random.choice pre-filled with its sequence of options.</p>
Dictionary Who's Keys Act Like Attributes As Well (Python)
2011-05-26T20:15:16-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577590-dictionary-whos-keys-act-like-attributes-as-well/
<p style="color: grey">
Python
recipe 577590
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/attribute/">attribute</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/keys/">keys</a>).
</p>
<p>Think of this as a JavaScript object. In JavaScript, the objects can be referenced by indexing (e.g. d[name]) or by directly using the dot (.) operator (e.g. d.name).</p>
<p>This is the same concept. </p>
<p><strong>Note to Python 2.4 Users:</strong> You will need to change the "except KeyError as e:" line to "except KeyError, (e):".</p>
Locate and import Python's standard regression tests (Python)
2010-09-17T11:31:31-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577394-locate-and-import-pythons-standard-regression-test/
<p style="color: grey">
Python
recipe 577394
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/regression/">regression</a>, <a href="/recipes/tags/testing/">testing</a>).
</p>
<p>The Python standard library comes with an extensive set of regression tests. I had need to import and run some of these tests from my own code, but the test directory is not in Python's path. This simple helper function solves the problem.</p>
Show all url patterns in django (Python)
2010-03-26T20:04:08-07:00Michael Grünewaldhttp://code.activestate.com/recipes/users/4172244/http://code.activestate.com/recipes/576974-show-all-url-patterns-in-django/
<p style="color: grey">
Python
recipe 576974
by <a href="/recipes/users/4172244/">Michael Grünewald</a>
(<a href="/recipes/tags/django/">django</a>, <a href="/recipes/tags/urls/">urls</a>).
Revision 5.
</p>
<p>This small script shows a simple formatted tree of all defined django url patterns.</p>
string multi-partitioning (Python)
2010-03-26T20:03:59-07:00Michael Grünewaldhttp://code.activestate.com/recipes/users/4172244/http://code.activestate.com/recipes/577017-string-multi-partitioning/
<p style="color: grey">
Python
recipe 577017
by <a href="/recipes/users/4172244/">Michael Grünewald</a>
(<a href="/recipes/tags/partition/">partition</a>, <a href="/recipes/tags/string/">string</a>).
Revision 6.
</p>
<p>Works like the <code>partition</code> method of strings in Python2.5+, but has support for more than one delimiter.</p>
<p><em>Better description by Gabriel Genellina:</em></p>
<blockquote>
<p>Split the string at separator boundaries. The separators are searched from left to right, in the same order specified, and one after another. Unlike <code>partition</code>, all of them must be present (else <code>ValueError</code> is raised). Only one split per separator occurrence is done. Returns a list containing one more element than separators were given: first, text from beginning of the string up to (but not including) the first separator; the first separator itself; text between the first separator and the second one; the second separator; and so on. The last element contains all text following the last separator.</p>
</blockquote>
Create SHA Hashes of a Directory (Python)
2009-04-14T08:10:26-07:00andrew.canithttp://code.activestate.com/recipes/users/4169843/http://code.activestate.com/recipes/576716-create-sha-hashes-of-a-directory/
<p style="color: grey">
Python
recipe 576716
by <a href="/recipes/users/4169843/">andrew.canit</a>
(<a href="/recipes/tags/encryption/">encryption</a>, <a href="/recipes/tags/hash/">hash</a>, <a href="/recipes/tags/security/">security</a>, <a href="/recipes/tags/sha/">sha</a>).
Revision 2.
</p>
<p>Walk through a Directory creating SHA Hashes of each file path</p>
text-to-html (Python)
2009-03-05T23:10:50-08:00nillgump nillgumphttp://code.activestate.com/recipes/users/4169273/http://code.activestate.com/recipes/576682-text-to-html/
<p style="color: grey">
Python
recipe 576682
by <a href="/recipes/users/4169273/">nillgump nillgump</a>
(<a href="/recipes/tags/html/">html</a>).
</p>
<p>将文本排版转化为html中相同的排版。
为google blog服务</p>
PyTube (Python)
2008-12-07T01:19:17-08:00Jonny Reeveshttp://code.activestate.com/recipes/users/4167414/http://code.activestate.com/recipes/576524-pytube/
<p style="color: grey">
Python
recipe 576524
by <a href="/recipes/users/4167414/">Jonny Reeves</a>
(<a href="/recipes/tags/internet/">internet</a>, <a href="/recipes/tags/script/">script</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/youtube/">youtube</a>).
Revision 3.
</p>
<p>Based off PyGoogle, this script searches Youtube. </p>
Date & time RFC822 formatted for RSS2, etc (straw man) (Python)
2008-05-07T17:33:26-07:00Bill Bellhttp://code.activestate.com/recipes/users/98151/http://code.activestate.com/recipes/572190-date-time-rfc822-formatted-for-rss2-etc-straw-man/
<p style="color: grey">
Python
recipe 572190
by <a href="/recipes/users/98151/">Bill Bell</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>I live in a geographical area whose daylight savings time provisions seem to fool software. I would therefore like to avoid mentioning time zone when I create a timestamp in RFC822 format. I have looked for but could not find an elegant way of expressing what I want. In hopes that someone will see this and be aghast at my ignorance, then provide something nice, I humbly offer the following.</p>
<p>It takes a datetime and gives back an RFC822 timestamp (I think).</p>
autovivication dictonaries (Python)
2007-12-19T05:23:08-08:00Mirko Dziadzkahttp://code.activestate.com/recipes/users/2504082/http://code.activestate.com/recipes/537637-autovivication-dictonaries/
<p style="color: grey">
Python
recipe 537637
by <a href="/recipes/users/2504082/">Mirko Dziadzka</a>
.
</p>
<p>A nested dictionary which creates subnotes on the fly.</p>
Very Simple Delimiter Separated Values File Reader Generator (Python)
2007-12-17T20:59:24-08:00N Nhttp://code.activestate.com/recipes/users/1639254/http://code.activestate.com/recipes/535172-very-simple-delimiter-separated-values-file-reader/
<p style="color: grey">
Python
recipe 535172
by <a href="/recipes/users/1639254/">N N</a>
(<a href="/recipes/tags/files/">files</a>).
</p>
<p>This is a bare-bones reader generator for file processing of typical Unix style configuration and data files.</p>
autovivication dictonaries (Python)
2007-12-19T05:23:05-08:00Mirko Dziadzkahttp://code.activestate.com/recipes/users/2504082/http://code.activestate.com/recipes/537636-autovivication-dictonaries/
<p style="color: grey">
Python
recipe 537636
by <a href="/recipes/users/2504082/">Mirko Dziadzka</a>
.
</p>
<p>A nested dictionary which creates subnotes on the fly.</p>