Common Mistake about Python default parameter value

October 1st, 2009 by Ahmed S. Farghal 8 comments »

I know it’s a little bit long title but didn’t find a better one :) It’s one of the common mistakes a python beginner does and even more experienced programmers can fall into the same mistake so I thought it might be useful if I illustrated that out here so you can know the concept.

you can easily fall into writing code that look like that:

def get_me_bad_list(arg1, myList=[]):
    myList.append(arg1)
    return myList

In [1]: get_me_bad_list(“hello”)
Out[1]: ['hello']

In [2]: get_me_bad_list(“world”)
Out[2]: ['hello', 'world']

Interesting, no?

The issue here is that the default value for function parameter is evaluated at function definition time (i.e, module import?) and as list objects are mutable they allow you change the value so you keep having the same default object for every function call and you modify it and return it back and even worse the caller can change it and it’ll reflect the list inside the function like the following

In [3]: x = get_me_bad_list(“Assalam”)

In[4]: x.append(“Alaykum”)

In[4]: get_me_bad_list(“Ya Shabab”)

Out[4]: ['hello', 'world', 'Assalam', 'Alaykum', 'Ya Shabab']

So, what’s the right way to do that? (if you are not doing that intentionally and you just want the plain default function parameters) that you define the initial value inside the body of the function (execution time)

def get_me_good_list(arg1, myList=None):
    myList = [] if myList is None else myList
    myList.append(arg1)
    return myList

In [1]: get_me_good_list(“hello”)
Out[1]: ['hello']

In [2]: get_me_good_list(“world”)
Out[2]: ['world']

and I’ll leave the rest of the tests to you, but generally the explanation is that everytime you call the function we create a new list (if you didn’t supply one explicitly) and that list is populated and returned so you won’t see any nasty behavior.

It’s interesting that you see that the deep you understand how the python interpreter scan and execute the code can really affect the way you fall into mistakes because if you are good enough then you will be somehow safe :)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • DZone
  • PDF
  • Reddit
  • RSS
  • Twitter

Do we really need H4ck3rZ?

September 28th, 2009 by Ahmed S. Farghal 3 comments »

An important question popped into my head, do we really need H4ck3rZ website?

We have done enormous efforts for renew our web footprint and now we have stuff that CAT never had, like the central authentication and social networking but with no result on the activity meter.

Currently almost nobody is visiting our website and no activity is being done except for the projects which are terribly slow because no serious contributers are around, so let’s think for a moment, what will happen if we gave up and stopped H4ck3rZ? Will you even notice?

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • DZone
  • PDF
  • Reddit
  • RSS
  • Twitter

Design Patterns Course

September 25th, 2009 by Ahmed S. Farghal 27 comments »

I’m planning to launch a software design patterns course in Mansoura on Fridays for 5 days that would introduce the most important patterns (8 Patterns) to those who are interested in software design.

The course is comprehensive and will have hands-on example using Java 5. The course is also considered the first professional design patterns course in Mansoura, if you are interested please post because I’m collecting votes.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • DZone
  • PDF
  • Reddit
  • RSS
  • Twitter

كل عام و انتم بخير

September 19th, 2009 by Ahmed S. Farghal 6 comments »

كل عام و امه محمد صلى الله عليه و سلم بخير بمناسبه عيد الفطر

اللهم اعد علينا رمضان و بلغنا اياه مرات عديده و سنين مديده و اكتب لنا القبول و العتق من النار يا ارحم الراحمين

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • DZone
  • PDF
  • Reddit
  • RSS
  • Twitter

Mercurial 2

September 16th, 2009 by Ahmed S. Farghal 1 comment »

Session 2 of Mercurial Tutorial in Arabic

http://www.vimeo.com/6597676

التسجيل كان قبل الافطار بنص ساعه فعذراً لو كان فيه لخبطه بسيطه في الكلام :)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • DZone
  • PDF
  • Reddit
  • RSS
  • Twitter

Screen Tricks

September 14th, 2009 by Ahmed S. Farghal 2 comments »

