h1

Bring Down IE6

March 28, 2009

I have copied the following post from this site.

Discover the mission

The premise is simple: Internet Explorer 6 is antiquated, doesn’t support key web standards, and should be phased out. This isn’t about being anti-Microsoft, it’s about Microsoft’s lack of development in the browser market. With IE7/8 not available for Windows 2000, IE6 accounts for up to 20% of web usage, primarily via business users. Clients pressure designers to ‘force’ sites to work in IE6, and designers, not wanting to lose business, comply, using hacks and workarounds. This wastes time and money. Microsoft needs to fix this, designers need to unite, and we all need to move on.

Read the article

In our article, ‘Calling time on IE6’, we ask designers and developers if it’s finally time to take IE6 behind the shed and shoot it. Major names in the web industry debate our mission, talking about the pros and cons behind dumping IE6. How feasible is it to shun the browser entirely? What can you do to minimise the impact IE6 has on you and your clients (and increasingly tight budgets)? And what can and should Microsoft do to help us all move the web and web standards on? Find out by reading the article, or just ‘skip to the end’ by looking to the right and getting involved.

Get involved

  • Put our logo on your site and then link to this site. Follow us on Twitter.
  • Educate your colleagues, users and clients regarding calling time on IE6.
  • Ensure sites work in IE6, but don’t waste a lot of time fixing non-critical issues.
  • Provide an upgrade notice for IE6 users by adding this code to your page:
    <style type=”text/css”>
    #ie6Warning {
    background:#E3E3E3 none repeat scroll 0 0;
    border:1px solid #BBBBBB;
    margin-bottom:10px;
    padding:10px 10px 1px;
    }
    #ie6Warning h2 {
    background:transparent url(http://www.bringdownie6.com/assets/ie6-warning.gif) no-repeat scroll 0 50%;
    padding-left:40px;
    }
    </style>
    <!–[if lt IE 7]>
    <div id=”ie6Warning”>
    <h2>Time to upgrade your browser</h2>
    <p>If you’re reading this, you’re surfing using Internet Explorer 6, an eight-year-old browser that cannot cope with the demands of the modern internet. For the best web experience, we strongly recommend upgrading to <a xhref=”http://www.getfirefox.com/”>Firefox</a>, <a xhref=”http://www.opera.com/”>Opera</a>, <a xhref=”http://www.apple.com/safari/”>Safari</a>, <a xhref=”http://www.google.com/chrome”>Google Chrome</a>, or a more recent version of <a xhref=”http://www.microsoft.com/windows/downloads/ie/getitnow.mspx”>Internet Explorer</a>.</p>
    </div>
    <![endif]–>
  • Spread the word via Digg, Facebook, Delicious, reddit and StumbleUpon
h1

I’m Learning Python part 9

March 20, 2009

I’m Learning Python part 9

Python Logo

Classes In Python

As I told you before, Python is a powerful programming language, and powerful it won’t be without Object Oriented design.
Python supports both Pure Object Oriented Programming (Every thing must be in a class) and Structural Programming (You can type code everywhere).
It supports also other programming paradigms like Functional Programming. I won’t write about OOP and its uses, why we should use it, I’ll just give you the keys to use OOP in Python.

Defining a Class

Classes in Python are defined like this:

class CLASS_NAME (BASE0,BASE1,BASE2):
	"""OPTIONAL DOC STRING"""
	#Class Code
	.
	.
	.
Yes, I know what you’re thinking of, Python supports multiple inheritance.
In Python 3.0 you must at least inherit the object class (or any other class that inherits object), this is called new-style-classes, and that’s because before Python 3.0 you could make a classic class that doesn’t inherit any class, not even object.
Because of Python’s dynamic data typing you don’t need interfaces in Python, so there is no implicit syntax for declaring interfaces.

Sample

