Thursday, July 9, 2009

Pythonic way to do static local variables

If you are using C++ or Java, you man have used static local variables to define variables that would retain its value between function calls.

In python there is no obvious way to do it, and you need to exploit some hidden python characteristics, namely function/method attributes.

If you need that trick with a function (not bound to an instance) it is rather straight forward:

def foo():
# foo.x is like a C static
try: foo.x +=1
except AttributeError:
foo.x = 1
return foo.x
for i in xrange(10): print foo()

If foo() is an instance method the straight froward way would have been to use an instance variable of the container class to hold the value for you, but that would pollute the instance namespace with variables of special needs and never used outside the method scope.

You can still do something similar to the trick above but it becomes a little more cumbersome.

Firstly because you have to refer to the class name, e.g. Bar.foo.*

Secondly and (less obviously) because user defined attributes are not allowed in instance methods!

I don't like as much as you do but it is a fact of life, read more here;

thus you'd have to refer to the wrapped function referenced by the method instance attribute im_func:

class Bar(object):
def foo(self):
try: Bar.foo.im_func.x += 1
except AttributeError:
Bar.foo.im_func.x = 1
return Bar.foo.x
foo = Bar().foo
for i in xrange(10): print foo()

You may argue that it is not an intuitive syntax, may be you are right, I only did it for the kicks :)


Monday, July 6, 2009

Kindle for IPhone now in Egypt

I love reading, paper book reading, where you can carry your book around, toss it anywhere, read while walking, or even read in the bathroom ... Working in the Software business made EBook and PDF reading on my PC is a must but was never as enjoyable as the intimacy you experience while holding your book
That's why always admired Kindle, Amazon's 6" Wireless Reading Device. It is really amazing, design and technology, if you don't know what is Kindle take a look here
What is amazing about it is the natural book feel
ing and the revolutionary electronic paper display


It has been on my wish list since its release in early 2008, but I live in Egypt, and we learned a lesson from our great great ancestors, the Pharaohs that you can (and in our days will have to) take your wish lists to the grave!! Kindle is supported in the US and that is it, if you live in the God forsaken parts of the Universe (which is in short outside the US) you can't have it. Of course nothing is perfect and you can argue a lot with Amazon's decision regarding the restrictions and the business model they follow with their Kindle.

A couple of days ago I was thrilled to find the Kindle for IPhone app, I downloaded the application without second thoughts and chose a sample of a book I was interested in, reading on the IPhone using this app was a rich experience and I enjoyed my 1.5 chapters of the book, now I want that book and I'm willing to pay the 8.99$ to get it on my IPhone, of course you can purchase it on Amazon used for 2.99 and up, and get free shipping with Super Saver ... but, uh, yeah, I remembered ... I live in Egypt! so shipping that 2.99 book will cost in total over 12$ so wireless shipping seems appealing.
I setup a new Amazon account, with an Egyptian Shipping Address (in case of future paper pack books shipping), and used my online credit card, typed in my Egyptian Billing address, configured 1-Click Shopping in Amazon and I'm ready to pay! !ERROR! "We are sorry ... we could not process your order because of geographical restrictions on the product which you were attempting to purchase ... blah blah blah"


"Ok, it is another one of those stupid restrictions because of the Shipping or the Billing address, or may be my online credit card has Egyptian origins and that makes it not good enough for online shopping!" I thought ... tried changing both addresses with no luck, so now it is either the credit card or the IP address I'm using to access the divine Amazon website, that should be easy. I accessed amazon using a new account from a Terminal Service on a remote machine in Europe and the same error message is there, provocatively staring at me, challenging me, depriving me from a simple item on my wish list, and dooming me with the same Pharaohs destiny of taking it along to the grave ... I searched around until I found this great tip on this blog of a fellow kindloholic in South Africa. It gave me some hope in the form of creating an account with a US shipping and billing address, NO credit card info and using an Amazon Gift Card to charge your account with the required amount of money. Can be risky but worth a try, logically a new account using a gift card shouldn't be considered 3rd-world and be deprived the luxury of reading on the go.


I purchased a Gift Card, and to add humor to the pain I personalized it with:

From: myself
To: myself
Message: Enjoy Kindle or lose a hard earned 10$ :)

Just to be sure, I used a different account to purchase the Card than the one I'm linking to my IPhone, got the redeeming code, charged my account with the 10$, trembling I clicked the 1-Click shopping and, YES! it works, I'm officially Kindled ... I was amazingly grateful that Amazon toke my money, thank you Amazon, I'm speechless ... you f*@%#ers
Now I have a Kindle book, have my IPhone and have my account balance reduced by 10$ plus fees ... I should be reading happily ever after

Sunday, July 5, 2009

Why and How to import Wordpress blog into Google's Blogger?

Why migrate from Wordpress.com to Blogger?

Wordpress is an amazing blogging framework that enables you to do a lot ... speaking of wordpress.org as a framework that you would install and host somewhere and be free to manipulate it, install plugins and addons and map your domain to your hosted blog, wordpress.com is a yet another story ... here are the shortcomings:

No free domain mapping

I was shocked after setting up my blog to know that wordpress.com won't allow me to map my domain www.shadyyoussef.com unless I purchase wordpress credit points, read the details here

No Plugins

You can't install any plugins or widgets unless from the wordpress.com collection, which is fairly good, they even have a cool widget for Twitter, still you feel bounded. They make a clear distinction between the power and flexibility of the wordpress framework, that you get by privately hosting wordpress.org software and having a blog on wordpress.com, read more about that here

Can not monetize

Like any other blogger I hoped to get some minor cash from the adSense and the like plugins, usually the cash is trivial but it pays for your domain costs at least. Wordpress.com doesn't allow any kind of ads that are linked to your account. That is fine for me, to have an Ad-Free blog is cool, but there is no guarantee that wordpress won't swamp my blog with Ads for their own profit at any point of time, and what is in it for me?, read more here

wordpress-to-blogger-blog

How to import your existing blog entries into Blogger?

For the above reasons, and the belief that Blogger blogs would have better google ratings, because I'm sure that google crawlers are not 100% fair and square indexing external content and there must be some favouritism to google platforms :) ... (I know it sounds like conspiracy theory but I think it is true) I decided to migrate my blog from wordpress.com to Blogger ... only to realize that however wordpress.com guys are smart enough to allow auto import from Blogger to wordpress, the reverse is not supported by Blogger ... too bad Google, you should have known better ...

After few hours in search and weighing the options, being extremely reluctant to do that migration manually, I found more that one tool ... one Java Swing desktop tool that doesn't work, I landed on this online app that does the conversion from the xml files you export from wordpress.com to the format that blogger can import, that neat app can be found here ... my first attempt was a failure, it did convert the xml files to "something" but Blogger refused to import that, with an error, it could have been blogger's problem or the tool's problem, we can't be sure.

My second attempt was a success, I was thrilled to see all my blog posts and comments, even the draft entries listed there in the import wizard for blogger. I selected all non-draft and hit "Publish" ... but that failed again. Now I think Blogger was the one to blame in the initial failure

Here is what you can do about it, select and publish your entries one by one, works like a charm. The entry is even published with its original date, same applies for comments ... after a few minutes and 34 clicks I had my blog up in Blogger restored in the exact state ... Me Happy

From this point on I'll be mainly posting to my Blogger blog, mapped to my domain www.shadyyoussef.com ... I admit that blogger's styles and templates looks a bit 90's to me but don't forget that you have the chance to manually edit the style sheets and import your own templates, which is great

Thursday, July 2, 2009