If you are using the famous ‘screen‘ command and you are using ‘windows‘ in screen alot then you might find it pretty useful to add the following lines in either your /etc/screenrc or ~/.screenrc file.

hardstatus alwayslastline
hardstatus string ‘%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %d/%m %{W}%c %{g}]‘
shelltitle “$ |bash”
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %d/%m %{W}%c %{g}]'
shelltitle "$ |bash"

This will add a very useful bar in the bottom of the screen where you can see in which window you are working and the process running and some other useful stuff.

Special thanks goes to Ahmed Kamal for the tip.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • DZone
  • PDF
  • Reddit
  • RSS
  • Twitter

Introduction to Mercurial

September 12th, 2009 by Ahmed S. Farghal 5 comments »

This is an introduction to mercurial distributed SCM published as part of my contributions to CAT H4c3krZ, the video is in arabic and all the next sessions will be in arabic also.

http://www.vimeo.com/6519247

Let me hear your opinion….

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • DZone
  • PDF
  • Reddit
  • RSS
  • Twitter

اجازة رسميه

September 9th, 2009 by Ahmed S. Farghal 12 comments »

كل سنه و انتم طيبين جميعاً ، النهاردة هاكلمكم عن حاجة غريبة و مستفزة كالعاده و هي موضوع الاجازات الرسمية ، الأجازات الرسمية دي يعني مجلس الوزراء اللي بيحددها للأحتفال بموضوع معين و في اجازات دينية و رسمية برضو زي اجازة عيد الفطر و عيد الأضحي للتحديد و دول الاجازتين اللي ربنا حددهم عشان المسلم يحتفل مع اهله و عائلته و يحس بالسعادة لكن سبحان الله تلاقي في اختراع طلع اسمه ان الاجازات الرسمية دي للقطاع العام بس و القطاع الخاص بيعمل اللي هو عايزو

