22 Ekim 2014 Çarşamba

Enlivepy A Different Approach to Html Templating in Python

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.
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.
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.
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:
  • 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.
  • Sometimes HTML/CSS developers break the logic in html pages by replacing something by mistake.
  • 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.
  • 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.
Read the rest of the article here.

7 Ekim 2014 Salı

Django Authentication Workflow

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.
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 blog

9 Kasım 2012 Cuma

Python ve defaultdict kullanımı

Python ve defaultdict kullanımı ile alakalı küçük bir blog yazısı : Link

29 Ağustos 2010 Pazar

Posix Öğrenelim - Ders 0

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 ...

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 github'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.

İ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. LINK

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ı ( fonksyonları ile alakalı): LINK

Tüm yazılan alıştırmalar bir Makefile'a sahip ve derlenmeleri o şekilde yapılacaktır.

11 Nisan 2010 Pazar

So Long, and Thanks for All the Fish

Bu 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 ...

6 Mart 2010 Cumartesi

Cache Sistemleri

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 ...

  1. Cache Sistemleri
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.
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
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 :
- 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.
- Yavaş belleğin hangi bölümünün tutulacağını belirlemek de önemlidir. Buna cache startejisi denir.
- Cache'in read only veya r/w olması performansı etkiler.

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.


2.Cache Stretejileri

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.
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.
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.
Fakat bu nasıl tespit eidlir ?
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 :

  • LFU (Least frequently used):
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.

  • LRU (Least recently used)
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.

  • Hibrit Modeller
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.

  • MRU (Most recently used)
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.

  • MFU (Most frequently used)
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.

3.Cache sistemlerinin kullanılmasına yönelik tipik yöntemler

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.
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.
Yeni win sistemlerinde default durum read only cache sistemidir. Örn linux sistemlerinde r/w kullanılır.
Bugün kullandığımız pc'lerde de donanımsal düzeyde bellek sistemi için cache kullanılmaktadır.
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.

5 Mart 2010 Cuma

New Theme And New Posts

Hi 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) ADK 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 ?

- 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.
- I worked for a while as Django/Python freelancer
- Now working as full time developer for Inomera A.Ş. on Netmera Social Platform.
- While writing that post i'm reading my C notes for the course i started.

Well thats, all nothing exciting in my life sorry :) I hope to post here more geeky stuff as i find some free time.

16 Ağustos 2009 Pazar

Gsoc 2009 is almost over !


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 Func that year :

  • Facts Framework - a different way to query more things on minions at once
  • Groups Api - new pluggable func groups api
  • Grep Modules - Query modules for a proper pattern
  • Real Time Output - Watch your minions output while they do their jobs.
  • Progress Report - Get progress report of your minions easily.
  • And many other small fixes and additions.
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.

30 Nisan 2009 Perşembe

Using GWT with Django


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.

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.

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...

  1. First create the server side code in Django :LINK
  2. You need to build a GET request in you JAVA code so u do that from here: LINK
  3. Parse the incoming XML string from Django and convert it to JAVA objects: LINK
  4. 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. : LINK
  5. Put GWT application in your media and put the generated HTML file in your templates directory.

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
an easy way to explain it to my designer :)

[1]: GWT-Django app

21 Nisan 2009 Salı

GSOC 2009 Again


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 :
  • Implementing Func Facts : 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.
  • Implementing a Pluggable Connection Manager for Func :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.
  • Structuring the Results : 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.
  • Funcweb Development : Func has a Web interface fo doing all the stuff visually,so new features should be added to that cool app.

I hope to finish all the stuff succesfully again like last year. (INSH ;))

17 Mart 2009 Salı

Ajaxian Even Calendar on Django

That 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.

Features are:
- Showing every month with events
- Small and big(excel like) versions
- Adding same event for a couple of days
- Adding lots of events on same day
- Facebox display of events
- Dual language arabic/english


Demo small : Link (on the left)
Demo Big : Link
Source : Link
Static directory : Link

For instalation look at README file in the source.
Have FUN and thanks to ADK for his design help ...

11 Şubat 2009 Çarşamba

Open Source Motivation

Just read the article of Lior Kaplan which is about the things that make us to use or develop opensource projects. Link:

I think these are mine reasons about doing that ;)

- Help other people that may have the same problem as you, so you make your code public or help via ML,IRC and etc.
- Opportunity to learn new things and finding areas to apply them.
- Coding about what you like,the topics you are interested.
- Having the feeling that more can do it better than single, so copy everything that is usable for you and continue from there (DRY).
- Becoming expert in area you like without thinking about it.
- Having a good CV(background) about things you have done, talks is cheap i'm showing the code.
- Having the opportunity to start own business without paying anything, you just need the idea
- Working together than alone is more fun.

Btw, i think people who are payed to work on opensource projects are really lucky :) Hope to be one of them ...

29 Ocak 2009 Perşembe

I 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 ...

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 :)

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.

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 :)

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 :)

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 :)

[1] http://github.com/makkalot/pysign/tree/master

20 Ocak 2009 Salı

Django Admin ManyToMAny Behaviour

In 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


class SchoolSection(models.Model):
#snip

class GalleryItem(models.Model):
school_section = models.ManyToManyField(SchoolSection)
#snip



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 :


sc=SchoolSection.objects.all()
gi = GalleryItem.objects.filter(school_section = sc[0])


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 :


class GalleryItemAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
super(GalleryItemAdmin, self).save_model(request, obj, form, change)
form.save_m2m()
obj.save()



That is all, hope to help someone who has the same problem :)

4 Ocak 2009 Pazar

import makkalot

Hello planet and thanks for adding me.

4 Aralık 2008 Perşembe

Command pattern in Python


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 :


class FileOperator:

def operation1(self):
pass

def operation2(self):
pass

#snip snip snip ....

Well that implementation seems enough for now, but what if we want to supply a simple api to users like that


f = FileOperator()
f.process_command()


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:

Here is my implementation of the Command Pattern : LINK

After that implementation you will able to do things like that :

def test_me_yo():
"""
Lets see that one
"""
tmp_file = open("some.txt","w")
tmp_file.write("hehehe\n")
tmp_file.close()

#end of initial data
#first lets read the contents of the stuff
fe = FileEditor()
fileman = FileMan("some.txt")
fread = FileReadCommand(fileman)
fwrite=FileWriteCommand(fileman)
fe.set_command(fread)
fe.process_command()

#append some info to the end of it
fe.set_command(fwrite)
fe.process_command("add some text here")
fe.set_command(fread)
fe.process_command()

#lets now go and create an command manager
cm = CommanManager()
cm.add_commanders(fwrite,fread)
fe.set_command(cm)

fe.process_command("\n Add another sting here ")
print "First undo :"
fe.undo_process()
print "Second undo :"
fe.undo_process()
print "Thirrd undo :"
fe.undo_process()

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 ...

2 Aralık 2008 Salı

Template Design Pattern


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 :


class MainClass(object):

def operation1(self):
#do stuff

def operation2(self):
#do stuff

def __template_method(self):
self.operation1()
self.operation2()



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:

class MainClass(object):

def operation1(self):
#do stuff

def operation2(self):
#do stuff

def hook(self):
pass

def do_you_want_hook(self):
return True

def __template_method(self):
self.operation1()
self.operation2()
if self.do_you_want_hook():
hook()


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).
That is all for the template pattern, we know you,you are not new here :)

1 Aralık 2008 Pazartesi

Factory Pattern in Python


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

if linux:
do sth
else:
do another


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 ...
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: LINK

To test the code you need sth like that :

from pyalgorithm.dp.factory import *

def test_facto():
my_value = "Some_strrrrrrrrr"
factory = ValidatorFactory()
validator = factory.create(my_value)
print validator.validate()

my_value = 11
validator = factory.create(my_value)
print validator.validate()

my_value = 11.12
validator = factory.create(my_value)
if not validator:
print "No object created ..."

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.

29 Kasım 2008 Cumartesi

Singleton Pattern in Python

The 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) :)

Here is the first version i found and think is good : LINK
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 :


from threading import *
import time
import thread

class ThreadSingleton(Thread):
"""
A simple thread to work on that
"""
def __init__(self,id):
Thread.__init__(self)
self.id = id

def run(self):
"""
Here initialize the singleton
"""
self.s=Singleton()

print "Im thread %d and %d"%(self.id,id(self.s))
time.sleep(2)

def get_singleton(self):
return id(self.s)



Here is the code that tests it :

def singleton(t="threaded"):
for j in range(100):
threads = []
for i in range(10):
if t!="threaded":
threads.append(ThreadSingleton(i))
else:
threads.append(RealThreadedSingleton(i))

for i in threads:
i.start()

for i in threads:
i.join()

print "The final ids are :"
t0 = threads[0]

for i in threads:
assert t0.get_singleton() == i.get_singleton()

t0.s.destroy()


if j%10 == 0:
print "************** %d COMPLETED **********"%j



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 LINK :

To test that new verison i nedded some new ThreadClass to call the new created Singleton :


class RealThreadedSingleton(ThreadSingleton):

def run(self):
"""
Here initialize the singleton
"""
self.s=ThSingleton()

print "Im thread %d and %d"%(self.id,id(self.s))
time.sleep(2)



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.

28 Kasım 2008 Cuma

Decorator Pattern in Python


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 :
1. You have a normal reader
2. You need a reader that converts the input to lowercase, so what you do is create a new UpperReader
3. You need now a reader that converts input to a sha1 sum , what you do is create a Sha1Reader
4. You need a reader that converts input to uppercase sha1sum , what you do is create a Sha1UpperReader
5. The 4. item can be removed if you used the decorator pattern

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.

Here is the implementation i wrote in Python and put on my personal github repo : LINK

A sample usage of above can be like that :

  1. def deco():

  2. from pyalgorithm.dp.decorator import *



  3. print "The normal file reader content"

  4. n = NormalFileReader("some.txt")

  5. print n.read_content()



  6. print "Sha1 sum of the read info"

  7. sh =DecoratoSha1Reader(

  8. NormalFileReader("some.txt")

  9. )

  10. print sh.read_content()



  11. print "Sha1 sum but with upper cases"

  12. up = DecoratoUpperFileReader(

  13. DecoratoSha1Reader(

  14. NormalFileReader("some.txt")

  15. )

  16. )



  17. print up.read_content()



The output is sth like that:

The normal file reader content
Hey im the content!!!

Sha1 sum of the read info
5fd77bd8e4f80ec432c71a09033a7ef51de60448
Sha1 sum but with upper cases
5FD77BD8E4F80EC432C71A09033A7EF51DE60448


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 :)