<?xml version="1.0"?>
<rss version="2.0"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:dcterms="http://purl.org/dc/terms/" >
<channel>
<title>rss</title>
<link>http://karamatli.com/rss/</link>
<description>Ertug Karamatli</description>
<item>
	
	<title>Architecture and Software Design</title>
	
	<guid>http://karamatli.com/stream/Architecture_and_Software_Design/</guid>
	<link>http://karamatli.com/stream/Architecture_and_Software_Design/</link>
	
	<pubDate>Sat, 09 Apr 2011 11:25:22 +0300</pubDate>
	<dcterms:modified>2011-04-10T08:46:46Z</dcterms:modified>
	
	<description><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Christopher_Alexander">Christopher Alexander</a> is an influential architect. His work on <a href="http://en.wikipedia.org/wiki/Pattern_language">pattern languages</a> and design has a notable impact on development of <a href="http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29">design patterns</a>. He has made a presentation at OOPSLA in 1996: <a href="http://www.patternlanguage.com/archive/ieee/ieeetext.htm">The Origins of Pattern Theory, the Future of the Theory, And The Generation of a Living World</a>.</p>

<h2>Is there a connection?</h2>

<p>First of all, he is not quite sure whether there is a strong connection or not. In architecture, he says, there is a profound moral component which is we are not completely aware of in our profession as software engineers. In my opinion, software engineering is also have a moral component or it <em>should</em> have. Maybe it is not much emphasized as in architecture due to the fact that we love to concentrate on the technical side as it is mostly a technically challenging area. But after all, what we are building in software engineering is for humans, for a better living, for improving the quality of life. If there is a human factor, it boils down to one thing: morality, just as Christopher Alexander says.</p>

<h2>Design Patterns</h2>

<p>He also questions whether patterns in software is the same as the patterns he discovered in architecture. He says software patterns is a subset of pattern languages. He sees software patterns as a tool to facilitate the communication between software engineers. He stated three essential features that exist in pattern languages; moral component, creating coherence, and generativeness.</p>

<h3>Moral component</h3>

<p>He emphasises the vitality of moral component in pattern languages and the absence of it in software engineering. I think it is true that using patterns in software engineering does not directly improves the quality of life. But using patterns will contribute to building better software, even it is just better in a technical way. Also, maybe it will make the programmer feel better because the code will have a better structure or it will have <em>life</em> in it. It will, in turn, help powering the quality of life.</p>

<h3>Creating coherence</h3>

<p>He mentions a very important aspect of pattern languages. One of the aims of pattern languages is that creating coherence when patterns used together. This means, all the entities created using the pattern language should be in a way that creating harmony.
On the other hand, if we look at the design patterns, there is no guarantee that what we will end up with is a coherent software. Software patterns seem to be more independent than patterns in architecture.</p>

<h3>Generativeness</h3>

<p>This is a larger and deeper concept than patterns. He defines generativity as a process that is capable of leading to a good structure. He says patterns should be generative thus allowing the process to produce an entity which is coherent and consistent with the context. I don't know exactly if we have this in software engineering but I think we should be after that. This is a very nice concept to have: a process or a path that leads to a good software.</p>
]]></description>
	
</item>
<item>
	
	<title>Streaming The Consciousness</title>
	
	<guid>http://karamatli.com/stream/Streaming_The_Consciousness/</guid>
	<link>http://karamatli.com/stream/Streaming_The_Consciousness/</link>
	
	<pubDate>Sun, 13 Mar 2011 13:48:19 +0200</pubDate>
	<dcterms:modified>2011-03-13T18:13:13Z</dcterms:modified>
	
	<description><![CDATA[<p>I am just starting to experiment a different kind of writing. It has always been hard for me to write a complete article for my web site, maybe I am too <a href="http://en.wikipedia.org/wiki/Perfectionism_%28psychology%29">perfectionist</a> or just doesn't have the necessary amount of time. While the drafts are piling up, I noticed that I generally just want to materilize my thoughts running through my head rather than a complete article. Hoping that it can provide me a level of <a href="http://en.wikipedia.org/wiki/Traceability">traceability</a>.</p>

<p>For more about Stream of consciousness at <a href="http://en.wikipedia.org/wiki/Stream_of_consciousness_%28psychology%29">Wikipedia</a>.</p>

<p>BTW, <a href="http://en.wikipedia.org/wiki/Stream_of_Consciousness_%28instrumental%29">Stream of Consciousness</a> by <a href="http://en.wikipedia.org/wiki/Dream_Theater">Dream Theater</a> is a great song.</p>
]]></description>
	
