Tag: python

Dissecting Python List allocator (CPython)

List is one of the most frequently used data structure in Python applications. The code for Python list object allocator is present in Python-2.7.5/Objects/listobject.c. Python allocates list objects in heap and uses underlying malloc based APIs. List is a container of Python object references. In plain C terminology. […]

memory_profiler: NameError: name ‘profile’ is not defined

I was experimenting with Python’s memory_profiler module and suddenly started getting following error: $ python -m memory_profiler memProf.py Traceback (most recent call last):  File “/usr/lib/python2.7/runpy.py”, line 162, in _run_module_as_main    “__main__”, fname, loader, pkg_name)  File “/usr/lib/python2.7/runpy.py”, line 72, in _run_code    exec code in run_globals  File “/usr/local/lib/python2.7/dist-packages/memory_profiler.py”, line 14, in […]

Python Internals: Understanding Python data model (I)

Python sees everything as object. Every object has an identity, value and a type. Object identity and type are invariable. Object type determines if value is mutable or otherwise. Lifetime of object is based on reference count mechanism. Object Container Object containers are: list, dictionary, tuple, set. Containers […]

Parenthesize an expression in Python

    def pref(op):         print “called with op”, op         ret = -1         if op == ‘+’:             print “matched +”             ret = 1         if op == ‘-‘:             print “matched -”            […]

Pylucene- Part II: Searching index

In the last post, we discussed how to create an index over a directory. Now, let’s search our index. from lucene import \             QueryParser, IndexSearcher, IndexReader, StandardAnalyzer, \         TermPositionVector, SimpleFSDirectory, File, MoreLikeThis, \             VERSION, initVM, Version import sys FIELD_CONTENTS = “contents” FIELD_PATH = “path” QUERY_STRING […]

Pylucene- Part I: Creating index

How to write a simple index generator with pylucene 1 import lucene 2 3 if __name__ == ‘__main__’: 4 INDEX_DIR = “/home/kanaujia/lucene_index” 5 6 # Initialize lucene and JVM 7 lucene.initVM() 8 9 print “lucene version is:”, lucene.VERSION 10 11 # Get the analyzer 12 analyzer = lucene.StandardAnalyzer(lucene.Version.LUCENE_CURRENT) […]