يعني مثلاُ عيد الفطر ربنا شرعة تلات ايام لكن الشركات )اغلبها( قرر انه يومين بس و تالت يوم ده شغل!! يعني السنه دي لقيت ان الاجازة في الشغل هي يوم الاحد و الاثنين بس على الرغم من ان الاجازة الرسمية من مجلس الوزراء اربع ايام تبدأ السبت و اخر يوم فيها هو الثلاثاء ، الشئ الاظرف ان في اجازات زي عيد العمال مثلاً الحكومة عملت الاجازة السبت عشان عيد العمال كان موافق يوم جمعة اصلاً و الشركات طنشت تماماً و ماقلتش مثلاً ندي احنا الاحد بداله. بس مااااشي السبت اصلاً اجازة في الشركات و مش هانتكلم لكن عيد الفطر؟؟؟ اللي الواحد كان بيستناه هو و عيد الاضحى عشان يشوف اهله و اصحابه و يخرج او يسافر؟!

السنه دي انا قررت اني اعرف حقي اللي قانون العمل بيحدده و لقيت الماده 52 اللي لازم كل الناس تشوفها

للعامل الحق في إجازة بأجر كامل في الأعياد التي يصدر بتحديدها قرار من الوزير المختص بحد أقصى ثلاثة عشر يوما فى السنة .

ولصاحب العمل تشغيل العامل في هذه الأيام إذا اقتضت ظروف العمل ذلك ، ويستحق العامل فى هذه الحالة بالإضافة إلى أجره عن هذا اليوم مثلى هذا الأجر .

يعني ببساطة شديدة كل واحد فينا من حقه على اليوم التالت ده ضعف مرتبة في هذا اليوم عشان الشركة شغلنه يوم الاجازة ا لرسمية

تفتكروا هانعرف ناخد حقنا في البلد دي؟

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • DZone
  • PDF
  • Reddit
  • RSS
  • Twitter

فطار الكات

September 6th, 2009 by Ahmed S. Farghal 5 comments »

بقالي فترة مكتبتش هنا و السبب ان حياتي فعلاً ملخبطة و مضطربة بسبب عدد كبير من المشغوليات لكن ان شاء الله الأمور تبقى اكثر تنظيما و يرجع الاستقرار و النظام لحياني قريباً ، المهم ان كان فيه افطار جماعي للكات امبارح و انا كنت سعيد جداً و حزين جداً جداً ، سعيد لأني قابلت اصحابي اللي بقالي فترة كبيرة ماقابلتهمش زي احمد بكر و ابراهم عبد الفتاح و السيد جمال و كمان المفاجئة ايهاب عبد الرحمن و اللي بقالي تقريباً اكتر من سنه و نص ماشفتوش و معرفش عنه اخبار و كان ليه راجع من كندا بقالو يومين. و كمان قابلت زمايل في الكات بعتز اني في يوم اشتغلت معاهم و شاركونا الحلم و بذلوا مجهود كبير زي احمد مدكور و محمد صفوت و محمود متولي )ميتو ( و  ناس كتير مش عايز انسى اساميهم لكن كل الحضور كانوا ناس محترمين بصراحة.

كنت حزين لأسباب كتير و اهمها ان العدد كان قليل ، حوالي 25 واحد بس من كل الكات و ده بيعكس حاجات كتير قوي و برضو انشغال الناس من الحاجات دي لكن كان نفسي احتفل باتحاد الكات اجتماعياً و اشوف الناس كلها اصدقاء و عايزين يشوفو بعض مهما كانت الظروف لكن للأسف الحقيقة ان الوضع الحالي للكات اكتر من مذري و ده اتناقشنا فيه في جلسه ثقافيه جميله بعد الفطار و اللي كان بيتزعمها السيد جمال بخطابه الحماسي المتفائل و ردودي المحطمة للآمال و كان فيه جزء من النقاش عن اسباب مشاكل الكات الحالية و ليه في انهيار سريع في المستوى و ازاي الجامعه مش بتساعدنا و الناس مش متعاونة و حاجات كتير عايزه مقاله منفصلة

المهم كان فيه تكريم بسيط لأبرز اعضاء الكات و اللي تشرفت اني اكون على اول القائمة مع السيد جمال و ابراهيم عبد الفتاح و اللي بنعتبر حاليا اقدم اعضاء في الكات تقريبا اللي لسه نشطاء و متواجدين و اخدت هديه ظريفه و هي “كأس” الكات :lol:

اتمنى اني اشوف جيل تاني زي الجيل اللي عمل الكات و كبرها و وصل بيها للنجاح ياخد الرايه و يبهرنا و يخليني اقول انا بجد فخور بالعمل ده و اتمني اني اشوف الجامعة و الشركات في المنصورة بيدعموا العمل ده بدون انتهازية او سلبيه

و كل عام و انتم طيبين

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • DZone
  • PDF
  • Reddit
  • RSS
  • Twitter

Cloud Computing

August 17th, 2009 by Ahmed S. Farghal 5 comments »

I’ve been busy for almost the last year in developing infrastructure services for cloud computing platform – mainly dispersed storage – and during that I’ve been working closely with cloud computing giants and have seen several cloud computing management and provisioning frameworks — that include Q-Layer framework — I can really see the change happening to the IT world because of the ‘Cloud’ and how the cloud will affect the way we think in everything, how we think in storage, how we think in application development and even how we can make business using IT.

Let me first define what cloud computing is. Cloud computing named to be the next generation of distributed systems where you use computer resources as a service and you don’t have to pay attention to all of how/where/when questions related to administration and management and even technical issues. you simply use the resource — storage as an example — and you put your files/data over and you don’t care how are they going to make sure that your data will be available at all times, how are they going to manage disk failures and even how to manage the network latency problems, this bring us to the most important point of cloud computing ’security’ how can I make sure that it’s only me who can actually read the data? so far, there are no good-known-standard way of storage that ensure that but you can always use the typical cryptography methods to make sure you are the only one who can read the data but that doesn’t necessarily have to be applied on other types of computer resources. So the cloud is there somewhere on the internet and you can access it using -maybe- some API or protocol and that’s it, no extra money for deployment, electricity, servers, management, security, etc. » Read more: Cloud Computing

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • DZone
  • PDF
  • Reddit
  • RSS
  • Twitter