Most viewed recipes tagged "meta:requires=sys"http://code.activestate.com/recipes/tags/meta:requires=sys/views/2017-07-15T00:46:59-07:00ActiveState Code RecipesConstants 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> 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> 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> 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> 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> Fork a daemon process on Unix (Python) 2001-07-10T14:01:38-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/66012-fork-a-daemon-process-on-unix/ <p style="color: grey"> Python recipe 66012 by <a href="/recipes/users/98061/">Jürgen Hermann</a> (<a href="/recipes/tags/threads/">threads</a>). </p> <p>Forking a daemon on Unix requires a certain sequence of system calls. Since Python exposes a full POSIX interface, this can be done in Python, too.</p> Named Tuples (Python) 2009-05-26T22:44:39-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/500261-named-tuples/ <p style="color: grey"> Python recipe 500261 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 15. </p> <p>Fast, lightweight attribute-style access to tuples.</p> Chat server & client using select.select (Python) 2007-09-28T08:57:53-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/531824-chat-server-client-using-selectselect/ <p style="color: grey"> Python recipe 531824 by <a href="/recipes/users/760763/">Anand</a> (<a href="/recipes/tags/network/">network</a>). Revision 3. </p> <p>This recipe demos how to write a simple command line chat server &amp; client using multiplexing using select.select. The server uses select call to multiplex multiple clients and the client uses it to multiplex command line &amp; socket I/O.</p> Tail Call Optimization Decorator (Python) 2006-02-26T15:02:54-08:00Crutcher Dunnavanthttp://code.activestate.com/recipes/users/2792865/http://code.activestate.com/recipes/474088-tail-call-optimization-decorator/ <p style="color: grey"> Python recipe 474088 by <a href="/recipes/users/2792865/">Crutcher Dunnavant</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>This decorator implements tail call optimization through stack introspection.</p> Compute Memory footprint of an object and its contents (Python) 2012-11-23T23:57:31-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577504-compute-memory-footprint-of-an-object-and-its-cont/ <p style="color: grey"> Python recipe 577504 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sizeof/">sizeof</a>). Revision 3. </p> <p>Recursive version sys.getsizeof(). Extendable with custom handlers.</p> Downloading a File from the Web (Python) 2007-06-13T09:15:37-07:00Aristotelis Mikropouloshttp://code.activestate.com/recipes/users/2881737/http://code.activestate.com/recipes/496685-downloading-a-file-from-the-web/ <p style="color: grey"> Python recipe 496685 by <a href="/recipes/users/2881737/">Aristotelis Mikropoulos</a> (<a href="/recipes/tags/ui/">ui</a>). </p> <p>This script reads the contents of a web file and copies them into a local file, named the same as the web file.</p> Search and replace text in a file. (Python) 2001-03-13T20:18:13-08:00Jeff Bauerhttp://code.activestate.com/recipes/users/98055/http://code.activestate.com/recipes/52250-search-and-replace-text-in-a-file/ <p style="color: grey"> Python recipe 52250 by <a href="/recipes/users/98055/">Jeff Bauer</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>A simple text search and replace program.</p> Module to allow Asynchronous subprocess use on Windows and Posix platforms (Python) 2006-12-01T17:30:02-08:00Josiah Carlsonhttp://code.activestate.com/recipes/users/1241800/http://code.activestate.com/recipes/440554-module-to-allow-asynchronous-subprocess-use-on-win/ <p style="color: grey"> Python recipe 440554 by <a href="/recipes/users/1241800/">Josiah Carlson</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 10. </p> <p>The 'subprocess' module in Python 2.4 has made creating and accessing subprocess streams in Python relatively convenient for all supported platforms, but what if you want to interact with the started subprocess? That is, what if you want to send a command, read the response, and send a new command based on that response?</p> <p>Now there is a solution. The included subprocess.Popen subclass adds three new commonly used methods: recv(maxsize=None), recv_err(maxsize=None), and send(input), along with a utility method: send_recv(input='', maxsize=None).</p> <p>recv() and recv_err() both read at most maxsize bytes from the started subprocess. send() sends strings to the started subprocess. send_recv() will send the provided input, and read up to maxsize bytes from both stdout and stderr.</p> <p>If any of the pipes are closed, the attributes for those pipes will be set to None, and the methods will return None.</p> <p>v. 1.3 fixed a few bugs relating to *nix support v. 1.4,5 fixed initialization on all platforms, a few bugs relating to Windows support, added two utility functions, and added an example of how to use this module. v. 1.6 fixed linux _recv() and test initialization thanks to Yuri Takhteyev at Stanford. v. 1.7 removed _setup() and __init__() and fixed subprocess unittests thanks to Antonio Valentino. Added 4th argument 'tr' to recv_some(), which is, approximately, the number of times it will attempt to recieve data. Added 5th argument 'stderr' to recv_some(), where when true, will recieve from stderr. Cleaned up some pipe closing. v. 1.8 Fixed missing self. parameter in non-windows _recv method thanks to comment. v. 1.9 Fixed fcntl calls for closed handles.</p> Open a GLUT window and draw a sphere using Python/OpenGL (Python) 2004-10-27T09:55:45-07:00Rick Mullerhttp://code.activestate.com/recipes/users/1461790/http://code.activestate.com/recipes/325391-open-a-glut-window-and-draw-a-sphere-using-pythono/ <p style="color: grey"> Python recipe 325391 by <a href="/recipes/users/1461790/">Rick Muller</a> (<a href="/recipes/tags/graphics/">graphics</a>). </p> <p>This code snippet uses Python/OpenGL (<a href="http://pyopengl.sourceforge.net" rel="nofollow">http://pyopengl.sourceforge.net</a>) to open a window using GLUT, and draw a sphere into it. I've used this many times as the starting point for more complicated applications.</p> Minimal http upload cgi (Python) 2004-03-20T01:47:04-08:00Noah Spurrierhttp://code.activestate.com/recipes/users/103276/http://code.activestate.com/recipes/273844-minimal-http-upload-cgi/ <p style="color: grey"> Python recipe 273844 by <a href="/recipes/users/103276/">Noah Spurrier</a> (<a href="/recipes/tags/web/">web</a>). Revision 3. </p> <p>This is a bare-bones cgi file upload. It will display an upload form and save the uploaded files to disk.</p> Simple Web Crawler (Python) 2011-01-31T21:57:58-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/576551-simple-web-crawler/ <p style="color: grey"> Python recipe 576551 by <a href="/recipes/users/4167757/">James Mills</a> (<a href="/recipes/tags/crawler/">crawler</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/parsing/">parsing</a>, <a href="/recipes/tags/web/">web</a>). Revision 2. </p> <p>NOTE: This recipe has been updated with suggested improvements since the last revision.</p> <p>This is a simple web crawler I wrote to test websites and links. It will traverse all links found to any given depth.</p> <p>See --help for usage.</p> <p>I'm posting this recipe as this kind of problem has been asked on the Python Mailing List a number of times... I thought I'd share my simple little implementation based on the standard library and BeautifulSoup.</p> <p>--JamesMills</p> DBF reader and writer (Python) 2009-04-29T07:28:14-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/362715-dbf-reader-and-writer/ <p style="color: grey"> Python recipe 362715 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/database/">database</a>). Revision 7. </p> <p>Reader iterates over records in Dbase or Xbase files. Writer creates dbf files from Python sequences.</p>