Popular recipes by Joel Lawhead http://code.activestate.com/recipes/users/545119/2003-11-29T05:12:14-08:00ActiveState Code RecipesInvert a dictionary (one-liner) (Python)
2003-11-17T20:38:28-08:00Joel Lawheadhttp://code.activestate.com/recipes/users/545119/http://code.activestate.com/recipes/252143-invert-a-dictionary-one-liner/
<p style="color: grey">
Python
recipe 252143
by <a href="/recipes/users/545119/">Joel Lawhead</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
</p>
<p>Dictionaries map keys to values. Looking up a value is as simple as typing:
mydict[key]. But what if you want to look up a key? The following one liner returns a new dictionary with the keys and values swapped:</p>
View an image URL with Jython and Swing (Python)
2003-11-29T05:12:14-08:00Joel Lawheadhttp://code.activestate.com/recipes/users/545119/http://code.activestate.com/recipes/252144-view-an-image-url-with-jython-and-swing/
<p style="color: grey">
Python
recipe 252144
by <a href="/recipes/users/545119/">Joel Lawhead</a>
(<a href="/recipes/tags/jython/">jython</a>).
Revision 2.
</p>
<p>The simple Jython function below accepts a url string for an image and displays it in a Swing window.</p>
Windows CDRom Module (Python)
2003-02-14T22:20:55-08:00Joel Lawheadhttp://code.activestate.com/recipes/users/545119/http://code.activestate.com/recipes/180919-windows-cdrom-module/
<p style="color: grey">
Python
recipe 180919
by <a href="/recipes/users/545119/">Joel Lawhead</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
Revision 2.
</p>
<p>This module provides easy control of cd drives on Win32 machines. Using the Python Win32 Extensions, the module automatically detects all cd drives and defaults to the first drive found or to a programmer specified drive. Methods include eject() to open the drive door, close() to close the drive door, and load() which tries to close the drive door and then make the drive available to the file system.</p>
<p>To test either import the WinCDRom module and create an instance of the Cdrom class:</p>
<p>import WinCDRom
cd = WinCDRom.Cdrom()
cd.eject()
cd.close()
cd.load()</p>
<p>Or just run the script from a DOS prompt to use the test function:</p>
<p>C:\python WinCDRom.py</p>
<p>Listing drives:
['D:']
Default drive: D:
Opening drive door...
Closing cd drive door... (CD read attempt will time out at 5 seconds.)
Loaded cd successfully in drive D:
WinCDRom Tests completed.</p>