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

Thursday, June 25, 2009

Move from Nokia to IPhone with style, thanks to Google Mobile Sync

I'm continuing to enjoy my newly unlocked IPhone 3G and tonight I started to snap out of the fascination and set out to sync all my contacts and calendar entries from my (now old and obsolete and dull) Nokia N76 to my IPhone, I had a very simple strategy in mind, sync the Nokia with PCSuite on Vista, and Sync that back to IPhone using ITunes. Unfortunately it wasn't that simple ...

On Vista PCSuite sync's your contacts with Windows Contacts which is stupid and dull and so 1990's, but the real issue for me is that it drops information while syncing, specifically if a contact has more than one mobile number it just syncs the default one, and it does the same with home numbers, a limitation that you simply can't live with because nowadays everyone has a mobile number for business hours, mobile number for evenings, mobile number for weekends, you name it ... and if you care to stay connected you got to keep all those

I thought that this can be a good time to explore the world of OTA (Over The Air) Syncing of mobile data, and I was reasoning that if I managed to do that from my Nokia, there is a big chance that I can Sync back to my IPhone, a simple query on Google lead me to one of their new "Beta" services surprisingly enough named "Google Sync" :)

I started at this page, Google Sync fo Nokia S60 and panicked at the fact that if I would setup Google Sync, I will not be able to sync anymore with PCSuite afterwards without losing data or at least messing it up ... but I can do without that at the moment, after all it hasn't done me much good anyways. To make sure that I'm not loosing any information from my contact list (which is over 800 entries by the way and that makes it worth the effort) I started by doing a one time OTA Nokia-Compatible-Only Backup on OVI free OTA service just to be safe

Here are the steps to setup OTA sync for Contacts, Calendar, Tasks and (Soon) Mail on Google Sync for your S60



Get Google Sync on your phonenokia_smart_large



  1. Backup your data

  2. Download mail software

  3. Set up Google Sync



First few times I tried to Sync with Google from my S60 I got errors, obvious ones like "Server Error, please try again later", and another more subtle one like "your account doesn't have permission to sync with current settings contact your administrator" ... guess that referred to the server side account, I looked around on my Google Account for something that can be related to Sync permissions but didn't find any. After a short while and some perseverance in re-trying as advised by the first error ... it succeeded. I located some forums and blog entries talking about the Google Sync Exchange server not being stable at the moment

Now for the Easier part Syncing with IPhone, that is an easy one, and doesn't require any software installation or re-tries, here are the steps I followed:

Get Google Sync on your phoneapple_large



  1. Backup your data

  2. Set up your iPhone

  3. Learn about options and features


While setting this up, I chose to delete all contacts on the IPhone when asked, usually you are asked when you turn ON Contacts Sync, and the same applies for the Calendar, after finishing the configuration just open your IPhone Contacts app and wait for few seconds, Sync will start automatically provided that you enabled "Push" for the Exchange Account you created.

Now I have my personal Contacts and my personal Calendar Sync'd with my personal Gmail account, life should be pink enough, but not for me ... My expectations are now raised and I wanted to do the same with my Business email account, hosted also on Gmail Business mail, but life can't be perfect ...