Let’s create a sample class, I always implement the stack when I want to show OOP examples, I don’t know why but that’s what I’m gonna do now:
I always start implementing stack by implementing its unit type, a class called StackItem.
StackItem is a stack unit that has two properties, its value, and a reference to its next item in the stack, so it will be defined:
class StackItem(object):
	def __init__(self, value):
		self.Value = value
		self.Next = None

	@property
	def Value(self):
		return self.__value
	@Value.setter
	def Value(self, value):
		self.__value = value

	@property
	def Next(self):
		return self.__next
	@Next.setter
	def Next(self, value):
		self.__next = value

Let’s describe the whole scene of code:

  1. StackItem inherits object, it is a way of saying “Everything is an object” :)
  2. __init__ is the constructor of StackItem, it takes two arguments, the instance of StackItem to construct and the value of it, it sets the given value to the Value property of the given StackItem instance. And it sets the Next property of the given StackItem instance to None (Which is the null pointer).
  3. @property is called function decorator, a decorator is a function which takes a function as a parameter and returns a function too. (Weird but handy).
    The decorator works as a transformer that transforms the function from one state to another. @property decorator creates a property from the given function, a property is like a variable, you store values in it for further reading/writing, but properties have a special function called a getter that returns the hidden value, and a special function called a setter that sets a given value to the hidden variable, the benefits you get from using properties instead of variables is that you can do some check before assigning the value to the variable, you can make sure that the give value is a positive number for instance.
    @property created a property called Value inside StackItem class, and it bounded its value to a private variable called __value.
    You’d ask how come __value is a private variable?
    The answer is that in Python there is no access modifiers but Python annotates that any class variable with two leading underscores at least and one trailing underscore at most will be a private class variable, so other classes can’t see it, so it is a way of creating private variables.
  4. @Value.setter is another decorator which binds the setting of the Value property tho the given function.
  5. Next is like Value, a property.
    A property can be used later by calling its name only, just like any variable: e.g.:

    s.Value = 5
    #This will call the function with
    #@Value.setter decorator,
    #passes s as the first argument,
    #and 5 as the second one.
    print(s.Value)
    #This will call the function with
    #@property decorator,
    #passes s as the first argument.
  6. self is passed to each function in the class, it refers to the instance of the class that the function was called from (Like this in C++, C#, Java and Me in Visual Basic).
    When not passing self to the function you’re declaring that this function doesn’t need an instance of the class to be called, in other words, you’re declaring this as a static function, but you must tell the compiler that you did it on purpose so you should add @staticmethod decorator to the function, e.g.:

    class C(object):
    	def NonStatic(self):
    		print ("NonStatic" )
    	@staticmethod
    	def Static():
    		print("Static" )
    
    cObj = C()
    cObj.NonStatic() #Will print NonStatic
    #(self = cObj)
    cObj.Static() #Will print Static
    C.Static() #Will also print Static
    C.NonStatic() #Would give an error because
    #NonStatic expects 1 argument
    #and was called using none.

The other part must be easy for you after understanding the first one, now we’ll implement the Stack class:

class Stack(object):
	@property
	def Head(self):
		return self.__head

	@property
	def Count(self):
		return self.__count

	def __init__(self):
		self.__head = None
		self.__count = 0

	def Push(self, value):
		if (self.__head is None):
			self.__head = StackItem(value)
		else:
			temp = StackItem(value)
			temp.Next = self.__head
			self.__head = temp
		self.__count += 1

	def Pop(self):
		if (self.__head is None):
			raise Exception("Empty Stack" )
		else:
			temp = self.__head
			self.__head = self.__head.Next
			self.__count -= 1
			return temp.Value

	def PopAll(self):
		while (self.__count > 0):
			yield self.Pop()

Notes:

  1. Stack has two read-only properties (They have no setters), Head (a reference to its top-most item) and Count (the count of items in the Stack).
  2. The constructor sets the Count to 0 and the Head to None.
  3. Push takes two arguments, the stack to push into, and the value to be pushed.
  4. Pop takes one argument, the stack to pop out from, and returns the popped value. If the stack is empty Pop method will raise (throw) a new Exception with a message that says “Empty Stack”.
  5. PopAll is called a Generator, it generates values from a data structure, it can be used with for loops. e.g.:
    for value in s.PopAll():
    	print(value)

    yield is a keyword in Python that yields :D a given value to the caller. PopAll method works this way: The first time the caller calls it it would return the first value and stop. Each next time it will continue from where it stopped.

Now we can use our stack freely :)

