My Technical Blog
Archive for February, 2009
I'm Learning Python part 8
Feb 21st
I’m Learning Python part 8

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.1You’d get this output:
print(d)
print(str(d))
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{You’d get this output:
public static void main(String[] args){
double a = 0.1;
System.out.println(a);
a = 0.10000000000000001;
System.out.println(a);
}
}
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
)
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):1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
"""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)
Now let’s describe what we have done:
- def is a keyword in Python, def defines
it defines methods and classes. - 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, _)
- (n) is the list of parameters, notice that no type names in this list. parameters are comma separated.
- : defines the scope of the method. All statements under this scope must be tab-aligned.
- “”"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.
- a, b = 0, 1. define two variables and give them two values. a handy way
- 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):[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
"""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)
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!"):Default arguments are assigned a default value if they aren’t when called.
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!")
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 = []):[1]
L.append(a)
return L
f(1)
print(f)
f(2)[1, 2]
print(f)
f(3)[1, 2, 3]
print(f)
If you don’t want the function to behave like this you can write:
def f(a, L = None):Let’s explain what I meant by saying “are evaluated only once”:
if L is None:
L = []
L.append(a)
return L
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 = 1012
def f(a, b = i):
return a + b
i = 11
print(f(2)) #Will it print 12 or 13?
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.
iCommunity FOSS 09
Feb 8th
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:
- What is Free Software? What is Open Source Software? and Why? Eng. Firas Kassem & Eng. Humam Hawasly.
- What are the licenses of Free and Open Source Software? Eng. Nada Al Benni.
- What is Creative Commons? Eng. Zyiad Maraqa from Jordan.
The Second Day 04/Feb/2009
We had 2 lectures:
- Arabic Free Open Source projects, and managing them. Php-ar as an example. Eng. Khaled Al Shamaa.
- Linux is a great choice for an operating system. Eng. Emad Mahayni.
The Third Day 05/Feb/2009
We had 3 lectures:
- Building unified communication using IP-Telephoney by open source projects. Eng. Ahmad Osman.
- Practical experiment with using Free Open Source Software in a daily life. Eng. Hani Al Safadi.
- 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.
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:
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
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.
And we're back
Feb 6th
After a month of not blogging, I’m back to blogging for Python.
This month had a lot in it:
- Computer Science exams @ Damascus University, so I had to study a lot.
- Scientific Calculations Project, the project of this semester.
- FOSS Workshop, which my friends and I arranged to in Damascus, and it was more than wonderful.
After finishing my Scientific Calculations Project I made the choice of removing Windows for good from my computer, I’m going to make Ubuntu my only operating system to use.
The workshop was more than I expected, I witnessed the great participation and work from my friends to make this workshop a successful story to be told.
You can see here that the FOSS workshop was a notable event in Damascus and Syria.
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.
And here are some articles about the event but they’re in Arabic:
Al Watan Newspaper:
http://www.alwatan.sy/dindex.php?idn=50788
http://www.alwatan.sy/dindex.php?idn=50843
eDamscus:
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
Thank you all for waiting a month
I’m Learning Python part 8 will be here soon.










