tag:blogger.com,1999:blog-19960148547088486052024-02-07T18:21:15.223-08:00Open SourcedAnonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.comBlogger35125tag:blogger.com,1999:blog-1996014854708848605.post-57762386155981339812014-10-22T05:17:00.000-07:002014-10-22T05:17:24.562-07:00Enlivepy A Different Approach to Html Templating in Python<div style="background: rgb(255, 255, 255); border: 0px; color: #000305; font-family: 'Trebuchet MS', Trebuchet, 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif; font-size: 14px; line-height: 20.0059986114502px; margin-bottom: 1em; margin-top: 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
Don’t worry - this blog entry won’t consist of me rambling about yet another templating engine that will change the world. Instead, I’ll be focusing on a practical aspect - the problems the current HTML templating libraries have and what can be possibly done about these issues.</div>
<div style="background: rgb(255, 255, 255); border: 0px; color: #000305; font-family: 'Trebuchet MS', Trebuchet, 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif; font-size: 14px; line-height: 20.0059986114502px; margin-bottom: 1em; margin-top: 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
Django and all other MVC/MVP frameworks do a great job at separating the Controller logic from the View logic. This way you can have all your complex operations in Controller and send back the data that needs to be shown or rendered to the end user. On the other end of this process you have a templating library - it takes Python objects and renders them.</div>
<div style="background: rgb(255, 255, 255); border: 0px; color: #000305; font-family: 'Trebuchet MS', Trebuchet, 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif; font-size: 14px; line-height: 20.0059986114502px; margin-bottom: 1em; margin-top: 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
Now, some of these templating libraries argue that having lots of logic in the representation layer is a bad thing (Django templating for example). Others, like Jinja2, are more lax and allow you to do lots of interesting things.</div>
<div style="background: rgb(255, 255, 255); border: 0px; color: #000305; font-family: 'Trebuchet MS', Trebuchet, 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif; font-size: 14px; line-height: 20.0059986114502px; margin-bottom: 1em; margin-top: 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
In both cases what we are actually doing is mixing data with logic. And while this might not seem like a problem with small applications, when you start operating on a greater scale, issues start to appear (a lot). Let me provide a few examples:</div>
<ul style="background: rgb(255, 255, 255); border: 0px; color: #000305; font-family: 'Trebuchet MS', Trebuchet, 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif; font-size: 14px; line-height: 20.0059986114502px; list-style-image: initial; list-style-position: outside; margin: 0em 0px 0px 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
<li style="background: transparent; border: 0px; margin: 0.5em 0px 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><div style="background: transparent; border: 0px; margin-bottom: 1em; margin-top: 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
Front end developers (CSS/HTML developers) start having difficulties when editing html pages when something does not appear the way it should be appearing. They have to learn the templating engine your framework uses.</div>
</li>
<li style="background: transparent; border: 0px; margin: 0.5em 0px 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><div style="background: transparent; border: 0px; margin-bottom: 1em; margin-top: 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
Sometimes HTML/CSS developers break the logic in html pages by replacing something by mistake.</div>
</li>
<li style="background: transparent; border: 0px; margin: 0.5em 0px 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><div style="background: transparent; border: 0px; margin-bottom: 1em; margin-top: 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
Backend developers are not the best at editing both HTML and CSS (well, in most cases, at least!). I have had this happen to me quite a few times too. I receive the HTML from a designer team, after that I put it into the templating engine...and it has a radically changed appearance. When I compare the way it looks on the server and the original things are just looking completely different.</div>
</li>
<li style="background: transparent; border: 0px; margin: 0.5em 0px 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><div style="background: transparent; border: 0px; margin-bottom: 1em; margin-top: 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
If HTML/CSS developers need to play or change something on the system, what they need is a working copy of the whole system. You need to give them ssh access to the staging server or something similar.</div>
</li>
</ul>
<div>
<span style="color: #000305; font-family: Trebuchet MS, Trebuchet, Lucida Sans Unicode, Lucida Grande, Lucida Sans, Arial, sans-serif;"><span style="font-size: 14px; line-height: 20.0059986114502px;">Read the rest of the article <a href="http://makkalot.github.io/posts/2014/Oct/22/enlivepy-html-transformation/" target="_blank">here.</a></span></span></div>
<div>
<span style="color: #000305; font-family: Trebuchet MS, Trebuchet, Lucida Sans Unicode, Lucida Grande, Lucida Sans, Arial, sans-serif;"><span style="font-size: 14px; line-height: 20.0059986114502px;"><br /></span></span></div>
<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com0tag:blogger.com,1999:blog-1996014854708848605.post-14123928583014368382014-10-07T00:49:00.001-07:002014-10-07T00:49:46.770-07:00Django Authentication Workflow<div style="background: rgb(255, 255, 255); border: 0px; color: #000305; font-family: 'Trebuchet MS', Trebuchet, 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif; font-size: 14px; line-height: 20.0059986114502px; margin-bottom: 1em; margin-top: 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
The existing Django Documentation explains everything about the Django authentication process in a pretty good way. Directions include adding your back end, replacing the built-in User model and many more. However, despite this good coverage, I had certain problems with seeing the big picture of the overall process. At times when I needed to create a new authentication backend, I always had to glance through the Django source again.</div>
<div style="background: rgb(255, 255, 255); border: 0px; color: #000305; font-family: 'Trebuchet MS', Trebuchet, 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif; font-size: 14px; line-height: 20.0059986114502px; margin-bottom: 1em; margin-top: 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
In this blog entry I’ll focus on the Django source code and documentation -- to be more exact, I will share my findings during reading both the source code and the documentation. Read the rest of the article on my new <a href="http://makkalot.github.io/posts/2014/Jul/13/django-auth-workflow/" target="_blank">blog</a></div>
<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com0tag:blogger.com,1999:blog-1996014854708848605.post-30649884979886914702012-11-09T09:30:00.001-08:002012-11-09T09:30:40.606-08:00Python ve defaultdict kullanımıPython ve defaultdict kullanımı ile alakalı küçük bir blog yazısı : <a href="http://www.makkalot.com/2012/11/python-defaultdict-maceralari-2/" target="_blank">Link</a><div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com0tag:blogger.com,1999:blog-1996014854708848605.post-76732629042010220042010-08-29T09:13:00.000-07:002010-08-29T09:37:27.088-07:00Posix Öğrenelim - Ders 0<div style="text-align: justify;"> Yazmayalı gerçekten uzun zaman olmuş. Bir geliştiricinin de yaptıkları hakkında blog yazmaması kabul edilebilir şey değil. Ama tembellik , yoğunluk gibi nedenlerden dolayı aksayabiliyor bloglar ... </div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"> Bu aralar POSIX, Linux Kernel , Embedded programlama gibi konulara balıklama dalmış durumdayım ve uzun bir süre çıkmaya da niyetim yok gibi. Hayatımda olan gelişmelere gelince , işimden zaman kaldığında sık sık C derneğine gidiyorum. C derneğinin işletim sistemi projesine katılmış durumdayım, henüz bir katkım olmadı ama umarım ileride olur. Bu aralar ayrıca C derneğinde Kaan Aslan'ın vermekte olduğu Unix Sistem Porgramama kursuna da devam etmekteyim. Bu kursta genellikle POSIX fonksyonlarının kullanımı , Linux kernel gibi konular hakkında bilgi veriliyor. Kursun daha iyi pekişmesi amacıyla da birtakım ödevler veriliyor. Ben de bu ödevler kaybolmasın, ayrıca başkalarına da lazım olur diye <a href="http://github.com/makkalot/learn_posix">github'a</a> koydum. Her ödev verildiğinde onunla alakalı bir post yazmaya çalışacağım ama bu önceki sözlerime benzeyebilir :) O yüzden reponun adresi belli ilgilenenler belli aralıklarla orayı kontrol edebilirler.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">İlk verilen ödev stat fonksyonun kullanımı ile alakalı bir ödev. Komut satırından girilen dosya isimleri ls -l formatında ekrana bastırılacaktır. <a href="http://github.com/makkalot/learn_posix/tree/master/statls_prj/">LINK</a></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Diğer bir alıştırma ise komut satırından verilen grup isimlerine karşılık gelen kullanıcı isimlerinin ekrana bastırılması (<grp.h> fonksyonları ile alakalı): <a href="http://github.com/makkalot/learn_posix/tree/master/grpusers/">LINK</a></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Tüm yazılan alıştırmalar bir Makefile'a sahip ve derlenmeleri o şekilde yapılacaktır.</div><div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com1tag:blogger.com,1999:blog-1996014854708848605.post-944254754667274542010-04-11T03:43:00.000-07:002010-04-11T03:54:12.673-07:00So Long, and Thanks for All the FishBu bir veda yazısı ve aynı zamanda yeni bir başlangıcın sembolü diyebiliriz :) Bu hafta itibari ile İnomera A.Ş. 'deki Java Web Developer pozisyonundan ayrılmış bulunmaktayım. Nedenine gelince ; son zamanlarda ilgilenmekte olduğum sistem programlama alanının bana çok ilgi çekici gelmesi ve bu yönde bir iş teklifi almış olmamdan kaynaklanıyor. Telekom sektöründe olan SkyNet Firması ile birlikte çalışmaya devam edeceğim. Umarım hepimiz için hayırlı bir değişimm olur. İnomera'dakilerin deyimi ile daha "Penguence" işler yapmak üzere ayrılıyorum. Bana sunduğunuz tüm imkanlar için teşekkür ediyorum ve ileride tekrar birlikte çalışmak dileyile burada bu yazımı noktalıyorum ...<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com0tag:blogger.com,1999:blog-1996014854708848605.post-42676820529105424872010-03-06T00:14:00.000-08:002010-03-06T00:45:35.985-08:00Cache Sistemleri<div style="text-align: justify;">Bu yazı ile yeni bir yazı serisine başlamış bulunmaktayım. Umarım düzenli olarak devam ettiririm. C derneğinde Sistem Programlama ve ile İleri C 1 kursuna başladım. Gerçekten İstanbul'da imkanı olan her programcının 1 kez uğramış olması gereken bir yer. Bu devirde C mi öğrenilir kardeşim? diyenler olabilir ama C herşeyin başlangıcı ve kaynağıdır onu bilmeyen diğerlerini de tam bilemez diye düşünüyorum.(Ya da IRC'de bana dedikleri gibi char* in 2009?) Bu yazı seirisinde C derneğinde tuttuğum notları ve yazdığım kodları buraya koymaya çalışacağım. Umarım bana ve buraya gelenlere bir faydası dokunur ...<br /><br /></div><ol style="text-align: justify; font-weight: bold;"><li>Cache Sistemleri</li></ol><div style="text-align: justify;">Bilgisayar sistemlerinde pekçok durumda bir yavaş bellek ve hızlı bellek vardır. Yavaş bellek ucuz olduğundan boldur. Hızlı bellek daha küçük miktardadır ve pahalıdır. Örn, RAM hızlı disK yavaş belleği temsil eder. Ya da cpu'nun içindeki bellek hızlı belleği normal RAM yavaş belleği temsil edebilir.<br />Bu tür durumlarda yavaş belleğin belli bir bölümü hızlı bellekte tutulur. Ve yavaş belleğe erişim miktarı azaltılmaya çalışılır. Bu tür sistemlere cache sistemleri denilmektedir.Bilgi elde edileceği zaman önce hızlı belleğe başvurulur ,hızlı belleğe cache de denir. Burada varsa<br />doğrudan alınır.Yoksa yavaş belleğe başvurulur. Bilginin alınma hızına cache hit,yoksa cache miss olur. Bir cache sisteminin amacı cache hit oranını yükseltmektir. Buna cache performansı denir. Cache performansı şu öğeler etkiler :<br /> - cache miktarı büyüklüğü,şüphesiz hızlı bellek ne kadar çok olursa yavaş belleğin o kadadr büyük bölümü tutulur performans artar.<br /> - Yavaş belleğin hangi bölümünün tutulacağını belirlemek de önemlidir. Buna cache startejisi denir.<br /> - Cache'in read only veya r/w olması performansı etkiler.<br /></div><div style="text-align: justify;"><div style="text-align: justify;"><br />Read Only cache sistemi; hızlı bellek yalnızca okuma amaçlı kullanılır. Bilgi yazılırken doğrudan yavaş belleğe yazılır. Böylece elektril kesilme gibi durumlarda bilgi kaybı engellenir. r/w cache sistemlerine hem okuma hem yazma yapılır.<br /><br /><br /></div> <span style="font-weight: bold;">2.Cache Stretejileri</span><br /><br />Cache sistemleri tek bloklu yada çok bloklu tasarlanır. Tek bloklu (single cache line) sistemlerinde yavaş belleğin ardışıl cache kadar bölümü cache'tet tutlur. şüphesz yavaş belleğin hangi bölümünün cache'te olduğu not alınmalıdır. Tek bloklu sistemler ardışıl erişimlerin yoğun olduğu durumlarda etkin olabilmektedir. Fakat ardışıl erişimler söz konusu değilse bu sistemde etkinlik çok azalır. Alternativ yöntem çok bloklu cache (multi cache line) sistemidir. Çok bloklu sistemlerde yavaş bellek bloklara ayrılır. Cache bellek de aynı biçimde bloklanmaktadır.<br /> Böylece yavaş belleğin farklı blokları aynı anda cache içinde bulunur. Bir bölgeye erişilmek istendiğinde önce ,cache'teki bloklarda bu bölgenin olup olmadığına bakılır.<br /> Cache içinde yavaş belleğin hangi blokları tutulmalıdıır? Şüphesiz en çok kulalnılan blokların cache'te tutulması ilk akla gelen yöntemdir.<br /> Fakat bu nasıl tespit eidlir ?<br /> Strateji olarak 2 durum önemlidir. Yavaş belleğin hangi bölümü hızlı bellekte tutulacaktır ve hızlı belleğin hangi bölümü atılacaktır? Bu konuda kullanılan algoritmalar vardır. Özellikle cache sistemlerinde bilgiyi çıkarma işlemi koyma işleminden daha önemlidir. Gelecekteki durum bilinmediğine göre bir chache miss oluştuğunda herzman yavaş belleğin o bloğunun cache'e çekilmesi yoluna gidilebilir. Aksi takdirede yavaş bloğun her kısmı için istatistik yapmak gerekir. Önemli cache startejileri (atım)şunlardır :<br /><br /></div><ul style="text-align: justify;"><li><span style="font-weight: bold;">LFU</span> (Least frequently used):</li></ul><div style="text-align: justify;">Bu alg.'da en az cache hit almış cache bloğu cache'ten çıkartılır. Bu yöntemi uyugulamak için her cache bloğu bir sayaçla kontrol edilir.Her cache hit'te sayaç arttırılır,sonra sayacı en düşük olan blok çıkartılır. Şüphesiz yeni alınan blok için ortalama bir sayaç değeri verilmelidir.<br /><br /></div><ul style="text-align: justify;"><li><span style="font-weight: bold;">LRU </span>(Least recently used)</li></ul><div style="text-align: justify;">Burada uzun süredir cache hit almayan blokların çıkrtılaması yoluna gidilir. Bu yöntem pekçok sistem için daha etkilidir. Bu yöntemle tipik olarak cache bloklarından ya da onların numaralarından bir bağlı liste oluşturlup ve bir cache hit oluştuğunda o blok bağlı listenin en önüne alınır. böylece bağlı listenin en arkasında son zmanalarda en az kullanılan bloklar kalmış olur. Cache'ten blok atılacağı zaman burdan atılır. LRU yöntemi pekçok olay için iyi bir performans oluşturmaktadır. Örneğin linux işletim sistemi çekirdeklerinde kullanılan cache sistemlerinin çoğu lru'dur.<br /><br /></div><ul style="text-align: justify; font-weight: bold;"><li>Hibrit Modeller</li></ul><div style="text-align: justify;">Bu yöntemlerde birkaç algoritma yöntemin ortalaması dikkate alınır. Örn hem lfu hem de lru değerleri kontrol edilebilir. İkisinin toplam durumuna göre bir karar uygulanır.<br /><br /></div><ul style="text-align: justify;"><li><span style="font-weight: bold;">MRU</span> (Most recently used)</li></ul><div style="text-align: justify;">Bazı sistemlerde son erişilen bölgelerin (blokların) gelecekte kullanım miktarlarının azalacağı bilinmektedir. Bu durumda tam tersi olarak cache'ten son zamanlarda en sık kullanılanı atmak uyugun bir yöntemdir. Böyle sistemler ile az karşılaşılmaktadır.<br /><br /></div><ul style="text-align: justify;"><li><span style="font-weight: bold;">MFU</span> (Most frequently used)</li></ul><div style="text-align: justify;">Bazı sistemlerde bir bloğun kullanımı arttıkça gelecekte onu kullanım azalır. Örn, belli bir sistemde eğer her bloğun benzer miktarda erişildiğini biliyosak bu durumda cache'te o zamana kadar en kullanılmışı atmak daha mantıklı olur.<br /><br /> <span style="font-weight: bold;">3.Cache sistemlerinin kullanılmasına yönelik tipik yöntemler</span><br /><br />OS'lerde tamamen çekirdek düzeyinde aşağı seviyeli disk cache sistemi oluşturulur. Buna UNIX dünyasında "buffer cache" denilmektedir. OS'nin çekirdeği son eirşilen disk bloklarını ram'de kullanılmayan bölgelerde saklar ve disk erişimin azaltmaya çalışır. disk cache sistemi disk erişimlerini %70 azaltır. OS'nin en önemli performans konularından birini oluşturur. Örn linux sistemleri mevcut kullanılmayan RAM'i disk cache olarak kullanılmaktadır. Bu cache sistemi r/w'dir. WIN ve UNIX sistemleri genellikle "delayed write" kullanırlar. Bu yöntemde yazma işlemi sırasında diske değil cache'e yazılır. Fakat, cache'in tazelenmesi için çok beklenmez.OS'nin bu amaçla kullanılan kernel treadleri belli periyotlarda cache bloklarını inceler dirty olanları diske yazar. Böylece elektrik kesilmesi gibi durumlarda kayıp ciddi boyutta olmamaktadır.<br /> Memory stick'ler teknoloji olarak belirli sayıda yazmaya izin vermektedir.(Bu yazma sayıları artmaktadır) İşletim sistemleri bu tür aygıtlara yazma yaparken hem hız için hem de bu gerekçe ile cache sistemi kullanabilmektedir. Örn win sistemlerinde bu durum ayarlanabilir.<br /> Yeni win sistemlerinde default durum read only cache sistemidir. Örn linux sistemlerinde r/w kullanılır.<br /> Bugün kullandığımız pc'lerde de donanımsal düzeyde bellek sistemi için cache kullanılmaktadır.<br /> Eskiden cpu'lar yavaştı ve dramlar cpu'lardan daha hızlıydı. Fakat zaman geçtikçe cpu'lar dramlara göre çok hızlandılar. Başa baş noktası 80286 işlemcilerinin kullanıldığı AT makinalarda yakalandı.(80 lerin sonu) Bu cpular 16mhz'de çalışıyordu ve o zamanki ramler 60ns. Bundan sonra artık boardlara cache sistemi eklenmiştir.İlk cache sistemi cpunun dışındaydı vs sram'lerden oluşuyordu. CPU önce sram'e sonra dram'e başvuruyordu. Cpular daha da hızlanınca cpu'nun içine de cache koyulmaya başlandı. Böylece dram 2 aşamada cachelenmekteydi. Cpu'nun içeresindeki cache'e l1 cache diğerine l2 cache deniyordu. Son yıllarda dışsal cache de cpu'nun içeresine kondu. cpu'nun cache miktarını performansta önemli etkisi vardır.Buradaki cache yazılımsal değil tamamen donanımsaldır.<br /><br /></div><div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com0tag:blogger.com,1999:blog-1996014854708848605.post-74056224668365404962010-03-05T23:09:00.000-08:002010-03-05T23:27:50.024-08:00New Theme And New PostsHi all, who came here to the end of the world by accident :) I decided to post more often to my blog than before (i hope). Therefore new starts need changes ... I changed my blog theme so it is cooler now with a penguin above :) I'm not a designer and dont have idea how to make those cool things so my friend (a great designer) <a href="http://meadk.blogspot.com/">ADK</a> helped me with it. Big thanks goes to him! Btw,you should hire him you wont be sorry believe me :) Well what is going on in my life ?<br /><br /> - After GSOC 2009 i finished my school,so now i'm a Computer Science Engineer (heh it sounds cool), though i dont feel like one.<br /> - I worked for a while as Django/Python freelancer<br /> - Now working as full time developer for <a href="http://www.inomera.com/">Inomera A.Ş</a>. on <a href="http://www.netmera.com/pages/web/home">Netmera Social Platform</a>.<br /> - While writing that post i'm reading my C notes for the <a href="http://www.csystem.org/course/course/display_course.php?id=4">course</a> i started.<br /><br />Well thats, all nothing exciting in my life sorry :) I hope to post here more geeky stuff as i find some free time.<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com0tag:blogger.com,1999:blog-1996014854708848605.post-3000632992086241632009-08-16T06:47:00.000-07:002009-08-16T06:56:37.832-07:00Gsoc 2009 is almost over !<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://farm4.static.flickr.com/3069/2428847785_16ff5f788e.jpg"><img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 255px; height: 165px;" src="http://farm4.static.flickr.com/3069/2428847785_16ff5f788e.jpg" alt="" border="0" /></a><br />As some of you may know the GSOC 2009 is going to be over. The code writing is over and some little formal things are remaining to pull the final trigger. As i posted in my previous posts i contributed to Func project this year as i did last year. Here are some of the items i added to <a href="https://fedorahosted.org/func/wiki">Func</a> that year :<br /><br /><ul><li><a class="wiki" href="https://fedorahosted.org/func/wiki/FactsFarmework">Facts Framework</a> - a different way to query more things on minions at once </li><li><a class="wiki" href="https://fedorahosted.org/func/wiki/GroupsApi">Groups Api</a> - new pluggable func groups api </li><li><a class="wiki" href="https://fedorahosted.org/func/wiki/GrepModules">Grep Modules</a> - Query modules for a proper pattern </li><li><a class="wiki" href="https://fedorahosted.org/func/wiki/RealTimeOutput">Real Time Output</a> - Watch your minions output while they do their jobs. </li><li><a class="wiki" href="https://fedorahosted.org/func/wiki/ProgressReport">Progress Report</a> - Get progress report of your minions easily. </li><li>And many other small fixes and additions.</li></ul>It was a great experience for me, thanks Func and of course many thanks to Google :) Probably that was my last GSOC and it was really fun to work on Open Source projects and get payed.<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com1tag:blogger.com,1999:blog-1996014854708848605.post-7738605220818024262009-04-30T13:52:00.000-07:002009-04-30T14:02:34.678-07:00Using GWT with Django<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.pathf.com/blogs/wp-content/uploads/2008/06/gwt.png"><img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 182px; height: 175px;" src="http://www.pathf.com/blogs/wp-content/uploads/2008/06/gwt.png" alt="" border="0" /></a><br /><div style="text-align: justify;">Well i tried GWT a few days ago and liked it very much. I'm not a js expert and for that reason dont use it too much in my web applications. But from time to time i need some fancy client-side apps to do ajaxian stuff easily. I had written an Independent Django app which was an ajax-driven event calendar. Therefore i did most of the work remotely via Django. I used jquey for ajaxian calls. But if i were a better js programmer i would do that job with less remote calls. But the Magic in GWT is cool you create your application with Java ; a very cool and easy language and it converts the written code into javascript which is compatible with most of the browsers. Therefore you dont bother with strange behaviour of the js and dont bother about if your code will run on BrowserA or BrowserX.<br /><br />I followed the tutorial in GWT site and had a running application (StockWather). To complete that tutorial you probbaly would need to complete the StockWatcher firstly and then go on from here or you can just pull the code i wrote for that purpose from here[1] and just inspect it.<br /><br />For communication of both ends i used XML,if you're not a fan of it you can use JSON also. I didnt use JSON because of the JSNI usage in Java code. I dont like hybrid codes like that...<br /><br /><ol><li>First create the server side code in Django :<a href="http://github.com/makkalot/pyalgorithm/blob/01334d2a6216e75b6f499939ec1d9a7f696153bf/gwt_test/gwt/views.py">LINK</a></li><li>You need to build a GET request in you JAVA code so u do that from here: <a href="http://github.com/makkalot/pyalgorithm/blob/01334d2a6216e75b6f499939ec1d9a7f696153bf/gwt_test/static/WithPlugin/src/com/makkalot/client/WithPlugin.java">LINK</a></li><li>Parse the incoming XML string from Django and convert it to JAVA objects: <a href="http://github.com/makkalot/pyalgorithm/blob/01334d2a6216e75b6f499939ec1d9a7f696153bf/gwt_test/static/WithPlugin/src/com/makkalot/client/StockObject.java">LINK</a></li><li>Write unittests for your client side code (GWT) because if you have errors u will get some strange JS errors which are not so decsriptive. : <a href="http://github.com/makkalot/pyalgorithm/blob/01334d2a6216e75b6f499939ec1d9a7f696153bf/gwt_test/static/WithPlugin/test/com/makkalot/client/StockObjectTest.java">LINK</a></li><li>Put GWT application in your media and put the generated HTML file in your templates directory.</li></ol><br />Well that is all,it took some time to glue both of them (Django and GWT) but we have here a happy end :) GWT is cool but it has some little weird stuffs i dont understand yet. One of them is Designing the applications.I should find<br />an easy way to explain it to my designer :)<br /><br />[1]: <a href="http://github.com/makkalot/pyalgorithm/tree/01334d2a6216e75b6f499939ec1d9a7f696153bf/gwt_test">GWT-Django app</a><br /></div><div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com8tag:blogger.com,1999:blog-1996014854708848605.post-83994415229841186512009-04-21T09:42:00.000-07:002009-04-21T11:03:08.435-07:00GSOC 2009 Again<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://code.google.com/images/2009socwithlogo.gif"><img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 300px; height: 200px;" src="http://code.google.com/images/2009socwithlogo.gif" alt="" border="0" /></a><br />Well i'm again in the Game :) That summer i will be working on my last year GSOC project FUNC again.Big Thanks to Adrian Alikins(my mentor) for choosing me again. And Bigger thanks to GOOGLE for paying us to do open source programming. If you hear for the first time FUNC,it is a great Remote management Framework for doing things reamotely easier. You can even open your shell and do management by playing with its Python API. My aim that time is to make that cool API even more cooler.Here is the list with ideas :<br /><ul><li><span style="font-weight: bold;">Implementing Func Facts </span>: Wouldnt it be cool to say func update machines that are ">f9" or list the machines with dual core cpus. That is what func facts claims to be an ORM like remote querying system.</li><li><span style="font-weight: bold;">Implementing a Pluggable Connection Manager for Func :</span>Well that is kind of hard one, currently he FUNC is using XML-RPC over SSL. The aim of that item is to make Func to support different connections by reading its conf files. The first connection to be implemented seems to be qpid/AMQP/qmf. After having the pluggable structure it will be easier to add different connections i hope.</li><li><span style="font-weight: bold;">Structuring the Results : </span>Currently FUNC recieves different kind of results in different formats. Having a way to query and process that result without too much loops will be realy great.</li></ul><ul><li><span style="font-weight: bold;">Funcweb Development : </span>Func has a Web interface fo doing all the stuff visually,so new features should be added to that cool app.</li></ul><br />I hope to finish all the stuff succesfully again like last year. (INSH ;))<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com0tag:blogger.com,1999:blog-1996014854708848605.post-49996974687155679652009-03-17T15:52:00.000-07:002009-03-17T15:57:15.768-07:00Ajaxian Even Calendar on DjangoThat is an independent small app that can be useful for others that need some calendar and want to show some events on it. I couldnt find any django calendars which are ajaxian out there. Therefore if you need something like that use it, it is completely free. Let me know if you have any trouble with it.<br /><br />Features are:<br /> - Showing every month with events<br /> - Small and big(excel like) versions<br /> - Adding same event for a couple of days<br /> - Adding lots of events on same day<br /> - Facebox display of events<br /> - Dual language arabic/english<br /><br /><br />Demo small : <a href="http://aaba.clients.kuwaitnet.net/">Link</a> (on the left)<br />Demo Big : <a href="http://aaba.clients.kuwaitnet.net/en/calendar/">Link</a><br />Source : <a href="git://github.com/makkalot/django-event-ajaxian-calendar.git">Link</a><br />Static directory : <a href="http://tutorials-place.com/media/makkalot_stuff/calendar_static.tar.bz2">Link</a><br /><br />For instalation look at README file in the source.<br />Have FUN and thanks to ADK for his design help ...<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com0tag:blogger.com,1999:blog-1996014854708848605.post-74916409956566412262009-02-11T13:46:00.000-08:002009-02-11T13:53:37.041-08:00Open Source MotivationJust read the article of Lior Kaplan which is about the things that make us to use or develop opensource projects. <a target="_blank" href="http://liorkaplan.wordpress.com/2009/02/11/working-for-free-–-motivations-of-participating-in-open-source-projects/">Link:</a><br /><br />I think these are mine reasons about doing that ;)<br /><br />- Help other people that may have the same problem as you, so you make your code public or help via ML,IRC and etc.<br />- Opportunity to learn new things and finding areas to apply them.<br />- Coding about what you like,the topics you are interested.<br />- Having the feeling that more can do it better than single, so copy everything that is usable for you and continue from there (DRY).<br />- Becoming expert in area you like without thinking about it.<br />- Having a good CV(background) about things you have done, talks is cheap i'm showing the code.<br />- Having the opportunity to start own business without paying anything, you just need the idea<br />- Working together than alone is more fun.<br /><br />Btw, i think people who are payed to work on opensource projects are really lucky :) Hope to be one of them ...<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com0tag:blogger.com,1999:blog-1996014854708848605.post-78696313228148382492009-01-29T13:42:00.000-08:002009-01-29T14:26:20.728-08:00I wrote one use mine !That is the story about a little and probably useless software i wrote. Everything began in 2007 during my internship in Pardus. Pardus uses pisi (a Python based package manager) and pisi doesnt have any sign/verify mechanism (still doesnt have) to secure package transmission. Well we all decided it would be cool to make the verification process via X.509 certificates so we can have more info about signer and etc. And everything began, i started designing the app. The idea was simple we have a package we sign it via X.509 and put the signature in package an send to users for installation. User has on other site a db with trusted certificates, he/she extracts the signature checks if the cert is trusted. If the cert is good to use user computes the hash of all files in package and compares with hash that is in the signature. Simple yes ...<br /><br />The implementation part was not so easy for me. I heard that there is a Python package for handling SSL things called Python OPenSSL. I was excited it was my first Open Source program but pyOpenSSL didnt have all my needed classes for certification handling, signing and verification parts (if has man i dont know). Than i found M2Crypto it had some ugly Python code and bad exception handling but had all the cool features i needed. And the 'imzaci' was there an alpha verison maybe, it didnt have a code base i was proud of. The application was not accepted by Pardus members and none asked me about it to make it better or to fix sth. Everyone was silent , man probably i wrote the ugliest program in the world if none says sth :)<br /><br />I thought it was the way how Open Source works. (People can think about things they have experinced) Well Open Source code development doesnt work like that believe me. People discuss,fight,share,apply patches that is the world i was imaging from the beginning. However i didnt make a good start :) The GSOC2008 and FUNC project showed me that secret and cool Opensource World.<br /><br />Well lets return to our little useless application, after GSOC2008 i have rewritten most of the code maybe better maybe worse i dont know. I changed its name it is not imzaci anymore it is pysign and it is an independent (no written for specific distro) application. Why i have rewritten the program ? I wondered if i became better after those years. Well the code is here [1] and you know what it was not fun to write it. If someone is going to write an app similar to that one please look at my code maybe you can grap some part of it. I wrote it once dont do it again :)<br /><br />Ok what is the point :) The point ;is it is very very very important to choose community you are going to contribute. Please before you join look at mail listing conversations, look at documentation of project or community has. The start is very important and if you have luck you will be in a place where people respect every bit of your code. That is the Open Source world i was looking for and found at the end :)<br /><br />There are two kind of programmers, good programmer gets the old code fixes it (if possible) and completes the job in less time, bad programmer writes all the code from scratch (maybe worse than first programmer). Be the good programmer i wrote it dont repeat yourself :)<br /><br />[1] http://github.com/makkalot/pysign/tree/master<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com0tag:blogger.com,1999:blog-1996014854708848605.post-78022619857777967942009-01-20T02:53:00.000-08:002009-01-20T03:17:25.612-08:00Django Admin ManyToMAny BehaviourIn that post want to share an experience with a Django behaviour that took all my day to solve it. In nowadays , i'm working on a freelance project and trying to finish project due to deadline (hate deadlines). Here is the small part of models i'm using<br /><br /><pre name="code" class="python"><br />class SchoolSection(models.Model):<br /> #snip<br /><br />class GalleryItem(models.Model):<br /> school_section = models.ManyToManyField(SchoolSection)<br /> #snip<br /><br /></pre><br /><br />A pretty easy structure , the problem occurred when i connected the post_save signal to a method which was using the models above. When a new GalleryItem was saved i wanted to pre-populate some xml files according to new commers. Here is the simple code that was pulling the gallery items :<br /><br /><pre name="code" class="python"><br />sc=SchoolSection.objects.all()<br />gi = GalleryItem.objects.filter(school_section = sc[0])<br /></pre><br /><br />When i save an object i was not able to see the new added in query above. When i saved the object second time (edit->save) the new entry appeared. In a moment when i was going go crazy someone from #django channel told me that Django doesnt save the m2m relationships immediately due to performance issues. Therefore the solution was to override a method in admin part , here is the solution :<br /><br /><pre name="code" class="python"><br />class GalleryItemAdmin(admin.ModelAdmin):<br /> def save_model(self, request, obj, form, change):<br /> super(GalleryItemAdmin, self).save_model(request, obj, form, change)<br /> form.save_m2m()<br /> obj.save()<br /><br /></pre><br /><br />That is all, hope to help someone who has the same problem :)<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com4tag:blogger.com,1999:blog-1996014854708848605.post-8098808538488431432009-01-04T02:33:00.001-08:002009-01-04T02:34:02.729-08:00import makkalotHello planet and thanks for adding me.<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com1tag:blogger.com,1999:blog-1996014854708848605.post-22584826182183989792008-12-04T06:01:00.000-08:002008-12-04T06:05:31.961-08:00Command pattern in Python<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.cartoonstock.com/newscartoons/cartoonists/rha/lowres/rhan1051l.jpg"><img style="margin: 0pt 0pt 10px 10px; float: right; cursor: pointer; width: 255px; height: 171px;" src="http://www.cartoonstock.com/newscartoons/cartoonists/rha/lowres/rhan1051l.jpg" alt="" border="0" /></a><br />I continue with "x pattern in Python" series, the articles i posted here are not tutorials or some professional learning resource. I poste them here only for myself to rememer the things when i need them :) What is the command pattern and why wee need it ? Well imagine you have a class that controls some operations on files, writing , reading , closing , and maybe many more. In first sight i would complete that like this :<br /><br /><pre name="code" class="python"><br />class FileOperator:<br /><br />def operation1(self):<br /> pass<br /><br />def operation2(self):<br /> pass<br /><br />#snip snip snip ....<br /></pre><br />Well that implementation seems enough for now, but what if we want to supply a simple api to users like that<br /><br /><pre name="code" class="python"><br />f = FileOperator()<br />f.process_command()<br /></pre><br /><br />Well, with current implementation that doesnt seem to be possible, the current implementation is kind fo static and user has to know the internals of the FileOperator class. If the users want some extra functionality like undo or logging, it will not be very easy to be implemented. And every change that has to be done will effect the current client code which is not cool. Also user may want when call the process_command to execute multiple commands like write_to_file and read_contents and etc. What we will do is again the basic OOP remove the parts that can change in the future. We will remove the executer parts and wiill assing them when we need them. Ok when reading it doesnt seem so easy but the code is easier:<br /><br />Here is my implementation of the Command Pattern : <a href="http://github.com/makkalot/pyalgorithm/tree/master/dp/command.py">LINK</a><br /><br />After that implementation you will able to do things like that :<br /><pre name="code" class="python"><br />def test_me_yo():<br /> """<br /> Lets see that one<br /> """<br /> tmp_file = open("some.txt","w")<br /> tmp_file.write("hehehe\n")<br /> tmp_file.close()<br /><br /> #end of initial data<br /> #first lets read the contents of the stuff<br /> fe = FileEditor()<br /> fileman = FileMan("some.txt")<br /> fread = FileReadCommand(fileman)<br /> fwrite=FileWriteCommand(fileman)<br /> fe.set_command(fread)<br /> fe.process_command()<br /><br /> #append some info to the end of it<br /> fe.set_command(fwrite)<br /> fe.process_command("add some text here")<br /> fe.set_command(fread)<br /> fe.process_command()<br /><br /> #lets now go and create an command manager<br /> cm = CommanManager()<br /> cm.add_commanders(fwrite,fread)<br /> fe.set_command(cm)<br /><br /> fe.process_command("\n Add another sting here ")<br /> print "First undo :"<br /> fe.undo_process()<br /> print "Second undo :"<br /> fe.undo_process()<br /> print "Thirrd undo :"<br /> fe.undo_process()<br /></pre><br />Well it is pretty pluggable design you can combine the commands you like do "undo" all the magic you need. What i learned from Design Patterns is they are based on the same principle "remove the parts that will change in the future" ,separating the concerns is very important ...<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com0tag:blogger.com,1999:blog-1996014854708848605.post-88080415964545402022008-12-02T11:48:00.000-08:002008-12-02T11:50:56.166-08:00Template Design Pattern<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.ida.liu.se/%7ETDDB84/pictures/goldfishjump.jpg"><img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 273px; height: 182px;" src="http://www.ida.liu.se/%7ETDDB84/pictures/goldfishjump.jpg" alt="" border="0" /></a><br />The next step in DP land is Template Design Pattern, well what is it ? As far as i understood there is nothing special and new under the hood. You have an abstract class with methods to be overriden by its sunclasses. The main difefrence is you have a one or more template methods which draw the flow of the things. A simple example maybe :<br /><br /><pre name="code" class="python"><br />class MainClass(object):<br /><br /> def operation1(self):<br /> #do stuff<br /><br /> def operation2(self):<br /> #do stuff<br /><br /> def __template_method(self):<br /> self.operation1()<br /> self.operation2()<br /><br /></pre><br /><br />What we did here is to supply an API for users to override the operation1 and operation2 but we want to control the overflow by template_method, if you want more extendible structure you may also make it public. Well i know that pattern i have used it in lots of places but didnt know its name :) The new thing i learned from Head First is the hook methods , you make them optional for the client code. For example:<br /><pre name="code" class="python"><br />class MainClass(object):<br /><br /> def operation1(self):<br /> #do stuff<br /><br /> def operation2(self):<br /> #do stuff<br /><br /> def hook(self):<br /> pass<br /><br /> def do_you_want_hook(self):<br /> return True<br /><br /> def __template_method(self):<br /> self.operation1()<br /> self.operation2()<br /> if self.do_you_want_hook():<br /> hook()<br /></pre><br /><br />Ok the difference is if user wants something extra to be executed he/she can override hook and do the extra stuf that is needed there,otherwise that method will not be invoked (well it will be but nothing will happen).<br />That is all for the template pattern, we know you,you are not new here :)<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com0tag:blogger.com,1999:blog-1996014854708848605.post-40778877113470488182008-12-01T03:26:00.000-08:002008-12-01T03:35:09.129-08:00Factory Pattern in Python<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://imgs.xkcd.com/comics/advanced_technology.png"><img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 190px; height: 244px;" src="http://imgs.xkcd.com/comics/advanced_technology.png" alt="" border="0" /></a><br />Our next step in DP is object factories. Creating objects with if else clauses sometimes may not be very flexible for our programs. Some of the objects may neeed more complex and long initializations other maybe simple etc. Because of that it is cooler to have some methods or some classes that give you the instance you need. For exmple think about a client program which needs a slash directory or file manager. If you're trying to have your program platform independent you should have different versions for Windows and Linux based machines. Therefore when you client is initializing the File manager it should everytime do<br /><pre name="code" class="python"><br /> if linux:<br /> do sth<br /> else:<br /> do another<br /></pre><br /><br />But for a framework that is not very flexible and when some code of FileManager changes (some code of initialization) you should change your client code which is not cool. It would be better if you have some class that gives you the instance you need, in our example FilemanagerFactory.get_instance() so client code will not care about the platform and other annoying stuf it will just get the instance it needs. Yep that is factory pattern ...<br />To demonstrate the factory thing i wrote a simple example that is responsible for creating Validators, if you have a string you get a string validator if you have an integer you have integer valiator and etc . Here is the link: <a href="http://github.com/makkalot/pyalgorithm/tree/master/dp/factory.py">LINK</a><br /><br />To test the code you need sth like that :<br /><pre name="code" class="python"><br />from pyalgorithm.dp.factory import *<br /><br />def test_facto():<br /> my_value = "Some_strrrrrrrrr"<br /> factory = ValidatorFactory()<br /> validator = factory.create(my_value)<br /> print validator.validate()<br /><br /> my_value = 11<br /> validator = factory.create(my_value)<br /> print validator.validate()<br /><br /> my_value = 11.12<br /> validator = factory.create(my_value)<br /> if not validator:<br /> print "No object created ..."<br /></pre><br />Well that is all, factory pattern may not be very important for little projects but if you have to initialize lots of classes it maybe wisw to have some class to do that for you.<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com0tag:blogger.com,1999:blog-1996014854708848605.post-40799019547333587652008-11-29T09:23:00.000-08:002008-11-29T10:27:23.649-08:00Singleton Pattern in PythonThe Singleton actually is not something very different or new invention. The purpose of it is to have only a single instance of some class so you can have a global point of access which is cool. For now i think i can use it for configuration manager classes, some cool loggers and some context variables. Maybe i will find more of em. The Singleton pattern is a little bit different from one that is in Java. In Java to make a singleton you have to make your constructor private and have some static method to do the instance initialization. In python what you have todo is override the __new__ method of the Singleton candidate. There are lots of examples on the net related with that topic so you can search one before use mine. Actually mine is not written or thought by me i found it also (from aspn CookBook site) :)<br /><br />Here is the first version i found and think is good : <a href="http://github.com/makkalot/pyalgorithm/tree/master/dp/singleton.py">LINK</a><br />As you will see there is some time and sleep things in it , i put em there to test what happens when some threaded classes use it. And as you may imagine some of the threads got different references of that Class. Here is the Thread i used :<br /><br /><pre name="code" class="python"><br />from threading import *<br />import time<br />import thread<br /><br />class ThreadSingleton(Thread):<br /> """<br /> A simple thread to work on that<br /> """<br /> def __init__(self,id):<br /> Thread.__init__(self)<br /> self.id = id<br /><br /> def run(self):<br /> """<br /> Here initialize the singleton<br /> """<br /> self.s=Singleton()<br /><br /> print "Im thread %d and %d"%(self.id,id(self.s))<br /> time.sleep(2)<br /><br /> def get_singleton(self):<br /> return id(self.s)<br /><br /></pre><br /><br />Here is the code that tests it :<br /><pre name="code" class="python"><br />def singleton(t="threaded"):<br /> for j in range(100):<br /> threads = []<br /> for i in range(10):<br /> if t!="threaded":<br /> threads.append(ThreadSingleton(i))<br /> else:<br /> threads.append(RealThreadedSingleton(i))<br /><br /> for i in threads:<br /> i.start()<br /><br /> for i in threads:<br /> i.join()<br /><br /> print "The final ids are :"<br /> t0 = threads[0]<br /><br /> for i in threads:<br /> assert t0.get_singleton() == i.get_singleton()<br /><br /> t0.s.destroy()<br /><br /><br /> if j%10 == 0:<br /> print "************** %d COMPLETED **********"%j<br /><br /></pre><br /><br />After those tests i got some Assertion Errors because some of the threads got different references of the object. What we know from here is that our Singleton is not thread-safe and we should fix it. Here is the threaded version of the Singleton <a href="http://github.com/makkalot/pyalgorithm/tree/master/dp/th_sibgleton.py">LINK</a> :<br /><br />To test that new verison i nedded some new ThreadClass to call the new created Singleton :<br /><br /><pre name="code" class="python"><br />class RealThreadedSingleton(ThreadSingleton):<br /><br /> def run(self):<br /> """<br /> Here initialize the singleton<br /> """<br /> self.s=ThSingleton()<br /><br /> print "Im thread %d and %d"%(self.id,id(self.s))<br /> time.sleep(2)<br /><br /></pre><br /><br />After final changes i run the tests 1000 times and didnt have any problems. Why we should bother to have Singleton threa-safe , because i'm going to use it in some frameworks that uses threads heavily, so icant be sure when that one will be called. To conclude, when write some cool classes always should think the posibility of theraded usages.<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com3tag:blogger.com,1999:blog-1996014854708848605.post-41518271402845283792008-11-28T05:47:00.000-08:002008-11-28T05:52:27.257-08:00Decorator Pattern in Python<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://best-practice-software-engineering.ifs.tuwien.ac.at/patterns/images/decorator_simple.jpg"><img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 263px; height: 172px;" src="http://best-practice-software-engineering.ifs.tuwien.ac.at/patterns/images/decorator_simple.jpg" alt="" border="0" /></a><br />Our next step in DP is Decorator pattern, as u know Python also has some decorator options you can apply to your programs by @ sign. But if you want to apply decoration to classes in object level you have to change the stuff a little bit. I always thought that Java's IO library is very confusing, you create a FileInput class then you put it in a BufferReader and etc. When i read the Decorator chapter in Head First i realized that the Java IO API is using that pattern there. Well why do you need decorator pattern? Lets go from IO API example :<br /> 1. You have a normal reader<br /> 2. You need a reader that converts the input to lowercase, so what you do is create a new UpperReader<br /> 3. You need now a reader that converts input to a sha1 sum , what you do is create a Sha1Reader<br /> 4. You need a reader that converts input to uppercase sha1sum , what you do is create a Sha1UpperReader<br /> 5. The 4. item can be removed if you used the decorator pattern<br /><br />Shortly, decorator pattern wraps an object and change its flow before or after the target operation. For the example above you can change the incoming input or process it to sth else.<br /><br />Here is the implementation i wrote in Python and put on my personal github repo : <a href="http://github.com/makkalot/pyalgorithm/tree/master/dp/decorator.py">LINK</a><br /><br />A sample usage of above can be like that :<br /><br /><div class="python" style="border: 1px solid rgb(208, 208, 208); color: rgb(0, 0, 102); background-color: rgb(240, 240, 240);font-family:monospace;"><ol><li style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);font-family:'Courier New',Courier,monospace;font-size:130%;"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);font-family:monospace;font-size:1em;"><span style="color: rgb(255, 119, 0); font-weight: bold;">def</span> deco<span style="color:black;">(</span><span style="color:black;">)</span>:</div></li><br /><li style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);font-family:'Courier New',Courier,monospace;font-size:130%;"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);font-family:monospace;font-size:1em;"> <span style="color: rgb(255, 119, 0); font-weight: bold;">from</span> pyalgorithm.<span style="color:black;">dp</span>.<span style="color:black;">decorator</span> <span style="color: rgb(255, 119, 0); font-weight: bold;">import</span> <span style="color: rgb(102, 204, 102);">*</span></div></li><br /><li style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);font-family:'Courier New',Courier,monospace;font-size:130%;"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);font-family:monospace;font-size:1em;"> </div><br /></li><br /><li style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);font-family:'Courier New',Courier,monospace;font-size:130%;"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);font-family:monospace;font-size:1em;"> <span style="color: rgb(255, 119, 0); font-weight: bold;">print</span> <span style="color: rgb(72, 61, 139);">"The normal file reader content"</span></div></li><br /><li style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);font-family:'Courier New',Courier,monospace;font-size:130%;"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);font-family:monospace;font-size:1em;"> n = NormalFileReader<span style="color:black;">(</span><span style="color: rgb(72, 61, 139);">"some.txt"</span><span style="color:black;">)</span></div></li><br /><li style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);font-family:'Courier New',Courier,monospace;font-size:130%;"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);font-family:monospace;font-size:1em;"> <span style="color: rgb(255, 119, 0); font-weight: bold;">print</span> n.<span style="color:black;">read_content</span><span style="color:black;">(</span><span style="color:black;">)</span></div></li><br /><li style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);font-family:'Courier New',Courier,monospace;font-size:130%;"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);font-family:monospace;font-size:1em;"> </div><br /></li><br /><li style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);font-family:'Courier New',Courier,monospace;font-size:130%;"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);font-family:monospace;font-size:1em;"> <span style="color: rgb(255, 119, 0); font-weight: bold;">print</span> <span style="color: rgb(72, 61, 139);">"Sha1 sum of the read info"</span></div></li><br /><li style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);font-family:'Courier New',Courier,monospace;font-size:130%;"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);font-family:monospace;font-size:1em;"> sh =DecoratoSha1Reader<span style="color:black;">(</span></div></li><br /><li style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);font-family:'Courier New',Courier,monospace;font-size:130%;"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);font-family:monospace;font-size:1em;"> NormalFileReader<span style="color:black;">(</span><span style="color: rgb(72, 61, 139);">"some.txt"</span><span style="color:black;">)</span></div></li><br /><li style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);font-family:'Courier New',Courier,monospace;font-size:130%;"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);font-family:monospace;font-size:1em;"> <span style="color:black;">)</span></div></li><br /><li style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48); font-family: 'Courier New',Courier,monospace; font-size: 130%;"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32); font-family: monospace; font-size: 1em;"> <span style="color: rgb(255, 119, 0); font-weight: bold;">print</span> sh.<span style="color:black;">read_content</span><span style="color:black;">(</span><span style="color:black;">)</span></div></li><br /><li style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48); font-family: 'Courier New',Courier,monospace; font-size: 130%;"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32); font-family: monospace; font-size: 1em;"> </div><br /></li><br /><li style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48); font-family: 'Courier New',Courier,monospace; font-size: 130%;"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32); font-family: monospace; font-size: 1em;"> <span style="color: rgb(255, 119, 0); font-weight: bold;">print</span> <span style="color: rgb(72, 61, 139);">"Sha1 sum but with upper cases"</span></div></li><br /><li style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);font-family:'Courier New',Courier,monospace;font-size:130%;"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);font-family:monospace;font-size:1em;"> up = DecoratoUpperFileReader<span style="color:black;">(</span></div></li><br /><li style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);font-family:'Courier New',Courier,monospace;font-size:130%;"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);font-family:monospace;font-size:1em;"> DecoratoSha1Reader<span style="color:black;">(</span></div></li><br /><li style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);font-family:'Courier New',Courier,monospace;font-size:130%;"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);font-family:monospace;font-size:1em;"> NormalFileReader<span style="color:black;">(</span><span style="color: rgb(72, 61, 139);">"some.txt"</span><span style="color:black;">)</span></div></li><br /><li face="'Courier New',Courier,monospace" size="4" style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);"><div face="monospace" size="1em" style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);"> <span style="color:black;">)</span></div></li><br /><li face="'Courier New',Courier,monospace" size="4" style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);"><div face="monospace" size="1em" style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);"> <span style="color:black;">)</span></div></li><br /><li face="'Courier New',Courier,monospace" size="4" style="vertical-align: top; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);"><div face="monospace" size="1em" style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);"> </div><br /></li><br /><li style="vertical-align: top; font-family: 'Courier New',Courier,monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 130%; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);"><div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);"> <span style="color: rgb(255, 119, 0); font-weight: bold;">print</span> up.<span style="color:black;">read_content</span><span style="color:black;">(</span><span style="color:black;">)</span></div></li><br /></ol></div><br /><br />The output is sth like that:<br /><br /><span style="font-weight: bold;">The normal file reader content<br />Hey im the content!!!<br /><br />Sha1 sum of the read info<br />5fd77bd8e4f80ec432c71a09033a7ef51de60448<br />Sha1 sum but with upper cases<br />5FD77BD8E4F80EC432C71A09033A7EF51DE60448</span><br /><br />Well, it is the end of the today's pattern "decorator man". The post will be a good reference for me when forget the things :)<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com0tag:blogger.com,1999:blog-1996014854708848605.post-16022049049682416402008-11-25T06:53:00.000-08:002008-11-25T07:23:24.977-08:00Observer Pattern in Python<a style="" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://robgarrett.com/photos/static/new/robgarrett_com/software/Bak2BasiksDesignPatternsTheStrategyPatte_919/image012.png"><img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 252px; height: 179px;" src="http://robgarrett.com/photos/static/new/robgarrett_com/software/Bak2BasiksDesignPatternsTheStrategyPatte_919/image012.png" alt="" border="0" /></a><br />Well here are the first things i got from "Head First Design Patterns" book :) I was working on a big web project and have also some small open source projects i try to finish. I stoppped codding on that projects immediately after read the first chapter of the book :) I realized that have big gaps in my programming skills about patterns. What i know now is :<br /><br />- Knowing OO principles and rules doesnt make u a OO guru.<br />- Inheritance is not the best way of code reuse.<br />- Inherit the static parts and use composition for things that can change in the future.<br />- Less dependency between classes is alway better.<br /><br />Will add to those more in the future i think.<br />Ok lets go back to the title of that entry. Sometimes we need to send some notifications to other classes from a specific class. What i would do before knowing the observer pattern would be sth like that. I would define a notifier() method and will call the classes i want to notify :<br /><br /><div class="python" style="font-family:monospace;color: #006; border: 1px solid #d0d0d0; background-color: #f0f0f0;"><ol><li style="font-weight: normal; vertical-align:top;font: normal normal 130% 'Courier New', Courier, monospace; color: #003030;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;color: #000020;"><span style="color: #ff7700;font-weight:bold;">def</span> notify<span style="color: black;">(</span><span style="color: #008000;">self</span><span style="color: black;">)</span>:</div></li><br /><li style="font-weight: normal; vertical-align:top;font: normal normal 130% 'Courier New', Courier, monospace; color: #003030;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;color: #000020;"> classA.<span style="color: black;">update</span><span style="color: black;">(</span>args<span style="color: black;">)</span></div></li><br /><li style="font-weight: normal; vertical-align:top;font: normal normal 130% 'Courier New', Courier, monospace; color: #003030;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;color: #000020;"> classB.<span style="color: black;">update</span><span style="color: black;">(</span>args<span style="color: black;">)</span></div></li><br /><li style="font-weight: normal; vertical-align:top;font: normal normal 130% 'Courier New', Courier, monospace; color: #003030;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;color: #000020;"> classC.<span style="color: black;">update</span><span style="color: black;">(</span>args<span style="color: black;">)</span></div></li><br /><li style="font-weight: normal; vertical-align:top;font: normal normal 130% 'Courier New', Courier, monospace; color: #003030;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;color: #000020;"> </div></li><br /></ol></div><br />Ok it seems good for now but wat happens if you want to notify the classes D and E in the future u have to add them to the list above. That means altering the existing code which may not be a good idea. Ok now the classB doesnt want anymore to be notified , so what happens now ? It is very clear that the structure above is not the best for that situation. What we need is a dynamic srtucture which will allow us to add and remove new observers during the runtime without altering the existing code, so that is where the observer pattern comes into the scene ...<br /><br />Here is the code i wrote to make a simple scenarion for observer pattern on my personal git_repo : <a href="http://github.com/makkalot/pyalgorithm/tree/master/dp%2Fobserver.py">LINK</a><br />Now we can do sth like that easily :<br /><br /><div class="python" style="font-family:monospace;color: #006; border: 1px solid #d0d0d0; background-color: #f0f0f0;"><span style="color: #ff7700;font-weight:bold;">from</span> pyalgorithm.<span style="color: black;">dp</span>.<span style="color: black;">observer</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span><br /><br /><br /><br /><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">"I creatte the our subject"</span><br /><br />c_s = ConcreteSubject<span style="color: black;">(</span><span style="color: black;">)</span><br /><br />c_s.<span style="color: black;">my_x</span> = <span style="color: #ff4500;">22</span><br /><br />c_s.<span style="color: black;">my_y</span> =<span style="color: #ff4500;">33</span><br /><br /><br /><br /><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">"I notify my subscribers :"</span><br /><br />c_s.<span style="color: black;">values_changed</span><span style="color: black;">(</span><span style="color: black;">)</span> <span style="color: #808080; font-style: italic;">#that can be done dynamic but it is cool for now</span><br /><br /><br /><br /><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">"Lets add a new XObserver"</span><br /><br />x_o = ObserverX<span style="color: black;">(</span><span style="color: black;">)</span><br /><br />g_o = ObserverGeneral<span style="color: black;">(</span><span style="color: black;">)</span><br /><br />c_s.<span style="color: black;">add_observer</span><span style="color: black;">(</span>x_o<span style="color: black;">)</span><br /><br /><br /><br /><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">"I notify my subscribers :"</span><br /><br />c_s.<span style="color: black;">values_changed</span><span style="color: black;">(</span><span style="color: black;">)</span> <span style="color: #808080; font-style: italic;">#that can be done dynamic but it is cool for now</span><br /><br /><br /><br /><br /><br /><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">"Lets add a new GeneralObserver"</span><br /><br />c_s.<span style="color: black;">add_observer</span><span style="color: black;">(</span>g_o<span style="color: black;">)</span><br /><br /><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">"I notify my subscribers :"</span><br /><br />c_s.<span style="color: black;">values_changed</span><span style="color: black;">(</span><span style="color: black;">)</span> <span style="color: #808080; font-style: italic;">#that can be done dynamic but it is cool for now</span><br /><br /><br /><br /><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">"The X observer doesnt want to know anything about us remove him"</span><br /><br />c_s.<span style="color: black;">remove_observer</span><span style="color: black;">(</span>x_o<span style="color: black;">)</span><br /><br /><br /><br /><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">"I notify my subscribers :"</span><br /><br />c_s.<span style="color: black;">values_changed</span><span style="color: black;">(</span><span style="color: black;">)</span> <span style="color: #808080; font-style: italic;">#that can be done dynamic but it is cool for now</span></div><br />The output for that will be as follow :<br /><br /><span style="font-weight: bold;">I creatte the our subject<br />I notify my subscribers :<br />Lets add a new XObserver<br />I notify my subscribers :<br />Im X observer and got : 22<br />Lets add a new GeneralObserver<br />I notify my subscribers :<br />Im X observer and got : 22<br />I got all of them Here is my values 22 and 33<br />The X observer doesnt want to know anything about us remove him<br />I notify my subscribers :<br />I got all of them Here is my values 22 and 33<br /></span><br />Well i think it became pretty dynamic ,i have lots of ideas where i can use that pattern. As a conclusion what i can say is that "Head First Design patterns" book is changing my vision to OO programming.<br /><br />TO BE CONTINUED ...<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com0tag:blogger.com,1999:blog-1996014854708848605.post-26554301132602107302008-11-20T06:11:00.000-08:002008-11-20T06:19:03.923-08:00Got My Amazon Books<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://ecx.images-amazon.com/images/I/41ETT7KQRRL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA240_SH20_OU01_.jpg"><img style="float:right; margin:0 0 10px 10px;cursor:pointer; cursor:hand;width: 240px; height: 240px;" src="http://ecx.images-amazon.com/images/I/41ETT7KQRRL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA240_SH20_OU01_.jpg" border="0" alt="" /></a><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://oreilly.com/catalog/covers/9780596007126_cat.gif"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 180px; height: 208px;" src="http://oreilly.com/catalog/covers/9780596007126_cat.gif" border="0" alt="" /></a><br /><br />Really excited, i just got my amazon orders two great books. I hope to be better programmer now with those pearls :) The books arrived two weeks later from the estimated date but who cares i got em. One of them is about design patterns and other is about algorithms and programming. Here are the title's of books :<br /><br />1.Programming Pearls <br />2. Head First Design Patterns<br /><br />After finish em will write my reviews :)<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com1tag:blogger.com,1999:blog-1996014854708848605.post-88739666756181732872008-10-29T04:33:00.000-07:002008-10-29T04:46:16.491-07:00Recursive fibonacci Case StudyIn that post i'm going show u a test that i made about recursive calls in Python compared to other languages. The tests used are Python,Python C extension and Java. I always heard that using recursive calls in Python is not very wise, that time i decided to discover it myself. Well i run the 3 programs to find finacci 40. number and got their times with time command in F9. Tests do not claim anything so do not bother me with garbage :) Here they are :<br /><br />Python<br />[makkalot@localhost pyalgorithm]$ time python py_fibo.py 40<br />The result is : 102334155<br /><br />real 6m32.110s<br />user 6m22.153s<br />sys 0m0.519s<br /><br /><br /><br />Python_Ex :<br />[makkalot@localhost fibo]$ time python ex_fibo.py rec 40<br />The recursive result is : 102334155<br /><br />real 0m3.039s<br />user 0m2.902s<br />sys 0m0.017s<br /><br /><br /><br />Java<br />[makkalot@localhost bin]$ time java sorts.FiboMan rec 40<br />The recursive result is 102334155<br /><br />real 0m4.053s<br />user 0m2.755s<br />sys 0m0.095s<br /><br /><span style="font-weight:bold;">Conclusion :</span> The results suprised me a little bit. I knew that Python as a recursive language is slow but the time above is a disaster. Therefore i know now if i need sth recursive in Python i will write it as a C extension. The java also suprised me because its time was very close to C extension. Those Java guys are getting better, maybe JavaFX may make to become a Java programmer :) <br /><br />Btw, here are the codes :<br />Python : <a href="http://github.com/makkalot/pyalgorithm/tree/master/py_fibo.py">Link</a><br />C extension : <a href="http://github.com/makkalot/pyalgorithm/tree/master/ext_algo<br />/fibo/fibo.c">Link</a><br />Java : <a href="http://github.com/makkalot/pyalgorithm/tree/master/jalgorithm/algoman/src/sorts/FiboMan.java">Link</a><div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com0tag:blogger.com,1999:blog-1996014854708848605.post-49108192653976999192008-10-29T04:13:00.000-07:002008-10-29T04:23:06.954-07:00A new Django Site Added to publicIn last 2 weeks i've been working as freelancer, searching for people around the world who need Django/Python help for their projects. It is kind of exciting because i meet new people and do different kind of work everytime. Well one of these was injazzat.com a static firm site which was converted to Django site. The guy who was responsible for that site hired me to fix some of bugzz in site. It seemed as an easy stuff there was a few typos some template errors. However when i went deeper into code saw that hardcoded parts that made that job a nightmare :) I spent all my week to fix that mess and now most of the parts are real dynamic and the site follows the DRY :) At the end of work the guy told me that i was the 4th person who tried to fix that site and succeed finally. All previous developers escaped or dissappeared ... The temporary link of site is <a href="http://injazzat.clients.kuwaitnet.net/eng/">here</a> ,i dont know if it will be there tomorrow but as reference just put it here. I hope got more exciting projects in the future ...<br /><br />To conclude , do not write hardcoded programs because someday they will be seen by other people believe me :)<br /><br />Note : and also thanks to adk for his help with those designer and css stuff<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com1tag:blogger.com,1999:blog-1996014854708848605.post-3506543134138883172008-10-29T03:46:00.000-07:002008-10-29T04:11:15.276-07:00Make Python Smoke<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgaxezmnDQMFa0ufnHZhfrY8QeMehz-A50ksX-aQAuLNUKEwylSKlJZqqquYqft4oujL236klCllOQ5SAEE0IEppU9ZqxkyB-iViK9YFG4yO_tFsgFc_HuhZczOcwvciCx1t_BA0ynQcV0/s1600-h/dreamstime_2622679.jpg"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 213px; height: 320px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgaxezmnDQMFa0ufnHZhfrY8QeMehz-A50ksX-aQAuLNUKEwylSKlJZqqquYqft4oujL236klCllOQ5SAEE0IEppU9ZqxkyB-iViK9YFG4yO_tFsgFc_HuhZczOcwvciCx1t_BA0ynQcV0/s320/dreamstime_2622679.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5262531676764064114" /></a><br />In that post i'm going to talk i little bit (not too much ) about some old computer problems. ı have always been excited bout that kind of problems some of most famous ones are : <br /><br /> * Producers-consumers problem<br /> * Dining philosophers problem<br /> * Sleeping barber problem<br /> * Readers-writers problem<br /><br />All of them has a main purpose to make computer to solve the problem by using threads/processes and semaphores/locks. When think about those problems always my head is going to explode (hey i'm not so clever) and that is the fun part :) I had OS lessons in university but never had chance to write the implementation of these problems (what a practical school). Therefore i decided to write some of these problems in Python so i can explode my head and have fun with Python. One that i have written is Cigarette smokers problem [1] and put its code on my public git repo so other folks can grab it and play with it. The code is here : <a href="http://github.com/makkalot/pyalgorithm/tree/master/cigarette.py">Link</a><br /><br />To run the code just python cigarrette.py and watch how people smoke and die :) If you discover some deadlock or any other fatal mistake please drop a comment to light me .Have fun!<br /><br />Resources :<br />[1]Wikipedia : http://en.wikipedia.org/wiki/Cigarette_smokers_problem<br />[2]My public git repo : http://github.com/makkalot/pyalgorithm/<div class="blogger-post-footer"><script type="text/javascript"><!--
google_ad_client = "pub-3714662699497744";
//footer_reklama
google_ad_slot = "4175878507";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>Anonymoushttp://www.blogger.com/profile/01410618392237707082noreply@blogger.com0