S = Stack()

map(S.Push, range(1, 10))

for value in S.PopAll():
	print value
Simple code, right?
Notes:
  1. S = Stack() initializes a new Stack and put its reference in S.
  2. map is a function that takes a function as first argument and a list as second argument, and it calls the given function on each value from the give list and returns a tuple with the values of calling the function, in our case there is no return because Push returns no value. range is a function that takes two numbers and returns a list of numbers between the first one inclusively and the second one exclusively. e.g.:
    print(rang(1, 10)) #prints from 1 to 9

    range can also take three numbers, the third would be the step between each two numbers. e.g.:

    print(range(1, 10, 2)) #prints 1, 3, 5, 7, 9

    So map(S.Push, range(1, 10)) will fill the stack with values from 1 to 9.

  3. I used the PopAll method which is a generator to iterate over the StackItems and print them.

Overriding and Overloading

In OOP we use Overriding for a method in the sub-class that overrides a method in the base-class, and Overloading for two methods with the same name but differ in signature (number of arguments, types of arguments, order of arguments).
Usually to override a method in the sub-class the method must be declared virtual in the base-class, but in Python all methods are implicitly virtual.
To override a method you simply have to define it in the sub-class. e.g:
class A(object):
	def M(self):
		print (self.X)
	def __init__(self):
		self.X = 5
class B(A):
	def M(self):
		print (self.X + 1)

b = B()
b.M() #Will print 6
Sometimes you need to extend the base method instead of overriding it completely, you can call the base method by using BaseClassName.MethodName(self, arguments).
e.g.:
class C(B):
	def M(self):
		B.M(self)
		print ("This is C" )
c = C()
c.M() #Will print 6 This is C
While overriding is a piece of cake, overloading is not supported by Python, you have to pick different names for different overloads from your method, that is because Python associates each function to a variable, so when overloading the variable will have the last function only.
A def statement is roughly executed like this:
  1. Compile the body of the function.
  2. Build a function object __f
  3. Assign it: FunctionName = __f

Bottom Line:

Python supports Pure Object Oriented strongly, You can write pure Object Oriented programs using Python, so why don’t you start writing your programs now?
Cheers.
h1

I’m Learning Python part 8

February 21, 2009

I’m Learning Python part 8

Python

Python 3.0

Since the last time I wrote an article Python many changes happened in my life, one of them is the release of Python 3.0 (Which is known also as Python3K and Python3000).

Python 3.0 is the first intentionally backwards incompatible Python release, which means that there are some changes you must notice before start coding in Python 3.0.

Don’t worry that much, the language has become more useful, and also there is 2to3 source-to-source conversion tool which converts your Python 2.x code to meet the requirements of Python 3.0 :)

To find more about change in Python please refer to Python website.

P.S.

From now on all the code will be written in Python 3.0.

Decimal vs. float

Please try the following code in your python console:
d = 0.1
print(d)
print(str(d))
You’d get this output:

0.10000000000000001

0.1
Strange ain’t it?
The second value is the one you put in the variable.
The first one is the real value represented in memory. This is not Python’s fault, this happens because of the way computer store float values in memory as 0’s and 1’s, computers use floating point IEEE-745 representation.

Using this representation unfortunately we can’t represent any float value using 0’s and 1’s, and 0.1 is one of those poor values (The reason is that it can’t be represented in sum of powers of 0.5).

So what all programming languages do is that they ignore the small fraction so it would be 0.1 instead of 0.10000000000000001, but Python doesn’t.

Try this code in Java if you have:
public class NoDifference{
  public static void main(String[] args){
    double a = 0.1;
    System.out.println(a);
    a = 0.10000000000000001;
    System.out.println(a);
  }
}
You’d get this output:

0.1

0.1

