Top-rated recipes tagged "meta:loc=106"http://code.activestate.com/recipes/tags/meta:loc=106/top/2014-01-31T02:39:48-08:00ActiveState Code Recipessocket.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>
Threads, Tkinter and asynchronous I/O (Python)
2001-10-21T07:14:35-07:00Jacob Hallénhttp://code.activestate.com/recipes/users/136936/http://code.activestate.com/recipes/82965-threads-tkinter-and-asynchronous-io/
<p style="color: grey">
Python
recipe 82965
by <a href="/recipes/users/136936/">Jacob Hallén</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>This recipe shows the easiest way of handling access to sockets, serial ports
and other asynchronous I/O ports while running a Tkinter based GUI.
It allows for a worker thread to block in a select(). Whenever something arrives
it will received and inserted in a queue. The main (GUI) thread then polls
the queue 10 times per second (often enough so the user will not notice any
significant delay), and processes all messages that have arrived.</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>
A simple Tkinter notebook-like widget (Python)
2004-12-26T19:11:42-08:00Iuri Wickerthttp://code.activestate.com/recipes/users/111694/http://code.activestate.com/recipes/188537-a-simple-tkinter-notebook-like-widget/
<p style="color: grey">
Python
recipe 188537
by <a href="/recipes/users/111694/">Iuri Wickert</a>
(<a href="/recipes/tags/ui/">ui</a>).
Revision 4.
</p>
<p>You have some single-toplevel Tkinter apps that you want to organize in a notebook-like fashion, associating each tab to an app, in a way which requires minimal changes in your original apps. This simple notebook class allows that, also supporting different tab orientations (TOP, BOTTOM, LEFT & RIGHT).</p>
Python: A event/signal dispatcher (Python)
2013-07-29T05:34:24-07:00Esteban Castro Borsanihttp://code.activestate.com/recipes/users/4184010/http://code.activestate.com/recipes/578307-python-a-eventsignal-dispatcher/
<p style="color: grey">
Python
recipe 578307
by <a href="/recipes/users/4184010/">Esteban Castro Borsani</a>
(<a href="/recipes/tags/dispatcher/">dispatcher</a>, <a href="/recipes/tags/events/">events</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/signals/">signals</a>).
</p>
<p>This is a thread-safe dispatcher.</p>
Eight queen problem (Python)
2013-05-10T03:44:56-07:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/577438-eight-queen-problem/
<p style="color: grey">
Python
recipe 577438
by <a href="/recipes/users/4174477/">Thomas Lehmann</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/queen/">queen</a>).
Revision 5.
</p>
<p><strong>Task</strong>:</p>
<ul>
<li>Think of a chess field 8x8.</li>
<li>Place 8 queens in a way that no one threaten another one.</li>
</ul>
<p><strong>Intention</strong>:</p>
<ul>
<li>Writing an algorithm to provide all solutions</li>
<li>Adjusting the algorithm in a way that it can handle chess fields by other sizes n x n.</li>
</ul>
<p><strong>Changes</strong>:</p>
<ul>
<li><strong>Revision 5</strong>:
<ul>
<li>can run providing "n" via command line parameter and </li>
<li>run half of board adding the other solutions by mirroring (12x12 takes 4 seconds instead of 8 on my slow notebook)</li>
</ul></li>
<li>...</li>
</ul>
<p><strong>Other languages</strong>:</p>
<ul>
<li>My <a href="http://code.activestate.com/recipes/578497/">recipe 578497</a> for JavaScript</li>
<li>...</li>
</ul>
Generate thumbnail image (Python)
2003-03-21T13:33:42-08:00Keisuke URAGOhttp://code.activestate.com/recipes/users/668964/http://code.activestate.com/recipes/180670-generate-thumbnail-image/
<p style="color: grey">
Python
recipe 180670
by <a href="/recipes/users/668964/">Keisuke URAGO</a>
(<a href="/recipes/tags/graphics/">graphics</a>).
Revision 7.
</p>
<p>On the web and various purpose, we want to thumbnail images.</p>
L-System Fractals (Python)
2010-03-27T19:50:05-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577159-l-system-fractals/
<p style="color: grey">
Python
recipe 577159
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>).
</p>
<p>L-System fractals.
I did not use the Turtle graphics module of Python because this way is much faster.</p>
client/server clipboard application (Tcl)
2002-08-09T08:14:45-07:00Alexander Eisenhuthhttp://code.activestate.com/recipes/users/617853/http://code.activestate.com/recipes/143899-clientserver-clipboard-application/
<p style="color: grey">
Tcl
recipe 143899
by <a href="/recipes/users/617853/">Alexander Eisenhuth</a>
.
</p>
<p>Script that can be used to create a clipboard connection between two machines. The script can be started as a client or server application. See script for more details</p>
Iterator Utilities (Python)
2001-07-30T19:30:23-07:00Sami Hangaslammihttp://code.activestate.com/recipes/users/118800/http://code.activestate.com/recipes/66448-iterator-utilities/
<p style="color: grey">
Python
recipe 66448
by <a href="/recipes/users/118800/">Sami Hangaslammi</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>A collection of small utility functions for iterators (all functions can also be used with normal sequences). Among other things, the module provides generator ("lazy") versions of the built-in sequence-manipulation functions. The generators can be combined to produce a more specialised iterator.</p>
pack multiple images of different sizes into one image (Python)
2013-06-27T14:50:37-07:00Hugohttp://code.activestate.com/recipes/users/4183663/http://code.activestate.com/recipes/578585-pack-multiple-images-of-different-sizes-into-one-i/
<p style="color: grey">
Python
recipe 578585
by <a href="/recipes/users/4183663/">Hugo</a>
(<a href="/recipes/tags/graphics/">graphics</a>).
</p>
<p>Packing images of different sizes into one image is often required in order to efficiently use hardware accelerated texture mapping functions of 3D video cards.</p>
Just Another Password Generator (Python)
2012-06-21T15:41:10-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578171-just-another-password-generator/
<p style="color: grey">
Python
recipe 578171
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/passwords/">passwords</a>, <a href="/recipes/tags/password_generator/">password_generator</a>, <a href="/recipes/tags/utility/">utility</a>).
</p>
<p>The following two programs are simple examples of how one might go about creating a password generator. The first one was created several years ago and was designed to return passwords where similarly classed characters would not appear beside each other. It works but gets unbearably slow when passwords exceed about forty characters. The second program was designed with similar requirements runs considerably better. Eight-character passwords appear to have the same randomness to their construction, and this program is considerably faster than the first. Hopefully, these program design differences will be helpful to others trying to write their own password generator.</p>
Wasted Money Calculator (Python)
2012-01-26T02:27:25-08:00userendhttp://code.activestate.com/recipes/users/4179007/http://code.activestate.com/recipes/578026-wasted-money-calculator/
<p style="color: grey">
Python
recipe 578026
by <a href="/recipes/users/4179007/">userend</a>
(<a href="/recipes/tags/calculate/">calculate</a>, <a href="/recipes/tags/calculator/">calculator</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/money/">money</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/save/">save</a>, <a href="/recipes/tags/tkinter/">tkinter</a>).
Revision 2.
</p>
<p>Calculate the amount of money you waste on the unnecessary items you buy everyday. </p>