</item>
<item>
	
	<title>Modern Botnets A Survey and Future Directions</title>
	
	<guid>http://karamatli.com/pages/papers/Modern_Botnets_A_Survey_and_Future_Directions/</guid>
	<link>http://karamatli.com/pages/papers/Modern_Botnets_A_Survey_and_Future_Directions/</link>
	
	<pubDate>Sun, 09 Jan 2011 23:47:44 +0200</pubDate>
	<dcterms:modified>2011-01-09T21:47:44Z</dcterms:modified>
	
	<description><![CDATA[<p>Modern Botnets: A Survey and Future Directions</p>

<p>Ertuğ Karamatlı</p>

<p>For Directed Studies I course at Bogazici University, Turkey</p>

<h2>Abstract</h2>

<p>Botnets are becoming a real challenge. New botnets are discovered everyday that employs more sophisticated methods to spread, defend, and attack. This article explores the techniques used by traditional botnets, current botnets, and also emerging threats by future botnets. Current botnets incorporate distributed network architectures and cryptography increasingly. Moreover, we present new techniques such as the use of strong cryptography, scripting languages, anonymity networks, embedded systems, steganography, and IPv6 tunneling.</p>

<p>Keywords: Botnets; P2P; Cryptography; Network Security; Information Security</p>

<p><a href="http://docs.karamatli.com/paper/karamatli-modern_botnets.pdf">Download (PDF)</a></p>
]]></description>
	
</item>
<item>
	
	<title>Cyclomatic Complexity in Software Maintenance</title>
	
	<guid>http://karamatli.com/pages/papers/Cyclomatic_Complexity_in_Software_Maintenance/</guid>
	<link>http://karamatli.com/pages/papers/Cyclomatic_Complexity_in_Software_Maintenance/</link>
	
	<pubDate>Sun, 09 Jan 2011 23:44:25 +0200</pubDate>
	<dcterms:modified>2011-01-09T21:50:19Z</dcterms:modified>
	
	<description><![CDATA[<p>Cyclomatic Complexity in Software Maintenance</p>

<p>Ertuğ Karamatlı, Supervised by Jean-Marc Desharnais</p>

<p>For Software Measures II course at Bogazici University, Turkey</p>

<h2>Abstract</h2>

<p>Increasing complexity is one of the difficulties that make software maintenance harder over time. This article discusses <span class="createlink">McCabe</span>’s cyclomatic complexity then explores the ways of incorporating cyclomatic complexity in software maintenance and also how to take actions against increasing complexity.</p>

<p>Keywords: <span class="createlink">McCabe</span>, Cyclomatic Complexity, Software Measurement, Software Maintenance, Software Engineering</p>

<p><a href="http://docs.karamatli.com/paper/karamatli-cyclomatic_complexity_in_maintenance.pdf">Download (PDF)</a></p>
]]></description>
	