So Java doesn’t differ those two values while they’re different. (Such a difference could make a disaster in a nuclear reactor :twisted: )
If you want to go with the flow and ignore this difference in your calculations you’d have to use another data type than float, you’ll have to use Decimal.
from decimal import Decimal
d = Decimal("0.1")
print(d)

0.1

Decimal data type allows you to represent any decimal value you want :)

It also supports addition, subtraction, multiplication, division and modulo, but the two sides must be Decimals.

Functions

Let’s write a function that prints Fibonacci series to a given boundary:
def fib(n):
  """Prints Fibonacci series up to n"""
  a, b = 0, 1
  while b < n:
    print(b, end = " ")
    a, b = b, a + b
#Now we can call the function by its name
fib(2000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

Now let’s describe what we have done:

  1. def is a keyword in Python, def defines :) it defines methods and classes.
  2. fib is the method’s name, method’s name must meet the same requirements for variable’s name. (first character a-z, A-Z or _, other characters a-z, A-Z, 0-9, _)
  3. (n) is the list of parameters, notice that no type names in this list. parameters are comma separated.
  4. : defines the scope of the method. All statements under this scope must be tab-aligned.
  5. “”"Prints …”"” is an optional string literal that describes what this method does, it will help you and other developers understand the purpose of the method, and it will also help you auto-generate documentation for your code.
  6. a, b = 0, 1. define two variables and give them two values. a handy way :)
  7. a, b = b, a + b. also assign two values to two variables.

The previous code defines a procedure (not a function) because it doesn’t return a value, let’s modify the code so it builds an array of Fibonacci values instead of printing them:
def fib(n):
  """Prints Fibonacci series up to n"""
  a, b = 0, 1
  result = []
  while b < n:
    result.append(b)
    a, b = b, a + b
  return result
f = fib(100)
print(f)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

What we have added is the return statement only, which returns a value to the caller.

One more thing to be told is default arguments:
def askYesNoQuestion(question, retries = 4, complaint = "Please write Yes or No only!"):
  while retries > 0:
    answer = raw_input(question)
    if answer in ['y', 'ye', 'yes', 'yep']: return True
    if answer in ['n', 'no', 'nop']: return False
    retries -= 1
    print(complaint)
  raise IOError, "stupid user"
#Now we can call it
askYesNoQuestion("Are you sure?", 1, "What?!")
askYesNoQuestion("Overwrite?", 1)
#Same as askYesNoQuestion("Overwrite?", 1, "Please write Yes or No only!")
askYesNoQuestion("Do you know me?")
#Same as askYesNoQuestion("Do you know me?", 4, "Please write Yes or No only!")
askYesNoQuestion("Do you have a pen?", complaint = "You can say no if you want!")
#Same as askYesNoQuestion("Do you have a pen?", 4, "You can say no if you want!")
Default arguments are assigned a default value if they aren’t when called.
You can set arguments values when calling by writing Name = Value.
Default parameters can’t be followed by non-default arguments.
Important note:
Default arguments are evaluated only once, so the following code will accumulate the values:
def f(a, L = []):
  L.append(a)
  return L
f(1)
print(f)
[1]
f(2)
print(f)
[1, 2]
f(3)
print(f)
[1, 2, 3]
If you don’t want the function to behave like this you can write:
def f(a, L = None):
  if L is None:
    L = []
  L.append(a)
  return L
Let’s explain what I meant by saying “are evaluated only once”:
When the Python interpreter reaches the line that defines the method, it allocates a list in the memory and assigns its address to L the reference.
Next time you call the function L will reference the same list it referenced the time it was created in memory, so it will always reference the same object.
The second code actually referenced to None, so every time you call the function it will assign None to L.
By understanding what I meant you can consider the following example:
i = 10
def f(a, b = i):
  return a + b
i = 11
print(f(2)) #Will it print 12 or 13?
12
That’s because b was given the value of i before the interpreter reached the line in which i was give the value of 11.

Bottom Line

Not that much I presented in this part, but you should consider upgrading your Python knowledge to Python 3.0 for the next time.
And by the way, I’m sorry, I’ve betrayed you and learned a lot during the last month, I have also started creating GUI Applications using Python and Qt, so please forgive me :)
Cheers.

