tag:blogger.com,1999:blog-45534708939972892482024-03-06T05:47:46.762+00:00A13X networks.. thirteen letters of X ..Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.comBlogger31125tag:blogger.com,1999:blog-4553470893997289248.post-60011302110241389762011-08-30T13:34:00.000+00:002012-06-08T08:24:49.563+00:00Testing authenticated handlers in Tornado<div dir="ltr" style="text-align: left;" trbidi="on">
<a href="http://www.tornadoweb.org/">Tornado </a>is an excellent little async HTTP framework and I have been using it in many projects I've been part of. Documentation is a bit lacking but access to <a href="https://github.com/facebook/tornado">sources</a> makes all the difference.<br />
<br />
But say you are implementing handlers that require authentication. You are versed in TDD and you want your handlers tested as well. Here's a little trick that I use to <i>"mock"</i> the authenticated user.<br />
<br />
When writing tests, I use excellent <a href="http://code.google.com/p/pymox/">pymox</a> library and the following example uses that, but you can use any mocking library you prefer.<br />
<br />
Here is the Handler which we want to test:<br />
<pre class="brush: python" name="code"># encoding: utf-8
__author__ = 'alex'
import tornado.web
class Protected(tornado.web.RequestHandler):
def get_current_user(self):
# get an user from somewhere
return self.retrieve_user_from_db()
@tornado.web.authenticated
def get(self):
self.render("protected-page.html")
</pre>
We want to test the <i>get()</i> method, but since it's decorated with <i>authenticated</i> decorator, testing the method will result in response code 403. To overcome that (and make our test green!), we'll mock <i>get_current_user()</i> in our Test - it's that easy:
<br />
<pre class="brush: python" name="code"># encoding: utf-8
__author__ = 'alex'
import os, os.path, sys
import tornado.web
import tornado.testing
import mox
import project.handlers
class TestAuthenticatedHandlers(tornado.testing.AsyncHTTPTestCase):
def get_app(self):
app = tornado.web.Application([(r'/protected', project.handlers.Protected)])
self.mox.StubOutWithMock(tornado.web.RequestHandler, 'get_current_user', use_mock_anything=True)
tornado.web.RequestHandler.get_current_user().AndReturn("authenticated_user")
return app
def setUp(self):
self.mox = mox.Mox()
super(TestAuthenticatedHandlers, self).setUp()
def tearDown(self):
self.mox.UnsetStubs()
self.mox.ResetAll()
def test_new_admin(self):
self.mox.ReplayAll()
resp = self.fetch('/protected')
self.assertEqual(resp.code, 200)
self.mox.VerifyAll()
</pre>
I stubbed out the <i>get_current_code()</i> method on the <b>RequestHandler</b>, but if you have implemented a <b>BaseHandler</b> and extend your handlers from that, you should mock its method (it's closer to your code).<br />
<br />
I mock objects extensively in my tests - it is a great practise and helps you focus better on the actual code you're testing. Hopefully this little trick will help you as well.
</div>Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com2tag:blogger.com,1999:blog-4553470893997289248.post-50740542553811501062010-10-08T14:19:00.001+00:002010-10-08T14:19:46.684+00:00Pink Mayor<div style="text-align: left; padding: 3px;"><a href="http://www.flickr.com/photos/a13xnet/5062528884/" title="photo sharing"><img src="http://farm5.static.flickr.com/4112/5062528884_ca67a5e054.jpg" style="border: solid 2px #000000;" alt="" /></a><br /><span style="font-size: 0.8em; margin-top: 0px;"><a href="http://www.flickr.com/photos/a13xnet/5062528884/">Pink Jon Gnarr</a>, originally uploaded by <a href="http://www.flickr.com/people/a13xnet/">a13xnet</a>.</span></div><p>I guess you can call it luck. I was passing the Mayor's office in Reykjavík today with my camera in hand, when the Mayor of Reykjavík, popular comedian Jon Gnarr appeared. As he moved quickly towards the awaiting car, I grabbed my camera and took couple of photos.<br /><br />Since it's Breast Cancer Awareness Day, or Pink Friday (as it is known here), it's only appropriate that he's is dressed in pink, as well.</p>Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com2tag:blogger.com,1999:blog-4553470893997289248.post-60689644567639900412010-09-08T11:20:00.002+00:002010-09-08T11:23:51.905+00:00Showing your work<div style="text-align: left; padding: 3px;"><a href="http://www.flickr.com/photos/a13xnet/4970198025/" title="photo sharing"><img src="http://farm5.static.flickr.com/4144/4970198025_85848482bd.jpg" style="border: solid 2px #000000;" alt="" /></a><br /><span style=" margin-top: 0px;font-size:0.8em;"><a href="http://www.flickr.com/photos/a13xnet/4970198025/">Many dots</a>, originally uploaded by <a href="http://www.flickr.com/people/a13xnet/">a13xnet</a>.</span></div><p>Learning curve of every aspiring photographer has to, at some point, reach a part where the photographs are shown to (very) critical eyes of others. Beginner photographers mostly dread to the thought of being criticized while still learning the trade. That's happening not just with photography or art, but with every skill that involves producing something.<br /><br />I went through the same dreadful process while learning icelandic language; fear of speaking, fear of saying something wrong and weer into the uknown parts of the grammar. Let me tell you, it's not pretty. Contrary to my belief, doing exactly what I feared of doing helped me improve, both my skill and my knowledge; I spoke icelandic even if it was terrible and completely gramatically wrong and it payed off.<br /><br />I am sure that there are many talented photographers and artists out there that still keep their work to themselves and that is a shame. No matter how good or bad you are, you will never know that for sure unless someone else sees your work. It is simply not possible to improve if you don't know for sure what to improve and where.<br /><br />Today I joined a huge community of photographers and artists on flickr.com - it is time for me to show my work. I wish to improve my skill, wish to learn new techniques and fine-tune ones I already am familiar with. Being in contact with others that share the same passion as me is incredibly valuable and I don't want to miss on that.<br /><br />So, expect to see more of my work over there and I hope I'll get comments and guidelines where to improve.</p>Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0tag:blogger.com,1999:blog-4553470893997289248.post-89135185457320178302010-08-17T15:08:00.000+00:002010-08-17T15:08:52.941+00:00Point of View matters<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">Photography is a big interest of mine and I have been enjoying taking photos for quite some time. As an aspiring hobbyist photographer, I always try to find ways to improve my skill and better define my photography style. I try not to worry too much about the equipment, but rather how to use it; although, I must admit I have just recently succumbed to the equipment bug and replaced my Olympus E-510 with a Nikon D90. So far, I'm quite happy with that decision. But I digress.<br />
<br />
I went out in the garden with my son to play the other day, and took my camera with me, just in case. It was a sunny morning with not a cloud in the sky. As I was coming out I noticed a little patch in the grass. The sun was shining on it and it produced vibrant greens as the light went through the plant's leaves. It felt like a nice photo opportunity. I could have just taken the photo at that moment from my standing point of view (and I actually did, to help me illustrate the point of this post), but that particular point of view did not offer anything of value as a photograph.<br />
<br />
</div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhwQosxWnNkXHC84hXK5QuNXApbBCQheNxEpe-JbdHe6eAkVzIyLNAy28KakEiRhpU0f3csLINDZNCev49NyaC1cfQQXVlb6KfD723dSTPwCzmWYU7yfXQsy7K4qbZfr31RwCrjAx1DMU/s1600/normal+POV.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="265" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhwQosxWnNkXHC84hXK5QuNXApbBCQheNxEpe-JbdHe6eAkVzIyLNAy28KakEiRhpU0f3csLINDZNCev49NyaC1cfQQXVlb6KfD723dSTPwCzmWYU7yfXQsy7K4qbZfr31RwCrjAx1DMU/s400/normal+POV.jpg" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Normal POV</td></tr>
</tbody></table>With that in mind, I started thinking about what can be done so the photograph tells the story better. I wanted to focus the viewer on the semi-transparency of the leaves and the beautiful greens so a change of background was definitely required. I decided to simply change the point of view and bring the camera to the same level as the leaves. The result is the image below.<br />
<table cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWASBmetxeVYgTnqMs61MFMimqLi43ciItXCRalN3mtmEwMFl1pnscwzM5fith49ZT9hkWOTAMm9cwNSPCqBK90UvjOBXfKz8xbPgbbTLKoikL3NShvsBRPn4OYOvlTUcxi4mkXzo8UQA/s1600/leveled+POV.jpg" imageanchor="1" style="clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;"><img border="0" height="265" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWASBmetxeVYgTnqMs61MFMimqLi43ciItXCRalN3mtmEwMFl1pnscwzM5fith49ZT9hkWOTAMm9cwNSPCqBK90UvjOBXfKz8xbPgbbTLKoikL3NShvsBRPn4OYOvlTUcxi4mkXzo8UQA/s400/leveled+POV.jpg" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Leveled POV</td></tr>
</tbody></table>This photograph is much different and it reveals well what I tried to accomplish. While it's not a winner, it's way better than the photo above. These photos are not processed in any way and this one could use a better crop and a bit of dodging.<br />
<br />
Every photographer will tell you that it's not just the subject that matters in a photo; the subject can tell all sorts of different stories depending on the point of view. When taking photos, try to explore the subjects as much as you can - sometimes you can yield many excellent photos from a single subject. Not only it's a great learning expirience, it's also a good exercise, especially if you're shooting with a prime (single focal length) lens.<br />
<br />
Go out, take some photos and share your expirience.<br />
<div class="separator" style="clear: both; text-align: center;"></div>Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0Kópavogur, Iceland64.10844722520126 -21.891717910766664.106104725201263 -21.899013410766603 64.110789725201258 -21.8844224107666tag:blogger.com,1999:blog-4553470893997289248.post-40554415437185857142010-03-02T20:55:00.007+00:002010-03-02T21:46:23.942+00:00Simple URL shortener in less than 30 lines of python codeI needed an URL shortener for a project that I'm working on that keeps URLs in their short form for a limited time. No need to go into the details about the project as this URL shortener is a helper hack so we don't need to do last-minute rewrite of the app itself.<br /><br />As an exercise, I decided to write a fully functional RESTful (*<span style="font-weight:bold;">buzz*, *buzz*</span>) app with a persistent store for the URLs. I chose <a href="http://beaker.groovie.org/">Beaker</a> cache for the storage and <a href="http://bobo.digicool.com/">bobo</a> as the boilerplate base for the app. Thanks to those two components, the actual script is less than 30 lines of python code.<br /><br />To save the URL and get it's short ID, you POST the URL to the app. The script is caching the URL for an hour, but it's using persistent storage in case the app crashes (it restores the cache contents). I used my <span style="font-style:italic;">short_id()</span> method I wrote about a while ago to generate the ID for the stored URL. If you supply the ID to the app it will redirect you to the stored URL.<br /><br />You can check the app the git repo <a href="http://github.com/a13x/various.py/blob/master/shorty.py">here</a>. Feel free to comment and feel free to use it, of course!<br /><br />To run the script, use: <span style="font-style:italic;">bobo -f shorty.py</span> (<span style="font-style:italic;">bobo</span> is the command to run an embedded bobo web server).<br />To save an URL, simply POST to it:<br /><span style="font-style:italic;">$ curl -d "url=http://www.a13x.info/" http://localhost:8080/</span><br />(you'll get something like "ax7dc" returned)<br />To fetch the URL do: <span style="font-style:italic;">http://localhost:8080/ax7dc</span>. The app will redirect you to the saved URL or throw a 404 if it expired (or it didn't exist).<br /><br />Simple!Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com1tag:blogger.com,1999:blog-4553470893997289248.post-79837145605659994932010-01-12T22:19:00.005+00:002010-01-12T22:42:56.167+00:00Install CouchDB with Homebrew with existing ErlangIf you're using excellent <a href="http://wiki.github.com/mxcl/homebrew">Homebrew</a> to install fantastic unix CLI utilities on your OS X, then you might find this handy. I wanted to install <a href="http://couchdb.apache.org/">CouchDB</a> using Homebrew, but I quickly realized it depends on Erlang being built with Homebrew as well.. As I have built my own Erlang (and don't need another one) for my machine, I decided to look into the formula for CouchDB to see if I can tweak it to use Erlang that's already on my machine.<br /><br />It turned out that the fix is very easy to apply. Only thing you need to do is to open the couchdb.rb file (in /usr/local/Library/Formula) and do the following:<br /><ul><li>remove the depends_on 'erlang' line</li><br /><li>edit the call to the configure script so the line --with-erlang points to your erlang installation (mine is in /usr/local/lib/erlang).</li></ul><br />Here's the patch:<br /><pre name="code" class="brush: diff"><br />10d9<br />< depends_on 'erlang'<br />16c15<br />< "--with-erlang=#{HOMEBREW_PREFIX}/lib/erlang/usr/include"<br />---<br />> "--with-erlang=/usr/local/lib/erlang/usr/include/"<br /></pre><br /><br />Now do <span style="font-style:italic;">brew install couchdb</span> and make something wonderful. Don't forget to let me know about it!Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0tag:blogger.com,1999:blog-4553470893997289248.post-24440364974473943522009-12-11T15:22:00.003+00:002009-12-12T13:43:04.144+00:00Release early, release often!Remember that mantra, known in the Open Source world? Well, it's a core part of Agile Methodologies for software development, for precisely the same reason: getting the <span style="font-style:italic;">feel</span> on how your project/product/app is behaving in the real environment and getting feedback from your customer. Feedback is especially important, as the requirements for the project might change (and they usually do!) - you need to be aware of that!<br /><br />But requirements are not the only ones that change; so does the environment in which your product is supposed to be running and used. While you might have total control over the development and test environments, you usually don't have little or no control over the live (production) environment. That environment is handled by some dedicated person or people.<br /><br />Keep in mind that those people have their own schedule (backlog if you wish) by which they operate the environment under their control. Systems are upgraded or reinstalled, new versions of services get installed and so forth. Usually, you, the developer, are the last one to find out about that. You find out about those changes when your app is being deployed and (oddly!) doesn't work.<br /><br />We all tried to talk to those people, hoping they will keep us in sync with the changes they make, but frankly (and I'm talking from personal expirience) that rarely goes as well as you think it would. So, make sure your product owners understand this as well, and push for releases as often as possible. That way, not only you'll be getting (great!) feedback from your owners, but will be able to keep tabs on your environment as well.<br /><br />This post reflects thoughts, problems (and frustration) over the last week, while trying to get our (quite complex) project into production. While we were really trying to be agile and follow all (or most) of the principles during development, we still managed to fail to deliver our project often to the production environment.<br /><br />What product owners sometimes fail to understand is that you must follow all of the principles, and follow them well. The developers often fail to help them understand. We must all learn from that, I say.Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0tag:blogger.com,1999:blog-4553470893997289248.post-32636998829393959982009-11-25T16:08:00.004+00:002009-11-26T16:14:49.249+00:00Andri Luka 2 years old && Erlang/OTP R13B03 releasedThis day is very special and remarkable - our son, Andri Luka was born on this day 2 years ago! Happy 2nd Birthday, dear son!<br /><br />Also, on the other side, Erlang/OTP R13B03 is released. With this release comes the GIT (mirror) repository of Erlang/OTP source code (available <a href="http://github.com/erlang/otp">here</a>) to enable people to send in contributions. That is awesome! The release also marks the introduction of <a href="http://www.erlang.org/doc/man/erl_nif.html">NIFs</a> (Native Implemented Functions), but that's still in experimental stage.Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0tag:blogger.com,1999:blog-4553470893997289248.post-45007723266471303242009-11-07T17:01:00.005+00:002009-11-07T20:52:15.082+00:00Icelandic Gaming IndustryJust came back from the first <a href="http://www.igi.is">IGI</a> workshop, first workshop for the <a href="http://www.igi.is/group/igia10">IGIA10</a>, the Icelandic Gaming Industry Awards competition.. It was an excellent starting point for icelandic indie game developers to meet and network. I came a bit late so I missed few talks, but the rest was really good. The talks were held by representatives from companies that are already established game developers (ie. <a href="http://ccpgames.com">CCP</a>, <a href="http://gogogic.is/">Gogogic</a>, <a href="http://dexoris.com">Dexoris</a>, etc). It was nice of them to share their expirience in their road to success.<br /><br />I asked couple of questions, mainly focusing on getting more information on how to get connected to artists and/or designers that want to bring their talent into the gaming industry. Lots of indie developers (such as yours truly) have zero-to-none artistic (design) skills. In my opinion, IGI initiative should also focus on that talent, as well as developers.<br /><br />As we all know, eye-candy and content is a huge help to make it in this business. I am close to a point where I would definitely need an artist to join me, otherwise it might be very difficult to get players to play the game. Not to mention possible funding :)<br /><br />Next to come, a bit of introduction to the game itself.<br /><br />p.s. I encourage you, dear reader, to check the <a href="http://www.igi.is">Icelandic Gaming Industry</a> website and join me and others in very interesting endeavour of bringing more games in the future..Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0tag:blogger.com,1999:blog-4553470893997289248.post-53004264606087597532009-07-11T14:55:00.003+00:002010-01-12T22:46:05.545+00:00Check if an application is runningI'm working on a utility application for Mac OS X, which will display the currently played song in iTunes on the OS X status bar. While working on it, I've made a small utility method to check if a program (identified by BundleID) is running or not.<br /><br />Here it is:<br /><pre name="code" class="brush: c"><br />-(BOOL)isRunning:(NSString *)programBundleId {<br /> return [[[NSWorkspace sharedWorkspace]<br /> valueForKeyPath:@"launchedApplications.NSApplicationBundleIdentifier"]<br /> containsObject:programBundleId] ? YES : NO;<br />}<br /></pre><br /><br />The method returns YES if an application is running - in my case, it was useful to check if iTunes is running or not.Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0tag:blogger.com,1999:blog-4553470893997289248.post-3563514276212373102009-06-29T15:32:00.004+00:002010-01-12T22:38:05.943+00:00TinyURLs reloaded, now with Python3Few days ago, very smart and great people behind <a href="http://python.org">Python</a> project released stable version of Python 3.1; that brings bunch of improvements (including performance ones) and new features. You can check what's new in this release <a href="http://docs.python.org/dev/py3k/whatsnew/3.1.html">right here</a>, if you wish.<br /><br />I was quite impressed by the speed improvements over the 3.0.1 version of Python. It's quite fast and comparable to Python 2.6 version (which is pretty damn fast). Running few of my sources with the new version (to test speed and comformance) I noticed that my TinyURL example doesn't work anymore.<br /><br />The <span style="font-weight:bold;">string.letters</span> is gone, and print is (as mentioned before) no longer a statement; it's a function. The updated function now looks like this:<br /><pre name="code" class="brush: python"><br />import random<br />import string<br /><br />def short_id(num): return "".join(random.sample(string.digits + string.ascii_letters, num))<br /><br />print(short_id(6))<br /></pre><br /><br />The only noticable change in the function itself is use of <span style="font-weight:bold;">string.ascii_letters</span>. There you have it - now Py3 compliant! :)Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0tag:blogger.com,1999:blog-4553470893997289248.post-39084285739900662172009-01-30T16:27:00.007+00:002010-01-12T22:37:54.744+00:00Tiny URLsWant tinyurl.com-style links in your application? Need to generate a random sequence of alphanumerics? Here's a quick function in Python:<br /><pre name="code" class="brush: python"><br />import random<br />import string<br /><br />def short_id(num): return "".join(random.sample(string.digits + string.letters, num))<br /><br />print short_id(6)<br /></pre><br />It will return something like <span style="font-weight:bold;">AnLrJl</span> or <span style="font-weight:bold;">9mLXut</span>, or even <span style="font-weight:bold;">bK5D4O</span>. Simple and powerful.Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0tag:blogger.com,1999:blog-4553470893997289248.post-30446956910936939942009-01-07T09:08:00.003+00:002009-01-07T09:24:52.879+00:00Христос се роди! Срећан Божић!To all the people that celebrate Christmas today, we wish you a Merry Christmas and that you and your family are healthy, happy and wealthy!Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0tag:blogger.com,1999:blog-4553470893997289248.post-67424700736678508172008-12-24T13:27:00.002+00:002008-12-24T13:28:42.270+00:00Merry Christmas and Happy Holidays!To all the people that celebrate Christmas, to all my friends and their families, we wish a very Merry Christmas!<br /><br />Happy Holidays!Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0tag:blogger.com,1999:blog-4553470893997289248.post-71075624574437193382008-12-22T09:06:00.005+00:002010-01-12T22:37:11.859+00:00Python 3.0 and print functionI have been following the python mailing list for quite some time now. I recommend it for any pythonista who's not afraid of a bit of spam and flame war here and there.<br /><br />There has been a lot of discussion and arguments about a change in Python 3.0 in which <span style="font-weight:bold;">print</span> is a built-in function now instead of a language-specific statement. The arguments started flowing in to the mailing list just minutes after Python 3.0 final was officially released.<br /><br />Most complaints seem to revolve around the fact that *now* we have to write extra two parenthesis, ie. <span style="font-style:italic;">print("something")</span> instead of <span style="font-style:italic;">print "something"</span>. Let us ignore that complaint for now, as it is minor (not to mention pointless) issue, something to get used to and easily solved with IDEs. Besides, we all <span style="font-weight:bold;">*know*</span> how to use functions, right? This is a non-issue.<br /><br />Another (much larger) complaint is about string formatting (<a href="http://www.python.org/dev/peps/pep-3101/">PEP-3101 - Advanced String Formatting</a>), which is a very big improvement over the formatting using "%". New built-in function, <span style="font-style:italic;"><a href="http://docs.python.org/3.0/library/functions.html#format">format()</a></span>, has been introduced along with (so-called) "<a href="http://docs.python.org/3.0/library/string.html#formatspec">Format Specification mini-language</a>" to specify and control formatting. Quite powerful, if you ask me.<br /><br />Particularly interesting formatting option is formatting of positive and negative numbers (a <span style="font-style:italic;">sign</span> option). Consider implementing this in the old syntax:<br /><pre name="code" class="brush: python"><br />print(format(-123456,'#> 20'))<br />print(format(123456, '#> 20'))<br />print(format(123456, '#>+20'))<br /></pre><br /><br />Furthermore, you can implement <span style="font-style:italic;">__format__()</span> method in your classes, which will be called by the <span style="font-style:italic;">format()</span> built-in function for producing a formatted version of the object. However, implementing formatting specification is up to you - you can use the standard formatting syntax or implement your own.<br /><br />Bottom line is, Python 3.0 brought a lot of changes and improvements; those changes have been discussed and polished over a period of several years prior to releasing the final version. What I don't understand is those people who are complaining about it now, instead of doing it before and contributing to those discussions. It will get some time to get used to the new changes, sure; but don't dismiss them straight away - give it a chance and try it out. You will soon realize that those changes are in fact good ones.Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0tag:blogger.com,1999:blog-4553470893997289248.post-89593216342078595232008-12-05T15:31:00.002+00:002008-12-05T15:34:57.283+00:00Python 3.0 releasedInterestingly, my previous post was about Python 2.6... This one is about <a href="http://www.python.org/download/releases/3.0/">3.0</a>, which finally saw the light of day in final edition two days ago! I just noticed it now (obvious proof I'm spending too much time on meetings here at work :).<br /><br />Sadly, no official package for OS X as of yet, but I'm sure it will arrive after the weekend. I'm itching to take it for a spin on my code.. There will be some incompatibility issues, but 2.6 version helps a lot to sort that out.<br /><br />Have a nice weekend, folks!Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0tag:blogger.com,1999:blog-4553470893997289248.post-82509885602782040492008-10-02T13:00:00.004+00:002008-10-08T11:13:01.310+00:00Python 2.6 finalIt's here, the final version of Python 2.6 has been <a href="http://www.python.org/download/releases/2.6/">released</a>. This is the last 2.x release of Python before the almighty <a href="http://docs.python.org/dev/3.0">Py3K</a>. You can read <a href="http://docs.python.org/whatsnew/2.6.html">what is new</a> in Python 2.6 and of course, grab your own <a href="http://www.python.org/download/releases/2.6/">copy</a>.<br /><br />I'm fetching the installation now, eager to see if the installation will work with <a href="http://pyobjc.sourceforge.net/">PyObjC</a> on my Mac. Next thing on the list is to install Stackless.Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0tag:blogger.com,1999:blog-4553470893997289248.post-33590474764879344202008-10-01T20:56:00.003+00:002008-10-01T21:04:07.063+00:00Apple dropping NDA for iPhone DevelopersI visited <a href="http://www.apple.com">Apple</a> <a href="http://developer.apple.com/">Developer Connection</a> site earlier, looking for some iPhone related resources. There was a message posted there, for all iPhone developers: <span style="font-style:italic;">Apple has decided to <span style="font-weight:bold;">drop</span> the non-disclosure agreement for released iPhone software</span>!<br /><br />In a released <a href="http://developer.apple.com/iphone/program/">message</a>, Apple stated that they have introduced the NDA to protect many of its inventions and innovations that have been put into the iPhone OS. They (finally) recognize that this has put a huge burden (and lots of problems) on the developers so they have decided to drop the whole thing.<br /><br />However, this agreement is dropped only for developers who have released their software on the Apple Store, the NDA still covers the unreleased software until its release.<br /><br />This will make a lot of developers much happier. Very good move by Apple!Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0tag:blogger.com,1999:blog-4553470893997289248.post-12767098394561684282008-09-17T21:22:00.034+00:002008-09-26T21:16:34.167+00:00World is concurrentLast few years or so, in my free time, I have been coding in <a href="http://stackless.com">Stackless Python</a>. I'm using Python almost exclusively for my projects, so it comes natural to use Stackless for concurrency. Concurrent programming is very interesting and challenging, and Stackless Python makes it (very) bearable and easy.<br /><br />Stackless Python introduces the concept of microthreads, where tasklets wrap functions allowing them to be launched as microthreads. Scheduling is built in and it can be either cooperative or preemptive. Finally, there are channels which can be used for communication between tasklets. Channels block tasklets, either receiving ones or ones sending, depending on if there is a waiting receiver (or sender, respectively). Another interesting thing is that tasklets can be serialized (pickled) to a disk (or any other storage media) and deserialized later on to be resumed.<br /><br />Using this functionality provided by Stackless (through a single module) is very easy and intuitive. It keeps the Python code very readable and understandable and it even improves the structure of the program. Common usage patterns are available from the authors of Stackless to help people new to concurrent programming understand principles of how Stackless is used. It allows creating custom tasklet functionality (ie. named tasklets), as well as custom channel functionality (ie. broadcast channels, sending of messages with a timeout, etc).<br /><br />As I mentioned, scheduling is built in and it is up to a programmer to choose the type: preemptive or cooperative. With cooperative scheduling one has to be careful to write code so that tasklets run cooperatively. During the design and implementation of the tasklets, programmer should pay attention to run the scheduler manually if the operation within the tasklet might make other tasklets suffer for not being able to run. On the other hand, with preemptive scheduling, the scheduler itself is configured to interrupt and reschedule running tasklets. I found the cooperative scheduling more useful in my implementations since it gives more control. You can, however, using preemptive scheduling kindly ask the scheduler not to put your running code back to the scheduling queue. One useful idiom is the <span style="font-style:italic;">Sleeping Tasklets</span>, which blocks the tasklets for a certain period of time. Interestingly, that idiom uses channels to accomplish this.<br /><br />Documentation of Stackless is available on <a href="http://stackless.com">the website</a> and it covers basic functionality. Also, it offers examples and common patterns (idioms). The module itself is very well documented and available in the python interactive shell by typing <span style="font-style:italic;">help(stackless)</span>. The community is active on the <a href="http://www.stackless.com/mailman/listinfo/stackless">mailing list</a>, where help is always available. Every now and then there's a good discussion about the advanced usages of Stackless, and I highly recommend subscribing to the list.<br /><br />Issues exist, however. Current CPython implementation suffers from the infamous <a href="http://docs.python.org/api/threads.html">GIL</a> (or, <span style="font-style:italic;">Global Interpreter Lock</span>) which makes it difficult for Python to fully utilize multicore systems (almost every recent computer nowadays). For those who don't know the effects of GIL, it is a mechanism to keep multiple threads from modifying the same object at the same time. Only one thread that acquires a lock on the object may modify that object, and the interpreter controls the acquiring and releasing of the lock, especially around potentially slow operations (such as I/O).<br /><br />There has been quite a lot of talk about the GIL and whether it should (or not) be removed from Python. Back in '99, few brave souls (Greg Stein and Mark Hammond) tried and removed GIL from the Python code using locks on all mutable structures. According to benchmarks, it actually slowed the execution of code. The results showed that the execution was twice as slow in single threaded environment than with the GIL. If ran on multi-CPU (multi-core) systems, there would be no actual performance gains by removing the GIL.<br /><br />To truly make the code distributed between CPUs, the solution is to run several python processes and communicate tasklets between them, however that can get (very) complicated, to say the least. Luckily, Python 2.6 (next major release of Python, with final release just around the corner) comes with <a href="http://docs.python.org/dev/library/multiprocessing.html#module-multiprocessing">multiprocessing</a> module. This module supports spawning processes in a similar fashion <span style="font-style:italic;">threading</span> module is used. More so, since the module follows the same API as threading module, it makes refactoring of your projects which use threading a breeze. <span style="font-weight:bold;">Process</span> objects can communicate between each other using <span style="font-weight:bold;">Queues</span> or <span style="font-weight:bold;">Pipes</span>, the latter being bidirectional (both parties represent ends of the pipe, with <span style="font-style:italic;">send()</span> and <span style="font-style:italic;">recv()</span> methods available for sending and receiving).<br /><br />Using <span style="font-style:italic;">multiprocessing</span> module, you can even distribute your processes remotely, and on top of that, it is possible to create a pool of workers for running tasks. Finally, since the module uses subprocesses instead of threads, the effects of aforementioned <span style="font-weight:bold;">GIL</span> can be circumvented.<br /><br />Stackless by design (because of the scheduler) does not allow tasklets to access (and modify) data structures at the same time. Moreover, in true concurrency fashion, it utilizes channels for passing the data between tasklets. It still has a notion of a shared state between tasklets, but of course, without the dangers. With multiprocessing on Python 2.6, Stackless Python programs will be much more scalable than they are currently, utilizing multi-cpu and multi-core environments more efficiently. That still, however remains to be seen, as porting of Stackless to Python 2.6 is a work in progress.<br /><br /><span style="font-weight:bold;">Enter Erlang!</span> For those of you who don't know what it is, Erlang is a functional programming language (an entirely different approach to programming compared to, ie OOP) designed in <a href="http://www.ericsson.com/">Ericsson</a> for the purpose of developing concurrent, distributed, fault-tolerant and (soft) real-time applications.<br /><br />Being functional, it does not have a concept of a state, it has single variable assignments (just as they taught you in Math classes), dynamic typing, pattern matching of functions (with guards), etc. On top of that, it has extremely lightweight processes with no shared memory. Processes pass messages around to communicate between themselves. Erlang runtime supports very high number of concurrent processes and they are actually abstracted from the operating system. It also supports dynamic distribution of processes, both locally (over multiple CPUs or cores) or remotely (over the network to another Erlang runtime node). Thus, you can build large scale distributed applications that run on machines with many CPUs and on many machines in the network. Since there's no shared state or shared variables, all traditional problems related to concurrency simply disappear as the need for locks is removed.<br /><br />Erlang does give a lot of headache to a lot of people, because of its syntax. Comparing the syntax with Python's, I can say (being biased and all) that I definitely prefer Python's. On the other hand, I quite like the syntax of Erlang, too. It looked quite bizarre at first, but going through the documentation and examples helped me understand the basics of it. I have just got delivered the <a href="http://www.pragprog.com/titles/jaerlang">Erlang book</a> I purchased, and am very excited to learn more about Erlang. As I have always been more interested in building backend applications, using Erlang seems like a good choice worth learning more about. <br /><br />As a follow up, next article on this topic will be sprinkled with some code examples, both in Stackless Python and Erlang. I really like learning about new programming languages and frameworks by actually implementing something useful using them, so I will try to do so with Erlang. Some ideas are popping into mind and in next articles I might elaborate more on that, too.Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com1tag:blogger.com,1999:blog-4553470893997289248.post-53627564816884609982008-08-27T10:27:00.004+00:002008-08-27T11:05:01.026+00:00Django on JythonFew days ago, <a href="http://blog.leosoto.com/">Leo Soto</a> announced on jython mailing list that <a href="http://www.djangoproject.com/">Django</a> now runs unmodified on latest <a href="http://jython.org">Jython</a> code. This is great news for both Jython and Django projects.<br /><br />Although not my first framework of choice for developing web applications using Python, Django has been adopted by many developers out there, even few ones defecting from Ruby on Rails. Django follows the MVC design pattern, it has url patterns similar to Routes in RoR, etc. One slight problem for developers might be the fact that Django has it's own database API - although I believe another ORM such as SqlAlchemy can be used instead.<br /><br />I have been following the development of Jython very closely, mainly because I'm planning to introduce it in my company. Our software policy states that the main development language is Java and I'm trying to push the initiative that enables us to use any language as long as it runs in JVM. Nowadays there are several very good (and strong) candidates to replace Java and Jython stands IMHO as one of the strongest. It takes the strengths of Python language and brings them into JVM.<br /><br />We adopted the <a href="http://www.springframework.org">Spring Framework</a> in our development to help us minimize the Time to Deliver and maximize the ROI. For developing web applications we're using Spring MVC in both portlet and servlet environments. It improved both the quality of our deliveries and shortened the time to deliver. However, there is still the huge overhead of the code compilation in our day-to-day development. Something like Jython (and Django, possibly) can eliminate that problem partly (or entirely).<br /><br />Django developers are going for a 1.0 release early next week. With that in mind, the fact that there's an excellent <a href="http://www.djangobook.com/">Django Book</a> online and that it runs in Jython are more than enough reasons to get to know Django better and (even) start using it in Enterprise environment. Why not?Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com2tag:blogger.com,1999:blog-4553470893997289248.post-29270934351953127322008-07-31T15:14:00.004+00:002008-07-31T15:28:22.652+00:00Python, the evolutionI just stumbled upon a project called <a href="http://vis.cs.ucdavis.edu/~ogawa/codeswarm/">code_swarm</a>, which is an experiment in <span style="font-style:italic;">organic software visualisation</span>. The project takes information from a source control system and creates a visual representation of the history of code commits. The end result looks quite awesome. Below is the <span style="font-style:italic;">code_swarm</span> video of Python source code and its evolution.<br /><br /><object width="400" height="302"> <param name="allowfullscreen" value="true" /> <param name="allowscriptaccess" value="always" /> <param name="movie" value="http://www.vimeo.com/moogaloop.swf?clip_id=1093745&server=www.vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=ff9933&fullscreen=1" /> <embed src="http://www.vimeo.com/moogaloop.swf?clip_id=1093745&server=www.vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=ff9933&fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="302"></embed></object><br /><br />The project's source code is available <a href="http://code.google.com/p/codeswarm/">here</a>, free of charge, of course. It is created by <a href="http://vis.cs.ucdavis.edu/~ogawa/">Michael Ogawa</a>, using <a href="http://processing.org/">Processing</a> environment. Great stuff!Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0tag:blogger.com,1999:blog-4553470893997289248.post-87994074679085707012008-07-11T14:00:00.005+00:002008-07-11T14:07:27.026+00:00Protocol Buffers<a href="http://www.google.com/">Google</a> decided to open their tool for serializing structured data, called <a href="http://code.google.com/apis/protocolbuffers/">Protocol Buffers</a>. It's language and platform neutral way of communicating data over networks or serializing it for storage. Interesting bit is that Google is using it in almost all of their projects.<br /><br />On the surface, tt reminds me a lot of <a href="http://en.wikipedia.org/wiki/CORBA">CORBA</a>, especially the way you define message structures, but it differs from it a lot in terms of message exchange and serialization - you can store and/or communicate your data structure across the network by any means, unlike CORBA where you're forced to use CORBa message brokers. In my opinion, that's the main reason why CORBA was never so widely accepted.<br /><br /><span style="font-weight:bold;">How does it all work?</span> First of all, you define your structure using a DSL and store it in a .proto file. You then compile that file using a tool and create data access classes for your language of choice. Classes can be generated for C++, Java and Python - my choice would be, as always, Python. Those generated classes are then used to create, populate, serialize and retrieve your protocol buffers messages.<br /><br />The messages are very flexible - you can add new fields to your messages without breaking old code that's using them; they will simply ignore it. That functionality comes in very handy, especially for larger systems (think versioning and deployment).<br /><br /><span style="font-weight:bold;">Yes, but what about XML</span>, you might ask? According to Google, <span style="font-weight:bold;">PB</span>s have many advantages over XML; they are:<br /><ul><li>simpler<br /></li><li>3 to 10 times smaller<br /></li><li>20 to 100 times faster<br /></li><li>less ambiguous<br /></li><li>generate data access classes that are easier to use programatically<br /></li></ul><br />I might disagree with the last one (think of JAXB in Java world), but I completely agree with the other ones - it's quite true that XML tends to be cumbersome and a big overkill, especially in environments where size and speed does matter.<br /><br />For the end, I saved a very interesting quote from Google's PB pages:<br /><blockquote>Protocol buffers are now Google's lingua franca for data - at time of writing, there are 48,162 different message types defined in the Google code tree across 12,183 .proto files. They're used both in RPC systems and for persistent storage of data in a variety of storage systems.</blockquote><br />Looks very interesting and very promising, considering Google is behind it. I'll follow up this article with some neat examples.Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0tag:blogger.com,1999:blog-4553470893997289248.post-8149678692785559072008-07-01T10:24:00.003+00:002008-07-01T15:57:33.182+00:00Back at work after a long vacationI was away on vacation for 7 weeks. Now I'm back at work. Tough, I must say. My mailbox is a total mess, loads of new mail, meeting invitations, updates and changes, etc. Then an idea hit me! Our corporate email suite comes with <span style="font-style:italic;">Out of Office</span> assistant, which helps you (and your coworkers) to manage the email while you're away on vacation (sending out auto-responders and such). It also has a very nice feature of reporting who emailed you and all that. I am still going through the emails I received, totally stressed out not to miss something important. I'm skimming through as fast as I can and it seems there's no end to it. I'm quite sure I missed something very important, although I'm trying my best to mark messages that I need to check again later. That means, of course, that I'll be reading my emails twice.<br /><br />Now, <span style="font-weight:bold;">the idea</span> - wouldn't it be great to have a feature which would, upon opening your email suite after a long vacation, populate your inbox little-by-little. Let's say you have 120 messages which have been sent to you while you were on vacation. The suite would sent you a dozen messages every 10 minutes, so you can slowly read it and catch up. Preferably over a good cup of coffee. No stress, no worries, you can read your mail at a normal rate as you would if you didn't go on vacation. I'm sure it would take some people few days to catch up, but it sure beats reading 500+ emails in one hour.<br /><br />Back to reading emails.Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0tag:blogger.com,1999:blog-4553470893997289248.post-48278868356797178492008-04-22T22:41:00.008+00:002008-04-22T23:35:01.931+00:00Core Spring Course<p>I'm attending a <a href="http://www.springsource.com/web/guest/training/spr001">Core Spring Training Course</a> this week, held and organized here in Iceland by <a href="http://www.siminn.is/">my company</a>. I have finally managed to persuade my superiors to invest into Spring training for all the employees of our department. The course is held by <a href="http://blog.springsource.com/main/author/arjenp/">Arjen Poutsma</a>, the developer behind <a href="http://static.springframework.org/spring-ws/site/">Spring Web Services</a> project.<br /><br />While I have been developing applications with Spring Framework for past few years, I am finding the Core Spring training course very informative and helpful. Most of the material covered so far is familiar and well-known to me, apart from the more advanced usage of Spring AOP, which I haven't used much so far. That is definitely going to change, I am sure. Every topic is followed by a very well designed lab where the newly learned material can be exercised.<br /><br />Things to cover in the next two days are Spring MVC and Web Flow, configuring Spring Security, Remoting with Spring Web Services, etc. I sure hope we'll be covering (soon to be released) Web Flow 2.0 - I promised to write more about it when we start adopting it in projects, but we have not reached that point yet (one of the reasons is the fact that Web Flow haven't reached final 2.0 version).<br /><br />While I prefer figuring how things work by experimenting on my own, it will be great to get information about Web Flow first-hand through the presentations and lab work on the course. Which brings me to the point -the real advantage and real value of this course is the chance to talk in person to people behind the Spring Framework, hear their opinion on things and get some good tips and advices (especially advices on AOP advices). Labs are in fact designed with that in mind.<br /><br />I will write more about Spring AOP and Web Flow in the following days, so stick around.</p>Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com2tag:blogger.com,1999:blog-4553470893997289248.post-41416332725254271282008-02-22T15:34:00.005+00:002008-02-22T15:56:05.088+00:00Spring MVC easy wayWriting web applications using <a href="http://www.springframework.org/">Spring</a> <a href="http://static.springframework.org/spring/docs/2.5.x/reference/mvc.html">MVC</a> has never been easier, now that we have annotated controllers in Spring 2.5. Basically, it all boils down to just couple of things, from defining the <span class="Apple-style-span" style="font-style: italic;">viewResolver</span> bean and the required XML configuration to scan for annotated based controllers to adding an annotation to (any) method in your controller class, just like this:<div><span class="Apple-style-span" style=" ;font-family:'courier new';">@RequestMapping(value="/url")</span><br /></div><div><span class="Apple-style-span" style="font-family:'courier new';">public ModelMap method()</span></div><div><span class="Apple-style-span" style="font-family: 'courier new';"><br /></span></div><div><div><div>Of course, there are few required beans you need to define for all that magic to work (shown in the Spring Framework documentation online) but I find that very acceptable and a big plus (showing how Spring is <span class="Apple-style-span" style="font-weight: bold;">very</span> flexible).</div><div><br /></div><div>Some people might argue that the (now) old-fashioned way of defining the links the controllers respond to via <span class="Apple-style-span" style="font-style: italic;">urlMapping</span> bean (<span class="Apple-style-span" style="font-style: italic;">SimpleUrlHandlerMapping</span>) is similar to routes in Rails where everything (in regards to URL requests) is in one place. Then again, how many times are you going to change the URL locations and where does it look to be more natural - especially when you basically do all your work within the controller.</div><div><br /></div><div>Similarly, this is how <a href="http://www.turbogears.org/">TurboGears</a> (a web framework for my favorite language, python) handles request mappings (with a twist), where the name of the method in the controller declares the request mapping. That can be easily achieved with Spring MVC annotated controllers, too.</div><div><br /></div><div>All this ease-of-use for writing web applications plus all the benefits of the Spring Framework - there's no need to switch to another framework, developers used to work with Spring can leverage all their knowledge and still be in the front lines, especially in regards to developing web applications.</div><div><br /></div></div></div>Aleksandar Radulovichttp://www.blogger.com/profile/01596589689497657130noreply@blogger.com0