</item>
<item>
	
	<title>How Popular Tech Companies Use Search Engines for Headhunting</title>
	
	<guid>http://karamatli.com/pages/analysis/How_Popular_Tech_Companies_Use_Search_Engines_for_Headhunting/</guid>
	<link>http://karamatli.com/pages/analysis/How_Popular_Tech_Companies_Use_Search_Engines_for_Headhunting/</link>
	
	<pubDate>Tue, 06 Jul 2010 21:04:13 +0300</pubDate>
	<dcterms:modified>2010-11-06T12:03:26Z</dcterms:modified>
	
	<description><![CDATA[<p>I have been seeing some interesting search queries on Google Analytics for a long time but didn't have time to dig deeper. Recently, I went through my Apache logs and realized that they were recruiters from tech companies accessing my <a href="http://karamatli.com/cv.html">CV</a>.</p>

<p>Here is the command I used to find the search queries:</p>

<pre><code>gunzip -c other_vhosts_access.log.* | grep cv\.html | grep "google\|bing\|search\|job\|project" | ccze -A
</code></pre>

<p>Then, I used whois command on IPs from interesting queries to locate where the queries are originating from.</p>

<p>Search strings are grouped by company name. Search query, search engine, OS, and browser combinations used are listed below.</p>

<h2>Google</h2>

<pre><code>(inurl:CV | inurl:Vitae | inurl:Resume | intitle:CV | intitle:Vitae | intitle:Resume) AND Computer.Science AND ("Opensource" OR "Open Source") AND (C++ OR Java OR Python) AND (Linux OR Ubuntu OR kernel) -apply -jobs
</code></pre>

<ul>
<li>Search Engine: Google</li>
<li>OS: Macintosh</li>
<li>Browser: Chrome</li>
</ul>

<p>&nbsp;</p>

<pre><code>(Python and c++) ("computer science"|CS) ((inurl:~resume) | (inurl:~vitae) | (intitle:~resume) | (intitle:~vitae)) -job -jobs -Ph.D
</code></pre>

<ul>
<li>Search Engine: Google</li>
<li>OS: Windows</li>
<li>Browser: Firefox</li>
</ul>

<h2>Microsoft</h2>

<pre><code>prototyping algorithms games cv resume
</code></pre>

<ul>
<li>Search Engine: Bing</li>
<li>OS: Windows</li>
<li>Browser: Internet Explorer</li>
</ul>

<h2>Amazon</h2>

<pre><code>resume 2010 (ca OR wa OR or) java -coffee c++ developer -job (unix OR linux) enterprise distributed (425 OR 415 OR 310 OR 206 OR 503 OR 509)
</code></pre>

<ul>
<li>Search Engine: Bing</li>
<li>OS: Windows</li>
<li>Browser: Firefox</li>
</ul>

<h2>Unknown origin but interesting</h2>

<pre><code>-inurl:dir -inurl:sample -inurl:samples -inurl:cvsamples -inurl:example -inurl:examples  scheduling (project OR management) (DoD OR security OR clearance)
</code></pre>

<ul>
<li>Search Engine: Recruiting.com (uses Google)</li>
<li>OS: Windows</li>
<li>Browser: Internet Explorer</li>
</ul>

<p>&nbsp;</p>

<pre><code>(intitle:resume OR intitle:cv OR intitle:vitae OR inurl:resume OR inurl:cv OR inurl:vitae)  "programming competition" javascript
</code></pre>

<ul>
<li>Search Engine: Google</li>
<li>OS: Windows</li>
<li>Browser: Firefox</li>
</ul>

<h2>Conclusions</h2>

<p>Google and Amazon used complicated and detailed queries while Microsoft preferred a simpler search string. Furthermore, there isn't much surprise in OS and browser choices. Google used its own browser Chrome and Microsoft used its own search engine Bing. Of course, these are based on my observations for last few months, they may use many different queries or browsers, I can't know for sure.</p>

<p>However, there is something very interesting about the usage of search engines for recruiting. Job seekers may apply <abbr title="Search Engine Optimization">SEO</abbr> techniques to their CVs/Resumes. Preferably, optimizing for the company they want to work for, according to the search queries these companies use. <strong>R</strong>SEO or <strong>CV</strong>SEO might become popular in the near future.</p>

<h2>Further Reading</h2>

<ul>
<li><a href="http://www.slideshare.net/Amitai_Givertz/searching-for-candidates-on-google-a-primer-part-1">Searching for Candidates On Google: A Primer</a></li>
<li><a href="http://www.therecruiterslounge.com/2008/09/13/google-search-strings-for-recruiters-looking-for-work/">Google Search Strings for Recruiters Looking For Work</a></li>
<li><a href="http://www.dakotta.com/articles/52-google-recruitment-101.html">Google Recruitment 101</a></li>
<li><a href="http://www.recruitingtrends.com/is-linkedin-actively-preventing-recruiters-from-searching-profiles-via-google">Is <span class="createlink">LinkedIn</span> Actively Preventing Recruiters from Searching Profiles via Google?</a></li>
<li><a href="http://www.recruiting.com/">Recruiting.com</a></li>
<li><a href="http://en.wikipedia.org/wiki/Search_engine_optimization">Search engine optimization on Wikipedia</a></li>
<li><a href="http://karamatli.com/pages/papers/Resume_Information_Extraction_with_Named_Entity_Clustering_based_on_Relationships/">Resume Information Extraction with Named Entity Clustering based on Relationships</a></li>
</ul>

<hr id="comments_line" />

<div id="disqus_thread"></div>

<script type="text/javascript">
    var disqus_identifier = "popular_tech_companies_search_engines_headhunting";
    var disqus_url = "http://karamatli.com/pages/analysis/How_Popular_Tech_Companies_Use_Search_Engines_for_Headhunting/";
  (function() {
   var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
   dsq.src = 'http://ertugkaramatli.disqus.com/embed.js';
   (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>

<noscript>Please enable <span class="createlink">JavaScript</span> to view the <a href="http://disqus.com/?ref_noscript=ertugkaramatli">comments powered by Disqus.</a></noscript>

<p><a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a></p>
]]></description>
	
</item>
<item>
	
	<title>Common Python Patterns</title>
	
	<guid>http://karamatli.com/pages/lang/Common_Python_Patterns/</guid>
	<link>http://karamatli.com/pages/lang/Common_Python_Patterns/</link>
	
	<pubDate>Wed, 30 Jun 2010 23:31:22 +0300</pubDate>
	<dcterms:modified>2010-11-06T12:03:26Z</dcterms:modified>
	
	<description><![CDATA[<p>Here are some tricks, hacks and patterns I like using most while coding Python. I will try to add more when I come across them.</p>

<h2>Basic One-Liners</h2>

<pre><code>print [item*2 for item in [1, 2, 3]]
# prints: [2, 4, 6]

cond = True
print 'yes' if cond else 'no'
# prints: yes
</code></pre>

<h2>Dynamic Function Arguments</h2>

<pre><code>def somefunc(*args, **kwargs):
    print 'args: %s' % str(args)
    print 'kwargs: %s' % kwargs

somefunc(1, 2, thirdarg=3)
# prints:
# args: (1, 2)
# kwargs: {'thirdarg': 3}
</code></pre>

<h2>Pseudo Methods</h2>

<pre><code>class &lt;span class="createlink"&gt;SomeClass&lt;/span&gt;(object):
    def __getattr__(self, fname):
        def wrapper(*args, **kwargs):
            print fname, args, kwargs
        return wrapper

someobj = &lt;span class="createlink"&gt;SomeClass&lt;/span&gt;()
someobj.somemethod(1, 2, 3)
# prints: somemethod (1, 2, 3) {}
</code></pre>

<h2>Dynamic Imports</h2>

<pre><code>def get_module(s):
    module = __import__(s)
    for i in s.split(".")[1:]:
        module = getattr(module, i)
    return module

module3 = get_module('module1.module2.module3')
</code></pre>

<h2>Object-Dict Hybrid</h2>

<pre><code>class &lt;span class="createlink"&gt;HybridObj&lt;/span&gt;(object):
    def __getitem__(self, name):
        return self.__dict__[name]

    def __setitem__(self, name, value):
        self.__dict__[name] = value

    def set_all(self, dict):
        self.__dict__ = dict

    def get_all(self):
        return self.__dict__

obj = &lt;span class="createlink"&gt;HybridObj&lt;/span&gt;()
obj['mykey'] = 'myval'

print obj.mykey
# prints: myval

print obj.get_all()
# prints: {'mykey': 'myval'}
</code></pre>

<h2>Error Recovery Pattern</h2>

<pre><code>app = App()

while True:
    try:
        app.main()
    except Exception:
        import StringIO, traceback
        output = StringIO.StringIO()
        traceback.print_exc(file=output)
        print output.getvalue()
        time.sleep(10)
</code></pre>

<h2>Type Checking</h2>

<pre><code>obj = 3.14

if isinstance(obj, basestring):
    type = 'string'
elif isinstance(obj, (int, float)):
    type = 'number'
else:
    type = obj.__class__

print type
# prints: number
</code></pre>

<h2>Decorators</h2>

<pre><code>def base_amount(value):
    def decorator(func):
        def wrapper(*args):
            a, b = args
            a += value
            return func(a, b)
        return wrapper
    return decorator

@base_amount(5)
def add(a, b):
    return a + b

print add(3, 4)
# prints: 12
</code></pre>

<h2>Slice Notation</h2>

<pre><code>a = [1, 2, 3, 4, 5]

#copy a list
print a[:]
# prints: [1, 2, 3, 4, 5]

#copy in reverse
print a[::-1]
# prints: [5, 4, 3, 2, 1]

print a[1:-1]
# prints: [2, 3, 4]

print a[-2:]
# prints: [4, 5]

print a[:-2]
# prints: [1, 2, 3]
</code></pre>

<h2>Functional style</h2>

<pre><code>map((lambda x: x*2), [1, 2, 3])
#prints: [2, 4, 6]

reduce(lambda x, y: x+y, [1, 2, 3])
#prints: 6

filter((lambda x: x&gt;2), range(6))
#prints: [3, 4, 5]
</code></pre>

<h2>Sorting according to a key</h2>

<pre><code>student_tuples = [
    ('john', 'A', 15),
    ('jane', 'B', 12),
    ('dave', 'B', 10),
]

print sorted(student_tuples, key=itemgetter(2), reverse=True)
# prints: [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]

# taken from: http://wiki.python.org/moin/HowTo/Sorting/
</code></pre>

<h2>Getting items from a dictionary according to a pattern</h2>

<pre><code>items = {
    'name': lambda x: x['user']['name'],
    'post_count': lambda x: x['posts']['count'],
}
dct = {
    'user': {
        'name': 'Someone',
    },
    'posts': {
        'count': 5,
    },
}

new_dct = {}
for n, fn in items.iteritems():
    new_dct[n] = fn(dct)

print new_dct
# prints: {'post_count': 5, 'name': 'Someone'}
</code></pre>

<h2>Global Variables in Threads</h2>

<pre><code>import threading
global = threading.local()
global.somevar = 3
</code></pre>

<h2>Multi-value return</h2>

<pre><code>def somefunc():
    return val1, val2

val1, val2 = somefunc()
</code></pre>

<h2>Checking if a value exists in a list</h2>

<pre><code>if 'somestring' in ['this', 'list', 'of', 'strings']:
    print 'yes!'
</code></pre>

<h2>Removing duplicate items in a list</h2>

<pre><code>print set(['a', 'b', 'a', 'c'])
# prints: ['a', 'b', 'c']
</code></pre>

<h2>Raising Meaningful Errors</h2>

<pre><code>raise &lt;span class="createlink"&gt;TypeError&lt;/span&gt;('Some error about type %s' % (t,))
raise &lt;span class="createlink"&gt;AttributeError&lt;/span&gt;('Some error about attr %s' % (a,))
raise &lt;span class="createlink"&gt;ValueError&lt;/span&gt;('Some error about val %s' % (v,))
</code></pre>

<h2>Updates</h2>

<p>I have got <a href="http://www.reddit.com/r/Python/comments/ckom9/common_python_patterns/">some comments</a> on Python reddit. I have fixed the errors, they are now working. I should have checked them, thanks for pointing them out!</p>

<hr id="comments_line" />

<div id="disqus_thread"></div>

<script type="text/javascript">
    var disqus_identifier = "common_python_patterns";
    var disqus_url = "http://karamatli.com/pages/lang/Common_Python_Patterns/";
  (function() {
   var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
   dsq.src = 'http://ertugkaramatli.disqus.com/embed.js';
   (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>

<noscript>Please enable <span class="createlink">JavaScript</span> to view the <a href="http://disqus.com/?ref_noscript=ertugkaramatli">comments powered by Disqus.</a></noscript>

<p><a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a></p>
]]></description>
	
</item>
<item>
	
	<title>Resume Information Extraction with Named Entity Clustering based on Relationships</title>
	
	<guid>http://karamatli.com/pages/papers/Resume_Information_Extraction_with_Named_Entity_Clustering_based_on_Relationships/</guid>
	<link>http://karamatli.com/pages/papers/Resume_Information_Extraction_with_Named_Entity_Clustering_based_on_Relationships/</link>
	
	<pubDate>Sun, 27 Jun 2010 21:36:52 +0300</pubDate>
	<dcterms:modified>2011-01-09T21:41:57Z</dcterms:modified>
	
	<description><![CDATA[<p>Resume Information Extraction with Named Entity Clustering based on Relationships</p>

<p>Ertuğ Karamatlı and Selim Akyokuş</p>

<p>In <a href="http://www.inista.org/">INISTA 2010</a>: Proceedings of International Symposium on INnovations in Intelligent <span class="createlink">SysTems</span> and Applications, Kayseri, Turkey, June 2010</p>

<h2>Abstract</h2>

<p>This paper presents an effective method for resume information extraction. The information extraction process consists of 4 phases. In the first step, a resume is segmented into blocks according to their information types. In the second step, named entities are found by using special chunkers for each information type. In the third step, found named entities are clustered according to their distance in text and information type. In the fourth step, normalization methods are applied to the text.</p>

<p><a href="http://docs.karamatli.com/paper/inista-resuner.pdf">Download (PDF)</a></p>
]]></description>
	
</item>
<item>
	
	<title>UserEntropy</title>
	
	<guid>http://karamatli.com/pages/projects/UserEntropy/</guid>
	<link>http://karamatli.com/pages/projects/UserEntropy/</link>
	
	<pubDate>Sun, 27 Jun 2010 16:51:34 +0300</pubDate>
	<dcterms:modified>2010-11-06T12:03:26Z</dcterms:modified>
	
	<description><![CDATA[<p><a href="http://karamatli.com/rss/../pages/projects/UserEntropy/">UserEntropy</a> is a distributed entropy collector from user actions on web pages. This method is to create an unpredictable random number generator that offers a good approximation of a True Random Number Generator. The source of unpredictability is mostly the human-computer interaction on web pages. It is implemented using <span class="createlink">JavaScript</span>, Python and web.py.</p>

<p>It collects user actions using a <span class="createlink">JavaScript</span> code that is placed in pages and hashes the collected data before sending to server periodically. Then your application requests generated random hashes from the server when needed.</p>

<p>User data utilized for now:</p>

<ul>
<li><em>Constant parameters:</em> User agent, platform, screen width, screen height, screen color depth, referrer, current URL, and cookies.</li>
<li><em>Dynamic parameters:</em> <span class="createlink">JavaScript</span> random function, current time, mouse moves, key presses, and key press timings.</li>
</ul>

<p>All these parameters are hashed with SHA-256 before being sent to server, so there shouldn't be any privacy issue for end-users.</p>

<p>Server chooses 2 hashes received from clients randomly and hashes again with a secret key (salt) before sending to application.</p>

<p>I inspired by the <a href="http://www.entropykey.co.uk/">USB True Random Number Generator</a> that is <a href="http://news.ycombinator.com/item?id=1453299">posted to HN</a>.</p>

<h2>Try</h2>

<p>My site (karamatli.com) runs <a href="http://karamatli.com/rss/../pages/projects/UserEntropy/">UserEntropy</a> since 06/27/2010. You can have a look at generated numbers <a href="http://karamatli.com:8081/">here</a>:</p>

<iframe src="http://karamatli.com:8081/" style="background:gray;width:300px;height:50px;"></iframe>

<h2>Download</h2>

<p>You can download it in <a href="http://github.com/ertug/userentropy/zipball/master">zip</a> or <a href="http://github.com/ertug/userentropy/tarball/master">tar</a> format.</p>

<p>Get the source using Git:</p>

<pre><code>&#036; git clone git://github.com/ertug/userentropy.git
</code></pre>

<p><a href="http://github.com/ertug/userentropy">UserEntropy on <span class="createlink">GitHub</span></a></p>

<h2>How to contribute?</h2>

<p><a href="http://github.com/ertug/userentropy">Fork it on <span class="createlink">GitHub</span></a>. Have a look at <a href="http://github.com/ertug/userentropy/blob/master/TODO">TODO</a>.</p>

<h2>Authors</h2>

<p>Ertug Karamatli (<a href="http://karamatli.com/rss/&#x6D;&#x61;&#105;&#108;&#x74;&#x6F;:&#x65;&#x72;&#116;&#117;&#x67;&#64;k&#97;&#114;&#x61;&#x6D;a&#x74;&#108;&#x69;&#46;c&#x6F;&#109;">&#x65;&#x72;&#116;&#117;&#x67;&#64;k&#97;&#114;&#x61;&#x6D;a&#x74;&#108;&#x69;&#46;c&#x6F;&#109;</a>)</p>

<h2>References</h2>

<ul>
<li>Wikipedia, <a href="http://en.wikipedia.org/wiki/Entropy_%28computing%29">Entropy</a></li>
<li>Wikipedia, <a href="http://en.wikipedia.org/wiki/SHA-2">SHA-2</a></li>
<li><a href="http://www.computer.org/portal/web/csdl/doi/10.1109/SYNASC.2008.25">Data Flow Entropy Collector</a></li>
<li><a href="http://webpy.org/">web.py</a></li>
</ul>
]]></description>
	
</item>
<item>
	
	<title>Pools in Python</title>
	
	<guid>http://karamatli.com/pages/lang/Pools_in_Python/</guid>
	<link>http://karamatli.com/pages/lang/Pools_in_Python/</link>
	
	<pubDate>Sat, 12 Jun 2010 22:00:14 +0300</pubDate>
	<dcterms:modified>2010-11-06T12:03:26Z</dcterms:modified>
	
	<description><![CDATA[<p><a href="http://karamatli.com/rss/../images/pool_jogjakarta.jpg"><img src="http://karamatli.com/rss/../pages/lang/Pools_in_Python/400x-pool_jogjakarta.jpg" width="400" height="273" class="floatright img" /></a></p>

<p>Object pooling is pre-allocating a number of objects to use later. It is done by creating a group of objects (pool) initially and when you need an object, you take one from the pool rather than creating it. When you are done with the object you send it back to the pool. It effectively means reusing of objects. This allows bypassing the object creation overhead and reducing the work of garbage collector. Thus, managing the memory yourself. This is, of course, should be used with care because if you really want to manage the memory, why use a high-level language like Python?</p>

<p>Despite the fact that many programs don't need object pooling, this technique can speed things up, especially in situations explained below.</p>

<h2>When to use Object Pooling?</h2>

<p>There are two main reasons to use object pooling:</p>

<ul>
<li>You need to create and destroy many objects quickly (i.e. limited lifetime of objects)</li>
<li>Creating many objects that takes time to be initiated (i.e. class constructor does many things)</li>
</ul>

<p>It is an optimization so it shouldn't be applied until there is a proved bottleneck. It will increase the overall complexity of your program and probably present additional bugs. Also, when you create many objects at start, your program may allocate too much memory throughout its lifetime. As a result, you should carefully analyze the trade-off here.</p>

<h2>Sample Implementation</h2>

<p>The harbor example is from Paul Graham's book ANSI Common Lisp. Here are two classes, one is using object pooling while the other is not.</p>

<pre><code>class Ship():
    def __init__(self):
        self.name = None
        self.flag = None
        self.tons = None

class &lt;span class="createlink"&gt;HarborWithPooling&lt;/span&gt;():
    def __init__(self, count):
        self.pool = [Ship() for i in range(count)]
        self.harbor = {}

    def enter(self, name, flag, tons):
        ship = self.pool.pop()
        ship.name = name
        ship.flag = flag
        ship.tons = tons
        self.harbor[name] = ship

    def find_ship(self, name):
        return self.harbor[name]

    def leave(self, name):
        ship = self.harbor[name]
        self.pool.append(ship)
        del self.harbor[name]

class &lt;span class="createlink"&gt;HarborWithoutPooling&lt;/span&gt;():
    def __init__(self, count):
        self.harbor = {}

    def enter(self, name, flag, tons):
        ship = Ship()
        ship.name = name
        ship.flag = flag
        ship.tons = tons
        self.harbor[name] = ship

    def find_ship(self, name):
        return self.harbor[name]

    def leave(self, name):
        ship = self.harbor[name]
        del self.harbor[name]
</code></pre>

<h2>Benchmark</h2>

<p>Its execution time is measured while <i>enter</i> and <i>leave</i> methods are called 100,000 times in both classes. The test machine has an Intel Core 2 Duo 1.80GHz CPU and it is running Linux. Tested using Python 2.5.4. If you have time to test it on another version or OS, please let me know. You can download the complete source code <a href="http://karamatli.com/files/pypooltest.py">here</a>.</p>

<p><img src="http://chart.apis.google.com/chart?cht=bvg&amp;chs=320x200&amp;chd=t:459,246|952,224&amp;chco=4D89F9,C6D9FD&amp;chf=bg,s,000000&amp;chdl=With Object Pooling|Without Object Pooling&amp;chbh=25,5,15&amp;chxt=x,y&amp;chxl=0:|Enter|Leave&amp;chxr=1,0,1000&amp;chds=0,1000" class="floatright"/></p>

<table style="width: 360px;">
<tr>
<td></td><td>Enter</td><td>Leave</td><td>Total</td>
</tr>
<tr>
<td>With Object Pooling</td><td>459 ms</td><td>246 ms</td><td>705 ms</td>
</tr>
<tr>
<td>Without Object Pooling</td><td>952 ms</td><td>224 ms</td><td>1176 ms</td>
</tr>
<tr>
<td>Speed Up</td><td style="color:green;">%107</td><td style="color:red;">-%9</td><td style="color:green;">%66</td>
</tr>
</table>

<p>There is about %66 overall speed increase when using object pooling.</p>

<h2>Conclusion</h2>

<p>Using object pooling in Python increases total object creation and destruction rate more than two-fold. When used appropriately, it will help developing fast Python programs. Particularly, the programs that needs to be fast such as games.</p>

<h2>Further Reading</h2>

<ul>
<li>Wikipedia, <a href="http://en.wikipedia.org/wiki/Pool_%28Computer_science%29">Pool</a></li>
<li>Brandon Corfman, <a href="http://www.devpicayune.com/entry/object-pooling-in-python">Object pooling in Python</a></li>
<li>Paul Graham, ANSI Common Lisp, 1996, pp. 226-227</li>
<li>Brian Goetz, <a href="http://www.ibm.com/developerworks/java/library/j-jtp09275.html">Java theory and practice: Urban performance legends, revisited</a></li>
</ul>

<p><i>Pool image from: <a href="http://www.flickr.com/photos/stuckincustoms/3176365000/">Stuck In Customs</a></i></p>

<hr id="comments_line" />

<div id="disqus_thread"></div>

<script type="text/javascript">
    var disqus_identifier = "pools<em>in</em>python";
    var disqus_url = "http://karamatli.com/pages/lang/Pools_in_Python/";
  (function() {
   var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
   dsq.src = 'http://ertugkaramatli.disqus.com/embed.js';
   (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>

<noscript>Please enable <span class="createlink">JavaScript</span> to view the <a href="http://disqus.com/?ref_noscript=ertugkaramatli">comments powered by Disqus.</a></noscript>

<p><a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a></p>
]]></description>
	
</item>
<item>
	
	<title>Referential Magic in Python</title>
	
	<guid>http://karamatli.com/pages/lang/Referential_Magic_in_Python/</guid>
	<link>http://karamatli.com/pages/lang/Referential_Magic_in_Python/</link>
	
	<pubDate>Sun, 16 May 2010 09:08:31 +0300</pubDate>
	<dcterms:modified>2010-11-06T12:03:26Z</dcterms:modified>
	
	<description><![CDATA[<p><img src="http://karamatli.com/rss/../images/python-logo-notext.png" width="300" height="298" class="floatright img" /></p>

<p>Did you ever find yourself struggling to find out why an assignment is not working as you expected or a variable is being modified <strong>magically</strong>?</p>

<p>You should have heard of call-by-value and call-by-reference. These are evaluation strategies that many popular programming languages use. An evaluation strategy is a set of rules that defines how expressions are evaluated in a programming language.</p>

<p>Python uses a different evaluation strategy named call-by-sharing (or call-by-object). Actually call-by-sharing term isn't used much, there is inconsistency across sources. For example, Java programmers say Java is pass-by-value, on the other hand, Ruby programmers say Ruby is pass-by-reference, even though both have the same semantics. Call-by-sharing is generally not fully understood by beginner Python programmers. You can do many useful things without knowing much about call-by-sharing but you should know it to use the full power of Python.</p>

<p>Assignment always updates the reference and don't change the previous referenced object.</p>

<pre><code>var = 3
var = 5
</code></pre>

<p>First assignment will create a variable named "var" in the local context then create an int object that has a value of 3. Finally it updates the reference of var to point to the new int object.
Doing one more assignment will create a new int object again and updates the reference of var to point to this object. So it won't do anything directly to the previous object that has a value of 3. Additionally, if there isn't any reference to the old object like in this example, it will be garbage collected.</p>

<pre><code>class A():
    pass

a = A()
a.var = 3
a.var = 5
</code></pre>

<p>Doing an assignment operation on an object variable will behave the same as previous example. Reference of "var" will <strong>change</strong> but reference of "a" <strong>won't be affected</strong> by the assignment.</p>

<p>There is actually two cases that makes it behave like call-by-value or call-by-reference. I will try to explain these cases.</p>

<h2>Mimicking call-by-value</h2>

<p>Call-by-value means the variable that you pass to a function will not change the original variable, instead use a copy. So, we need to create copies of variables.</p>

<p>To create a copy you can use the slice syntax if the object in question is iterable:</p>

<pre><code>copy_of_var = var[:]
</code></pre>

<p>Or if it is a builtin:</p>

<pre><code>copy_of_var1 = list(var1)
copy_of_var2 = dict(var2)
copy_of_var3 = set(var3)
</code></pre>

<p>Or as a last resort you can use the <a href="http://docs.python.org/library/copy.html">copy module</a>.</p>

<p>You can create a copy of a variable before passing to a function or before returning a value from a function. Below are some examples.</p>

<p>Copy before passing:</p>

<pre><code>def append_one(lst):
    lst.append(1)
    return lst

lst = [2, 3]
append_one(list(lst)) # here!
</code></pre>

<p>Copy before returning:</p>

<pre><code>def append_one(lst):
    lst = list(lst) # here!
    lst.append(1)
    return lst

lst = [2, 3]
append_one(lst)
</code></pre>

<p>I think copying inside the function is more intuitive than the other but I don't see any side effect of using the other.</p>

<h2>Mimicking call-by-reference</h2>

<p>Call-by-reference means a function can modify the original variable that is passed from the context where the function call occurred. Changing items in an object does not change the object itself (object should mutable, of course). So if we wrap a variable inside a list, for example, it will be accessible both from the context of the function call and inside the function.</p>

<pre><code>def add_one(n):
    n[0] += 1

n = [3]
add_one(n)
print n[0] # prints 4
</code></pre>

<p>Also, some of the builtin functions work "in place":</p>

<pre><code>def sort_list(lst):
    lst.sort()

lst = [7, 3, 9]
sort_list(lst)
print lst # prints [3, 7, 9]
</code></pre>

<h2>Further Reading</h2>

<ul>
<li>Wikipedia, <a href="http://en.wikipedia.org/wiki/Evaluation_strategy">Evaluation strategy</a></li>
<li>Stack Overflow, <a href="http://stackoverflow.com/questions/986006/python-how-do-i-pass-a-variable-by-reference">Python: How do I pass a variable by reference?</a></li>
<li>Fredrik Lundh, <a href="http://effbot.org/zone/python-objects.htm">Python Objects</a></li>
<li>Fredrik Lundh, <a href="http://effbot.org/zone/call-by-object.htm">Call By Object</a></li>
</ul>

<h3>Updates</h3>

<ul>
<li>1 (16 May 2010): Fixed code samples to demonstrate mutable objects instead of immutable objects that doesn't reflect the idea correctly.</li>
<li>2 (16 May 2010): Added more information about call-by-sharing and an example to demonstrate how assignments work.</li>
</ul>

<hr id="comments_line" />

<div id="disqus_thread"></div>

<script type="text/javascript">
    var disqus_identifier = "referencial<em>magic</em>in_python";
    var disqus_url = "http://karamatli.com/pages/lang/Referencial_Magic_in_Python/";
  (function() {
   var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
   dsq.src = 'http://ertugkaramatli.disqus.com/embed.js';
   (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>

<noscript>Please enable <span class="createlink">JavaScript</span> to view the <a href="http://disqus.com/?ref_noscript=ertugkaramatli">comments powered by Disqus.</a></noscript>

<p><a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a></p>
]]></description>
	
</item>

</channel>
</rss>
