Popular recipes by Pierre Quentel http://code.activestate.com/recipes/users/1552957/2012-11-02T12:29:25-07:00ActiveState Code RecipesTyping skills meter (Python) 2012-11-02T12:29:25-07:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/578318-typing-skills-meter/ <p style="color: grey"> Python recipe 578318 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/typing/">typing</a>). </p> <p>A Tkinter-based program that shows your typing skills (speed and error rate) in the form of a car dashboard</p> <p>The text to type is displayed in a text zone, the cursor and the dashboard needles progress as you type the letters. Text can be copied-pasted, or loaded from a file that users can store in the subfolder "texts"</p> <p>The interface language can be configured (in this version, only French-English, but easily extendable to other languages)</p> Funny text generator (Python) 2011-12-25T11:14:39-08:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/577988-funny-text-generator/ <p style="color: grey"> Python recipe 577988 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/generation/">generation</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/text/">text</a>). Revision 4. </p> <p>This class takes a text (preferably long enough) and generates another text that "looks like" the original. It won't mean anything, or just by chance ;-)</p> <p>For example, taking Hamlet, Act I, the program generates things like :</p> <p>Hamlet</p> <blockquote> <p>And vanish'd from our watch; <br /> His further. Fare third nights of the flushing immortal as it draw you into the flushing thy complete steel <br /> 'Tis sweet and each new-hatch'd: <br /> A country's father; <br /> To business and is prodigal thee! <br /> Have of crowing more the should I have heaven, <br /> Forward, therefore as ourself in the business it, Horatio <br /> To what is't that your watch, bid this here! </p> </blockquote> <p>Usage :</p> <pre class="prettyprint"><code>generator = TextGenerator(txt) result = generator.random_text(3000) </code></pre> An asynchronous HTTP server with CGI support (Python) 2007-04-05T05:46:26-07:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/511454-an-asynchronous-http-server-with-cgi-support/ <p style="color: grey"> Python recipe 511454 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/cgi/">cgi</a>). </p> <p>This recipes implements the HTTP protocol with persistent connection and CGI support on top of the generic asynchronous server provided in <a href="http://code.activestate.com/recipes/511453/">recipe 511453</a></p> A generic, multi protocol asynchronous server (Python) 2007-04-05T05:35:23-07:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/511453-a-generic-multi-protocol-asynchronous-server/ <p style="color: grey"> Python recipe 511453 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/web/">web</a>). </p> <p>This recipe implements the concept of asynchronous socket programming in a more straighforward way than in the modules asyncore and asynchat of the standard distribution, which I find difficult to understand</p> PyDbLite, a small in-memory database engine (Python) 2011-07-18T19:36:04-07:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/496770-pydblite-a-small-in-memory-database-engine/ <p style="color: grey"> Python recipe 496770 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/database/">database</a>). Revision 4. </p> <p>A small, fast, in-memory database management program</p> <p>The database object supports the iterator protocol, so that requests can be expressed with list comprehensions or generator expressions instead of SQL. The equivalent of :</p> <pre class="prettyprint"><code>cursor.execute("SELECT name FROM table WHERE age=30") rows = cursor.fetchall() </code></pre> <p>is :</p> <pre class="prettyprint"><code>rows = table(age=30) </code></pre> <p>The module stores data in a cPickled file. Records are indexed by a unique record identifier, that can be used for direct access. Since operations are processed in memory they are extremely fast, nearly as fast as SQLite in the few tests I made, and MUCH faster than other pure-Python modules such as Gadfly or KirbyBase. An index can be created on a field to even speed up selections</p> <p>Concurrency control is supported by a version number set for each record</p> <p>Complete documentation is <a href="http://www.pydblite.net">here</a></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 &gt; 500 )</p> <p>The type of the items is set by query.return_type = object, dict or list</p> HTMLTags - generate HTML in Python (Python) 2009-10-24T10:30:38-07:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/366000-htmltags-generate-html-in-python/ <p style="color: grey"> Python recipe 366000 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/web/">web</a>). Revision 11. </p> <p>The HTMLTags module defines a class for each valid HTML tag, written in uppercase letters. To create a piece of HTML, the general syntax is :</p> <pre class="prettyprint"><code>t = TAG(innerHTML, key1=val1,key2=val2,...) </code></pre> <p>so that "print t" results in :</p> <pre class="prettyprint"><code>&lt;TAG key1="val1" key2="val2" ...&gt;innerHTML&lt;/TAG&gt; </code></pre> <p>For instance :</p> <pre class="prettyprint"><code>print A('bar', href="foo") ==&gt; &lt;A href="foo"&gt;bar&lt;/A&gt; </code></pre> List comprehensions for database requests (Python) 2005-10-04T12:44:32-07:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/440653-list-comprehensions-for-database-requests/ <p style="color: grey"> Python recipe 440653 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/database/">database</a>). </p> <p>The usual way to make a request to a database is to write a string with the SQL syntax, then execute this request and get the result as a list with cursor.fetchall() or cursor.fetchone()</p> <p>Python has list comprehensions to select items in an iterable if a certain condition is true ; this is very similar to database requests</p> <p>This recipe wraps a table of a DB-API compliant database in a class that implements the iteration protocol, so that you can use the for ... in ... if ... syntax</p> My first application server (Python) 2009-02-23T11:53:57-08:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/392879-my-first-application-server/ <p style="color: grey"> Python recipe 392879 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/web/">web</a>). Revision 8. </p> <p>For those who want to start dynamic web programming, but don't know what to choose among the many Python web frameworks, this program might be a good starting point</p> <p>ScriptServer is a minimalist application server, handling both GET and POST requests, including multipart/form-data for file uploads, HTTP redirections, and with an in-memory session management. It can run Python scripts and template files using the standard string substitution format</p> <p>The scripts are run in the same process as the server, avoiding the CGI overhead. The environment variables are provided in the namespace where the script runs</p> <p>To start the server, run </p> <pre class="prettyprint"><code>python ScriptServer.py </code></pre> <p>In your web browser, enter <a href="http://localhost" rel="nofollow">http://localhost</a>, this will show you a listing of the directory. Add the scripts in the same directory as ScriptServer</p> Simple HTTP server based on asyncore/asynchat (Python) 2005-10-16T08:26:59-07:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/259148-simple-http-server-based-on-asyncoreasynchat/ <p style="color: grey"> Python recipe 259148 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/web/">web</a>). Revision 8. </p> <p>A simple HTTP Server, intended to be as simple as the standard module SimpleHTTPServer, built upon the asyncore/asynchat modules (uses non-blocking sockets). Provides a Server (copied from medusa http_server) and a RequestHandler class. RequestHandler handles both GET and POST methods and inherits SimpleHTTPServer.SimpleHTTPRequestHandler</p> <p>It can be easily extended by overriding the handle_data() method in the RequestHandler class</p>