Sunday, January 30, 2011

The Skype Problem

Skype doesn't play well by default with web developers. By default it binds to port 80 (standard HTTP port). It uses this port to listen in incoming communications from other skype users. In addition, Skype sends outgoing UDP packets from these ports.

To disable this you should open skype :

  • Go to File -> Options -> Advanced -> Connection
  • Uncheck "Use port 80 as an alternative for incoming connections" checkbox
  • Stop or restart Skype.

You should be good to go. Your apache/nginx web servers can now bind to port 80 without any issues.

For more information see here.

Saturday, January 29, 2011

Testing whether an object is iterable or not in Python

Following is a simple snippet for checking whether an object is iterable or not in Python:

  1: def can_loop_over(maybe):
  2:     """Test value to see if it is list like"""
  3:     try:
  4:         iter(maybe)
  5:     except:
  6:         return 0
  7:     else:
  8:         return 1

Courtesy: satchmo_utils []

Friday, January 28, 2011

Allocation of an array of objects lacking default constructors in C++

All of us know that in C++ when we wish to create an array of objects of some class T then T must have a default constructor. But what if, the default constructor is not available in class T? Is there any way you could create an array of objects which have parametric constructors?

Lets consider an example:

  1: class Account
  2: {
  3: private:
  4:  double m_security;
  5:  double m_principal;
  6:  float m_rate;
  8: public:
  9:  Account(double security);
 10:  double calculateInterest(int time, float rate);
 11:  void deposit(double amount);
 12:  void withdraw(double amount);
 13:  double checkBalance();
 14: };
Can you write :
Account typeAaccount[100];

Or something like this:
Account typeBaccount[50](500);

Well second option as we all know is outrageously wrong! Nevertheless first option is wrong either...
Had there be no constructor at all, your faithful compiler would have provided a default constructor and would have made the array allocation possible...But now it will shout with a compile-time error. Well you can go all the way round, add a static variable, set its value before each array allocation and use it in your parameter-less default constructor. That is to say:
  1: class Account
  2: {
  3: private:
  4:  double m_security;
  5:  double m_principal;
  6:  static int security;
  7: public:
  8:  Account();
  9:  double calculateInterest(int time, float rate);
 10:  void deposit(double amount);
 11:  void withdraw(double amount);
 12:  double checkBalance();
 13: };
 14: double Account::security = 0.0;
 15: Account::Account()
 16: {
 17:  m_security = security;
 18:  m_principal = security;
 19: }
Now you can create the array, supplying the argument through static variable:
Account::security = 1000.0;
Account typeAaccount[100];
Account::security = 500.0;
Account typeBaccount[100];

But that said, there can be a solution as elegant as this for dynamically allocated object arrays:
  1: char* storage = new char[100 * sizeof(Account)];
  2: Account* pvui = (Account*)storage;
  4: for(int i=0; i<100;++i)
  5: {
  6:  new(pvui + i)Account(500);
  7: }
  9: // use the array of Account objects
 11: // call destructors now 
 12: for(int i=0; i<100;++i)
 13: { 
 14:  (pvui + i)->~Account(); 
 15: }
 17: // finally delete the allocated buffer. 
 18: delete[] storage; 

@Shailesh: I must confess, your challenge "What's so special in this piece of C++ code" held me in awe for an hour... :)
Well if anybody like me is surprised to see the special "new", I must dive in details. That's placement new operator and takes a pointer to an already allocated memory chunk. So you can allocate a raw memory and then construct you objects over it calling the constructor in a loop. Since the objects are adjacent to each other you can use them wherever you would have used them as an array.
And yes destructors can be called explicitly. Do take care and call them to clean up the mess you might have created and finally deallocate the raw memory buffer.

As easy as this!

Monday, January 24, 2011


Blogofile interesting tool: Blogofile is a static website compiler

Sunday, January 23, 2011

Django DRY

If you read any blog or book on Django, you will notice that they keep repeating "Don't Repeat Yourself" in all the discussions. Many a times its clear that DRY is being done but there is no need to keep chanting 'DRY' again n again in the discussions. It has sort of become a rhetoric I guess in Django world.