There is a limitation that you can only setup ONE and only ONE Microsoft Exchange account on your IPhone, so here goes my dream, I reduced my expectations and wanted to overwrite the Exchange account settings for my business email and choose to Sync my business Calendar only, such that I have my personal Contacts (Sync'd once in the begining and left in peace) and my business Calendar (actively syncing) on my IPhone, so I tried to switch off the Contacts Syncing now in order not to delete or mess up my happily stashed 800 contacts, but if you do so IPhone tells you that if you turned it off all contacts on your IPhone would be deleted!! didn't find a way to work around this one yet ... again looks like life indeed can't be perfect

Still at least I have all my Contacts, and active Syncing is really cool, add, edit or delete either on the mobile or on your email account and the changes are reflected to the other side, another neat service from logo_sm

Tuesday, June 23, 2009

IPhone 3G firmware 2.2.1, BB 2.30.03, BL 5.9 UNLOCKED !

Finally and after a lot of waiting and frustration I unlocked my two IPhone 3G with the cursed 2.2.1 operating system, baseband version 2.30.03 that were not downgradable to 2.28 because both had Boot Loader 5.9 ... yes, I have 2 and got them separately from eBay and both were stuck and have been used as thick IPods for the past 2 months

I tried to get an Ultra SIM, I purchased one from a reseller in Mansoura, Egypt, here is their website http://www.gsmegypt.com/VB/index.php, they started by stating that it doesn't work for Mobinil Egypt SIM cards, not sure why ... then they got it to work once on a Vodafone Egypt line of their own, when I tried my own Vodafone SIM, it didn't work, taking the iPhone back to them they tried to re-program the Ultra SIM to work with my SIM card, but failed big time and they refused to refund the money, that attempt costed 70 EGP and a lot of time from my "Mansourian" colleagues, thanks to Ahmed Abdalla and Ahmed Soliman for the help in that. Bottom line is that I won't recommend that Ultra SIM to anyone, because it is not stable and it can register to your network for a while and later you would lose that and get back to the locked state.

Finally the Dev-Team guys released ultrasn0w, the unlock for IPhone 3G firmware 3.0, the previously blogged here that they are skipping the Bootloader 5.9 problem and had an unlock ready for firmware 3.0 but according to the cat and mouse game they won't release it until Apple has done its official release of the new IPhone 3G S fearing that if they would release it earlier Apple would notice the crash that allowed them to inject the unlock and fix it and they would have lost an important edge

so, here it is, and without further ado, the steps that I followed and redeemed my IPhone, quoted from the Dev-Team official blog with a couple of links added




Ultrasn0w for iPhone 3G is ready!

  1. Ensure you have upgraded to iPhone OS 3.0 (download the image here), press SHIFT while clicking on the restore button in iTunes then browse for the image file

  2. Jailbreak your iPhone 3G using redsn0w or PwnageTool (this will also install Cydia/Icy)

  3. Run Cydia or Icy

  4. Please add the repo repo666.ultrasn0w.com to Cydia or Icy. That last “o” is actually the number zero “0”! If you use the letter “o” you’ll get an error.

  5. Search for ‘ultrasn0w’ in cydia or icy and install ultrasn0w

  6. Reboot your iPhone 3G

  7. T-Mobile USA users should disable 3G before using ultrasn0w

  8. Enjoy



And I'm enjoying indeed


Big thanks to Ahmed Soliman for his support in this one as well, Soli, you are the God Father of my iPhone ;)

Sunday, June 14, 2009

Getting You Started with Python

I was introduced to the world of Python 18 months ago, and since then I love the language and always consider it my first choice and my fluent tongue, specially for fast prototyping or small to medium scale web development

A problem that many software engineers confront when they first come to python is that they are very occupied with (and religious to) their original tongue and they are trying to speak python with a Java accent or adding a C/C++ dialect to the language ... simply don't try to do that, you would only feel the beauty of a language and benefit from its strength when you surrender to its unique nature.

Long time ago when I was still struggling with my English, a teacher gave me a very useful piece of advice that helped me a lot since then, he said "if you wish to be a good English speaker, you got to THINK in English" ... he put a lot of emphasis on how harmful it is to your learning of a new language to think in your mother tongue and then translate the meaning to the new language, you would learn very slowly and your English will be also polluted with odd words and language constructs that are not applicable in such context.

I have a lot to say in this subject, but I thought it is better to share here some basic resources, blog entries, articles and videos that I used to learn to THINK in Python and allow you to be shaped by that literature first hand ...

Python is not Java (A must read if you have a Java background)

http://dirtsimple.org/2004/12/python-is-not-java.html
http://www.ferg.org/projects/python_java_side-by-side.html


Basics


Python home:
http://python.org/

Official Tutorial

http://docs.python.org/tutorial/

Are you using Eclipse, here is the Eclipse plugin for python development:
http://pydev.sourceforge.net/

Be Pythonic
http://wordaligned.org/articles/essential-python-reading-list
http://eikke.com/how-not-to-write-python-code/

Noteworthy Persons


Guido Van Rossom

Alex Martelli

Python on Youtube

a very soft introduction to python, by Alex Martelli, Author of the Oreily book "Python in a nutshell":
http://www.youtube.com/watch?v=bDgD9whDfEY (part 1)
http://www.youtube.com/watch?v=y7vwZ20SDzc (part 2)

python, a dynamic language for all kind of applications (Guido van Rossum):
http://www.youtube.com/watch?v=ImfbpMjpKe8

More advanced, Design Patterns in python (may be you would come to those later):
http://www.youtube.com/watch?v=1Sbzmz1Nxvo (part 1)
http://www.youtube.com/watch?v=tJXhtncDBu4 (part 2)

More and more advanced, in depth talks:
http://www.youtube.com/watch?v=VOzvpHoYQoo (python object model)
http://www.youtube.com/watch?v=23s9Wc3aWGY (python internals)

Guido talking about the new python, 3000:
http://www.youtube.com/watch?v=66NThkgfc1U

Python & Machine Learning

Peter Norvig talks to facebook engineers about Google translate and machine learning in general, and uses python code to demo his ideas

Have a good code