Most viewed Python recipes http://code.activestate.com/recipes/langs/python/views/2017-07-15T00:46:59-07:00ActiveState Code RecipesConvert string to hex (Python) 2006-08-18T05:17:53-07:00Mykola Kharechkohttp://code.activestate.com/recipes/users/2950701/http://code.activestate.com/recipes/496969-convert-string-to-hex/ <p style="color: grey"> Python recipe 496969 by <a href="/recipes/users/2950701/">Mykola Kharechko</a> (<a href="/recipes/tags/web/">web</a>). </p> <p>Qoute string converting each char to hex repr and back</p> Http client to POST using multipart/form-data (Python) 2002-08-23T07:56:39-07:00Wade Leftwichhttp://code.activestate.com/recipes/users/98656/http://code.activestate.com/recipes/146306-http-client-to-post-using-multipartform-data/ <p style="color: grey"> Python recipe 146306 by <a href="/recipes/users/98656/">Wade Leftwich</a> (<a href="/recipes/tags/web/">web</a>). </p> <p>A scripted web client that will post data to a site as if from a form using ENCTYPE="multipart/form-data". This is typically used to upload files, but also gets around a server's (e.g. ASP's) limitation on the amount of data that can be accepted via a standard POST (application/x-www-form-urlencoded).</p> Send an HTML email with embedded image and plain text alternate (Python) 2006-01-29T18:40:36-08:00darrin massenahttp://code.activestate.com/recipes/users/1987292/http://code.activestate.com/recipes/473810-send-an-html-email-with-embedded-image-and-plain-t/ <p style="color: grey"> Python recipe 473810 by <a href="/recipes/users/1987292/">darrin massena</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>HTML is the method of choice for those wishing to send emails with rich text, layout and graphics. Often it is desirable to embed the graphics within the message so recipients can display the message directly, without further downloads.</p> <p>Some mail agents don't support HTML or their users prefer to receive plain text messages. Senders of HTML messages should include a plain text message as an alternate for these users.</p> <p>This recipe sends a short HTML message with a single embedded image and an alternate plain text message.</p> The Game of Tic Tac Toe in Python (Python) 2014-01-31T02:39:48-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578816-the-game-of-tic-tac-toe-in-python/ <p style="color: grey"> Python recipe 578816 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>I thought this is a fun game to program. Easy to program and can teach a lot. </p> <p><a href="http://thelivingpearl.com">Captain DeadBones</a></p> Constants in Python (Python) 2001-08-20T07:12:14-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/65207-constants-in-python/ <p style="color: grey"> Python recipe 65207 by <a href="/recipes/users/97991/">Alex Martelli</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>In Python, any variable can be re-bound at will -- and modules don't let you define special methods such as an instance's __setattr__ to stop attribute re-binding. Easy solution (in Python 2.1 and up): use an instance as "module"...</p> Creating a daemon the Python way (Python) 2005-10-03T16:49:09-07:00Chad J. Schroederhttp://code.activestate.com/recipes/users/1760491/http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/ <p style="color: grey"> Python recipe 278731 by <a href="/recipes/users/1760491/">Chad J. Schroeder</a> (<a href="/recipes/tags/threads/">threads</a>). Revision 6. </p> <p>The Python way to detach a process from the controlling terminal and run it in the background as a daemon.</p> Testing if a variable is defined (Python) 2001-06-06T11:03:36-07:00Hamish Lawsonhttp://code.activestate.com/recipes/users/98049/http://code.activestate.com/recipes/59892-testing-if-a-variable-is-defined/ <p style="color: grey"> Python recipe 59892 by <a href="/recipes/users/98049/">Hamish Lawson</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>You want to take different courses of action based on whether a variable is defined or not.</p> "To sort a dictionary" (Python) 2001-04-08T19:35:01-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52306-to-sort-a-dictionary/ <p style="color: grey"> Python recipe 52306 by <a href="/recipes/users/97991/">Alex Martelli</a> (<a href="/recipes/tags/search/">search</a>). Revision 2. </p> <p>Dictionaries can't be sorted -- a mapping has no ordering! -- so, when you feel the need to sort one, you no doubt want to sort its <em>keys</em> (in a separate list). Sorting (key,value) pairs (items) is simplest, but not fastest.</p> urrlib2 opener for SSL proxy (CONNECT method) (Python) 2005-11-16T15:04:54-08:00Alessandro Budaihttp://code.activestate.com/recipes/users/2668504/http://code.activestate.com/recipes/456195-urrlib2-opener-for-ssl-proxy-connect-method/ <p style="color: grey"> Python recipe 456195 by <a href="/recipes/users/2668504/">Alessandro Budai</a> (<a href="/recipes/tags/network/">network</a>). Revision 2. </p> <p>This small module builds an urllib2 opener that can be used to make a connection through a proxy using the http CONNECT method (that can be used to proxy SSLconnections). The current urrlib2 seems to not support this method.</p> Snake the game (Python) 2015-01-03T02:04:02-08:00Burak Tandoganhttp://code.activestate.com/recipes/users/4191373/http://code.activestate.com/recipes/578996-snake-the-game/ <p style="color: grey"> Python recipe 578996 by <a href="/recipes/users/4191373/">Burak Tandogan</a> (<a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/pygame/">pygame</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/snake/">snake</a>, <a href="/recipes/tags/the/">the</a>). Revision 3. </p> <p>Popular Snake game with Python-Pygame module. <strong>Important:</strong> If you try to run this script without special <strong>png-wav</strong> files it will fail, you have to put following files to a directory with this script. "apple.png","snakehead.png","intro.wav","dead.wav". They are basically apple picture, head of snake, intro sound and dead sound.Without them script will not run. Just make inactive the statements intro.wav and dead.wav, and find a little apple picture and snake head :).Or you can play the game just install it. <strong>Here is the installer:</strong> <a href="http://dosya.co/7c6f15c0f4d04514/Flafel-1.26-win32.rar" rel="nofollow">http://dosya.co/7c6f15c0f4d04514/Flafel-1.26-win32.rar</a> It will install the game in program files, you can find it in search "Flafel" and click to Flafel.exe. </p> Byte to Hex and Hex to Byte String Conversion (Python) 2007-03-21T06:35:40-07:00Simon Peveretthttp://code.activestate.com/recipes/users/1410328/http://code.activestate.com/recipes/510399-byte-to-hex-and-hex-to-byte-string-conversion/ <p style="color: grey"> Python recipe 510399 by <a href="/recipes/users/1410328/">Simon Peverett</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>I write a lot of ad-hoc protocol analysers using Python. Generally, I'm dealing with a byte stream that I want to output as a string of hex. Sometimes, I want to convert it back again. Eventually, I got round to putting the functions in a module so I wouldn't keep cut and pasting them :)</p> Uno (Text-Based) (Python) 2017-07-15T00:46:59-07:00Brandon Martinhttp://code.activestate.com/recipes/users/4194238/http://code.activestate.com/recipes/580811-uno-text-based/ <p style="color: grey"> Python recipe 580811 by <a href="/recipes/users/4194238/">Brandon Martin</a> (<a href="/recipes/tags/artificial_intelligence/">artificial_intelligence</a>, <a href="/recipes/tags/cards/">cards</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/text_game/">text_game</a>, <a href="/recipes/tags/uno/">uno</a>). </p> <p>A text based recreation of the classic card game featuring functional AIs to play with. Some rules have been modified. User interface is text based, non-curses, using only simple python commands to draw it. </p> OrderedSet (Python) 2012-12-19T07:12:32-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576694-orderedset/ <p style="color: grey"> Python recipe 576694 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/ordered/">ordered</a>, <a href="/recipes/tags/set/">set</a>). Revision 9. </p> <p>Set that remembers original insertion order.</p> Dijkstra's algorithm for shortest paths (Python) 2002-04-04T12:38:22-08:00David Eppsteinhttp://code.activestate.com/recipes/users/218935/http://code.activestate.com/recipes/119466-dijkstras-algorithm-for-shortest-paths/ <p style="color: grey"> Python recipe 119466 by <a href="/recipes/users/218935/">David Eppstein</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>Dijkstra(G,s) finds all shortest paths from s to each other vertex in the graph, and shortestPath(G,s,t) uses Dijkstra to find the shortest path from s to t. Uses the priorityDictionary data structure (<a href="http://code.activestate.com/recipes/117228/">Recipe 117228</a>) to keep track of estimated distances to each vertex.</p> Converting between ASCII numbers and characters (Python) 2001-06-10T15:39:16-07:00Hamish Lawsonhttp://code.activestate.com/recipes/users/98049/http://code.activestate.com/recipes/65117-converting-between-ascii-numbers-and-characters/ <p style="color: grey"> Python recipe 65117 by <a href="/recipes/users/98049/">Hamish Lawson</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>You want to get the ASCII number of a character, or you want to get the character given by an ASCII number.</p> Get the path of the currently executing python script using import. (Python) 2006-02-24T05:23:36-08:00Jitender Cheemahttp://code.activestate.com/recipes/users/2790517/http://code.activestate.com/recipes/474083-get-the-path-of-the-currently-executing-python-scr/ <p style="color: grey"> Python recipe 474083 by <a href="/recipes/users/2790517/">Jitender Cheema</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <h4>1. sys.arg has the script name</h4> <h4>2. Although there can be many ways, e.g. os.cwd() but, there is another</h4> <h4>trick to</h4> <h4>obtain the Complete URI or Location of the current script.</h4> <h4>You can argue, os.getcwd()</h4> <h4>import can give you working directory of the current script</h4> <h4>Third party Java code calling your script.... ;)</h4> <p>#</p> How to use super() effectively -- Python 2.7 version (Python) 2011-06-01T19:19:37-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577721-how-to-use-super-effectively-python-27-version/ <p style="color: grey"> Python recipe 577721 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/cooperative/">cooperative</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/multiple/">multiple</a>, <a href="/recipes/tags/next_in_mro/">next_in_mro</a>, <a href="/recipes/tags/super/">super</a>). Revision 8. </p> <p>Python's super() provides a unique and amazing capability. It allows subclasses to be written to reorder a chain method calls. The recipe demonstrates all of the tradecraft needed to get super() to do your bidding.</p> socket.recv -- three ways to turn it into recvall (Python) 2005-04-05T13:47:57-07:00John Nielsenhttp://code.activestate.com/recipes/users/135654/http://code.activestate.com/recipes/408859-socketrecv-three-ways-to-turn-it-into-recvall/ <p style="color: grey"> Python recipe 408859 by <a href="/recipes/users/135654/">John Nielsen</a> (<a href="/recipes/tags/network/">network</a>). Revision 5. </p> <p>An issue with socket.recv is how to know when you are done receiving data. A TCP stream guarantees the bytes will not arrive out of order or be sent more than once. But you do not know the size of the data that will be sent to you. 100 bytes could be sent as group of 10 bytes or maybe in one shot. Ultimately, this means you have to use a loop in some fashion until you know it is done.</p> <p>The basic recv returns an empty string when the socket is disconnected. From that you can build a simple loop that will work as long as the sender manages to disconnect the socket at the appropriate time. However, there could be situations where a local error will mask as a clean shutdown or maybe a close() is never called.</p> <p>Three very basic methods are shown below that try to fix that problem. They use either a time-based, end marker, or size of payload method. Since you cannot be sure just what you are going to receive, you have to be careful that you get enough of a message to determine the size of payload or end marker.</p> <p>I updated the recv_size method to allocate data in larger chunks if it gets a large stream of data, which can increase performance.</p> getch()-like unbuffered character reading from stdin on both Windows and Unix (Python) 2003-01-07T02:31:32-08:00Danny Yoohttp://code.activestate.com/recipes/users/98032/http://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin/ <p style="color: grey"> Python recipe 134892 by <a href="/recipes/users/98032/">Danny Yoo</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 2. </p> <p>A small utility class to read single characters from standard input, on both Windows and UNIX systems. It provides a getch() function-like instance.</p> Singleton? We don't need no stinkin' singleton: the Borg design pattern (Python) 2001-08-27T08:05:21-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/66531-singleton-we-dont-need-no-stinkin-singleton-the-bo/ <p style="color: grey"> Python recipe 66531 by <a href="/recipes/users/97991/">Alex Martelli</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>The Singleton design pattern (DP) has a catchy name, but the wrong focus -- on identity rather than on state. The Borg design pattern has all instances share state instead, and Python makes it, literally, a snap.</p>