<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ed The Dev .com &#187; Example Code</title>
	<atom:link href="http://www.edthedev.com/topics/example-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.edthedev.com</link>
	<description>Edward Delaporte&#039;s Technical Journal</description>
	<lastBuildDate>Fri, 30 Jul 2010 14:20:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Application launcher in Python</title>
		<link>http://www.edthedev.com/2010/application-launcher-in-python/</link>
		<comments>http://www.edthedev.com/2010/application-launcher-in-python/#comments</comments>
		<pubDate>Sat, 24 Apr 2010 14:32:53 +0000</pubDate>
		<dc:creator>Edward Delaporte</dc:creator>
				<category><![CDATA[Example Code]]></category>

		<guid isPermaLink="false">http://www.edthedev.com/2010/application-launcher-in-python/</guid>
		<description><![CDATA[I&#8217;ve been playing with writing application and python tool launchers to turn my EeePC into some kind of freakishly powerful custom PDA. The source code for my first two proofs-of-concept are included below. You can grab the latest source at http://svn.edthedev.com/projects/eeePy/. cLauncher.py Curses application launcher #!/usr/bin/env python # Copyright Edward Delaporte 2010 # URI: edthedev.com [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing with writing application and python tool launchers to turn my EeePC into some kind of freakishly powerful custom PDA. The source code for my first two proofs-of-concept are included below.</p>

<p>You can grab the latest source at <a href="http://svn.edthedev.com/projects/eeePy/">http://svn.edthedev.com/projects/eeePy/</a>.</p>

<h3>cLauncher.py</h3>

<p>Curses application launcher</p>

<p>
<pre><code>
#!/usr/bin/env python

# Copyright Edward Delaporte 2010
# URI: <a href="http://edthedev.com" title="http://edthedev.com" target="_blank">edthedev.com</a>

# Licensed under the Eclipse Public License v. 1.0
# <a href="http://www.opensource.org/licenses/eclipse-1.0.php" title="http://www.opensource.org/licenses/eclipse-1.0.php" target="_blank">www.opensource.org/licenses/eclipse-1.0.php</a>

from os import system
from FavoriteApps import FavoriteApps
from cWindow import CursesWindow
from ClassInspector import ClassInspector

def runMethod(screen, method, historyFile=None):
    args = ClassInspector.getMethodArguments(FavoriteApps, method)
    if(len(args)==0):
        # Run the command.
        getattr(FavoriteApps, method)()
    else:
        argInputs = {}
        for arg in args:
            argInputs[arg]= getInput(screen, "Enter argument for %s" % arg)
        # Run the command with arguments.    
        getattr(FavoriteApps, method)(**argInputs)


myClasses = {'FavoriteApps':FavoriteApps}

window = CursesWindow()

selection = ''
while selection != 'exit':
    window.displayList(myClasses, 'Available classes:')
    selection = window.getInput("Choose a class: ")
    for className in myClasses:
        if selection in className:
            classObj = ClassInspector(myClasses[className])

            command = ''
            while command != 'exit':
                window.displayList(classObj.methods, 'Available commands:')
                # window.drawMethodMenu(classObj.methods)
                command = window.getInput("Enter a command: ") 
                for method in classObj.methods:
                    if command in method:
                        window.output("Running %s" % method)
                        runMethod(window.screen, method)

del window
</code></pre>
</p>

<h3>ClassInspector.py</h3>

<p><pre><code>
import inspect</p>

<p>class ClassInspector(object):
    def <strong>init</strong>(self, classObj):
        self.classObj = classObj
        self.methods = self.getMethodsInClass(self.classObj)
        self.methodArgs = {}
        #for method in self.methods:
        #    self.methodsArgs[method] = self.getMethodArguments(self.classObj, method)</p>

<pre><code>@staticmethod
def getMethodsInClass(classObj):
    results = []
    for name in dir(classObj):
        obj = getattr(classObj, name)
        if (inspect.ismethod(obj) or inspect.isfunction(obj)):
            results.append(name)
    return results

@staticmethod
def getMethodArguments(classObj, methodName):
    method = getattr(classObj, methodName)
    argDetails = inspect.getargspec(method)
    (args, _, _, defaults) = argDetails
    return args
</code></pre>

<p></code></pre></p>

<p>UPDATE: I updated it to use the curses (plain text) library rather than requiring tkinter (graphics). If you&#8217;re running Linux or Apple OSX you have curses installed already. If you&#8217;re running Windows, you can install NCurses but may need to tweak the code a little. You can still find the tkinter version in SVN repository.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.edthedev.com/2010/application-launcher-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java array copy</title>
		<link>http://www.edthedev.com/2010/java-array-copy/</link>
		<comments>http://www.edthedev.com/2010/java-array-copy/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 03:14:34 +0000</pubDate>
		<dc:creator>Edward Delaporte</dc:creator>
				<category><![CDATA[Example Code]]></category>

		<guid isPermaLink="false">http://www.edthedev.com/?p=10837</guid>
		<description><![CDATA[System.arraycopy(intervals,0,temp,0, intervals.length); System.arraycopy(biggerByteArray,0,temp,intervals. length, biggerByteArray.length);]]></description>
			<content:encoded><![CDATA[<p><pre><code>
System.arraycopy(intervals,0,temp,0, intervals.length);
System.arraycopy(biggerByteArray,0,temp,intervals. length,
biggerByteArray.length);
</code>
</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edthedev.com/2010/java-array-copy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Standard Library Bingo</title>
		<link>http://www.edthedev.com/2010/standard-library-bingo/</link>
		<comments>http://www.edthedev.com/2010/standard-library-bingo/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 20:15:41 +0000</pubDate>
		<dc:creator>Edward Delaporte</dc:creator>
				<category><![CDATA[Example Code]]></category>

		<guid isPermaLink="false">http://www.edthedev.com/?p=10842</guid>
		<description><![CDATA[As Google gets worse and worse at seeing through spam to identify standard libraries, I find it compelling to create a cheat sheet. Be sure to check the &#8216;last updated&#8217; date on this post before you decide to use it. If this hasn&#8217;t been updated in awhile (because I&#8217;ve joined the first colony ship on [...]]]></description>
			<content:encoded><![CDATA[<p>As Google gets worse and worse at seeing through spam to identify standard libraries, I find it compelling to create a cheat sheet. 
Be sure to check the &#8216;last updated&#8217; date on this post before you decide to use it. If this hasn&#8217;t been updated in awhile (because I&#8217;ve joined the first colony ship on it&#8217;s way to Alpha Centauri), you may want to look for a more recent version.</p>

<dl>
<dt>Convert a string into bytes</dt>
<dd>Java &#8211; <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#getBytes(java.lang.String)">String.getBytes(&#8220;US-ASCII&#8221; or &#8220;UTF-8&#8243;)</a></dd>
<dd>C# 
<pre><code>System.Text.UTF8Encoding stringToBytes = new System.Text.UTF8Encoding();
Byte[] inputBytes = stringToBytes.GetBytes(input);
</code></pre>
            
</dd>
<dt>Check Endian-ness of your system.</dt>
<dd>Java &#8211; Emulates a big endian system </dd>
<dd>Java &#8211; Let&#8217;s you inspect the underlying chip with java.nio.ByteOrder.nativeOrder() </dd>
<dd>C# &#8211; System.BitConverter.IsLittleEndian </dd>
<dd>C# &#8211; IPAddress.HostToNetworkOrder </dd>
<dt>SHA One way hash</dt>
<dd>C# &#8211; System.Security.Cryptography.SHA256Managed</dd>
<dd>Java &#8211; <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/security/MessageDigest.html">java.security.MessageDigest</a></dd>
</dl>
]]></content:encoded>
			<wfw:commentRss>http://www.edthedev.com/2010/standard-library-bingo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get a long random string from /dev/random</title>
		<link>http://www.edthedev.com/2010/get-a-long-random-string-from-devrandom/</link>
		<comments>http://www.edthedev.com/2010/get-a-long-random-string-from-devrandom/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 17:58:24 +0000</pubDate>
		<dc:creator>Edward Delaporte</dc:creator>
				<category><![CDATA[Example Code]]></category>
		<category><![CDATA[Solutions]]></category>

		<guid isPermaLink="false">http://www.edthedev.com/?p=10819</guid>
		<description><![CDATA[Use word count to make sure you&#8217;re getting the length you expected. cat /dev/urandom&#124; tr -dc 'a-zA-Z0-9-_!@#$%^&#38;*()_+{}&#124;:&#60;&#62;?=' &#124; fold -w 1048 &#124; head -n1 &#124; wc -m And use this to actually fetch the long string. cat /dev/urandom&#124; tr -dc 'a-zA-Z0-9-_!@#$%^&#38;*()_+{}&#124;:&#60;&#62;?=' &#124; fold -w 1048 &#124; head -n1 &#124; wc -m Here&#8217;s a much simpler [...]]]></description>
			<content:encoded><![CDATA[<p>Use word count to make sure you&#8217;re getting the length you expected.</p>

<blockquote><code>cat /dev/urandom| tr -dc 'a-zA-Z0-9-_!@#$%^&amp;*()_+{}|:&lt;&gt;?=' | fold -w 1048 | head -n1 | wc -m
</code></blockquote>

<p>And use this to actually fetch the long string.</p>

<blockquote><code>cat /dev/urandom| tr -dc 'a-zA-Z0-9-_!@#$%^&amp;*()_+{}|:&lt;&gt;?=' | fold -w 1048 | head -n1 | wc -m</code></blockquote>

<p>Here&#8217;s a much simpler example, in case you just want a quick four digit number:</p>

<blockquote>cat /dev/urandom| tr -dc &#8217;0-9&#8242; | fold -w 4 | head -n1</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.edthedev.com/2010/get-a-long-random-string-from-devrandom/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Debug Output</title>
		<link>http://www.edthedev.com/2010/java-debug-output/</link>
		<comments>http://www.edthedev.com/2010/java-debug-output/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 17:30:15 +0000</pubDate>
		<dc:creator>Edward Delaporte</dc:creator>
				<category><![CDATA[Example Code]]></category>

		<guid isPermaLink="false">http://www.edthedev.com/?p=10821</guid>
		<description><![CDATA[Since my last several modules have been core enough to merit creating both Java and .Net, I&#8217;m getting pretty proficient at translating between the two languages. Here&#8217;s my Java version of my previous article about C# Debug Output. private static final boolean DEBUG = true; private static void DebugOutput(String message) { DebugOutput(message, null); } private [...]]]></description>
			<content:encoded><![CDATA[<p>Since my last several modules have been core enough to merit creating both Java and .Net, I&#8217;m getting pretty proficient at translating between the two languages.</p>

<p>Here&#8217;s my Java version of my previous article about <a href="http://www.edthedev.com/2010/c-debug-output">C# Debug Output.</a>
<pre>
<code>
    private static final boolean DEBUG = true;
    private static void DebugOutput(String message)
    {
        DebugOutput(message, null);
    }</p>

<pre><code>private static void DebugOutput(String message, Object details)
{
    if(DEBUG)
    {
        System.out.println(message + ": " + details.toString());
    }
}
</code></pre>

<p></code>
</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edthedev.com/2010/java-debug-output/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Array.ToString</title>
		<link>http://www.edthedev.com/2010/c-array-tostring/</link>
		<comments>http://www.edthedev.com/2010/c-array-tostring/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 15:52:48 +0000</pubDate>
		<dc:creator>Edward Delaporte</dc:creator>
				<category><![CDATA[Example Code]]></category>

		<guid isPermaLink="false">http://www.edthedev.com/?p=10793</guid>
		<description><![CDATA[Got tired of searching up this line elsewhere. Now that I&#8217;m writing this, I&#8217;ll probably memorize it in the process. That&#8217;ll be nice. public override String ToString() { return "' Search scope: " + this.searchScope + " Filter: '" + this.getFilter() + "' Search base: '" + this.searchBase + "'" + " Returned attributes: {" [...]]]></description>
			<content:encoded><![CDATA[<p>Got tired of searching up this line elsewhere. Now that I&#8217;m writing this, I&#8217;ll probably memorize it in the process. That&#8217;ll be nice.
<pre>
<code>
public override String ToString() 
{
   return "' Search scope: " + this.searchScope
    + " Filter: '" + this.getFilter()
    + "' Search base: '" + this.searchBase + "'"
    + " Returned attributes: {" + String.Join(" , ", this.returnedAttributes.ToArray()) + "}";
}
</code>
</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edthedev.com/2010/c-array-tostring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Debug output</title>
		<link>http://www.edthedev.com/2010/c-debug-output/</link>
		<comments>http://www.edthedev.com/2010/c-debug-output/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 15:33:18 +0000</pubDate>
		<dc:creator>Edward Delaporte</dc:creator>
				<category><![CDATA[Example Code]]></category>
		<category><![CDATA[Solutions]]></category>

		<guid isPermaLink="false">http://www.edthedev.com/2010/c-debug-output/</guid>
		<description><![CDATA[Here&#8217;s a snippet that I use to push debug output to the console, only when compiled in Debug mode. private static void DebugOutput(String message) { DebugOutput(message, null); } private static void DebugOutput(String message, object details) { if DEBUG System.Console.WriteLine(message, details); endif }]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a snippet that I use to push debug output to the console, only when compiled in Debug mode.
<pre>
private static void DebugOutput(String message)
{
   DebugOutput(message, null);
}</p>

<p>private static void DebugOutput(String message, object details)
{</p>

<h1>if DEBUG</h1>

<p>System.Console.WriteLine(message, details);</p>

<h1>endif</h1>

<p>}
</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edthedev.com/2010/c-debug-output/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Google Playground</title>
		<link>http://www.edthedev.com/2010/google-knows-how-to-play/</link>
		<comments>http://www.edthedev.com/2010/google-knows-how-to-play/#comments</comments>
		<pubDate>Sat, 27 Mar 2010 14:22:48 +0000</pubDate>
		<dc:creator>Edward Delaporte</dc:creator>
				<category><![CDATA[Example Code]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.edthedev.com/2010/google-knows-how-to-play/</guid>
		<description><![CDATA[The Google code playground gets it exactly right. The panel in the upper left is for exploring their example code. The panel to it&#8217;s right is for both reading and tweaking their example code. The panel on the bottom is for running it and seeing the output, including whatever changes you have made. Well played [...]]]></description>
			<content:encoded><![CDATA[<p>The Google code playground gets it exactly right. The panel in the upper left is for exploring their example code. The panel to it&#8217;s right is for both reading and <em>tweaking</em> their example code. The panel on the bottom is for running it and seeing the output, including whatever changes you have made.
Well played Google, well played!

<a href="http://code.google.com/apis/ajax/playground/</p>&#8221; title=&#8221;http://code.google.com/apis/ajax/playground/</p>&#8221; target=&#8221;_blank&#8221;>code.google.com/apis/ajax/playground/</p></a>
]]></content:encoded>
			<wfw:commentRss>http://www.edthedev.com/2010/google-knows-how-to-play/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Android Development with Eclipse</title>
		<link>http://www.edthedev.com/2010/android-development-with-eclipse/</link>
		<comments>http://www.edthedev.com/2010/android-development-with-eclipse/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 04:28:06 +0000</pubDate>
		<dc:creator>Edward Delaporte</dc:creator>
				<category><![CDATA[Example Code]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Solutions]]></category>

		<guid isPermaLink="false">http://www.edthedev.com/2010/android-development-with-eclipse/</guid>
		<description><![CDATA[Here&#8217;s a guide to get you started developing Android applications with Eclipse. The whole process took me about an hour tonight. Step 1: Get Eclipse setup for Android development: Installing the Eclipse Android Plugin Step 2: Write &#8216;Hello World&#8217;: Hello World for Android with Eclipse Step 3: Bask in the aura of how much nicer [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a guide to get you started developing Android applications with Eclipse.
The whole process took me about an hour tonight.</p>

<p>Step 1: Get Eclipse setup for Android development:
<a href="http://developer.android.com/guide/developing/eclipse-adt.html">Installing the Eclipse Android Plugin</a></p>

<p>Step 2: Write &#8216;Hello World&#8217;:
<a href="http://developer.android.com/guide/tutorials/hello-world.html">Hello World for Android with Eclipse</a></p>

<p>Step 3: Bask in the aura of how much nicer Java is than Objective-C, and how much simpler it is to read two web pages than watch 15 videos through iTunes.</p>

<p>It&#8217;s hard to express how nice a first impression the Android plugin for Eclipse makes. It&#8217;s a much nicer experience to work with than XCode. Of course, so is getting poked in the eye by a monkey; so not the most relevant comparison.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.edthedev.com/2010/android-development-with-eclipse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>If you&#8217;re thinking of using isInstance</title>
		<link>http://www.edthedev.com/2010/if-youre-thinking-of-using-isinstance/</link>
		<comments>http://www.edthedev.com/2010/if-youre-thinking-of-using-isinstance/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 17:35:51 +0000</pubDate>
		<dc:creator>Edward Delaporte</dc:creator>
				<category><![CDATA[Example Code]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.edthedev.com/?p=10705</guid>
		<description><![CDATA[Think again:]]></description>
			<content:encoded><![CDATA[<p>Think again: <a href="http://www.canonical.org/~kragen/isinstance/</p>&#8221; title=&#8221;http://www.canonical.org/~kragen/isinstance/</p>&#8221; target=&#8221;_blank&#8221;>www.canonical.org/~kragen/isinstance/</p></a>

<p>I was tempted to use isInstance, when what I really needed was has_attr.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.edthedev.com/2010/if-youre-thinking-of-using-isinstance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
