Monday, February 27, 2012

Funny Quote of the Day

- Thomas A. Edison - "To invent, you need a good imagination and a pile of junk."

Thursday, February 16, 2012

Wisdom of the day

"Logic is the art of going wrong with confidence." -Joseph Wood Krutch

Sunday, May 9, 2010

How to know what version of ubuntu you are running

To Know what is the version of ubuntu you are running just run the following command:

sudo cat /etc/lsb-release

your output should be somethinglike that:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=$version
DISTRIB_CODENAME=$codename
DISTRIB_DESCRIPTION=$description


thats it, peace of cake ha...

Wednesday, March 31, 2010

How to disable the your linux firewall(iptables)

Lately i have a new fedora box at work and i was working in a demo project so that i wanted to run a local mercurial server so that my colleagues and myself can use it to work on our demo project.

I tried the hg serve command and it worked but only locally on my machine and it didnt work with any of my local network machines, so i found out that my firewall(iptables) is on and a colleague helped me to turn if off, here is the How ?

sudo service iptables stop
sudo iptables -F
sudo service iptables save


thats it, hope it helps

Friday, February 5, 2010

The difference between staticmethod and classmethod in python

well, since i am a person that depend heavily on the vast opensource community as a hough resource of knowledge. I like to be able to contribute with the little knowledge that i gain.
So today i will explain the difference between staticmethod and classmethod in python, lets start..

You may have read a couple of python books/tutorials and i think that none of them mentions the difference between the staticemethod and classmethod decorator, even some tutorials says that there is no difference at all !!!

Why would i need to use static/class methods?

static/class methods are methods that binded to the class they created in not the object of that class, so you dont need to make an instance object of the class to call these methods, and it is used when your method doesnot depend on any object specific attributes.

Example of static method:

class FactoryClass(object):
@staticmethod
def createObject(objectType):
do some stuff


the code just define a class called FactoryClass and define a static method in this class called createOject, so how would it be to do the same using class method

Example of class method:

class FactoryClasss(object):
@classmethod
def createObject(cls, objectType):
do some stuff

so not so much of a difference ha, the only thing that is different is that the class method is passed the class object by default as its first paramter, so what i can do wiht the class object paramters ?
lets say that you not only want to create object of a certain type but also you want to make sure that at any point of time there is only one instance object of each type.
Ok easy, the first thing you would think of is to create an attribute(map) that hold each type as key and the value is the object and in the createObject method you will check if the type already exits in the map, then you will not create any new instance but will return the already created one, lets do that

Using class method:

class ClassFactory(object):
objectMap = dict()
classMap = {'type1': Class1, 'type2': Class2}
@classmethod
def createObject(cls, objectType):
if objectType in cls.objectMap:
return cls.objectMap.get(objectType)
else:
object = cls.classMap.get(objectType)
cls.objectType[objectType] = object
return object

pretty neat ha, but it does not mean you cannot do that using a static method, the thing is that it will not be as clean as the class method one, lets have a look

Using static method:

class ClassFactory(object):
objectMap = dict()
classMap = {'type1': Class1, 'type2': Class2}
@staticmethod
def createObject(objectType):
if objectType in ClassFactory.objectMap:
return ClassFactory.objectMap.get(objectType)
else:
object = ClassFactory.classMap.get(objectType)
ClassFactory.objectType[objectType] = object
return object

hope it helps...


Wednesday, January 13, 2010

[CES 2010] RCA Airnergy Charger Harvests Electricity From WiFi Signals


well this is very cool stuff...

the idea here is that the device is kind of circuitry that harvests the WiFi energy out of the air and convert it to electricity.
The device(Airnergy) has a small battery in it, so you can just carry it around and as long as there is a WiFi near you, it charges itself.
The great news is the device will be out in the market soon for 40$ !!!

for more info check that blog

Wednesday, January 6, 2010

Google wants to administer the first white spaces database

http://www.infoq.com/news/2010/01/Google-White-Spaces-Database

pretty cool..

Saturday, August 15, 2009

How to decode a URL using python

I was doing a task and i needed a function to decoded a url as a dictionary of parameters and its values,

since i used urllib.urlencode before i was sure that i will find urllib.decode method as well, but i didn't.

Here is the result of my search, to decode a url into dictionary of parameter and its values user the cgi module as the follwoing:



import cgi

import urllib

url = 'http://www.test.com/test?%s'

params = {'param1': 'value1', 'param2': 'value2'}

urlParams = urllib.urlencode(params)

url = url%urlParams

resultParams = dict(cgi.parse_qsl(url))

print result_params



Enjoy

Sunday, July 19, 2009

Notes on REST Architecture

REST stands for Representation State Transfer

the main characteristics of REST are:

  • Distributed Network of interlinked documents
  • Client-server architecture
  • Stateless Server
  • The concept of Resource and Resource Representation
the action of retrieving a resource must be Idempotent, the fact that you made the request should not change the contents of the resource.

From "Beginning Python 2005"

Wednesday, July 1, 2009

My trip to my hometown

I am traveling to my home city and while i am at the train i see the life of the way to the upper egypt and i wonder how ignorant i was, how much i dont know about my own country and the people who i share with them a religion , home and a future.

the thing that striks me is how different the life here from the crowded city of cairo, how peacefull the people are and how beautiful our upper egypt is.

Today i visited my grandparents grave and i got a feeling my words cannot help me explaining them, but one of the feelings is definitaly sadness, i was sad because the memories that suddenlly became like reality, i got the feeling that my grandfather have past away only yesterday and there is nothing compared to that feeling, those kind of feelings that makes even taking your bearth very defficult process.

and there were the part where i just sat there at this beautifull place outside the mosque, where the wheher cannot be better(the weather was one of the things that made me hisitate going on this trip).