h1

iCommunity FOSS 09

February 8, 2009

I mentioned many times before that I was planning to make a workshop about Free Open Source Software (FOSS) in Damascus, and thankfully I did :)

The Idea

The idea was to spread the culture of FOSS between the interested people here in Damascus.
What I noticed was that almost 99% of computer science students didn’t know what Free Software is and what Open Source software is!

The Process

So I started contacting engineers and people who I know that they’re capable of teaching this culture to our faculty students, and I managed to make 3 people agree on lecturing in the workshop.
After a while I realized that I can’t make the workshop as I thought, I won’t be able to make lectures inside the faculty because of some freaky stupid bureaucracy.
So the solution came with the idea of iCommunity, and thankfully it solve the problem.

iCommunity

iCommunity is a committee inside SCS – Syrian Computer Society, which is responsible of connecting the youth
computer science interested people with the SCS and other governmental offices.
It has the power that SCS gave to it, which is to think and prepare for projects that young people want to do and they can’t.
By having iCommunity we can achieve almost what we want with some powerful support from SCS.
And I’m one of the 6 managers of iCommunity, together we decieded that we will make this FOSS Workshop a reality.

The Workshop

The workshop took place in Damascus, Tishreen Park, SCS Center. From February 3rd to February 5th.
Everyday had lectures, coffee break and installation festival.

The First Day 03/Feb/2009

We had 3 lectures:

  1. What is Free Software? What is Open Source Software? and Why? Eng. Firas Kassem & Eng. Humam Hawasly.
  2. What are the licenses of Free and Open Source Software? Eng. Nada Al Benni.
  3. What is Creative Commons? Eng. Zyiad Maraqa from Jordan.

The Second Day 04/Feb/2009

We had 2 lectures:

  1. Arabic Free Open Source projects, and managing them. Php-ar as an example. Eng. Khaled Al Shamaa.
  2. Linux is a great choice for an operating system. Eng. Emad Mahayni.

The Third Day 05/Feb/2009

We had 3 lectures:

  1. Building unified communication using IP-Telephoney by open source projects. Eng. Ahmad Osman.
  2. Practical experiment with using Free Open Source Software in a daily life. Eng. Hani Al Safadi.
  3. Finding Free Open Source alternatives to proprietary software. Abd Allah Diab.

The Installation Festivals

We told people to bring their laptops so we can teach them how to install Ubuntu 8.10 Intrepid Ibex on it.
There were many interested people so the festival was great.
My friends in the college helped us in the festival, I can’t find any way to thank them.
We also played this video, that describes what would have happened if The Matrix was real and ran on Windows :)

DVDs

We distributed two DVD’s to the audience, the first one was Ubuntu 8.10 Desktop Edition DVD i386, and the other was an Open DVD which included a debian repository and free open source software for both operating systems Linux and Windows.

iCommunity

Media Coverage and Articles

Creative Commons website wrote about Zyiad’s lecture in the worksop:

Ziad Maraqa, co-Project Lead from CC Jordan, spoke yesterday in Damascus at the iCommunity FOSS Workshop, a notable gathering for the Syrian Free Software community.

Al Watan Newspaper in Syria wrote about the workshop daily (Arabic):

http://www.alwatan.sy/dindex.php?idn=50788
http://www.alwatan.sy/dindex.php?idn=50843

eSyria, Syria blog website wrote about the workshop daily in its Damascus website (Arabic):

http://www.edamascus.sy/_activities.php?filename=200902021845011

http://www.edamascus.sy/_business.php?filename=200902031620011

http://www.edamascus.sy/_news.php?filename=200902041650013

http://www.edamascus.sy/_news.php?filename=200902060300011

Photos

This is a photo of the iCommunity Managers:

iCommunity

This is a photo of the installation team, 4 iCommunity managers, and some students:

I’m the one with the gray suit and glasses in the middle 8)
And this one is from above of more students, 5 iCommunity Managers (the 6th is the one behind the camera) and the installation team :)

We had wonderful time, and we got great feedback of people attended the workshop.