Top-rated recipes by Sunjay Varma http://code.activestate.com/recipes/users/4174115/top/2012-03-31T21:28:12-07:00ActiveState Code RecipesPython Tkinter Canvas Rectangle Selection Box (Python)
2010-10-01T02:17:50-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577409-python-tkinter-canvas-rectangle-selection-box/
<p style="color: grey">
Python
recipe 577409
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/canvas/">canvas</a>, <a href="/recipes/tags/rectangle/">rectangle</a>, <a href="/recipes/tags/selection/">selection</a>, <a href="/recipes/tags/tkinter/">tkinter</a>).
</p>
<p>A simple, yet effective rectangle selection box. :)</p>
<p>Works with a tkinter canvas! Just add the class and set it up like in the example code. The cross heir was my own touch, the RectTracker only draws a box.</p>
<p>Have fun! And please don't just vote down, post what you don't like if you don't like it.</p>
Python Template Engine (Python)
2012-03-31T21:28:12-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/578090-python-template-engine/
<p style="color: grey">
Python
recipe 578090
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/engine/">engine</a>, <a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/macro/">macro</a>, <a href="/recipes/tags/template/">template</a>).
</p>
<p>This is a simple template engine which allows you to replace macros within text. This engine allows for attributes and filters. The default implementation provides the entire string module as filters. Trying to use arguments will of course not work (since the framework supports no other arguments for the filter other than the filtered string itself).</p>
Dictionary Who's Keys Act Like Attributes As Well (Python)
2011-05-26T20:15:16-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577590-dictionary-whos-keys-act-like-attributes-as-well/
<p style="color: grey">
Python
recipe 577590
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/attribute/">attribute</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/keys/">keys</a>).
</p>
<p>Think of this as a JavaScript object. In JavaScript, the objects can be referenced by indexing (e.g. d[name]) or by directly using the dot (.) operator (e.g. d.name).</p>
<p>This is the same concept. </p>
<p><strong>Note to Python 2.4 Users:</strong> You will need to change the "except KeyError as e:" line to "except KeyError, (e):".</p>
Simple recursive function to non-recursive function (Python)
2011-05-27T01:47:37-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577724-simple-recursive-function-to-non-recursive-functio/
<p style="color: grey">
Python
recipe 577724
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/recursive/">recursive</a>, <a href="/recipes/tags/substitute/">substitute</a>).
</p>
<p>This recipe is a simple solution for turning a recursive function into a non-recursive function.</p>
Fibonacci Number Generator (Python)
2011-04-13T00:00:59-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577651-fibonacci-number-generator/
<p style="color: grey">
Python
recipe 577651
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/fibonacci/">fibonacci</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/memorizing/">memorizing</a>, <a href="/recipes/tags/number/">number</a>).
</p>
<p>This recipe allows for individual number generation, and allows for generations of number ranges as well. </p>
<p>The recipe automatically memorizes numbers generated before and keeps them through out the running of the script. This recipe cannot handle too large of numbers, but does act as a simple solution for most cases.</p>
Special Range Function for Different Kinds of Ranges (int, float, character) (Python)
2011-03-30T16:42:47-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577583-special-range-function-for-different-kinds-of-rang/
<p style="color: grey">
Python
recipe 577583
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/character/">character</a>, <a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/integer/">integer</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/special/">special</a>).
Revision 3.
</p>
<p>This module allows the user to create a more verbose set of ranges. Simple character ranges, and float ranges are supported.</p>
<p>Supported Ranges:</p>
<ul>
<li>Basic Integer Ranges</li>
<li>Float Ranges (as accurate as a float range can get)</li>
<li>Simple character ranges (lowercase to lowercase, uppercase to uppercase, etc.)</li>
</ul>
<p>It should work in Python 2 and Python 3.</p>
<p><strong>If you tested this for speed, or want to test this for speed, please post the results! (And your system specs)</strong></p>
<p><strong>Edit:</strong> Found a really silly error of mine when using range instead of xrange in these functions!</p>
Python Sessions (Python)
2011-01-08T03:23:05-08:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577524-python-sessions/
<p style="color: grey">
Python
recipe 577524
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/cgi/">cgi</a>, <a href="/recipes/tags/cookie/">cookie</a>, <a href="/recipes/tags/management/">management</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/security/">security</a>, <a href="/recipes/tags/sessions/">sessions</a>).
Revision 2.
</p>
<p>I think this script should now be functional enough to post here at ActiveState. When you reply or vote down, please post some information on your problem with the code, and if you can, maybe something about the solution. This interface works, and is designed especially for Python. If you like it, don't be afraid to post that either, constructive criticism is all good, but compliments are even better! ;)</p>
<p>Get the latest version of this code here:
<a href="http://wiki.sunjay.ca/Python:Contents/Python_Session" rel="nofollow">http://wiki.sunjay.ca/Python:Contents/Python_Session</a></p>
<p><strong>Latest Changes:</strong></p>
<ul>
<li>Added a new function for making the session cookie last longer. See code for <code>set_expires</code>. Note: This new function is just in case you need to include a session for longer than one brower session (for example, one day instead). </li>
</ul>
Simple Regular Expression Tester (Python)
2010-12-25T00:12:44-08:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577517-simple-regular-expression-tester/
<p style="color: grey">
Python
recipe 577517
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/line/">line</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/quick/">quick</a>, <a href="/recipes/tags/re/">re</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/testing/">testing</a>).
Revision 2.
</p>
<p><strong><em>Is it possible to create a simple command line program that I can use to quickly test my regular expressions?</em></strong></p>
<p>Yes it is. This simple regular expression tester uses Python's re module as well as others to quickly allow you to test your regular expression in one go.</p>
<p>TODO:</p>
<ul>
<li>Add Support For Multiple Regular Expression Input</li>
</ul>
<p>Recent Changes:</p>
<ul>
<li>Made the output prettier with a little more whitespace. More bytes, but at least it's easier to read!</li>
</ul>
Python Tkinter Tabs (Python)
2010-06-15T01:50:58-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577261-python-tkinter-tabs/
<p style="color: grey">
Python
recipe 577261
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sunjay/">sunjay</a>, <a href="/recipes/tags/tabs/">tabs</a>, <a href="/recipes/tags/tkinter/">tkinter</a>, <a href="/recipes/tags/varma/">varma</a>).
Revision 4.
</p>
<p>This tabbed interface is very useful for Tkinter users. I am sure there is a better way to do it, but I did the best I could.</p>
<p>Please enjoy this tabbed interface by SunjayVarma - <a href="http://www.sunjay-varma.com" rel="nofollow">www.sunjay-varma.com</a></p>
<p>UPDATES:
Fixed the index issues. Made it easier to use.</p>
Improved range function (Python)
2010-06-07T01:50:20-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577256-improved-range-function/
<p style="color: grey">
Python
recipe 577256
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/replace/">replace</a>).
Revision 2.
</p>
<p>While using the built-in range function a while ago. I found an odd (perhaps bug) where the range function couldn't use float steps. I am not sure if that was intended for simplicity or not, but I wrote my own range function that goes beyond that anyway.</p>
Tkinter Splash Screen (Python)
2010-06-21T18:09:06-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577271-tkinter-splash-screen/
<p style="color: grey">
Python
recipe 577271
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/screen/">screen</a>, <a href="/recipes/tags/splash/">splash</a>, <a href="/recipes/tags/tkinter/">tkinter</a>).
</p>
<p>I wanted to mess around with the overrideredirect function of the window manager, and ended up making a pretty good Python Tkinter splash screen. :)</p>
<p>Just run it for a demo.</p>
<p>There are instructions and more info in the script</p>
Flatten a list (or list of lists, etc.) (Python)
2011-03-01T03:46:26-08:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577255-flatten-a-list-or-list-of-lists-etc/
<p style="color: grey">
Python
recipe 577255
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/extend/">extend</a>, <a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/optimization/">optimization</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/smart/">smart</a>).
Revision 2.
</p>
<p>I created this function sometime ago for my own purposes. It simply flattens a list.</p>
<p>Check out the first comment for another version of someone else's design.</p>