Popular recipes tagged "meta:loc=310"http://code.activestate.com/recipes/tags/meta:loc=310/2014-02-22T06:13:41-08:00ActiveState Code RecipesThe Game of Battleships in Python (Python)
2014-02-22T06:13:41-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578836-the-game-of-battleships-in-python/
<p style="color: grey">
Python
recipe 578836
by <a href="/recipes/users/4184772/">Captain DeadBones</a>
(<a href="/recipes/tags/beginner/">beginner</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>Another fun game that is fun to program and play. No special AI (yet). But still and entertaining game. </p>
<p>For more info about <a href="http://thelivingpearl.com/2014/02/17/the-game-of-battleships-in-python/">Battleships in Python</a> follow the link. </p>
Shortest Common Supersequence algorithms (Python)
2013-10-02T12:52:23-07:00Rutger Saalminkhttp://code.activestate.com/recipes/users/4187940/http://code.activestate.com/recipes/578678-shortest-common-supersequence-algorithms/
<p style="color: grey">
Python
recipe 578678
by <a href="/recipes/users/4187940/">Rutger Saalmink</a>
(<a href="/recipes/tags/approximation/">approximation</a>, <a href="/recipes/tags/bound/">bound</a>, <a href="/recipes/tags/breadth_first_search/">breadth_first_search</a>, <a href="/recipes/tags/common/">common</a>, <a href="/recipes/tags/depth_first_search/">depth_first_search</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/shortest/">shortest</a>, <a href="/recipes/tags/super/">super</a>).
</p>
<p>The Shortest Common Supersequence (SCS) problem is an NP-hard problem (<a href="https://en.wikipedia.org/wiki/Shortest_common_supersequence" rel="nofollow">https://en.wikipedia.org/wiki/Shortest_common_supersequence</a>), which occurs in problems originating from various domains, e.g. Bio Genetics. Given a set of strings, the common supersequence of minimal length is sought after. Below a set of algorithms is given, which I used in approximating and/or backtracking the optimal solution(s). </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>
Generator expressions for database requests (Python)
2005-10-18T12:21:33-07:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/442447-generator-expressions-for-database-requests/
<p style="color: grey">
Python
recipe 442447
by <a href="/recipes/users/1552957/">Pierre Quentel</a>
(<a href="/recipes/tags/database/">database</a>).
</p>
<p>This recipe is a follow-up to #440653, which was easy to implement but very slow because the iteration required to read all the rows of a table</p>
<p>As suggested by Matteo Dell'Amico in a comment, it would be much better if we could write something like</p>
<pre class="prettyprint"><code>query(r.name for r in plane_tbl if r.country == "France")
</code></pre>
<p>where the generator expression is first translated into an SQL select, so that the iteration on the instance of query only reads the rows selected by the SQL statement</p>
<p>The present recipe is an attempt to achieve this. The first problem is to get the source code of the generator expression. I use information from the stack frame to get the file name and the line number, then the tokenize module to read the elements of the generator expression in the source code</p>
<p>Then, to build the SQL statement, the source code must be parsed : this is done using the compiler package and "visitors" that walk the AST tree returned by compiler.parse and do operations on the nodes, depending on their type</p>
<p>Finally, once the SQL statement is built, the iteration on the query instance can start : for the first one, the SQL statement is executed ; then the iteration yields the selected rows one by one.</p>
<p>The items can be :
- objects, with attribute names matching those in the generator expression, except that qualified names (table.name) are converted to table_name
- dictionaries : the keys are the same as the attribute names above
- lists</p>
<p>For instance :
- iterating on query(name for r in plane_tbl) returns objects with an attribute name
- iterating on query(r.name for r in plane_tbl) returns objects with an attribute r_name</p>
<p>This is because of iteration on tables which have the same field names</p>
<p>query((r.name,c.name) for r in plane_tbl for c in country_tbl if r.speed > 500 )</p>
<p>The type of the items is set by query.return_type = object, dict or list</p>
Cloudscape - start network server. (Tcl)
2005-12-07T21:36:51-08:00Patrick Finneganhttp://code.activestate.com/recipes/users/1220635/http://code.activestate.com/recipes/461728-cloudscape-start-network-server/
<p style="color: grey">
Tcl
recipe 461728
by <a href="/recipes/users/1220635/">Patrick Finnegan</a>
(<a href="/recipes/tags/database/">database</a>).
</p>
<p>Start and monitor Cloudscape network server.</p>
Extending python with prolog syntax *and resolution* (Python)
2004-12-24T17:46:14-08:00Francisco Coelhohttp://code.activestate.com/recipes/users/2228114/http://code.activestate.com/recipes/360698-extending-python-with-prolog-syntax-and-resolution/
<p style="color: grey">
Python
recipe 360698
by <a href="/recipes/users/2228114/">Francisco Coelho</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 2.
</p>
<p>This extends the previous "pythologic" recipe with resolution, based on unification and greattly inspired by the AIMA books and examples.</p>
<p>The author is <em>not</em> a python expert and expects to see this contribution further discussed and developed.</p>