<?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>Times New Rohan &#187; Python</title>
	<atom:link href="http://robrohan.com/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://robrohan.com</link>
	<description>技术任意</description>
	<lastBuildDate>Fri, 28 May 2010 23:07:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Monkey Spoon</title>
		<link>http://robrohan.com/2010/05/28/monkey-spoon/</link>
		<comments>http://robrohan.com/2010/05/28/monkey-spoon/#comments</comments>
		<pubDate>Fri, 28 May 2010 22:48:51 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Miscellanea]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Web Apps]]></category>

		<guid isPermaLink="false">http://robrohan.com/2010/05/28/monkey-spoon/</guid>
		<description><![CDATA[For the past year or so, the main project I&#8217;ve been working on is
UNSWTV.  It&#8217;s a system similar to YouTube, but in addition to
hosting videos it can also publish to YouTube and iTunes.  It&#8217;s a vast
project with lots of facets.



Like any architect, seeing people use the system and what they put out on
it [...]]]></description>
			<content:encoded><![CDATA[<p>For the past year or so, the main project I&#8217;ve been working on is
<a href="http://tv.unsw.edu.au" title="UNSWTV">UNSWTV</a>.  It&#8217;s a system similar to YouTube, but in addition to
hosting videos it can also publish to YouTube and iTunes.  It&#8217;s a vast
project with lots of facets.</p>

<p><span id="more-1306"></span></p>

<p>Like any architect, seeing people use the system and what they put out on
it is where the pleasure of building comes from.  I was debugging some
bits the other day and happened upon this movie made by the College of
Fine Arts (COFA Digital Media student John Turello). It&#8217;s a wonderful movie I can &#8211; and I am sure you can -
relate to:</p>

<div class="movie">
<object width="450" height="300"><param name="movie" value="http://tv.unsw.edu.au/p/v.v=wJicwFvLEd+RHwBQVoM23A==&#038;s=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://tv.unsw.edu.au/p/v.?v=wJicwFvLEd+RHwBQVoM23A==&#038;s=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="450" height="300"></embed></object>
</div>

<p>You can also watch <a href="http://tv.unsw.edu.au/video/monkey-spoon" title="Monkey Spoon">Monkey Spoon</a> on UNSWTV or on <a href="http://www.youtube.com/watch?v=7ytprHwzNJ0" title="Monkey Spoon">YouTube</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://robrohan.com/2010/05/28/monkey-spoon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>@objc.signature(&#8216;v@:@@o^@&#8217;)</title>
		<link>http://robrohan.com/2009/09/14/objc-signaturevo/</link>
		<comments>http://robrohan.com/2009/09/14/objc-signaturevo/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 04:59:12 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://robrohan.com/?p=1156</guid>
		<description><![CDATA[Another quick post to hopefully save some one some time.

If you&#8217;re using python with Cocoa, you might run into problem with method signatures.   Because python is type-less, sometimes you have to explicitly set the method signatures for your object&#8217;s methods.   If you are getting wacky errors for seemingly no reason this [...]]]></description>
			<content:encoded><![CDATA[<p>Another quick post to hopefully save some one some time.</p>

<p>If you&#8217;re using python with Cocoa, you might run into problem with method signatures.   Because python is type-less, sometimes you have to explicitly set the method signatures for your object&#8217;s methods.   If you are getting wacky errors for seemingly no reason this might be why.  The errors this type of problem generate look something like:</p>

<pre>
does not implement protocol NSDraggingDestination: the signature for method performDragOperation: is c12@0:4@8 instead of B12@0:4@8
</pre>

<p><span id="more-1156"></span></p>

<p>You can fix this by explicitly setting the method&#8217;s signature using the @objc.signature decorator.  For example:</p>

<pre>
import objc

...

@objc.signature('B12@0:4@8')
def prepareForDragOperation_(self, sender):
        #print "prepareForDragOperation_"
        return True
</pre>

<p>You probably have used a decorators when creating methods for @objc.IBAction.</p>

<p>But the question for me became, how the heck do you define the signature?  That is some alien looking code.  Well, those things are called &#8220;Type Encodings&#8221; (so you have a search term), and you can read about them in the Apple documentation.</p>

<p><a href="http://developer.apple.com/iPhone/library/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html">Here</a> is the current link, but for some crazy reason Apple seems to be moving around their documentation and breaking links all over the place.  Plus the back button doesn&#8217;t work on their new documentation format &#8211; I am not amused.  You should also note that the documentation is for the iPhone which I am assuming is the same for the desktop (you can obviously only use python on the desktop).</p>

<p>Here is a copy of the table that is on that page</p>

<table>
<tr>
<th>Code</th>
<th>Meaning</th>
</tr>
<tr>
<td>c</td>
<td>A char</td>
</tr>
<tr>
<td>i</td>
<td>An int</td>
</tr>
<tr>
<td>s</td>
<td>A short</td>
</tr>
<tr>
<td>l</td>
<td>A long l is treated as a 32-bit quantity on 64-bit programs.</td>
</tr>
<tr>
<td>q</td>
<td>A long long</td>
</tr>
<tr>
<td>C</td>
<td>An unsigned char</td>
</tr>
<tr>
<td>I</td>
<td>An unsigned int</td>
</tr>
<tr>
<td>S</td>
<td>An unsigned short</td>
</tr>
<tr>
<td>L</td>
<td>An unsigned long</td>
</tr>
<tr>
<td>Q</td>
<td>An unsigned long long</td>
</tr>
<tr>
<td>f</td>
<td>A float</td>
</tr>
<tr>
<td>d</td>
<td>A double</td>
</tr>
<tr>
<td>B</td>
<td>A C++ bool or a C99 _Bool</td>
</tr>
<tr>
<td>v</td>
<td>A void</td>
</tr>
<tr>
<td>*</td>
<td>A character string (char *)</td>
</tr>
<tr>
<td>@</td>
<td>An object (whether statically typed or typed id)</td>
</tr>
<tr>
<td>#</td>
<td>A class object (Class)</td>
</tr>
<tr>
<td>:</td>
<td>A method selector (SEL)</td>
</tr>
<tr>
<td>[array type]</td>
<td>An array</td>
</tr>
<tr>
<td>{name=type&#8230;}</td>
<td>A structure</td>
</tr>
<tr>
<td>(name=type&#8230;)</td>
<td>A union</td>
</tr>
<tr>
<td>bnum</td>
<td>A bit field of num bits</td>
</tr>
<tr>
<td>^type</td>
<td>A pointer to type</td>
</tr>
<tr>
<td>?</td>
<td>An unknown type (among other things, this code is used for function pointers)</td>
</tr>
</table>

<p>It goes on to define &#8220;Objective-C method encodings&#8221;</p>

<table>
<tr>
<th>Code</th>
<th>Meaning</th>
</tr>
<tr>
<td>r</td>
<td>const</td>
</tr>
<tr>
<td>n</td>
<td>in</td>
</tr>
<tr>
<td>N</td>
<td>inout</td>
</tr>
<tr>
<td>o</td>
<td>out</td>
</tr>
<tr>
<td>O</td>
<td>bycopy</td>
</tr>
<tr>
<td>R</td>
<td>byref</td>
</tr>
<tr>
<td>V</td>
<td>oneway</td>
</tr>
</table>

<p>Even with that information though, it is a bit complicated to understand or write these signatures by hand.  For example, the post&#8217;s title: &#8216;v@:@@o^@&#8217;  appears to be a method that returns void (or an object?) and takes three parameters.  First a normal object, the second an out object and the third is a pointer to an object.</p>

<p>Kind of an interesting gotcha for a type-less high level language.</p>
]]></content:encoded>
			<wfw:commentRss>http://robrohan.com/2009/09/14/objc-signaturevo/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>TypeError: Unsupported format string type</title>
		<link>http://robrohan.com/2009/09/02/typeerror-unsupported-format-string-type/</link>
		<comments>http://robrohan.com/2009/09/02/typeerror-unsupported-format-string-type/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 00:47:23 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[FarCry]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Web Apps]]></category>

		<guid isPermaLink="false">http://robrohan.com/?p=1142</guid>
		<description><![CDATA[Quick post so Google might return something helpful for others hitting this error (Since I couldn&#8217;t find a post on the matter).

I hit this error using python on Mac OS X Leopard while writing a Cocoa based application and using minidom.  I was doing a simple httplib.HTTPConnection request to a server and getting back [...]]]></description>
			<content:encoded><![CDATA[<p>Quick post so Google might return something helpful for others hitting this error (Since I couldn&#8217;t find a post on the matter).</p>

<p>I hit this error using python on Mac OS X Leopard while writing a Cocoa based application and using minidom.  I was doing a simple httplib.HTTPConnection request to a server and getting back XML in the body of the document.  I was pulling the XML out using the response.read() method.</p>

<p>The server was setting the mime type and encoding correctly, and a type() call against the result showed it as a valid python string (&lt;type &#8217;str&#8217;&gt;) and it was well formed XML. However, for some reason whenever I would feed it to minidom.parseString() Cocoa (or the PyObjC bridge or whatever) would die with the error &#8220;TypeError: Unsupported format string type&#8221;</p>

<p><span id="more-1142"></span></p>

<p>I tried making the string an NSString thinking it was some Mac OS X implementation problem (various versions of NSString.alloc().initWithString_(str_response) setting encodings, copies of objects, etc), but it always caused the same error.</p>

<p>In the end, I simply gave up on the minidom library and used ElementTree instead (from xml.etree import ElementTree).  ElementTree.fromstring(str_response) worked out of the box.</p>

<p>I hope this saves someone some time. It had me stumped for several hours since there is no other information beyond &#8220;Unsupported format string type&#8221; with the stack trace of:</p>

<pre>
File "/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjCTools/AppHelper.py", line 235, in runEventLoop
</pre>

<p>I am still loving python though :)</p>

<p><b>Update</b></p>

<p>After working with ElementTree a bit more, I&#8217;ve discovered that the real culprit to this difficult-to-figure-out error message was likely due to the try/except statement around the method call.  For example it was doing this:</p>

<pre>
try:
    crazy.thing()
except Exception, e:
    rsp = NSRunAlertPanel(
        u"Bad Things", 
        e, 
        u"Let Me Try Again", 
        u"Quit", 
        None)
</pre>

<p>Any exception in the crazy.thing method will be masked by the &#8220;TypeError: Unsupported format string type&#8221; error when it tries to display e.  To fix it I simply casted e to a string:</p>

<pre>
try:
    crazy.thing()
except Exception, e:
    rsp = NSRunAlertPanel(
        u"Bad Things", 
        str(e), 
        u"Let Me Try Again", 
        u"Quit", 
        None)
</pre>

<p>So the real cause of my minidom problem is unknown since I&#8217;ve moved on to ElementTree.  I am a bit disappointed in the error reporting with python Cocoa, but other than a few elusive and confusing errors it is sweet, and fun development platform.</p>
]]></content:encoded>
			<wfw:commentRss>http://robrohan.com/2009/09/02/typeerror-unsupported-format-string-type/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
