<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title type="text">g.raphaelli's weblog</title>
  <id>http://g.raphaelli.com/2009/04/feed.atom</id>
  <updated>2009-04-08T03:37:34Z</updated>
  <link href="http://g.raphaelli.com/" />
  <link href="http://g.raphaelli.com/2009/04/feed.atom" rel="self" />
  <generator uri="http://zine.pocoo.org/" version="0.1.2">Zine</generator>
  <entry xml:base="http://g.raphaelli.com/2009/04/feed.atom">
    <title type="text">libyahoo2 python bindings</title>
    <id>tag:g.raphaelli.com,2009-04-02:/entry;2009/4/2/liyahoo2-python-bindings</id>
    <updated>2009-04-08T03:37:34Z</updated>
    <published>2009-04-02T23:23:00Z</published>
    <link href="http://g.raphaelli.com/2009/4/2/libyahoo2-python-bindings" />
    <author>
      <name>g</name>
    </author>
    <content type="html">&lt;p&gt;I have wanted Python bindings for libyahoo2 for a long time and finally sat down with Ned Batchedler's Pycon '09 slides, &lt;a href="http://nedbatchelder.com/text/whirlext.html"&gt;A Whirlwind Excursion through Python C Extensions&lt;/a&gt;, and wrote some basic ones available at:
&lt;/p&gt;&lt;div style="text-align: center;"&gt;
&lt;a href="http://bitbucket.org/graphaelli/ymsgrclient"&gt;http://bitbucket.org/graphaelli/ymsgrclient&lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;
Thanks Ned for the great presentation.  He's right that it's just an introduction as things like &lt;a href="http://docs.python.org/extending/extending.html#calling-python-functions-from-c"&gt;calling python from C&lt;/a&gt; couldn't even be covered.  I would recommend that aspiring module writers review Ned's presentation, move on to &lt;a href="http://docs.python.org/extending/index.html"&gt;Extending and Embedding the Python Interpreter&lt;/a&gt;, and then bookmark the crap out of the &lt;a href="http://docs.python.org/c-api/index.html"&gt;Python/C API Reference Manual&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;
Back to the bindings - a simple bot that will parrot your words back to you looks like this:

&lt;/p&gt;&lt;div class="syntax"&gt;&lt;pre&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;YmsgrClient&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ParrotBot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;YmsgrClient&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;YmsgrClient&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;got_im&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;who&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send_im&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;who&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ParrotBot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;YmsgrClient&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;STATUS_AVAILABLE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There are a number of rough edges and simply bad ideas in the code but it is sufficient for writing a single account client.  We've been using this for our bot at work for a few days without problems [or segfaults].  What we have had is an increase in bot functionality, as the barrier to writing few lines of Python and restarting the bot is much lower than writing a bunch of C and recompiling repeatedly until it works [or doesn't, giving up, and rolling it all back].&lt;/p&gt;

&lt;p&gt;Some things I'd like to do immediately:
&lt;/p&gt;&lt;ul&gt;
&lt;li&gt;Convert the data structures and functions lifted directly from the &lt;a href="http://libyahoo2.svn.sourceforge.net/viewvc/libyahoo2/trunk/libyahoo2/src/sample_client.c?revision=308&amp;amp;view=markup"&gt;libyahoo2 sample client&lt;/a&gt; to Python objects and expose them&lt;/li&gt;
&lt;li&gt;Redo the login poll loop or at least allow callbacks to be scheduled somehow&lt;/li&gt;
&lt;li&gt;Add Documentation&lt;/li&gt;
&lt;li&gt;Throw better exceptions and use the logging module properly&lt;/li&gt;
&lt;/ul&gt;

Some things that someone should do eventually:
&lt;ul&gt;
&lt;li&gt;Add Webcam support and sample usage documentation&lt;/li&gt;
&lt;li&gt;Test functionality on Windows&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Contributions are welcome.  Constructive criticism is greatly appreciated.&lt;/p&gt;</content>
  </entry>
</feed>

