Latest recipes tagged "unique"http://code.activestate.com/recipes/tags/unique/new/2014-11-09T11:19:07-08:00ActiveState Code RecipesPython Short URL Generator (Python)
2014-11-09T11:19:07-08:00Des Wagnerhttp://code.activestate.com/recipes/users/4191097/http://code.activestate.com/recipes/578961-python-short-url-generator/
<p style="color: grey">
Python
recipe 578961
by <a href="/recipes/users/4191097/">Des Wagner</a>
(<a href="/recipes/tags/deterministic/">deterministic</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/short/">short</a>, <a href="/recipes/tags/tiny/">tiny</a>, <a href="/recipes/tags/unique/">unique</a>, <a href="/recipes/tags/url/">url</a>).
</p>
<p>Python implementation for generating Tiny URL- and bit.ly-like URLs.</p>
Multiple unique class instances - a tentative design pattern (Python)
2013-02-23T14:11:44-08:00Moritz Beberhttp://code.activestate.com/recipes/users/4185350/http://code.activestate.com/recipes/578471-multiple-unique-class-instances-a-tentative-design/
<p style="color: grey">
Python
recipe 578471
by <a href="/recipes/users/4185350/">Moritz Beber</a>
(<a href="/recipes/tags/design_pattern/">design_pattern</a>, <a href="/recipes/tags/pattern/">pattern</a>, <a href="/recipes/tags/unique/">unique</a>).
</p>
<p>Coming from Bioinformatics and having to deal with multiple objects with unique properties, like genes, proteins, and many more, I felt the need for a class that would always yield a unique instance based on a chosen identifier. This is because I always wanted the same instances whose attributes were filled with information and track them in various storage classes, like dictionaries, lists, etc. The code for the class lives <a href="https://github.com/Midnighter/unique-base">over on github</a>. Recently, I've added a lot of parallel-processing code so I adapted the pickling behaviour for this class to also yield existing instances. What follows is an example of how to use it and some discussion questions.</p>
Find a unique name based on prefix + digit in log2(n) + log2(n/2) (Python)
2011-10-31T20:30:54-07:00Garel Alexhttp://code.activestate.com/recipes/users/2757636/http://code.activestate.com/recipes/577934-find-a-unique-name-based-on-prefix-digit-in-log2n-/
<p style="color: grey">
Python
recipe 577934
by <a href="/recipes/users/2757636/">Garel Alex</a>
(<a href="/recipes/tags/name/">name</a>, <a href="/recipes/tags/unique/">unique</a>).
</p>
<p>Find a unique name based on a prefix for a content in a container
(eg. a file in a directory) adding a digit to the name</p>
<p>Does it in log2(n) + log2(n/2) were n is the number of duplicates</p>
<p>This would be used eg. in a CMS giving identifiers based on content title, or when shortening file names on a file system, etc.</p>
lazy ordered unique elements from an iterator (Python)
2011-06-23T16:36:38-07:00Andrew Dalkehttp://code.activestate.com/recipes/users/912777/http://code.activestate.com/recipes/577768-lazy-ordered-unique-elements-from-an-iterator/
<p style="color: grey">
Python
recipe 577768
by <a href="/recipes/users/912777/">Andrew Dalke</a>
(<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/unique/">unique</a>).
Revision 2.
</p>
<p>This implements a "unique" filter. Its input is an iterator of hashable items. It returns an iterator containing only the unique items of the input, in input order. That is, list(unique("cabbage")) produces ["c", "a", "b", "g"]. The implementation is lazy. The function supports the "key" parameter, which provides an alternate form of comparison.</p>
<p>(Note: a better version of this is available from the itertools documentation as unique_everseen )</p>
YouTube-like primary key generation (PHP)
2011-02-24T21:43:55-08:00Slava Yansonhttp://code.activestate.com/recipes/users/4176967/http://code.activestate.com/recipes/577571-youtube-like-primary-key-generation/
<p style="color: grey">
PHP
recipe 577571
by <a href="/recipes/users/4176967/">Slava Yanson</a>
(<a href="/recipes/tags/activerecord/">activerecord</a>, <a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/php/">php</a>, <a href="/recipes/tags/unique/">unique</a>).
Revision 4.
</p>
<p>If you have more then 1 primary database servers - you have probably experienced the pain of primary key overlapping... Here is a super simple solution that will generate a hash of random letters and numbers.</p>
<p>Included is a sample ActiveRecord class (MyTable.php).</p>
Sort file (with unique option) (JavaScript)
2010-05-30T21:47:36-07:00Glenn Jenkinshttp://code.activestate.com/recipes/users/4174057/http://code.activestate.com/recipes/577247-sort-file-with-unique-option/
<p style="color: grey">
JavaScript
recipe 577247
by <a href="/recipes/users/4174057/">Glenn Jenkins</a>
(<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/komodo/">komodo</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/unique/">unique</a>).
</p>
<p>This will sort the lines in the current document into alphabetical order. It doesn't apply any special rule (so 5 will come <em>after</em> 42).</p>
<p>On running, it asks if you want a unique sort (Cancel for no, Ok for yes), in which case it'll remove duplicate lines.</p>
Sets with a custom equality/uniqueness function (Python)
2009-10-29T21:08:22-07:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/576932-sets-with-a-custom-equalityuniqueness-function/
<p style="color: grey">
Python
recipe 576932
by <a href="/recipes/users/924636/">Gabriel Genellina</a>
(<a href="/recipes/tags/abstract_base_class/">abstract_base_class</a>, <a href="/recipes/tags/mutableset/">mutableset</a>, <a href="/recipes/tags/set/">set</a>, <a href="/recipes/tags/unique/">unique</a>).
Revision 4.
</p>
<p>The builtin <code>set</code> and <code>frozenset</code> types are based on object equality; they call __eq__ to determine whether an object is a member of the set or not. But there are cases when one needs a set of objects that are compared by other means, apart from the default __eq__ function. There are several ways to achieve that; this recipe presents two classes, FrozenKeyedSet and KeyedSet, that take an additional function <code>key</code> which is used to determine membership and uniqueness. Given two objects which return the same value for <code>key</code>, only one of them will be in the set.</p>
Python Short URL Generator (Python)
2011-09-19T14:06:36-07:00Michael Foglemanhttp://code.activestate.com/recipes/users/4171845/http://code.activestate.com/recipes/576918-python-short-url-generator/
<p style="color: grey">
Python
recipe 576918
by <a href="/recipes/users/4171845/">Michael Fogleman</a>
(<a href="/recipes/tags/deterministic/">deterministic</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/short/">short</a>, <a href="/recipes/tags/tiny/">tiny</a>, <a href="/recipes/tags/unique/">unique</a>, <a href="/recipes/tags/url/">url</a>).
Revision 3.
</p>
<p>Python implementation for generating Tiny URL- and bit.ly-like URLs.</p>