<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Python/Django Solr Lucene ubuntu linux developer</title>
	<atom:link href="http://aamirhussain.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://aamirhussain.com</link>
	<description>An another open-source ,python,django,solr,twilio developer&#039;s blog</description>
	<lastBuildDate>Thu, 02 May 2013 18:00:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='aamirhussain.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/a3d632cb1a0e269e14e9d8465a43fe70?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Python/Django Solr Lucene ubuntu linux developer</title>
		<link>http://aamirhussain.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://aamirhussain.com/osd.xml" title="Python/Django Solr Lucene ubuntu linux developer" />
	<atom:link rel='hub' href='http://aamirhussain.com/?pushpress=hub'/>
		<item>
		<title>Best User File Management using math.fmod</title>
		<link>http://aamirhussain.com/2013/04/12/best-user-file-management-using-math-fmod/</link>
		<comments>http://aamirhussain.com/2013/04/12/best-user-file-management-using-math-fmod/#comments</comments>
		<pubDate>Fri, 12 Apr 2013 09:38:48 +0000</pubDate>
		<dc:creator>Aamir Hussain</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology/Info]]></category>
		<category><![CDATA[file management]]></category>
		<category><![CDATA[file storing architecture]]></category>
		<category><![CDATA[file upload architecture]]></category>
		<category><![CDATA[fmod]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[resume management]]></category>
		<category><![CDATA[upload file management]]></category>

		<guid isPermaLink="false">http://aamirhussain.com/?p=309</guid>
		<description><![CDATA[Introduction Where is the best place to store images on your Web site? If you have a huge content application and &#8230;<p><a href="http://aamirhussain.com/2013/04/12/best-user-file-management-using-math-fmod/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=309&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h3 id="introduction">Introduction</h3>
<p>Where is the best place to store images on your Web site? If you have a huge content application and store all your images in a single folder called &#8220;images&#8221;.you might land up you application in <strong>&#8220;BLack Hole&#8221;</strong> situation..</p>
<p>Storing all the images of the application in just one folder have many problems like.</p>
<ol>
<li><strong><span style="font-style:inherit;line-height:1.625;">The &#8220;black hole&#8221;.</span></strong></li>
<li><strong><span style="font-style:inherit;line-height:1.625;">Name conflict.</span></strong></li>
<li><strong><span style="font-style:inherit;line-height:1.625;">Access control.</span></strong></li>
<li><strong><span style="font-style:inherit;line-height:1.625;">Scalability.</span></strong></li>
<li><strong><span style="font-style:inherit;line-height:1.625;">Performance.</span></strong></li>
</ol>
<p><em><strong>The Black Hole:</strong></em></p>
<p>&#8220;Black hole&#8221; is an appropriate name for the  folder where all the files are stored , because files go into this folder but never come out. Over time, more and more new files are added to the &#8220;Black Hole&#8221; folder. However, when users are deleted files that are no longer useful are left in the folder. Why? The primary reason is because files stored in the  folder may be  shared resource and it&#8217;s hard to tell if a given file is being used or not . So the &#8220;Black Hole&#8221; folder is left unmanaged and becomes a stale collection of any file that has ever been used on the application.</p>
<p><em><strong>Name Conflict:</strong></em></p>
<p><em><strong></strong></em>One of the first problems content management encounters is file name conflict, when a user uploads a files with a file name that already exists in the repository.Most of the management systems append &#8216;_1&#8242; , &#8216;_2&#8242; to the file name to remove the conflicts but that looks very ugly for the system as well as users.The more file you have in a single folder, the greater the chances that a name conflict will occur.</p>
<p><em><strong>Access control:</strong></em></p>
<p>Your management team may control access to the  documents/files .Using Singe folder to store all the files will cause the access control conflicts. Content authors are permitted to delete, rename or replace images in that folder. Content creators can manipulate images used by documents that they don&#8217;t have access to, and can even change the branding of the application.</p>
<p><em><strong>Scalability:</strong></em></p>
<p>Most application provide an file library interface that either displays a list box or thumbnails of files in the library. If the  folder contains hundreds or thousands of files , it becomes impractical for applications to present these images in this way to content authors and for content authors to find the appropriate files.</p>
<p><em><strong>Performance:</strong></em></p>
<p><em><strong></strong></em><span style="font-style:inherit;line-height:1.625;">File systems don&#8217;t perform well when a large number of files are stored in a single folder. The file management library may open slowly for users, and if the  library displays metadata about file such as dimensions, file size or date last modified, this slows performance even further. Large  libraries also require more data to be transmitted over the network, which can be resource intensive and slow. </span></p>
<h3 id="better-way">A better way to manage Files</h3>
<p>In this article, we will limit the discussion to storing images on the file system, although this approach can be adapted to storing images in the database as well.</p>
<p>The solution to the problems created by using a single folder can be addressed by creating a different folder on the file system for each User/Object. The folder name should be the ID of the User/Object in the <abbr>application</abbr> and all the files used by a given User/Object should be stored in this folder. Although these folders can be created anywhere on your site, the examples in this article will assume all these folders are sub-folders stored in a folder called &#8220;User_Files&#8221; of the root of the application.</p>
<p>As discussed previously, file systems don&#8217;t perform well when a large number of files are stored in a single folder. The same is true for sub-folders. The solution is therefore to create grouping folders that will significantly reduce the number of immediate sub-folders in any given folder.</p>
<p>If your system uses sequentially numbered User/Object <abbr>ID</abbr>s, then you can use a <strong>math.fmod(x, y)</strong> arithmetic operation to create the name of the grouping folders. (<strong>x</strong> is your User/Objects ID and <strong>Y</strong> is your max folder limit number lets say <strong>999</strong>). <strong>fmod</strong> operations return the remainder from a division of two numbers. So to get 999 grouping folders, simply <strong>fmod</strong> the User/Object <abbr>ID</abbr> by 999, which will produce numbers from 0 to 999, which you can use to name your grouping folders. The following illustrates how these folders will be nested:</p>
<ul>
<li><strong>User_Files</strong>
<ul>
<li>/1     #fmod(objectID,999)
<ul>
<li>/1000    #objectID
<ul>
<li>/my_resume.pdf   #FileName</li>
<li>/project_details.doc</li>
</ul>
</li>
<li>/1256
<ul>
<li>/NDA.pdf</li>
</ul>
</li>
</ul>
</li>
<li>/337
<ul>
<li>/2335
<ul>
<li>/company_policy.docx</li>
<li>/designs.psd</li>
</ul>
</li>
<li>/1257
<ul>
<li>/my_image.gif</li>
</ul>
</li>
<li>/1513</li>
</ul>
</li>
<li>/338
<ul>
<li>/2336
<ul>
<li>/logos.jpg</li>
<li>/product1.tiff</li>
<li>/HomePage.jpeg</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>&nbsp;</p>
<p><span style="color:#675f34;font-family:'Adobe Garamond Pro', Garamond, Palatino, 'Palatino Linotype', Times, 'Times New Roman', Georgia, serif;font-size:18px;font-variant:small-caps;font-weight:bold;line-height:1em;font-style:inherit;">Conclusion</span></p>
<p>Storing files  in separate folders solves many of the problems caused by using a single folder. This approach is simple and reliable and can  be used for attachments (documents like <abbr title="Portable Document Format">PDF</abbr>, Microsoft Word, Zip files, etc.). In fact, you can store both images and attachments in the same folder.</p>
<br /> Tagged: <a href='http://aamirhussain.com/tag/file-management/'>file management</a>, <a href='http://aamirhussain.com/tag/file-storing-architecture/'>file storing architecture</a>, <a href='http://aamirhussain.com/tag/file-upload-architecture/'>file upload architecture</a>, <a href='http://aamirhussain.com/tag/fmod/'>fmod</a>, <a href='http://aamirhussain.com/tag/math/'>math</a>, <a href='http://aamirhussain.com/tag/python/'>python</a>, <a href='http://aamirhussain.com/tag/resume-management/'>resume management</a>, <a href='http://aamirhussain.com/tag/upload-file-management/'>upload file management</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/amirzargar.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/amirzargar.wordpress.com/309/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=309&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aamirhussain.com/2013/04/12/best-user-file-management-using-math-fmod/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<georss:point>34.065015 74.825729</georss:point>
		<geo:lat>34.065015</geo:lat>
		<geo:long>74.825729</geo:long>
		<media:content url="http://0.gravatar.com/avatar/942a6b34f7b6115469ed71da97ba2d42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aamirhussain055</media:title>
		</media:content>
	</item>
		<item>
		<title>Using Solr/ Solr&#8217;s DataImportHandler (the DIH) and MySQL-DB</title>
		<link>http://aamirhussain.com/2012/12/04/using-solr-solrs-dataimporthandler-the-dih-and-mysql-db/</link>
		<comments>http://aamirhussain.com/2012/12/04/using-solr-solrs-dataimporthandler-the-dih-and-mysql-db/#comments</comments>
		<pubDate>Tue, 04 Dec 2012 08:09:36 +0000</pubDate>
		<dc:creator>Aamir Hussain</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology/Info]]></category>
		<category><![CDATA[dataimporthandler]]></category>
		<category><![CDATA[DIH]]></category>
		<category><![CDATA[lucene]]></category>
		<category><![CDATA[solr]]></category>

		<guid isPermaLink="false">http://aamirhussain.com/?p=295</guid>
		<description><![CDATA[Solr is a standalone webservice application that can be installed on any servlet container like tomcat, jetty etc.  It uses &#8230;<p><a href="http://aamirhussain.com/2012/12/04/using-solr-solrs-dataimporthandler-the-dih-and-mysql-db/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=295&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong>Solr</strong> is a standalone webservice application that can be installed on any servlet container like tomcat, jetty etc.  It uses the popular Lucene java library to provide enterprise level search results from databases, filesystem, web services etc.    Solr runs as a web service, so in effect it provides a cross platform search engine.  The results can be accessed from <strong>php</strong>, <strong>java</strong>, <strong>RoR</strong> or <strong>.NET</strong>,<strong>python</strong> by invoking its web service.  The only requirement on your server is that it should allow you to run java application or have deploy solr as a webapp in an existing servlet container such as tomcat or jetty.</p>
<p>This article shows how to get Solr index and provide search results for a simple MySQL table.  The search results will be provided in XML/JSON, so you can get your web application to read the results, parse it and display it any form desired.</p>
<p>For this article, we will use the solr 3.6.1 build (17-July-2012).You can download the  Solr from <a href="http://www.apache.org/dyn/closer.cgi/lucene/solr/">Download latest Solr Release</a>. As we are going to use Direct database and XML import through Solr&#8217;s <b>DataImportHandler </b>(the DIH).you also need to download the latest MySQL JDBC driver from <a href="http://dev.mysql.com/downloads/connector/j/" target="_blank">http://dev.mysql.com/downloads/connector/j/</a></p>
<h1 style="color:#274c9c;font-size:19px;">Install Solr</h1>
<ol>
<li>Unzip or untar the downloaded solr  build package. Assume the unzipped directory is solr.</li>
<li>cd into solr/example directory which has a standalone solr server running on jetty.</li>
<li>Execute the server by &#8220;j<b>ava -jar start.jar&#8221;</b></li>
<li>Test the server by accessing <a href="http://&lt;servername_or_ip&#038;gt" rel="nofollow">http://&lt;servername_or_ip&#038;gt</a>;:8983/solr/admin/ .  If you get an admin page with a search box, then your solr is running well, and ready.</li>
</ol>
<h1 style="color:#274c9c;font-size:19px;">Configure MySQL Database</h1>
<ol>
<li>Copy the downloaded mysql jdbc driver file into solr/lib directory.</li>
<li>Create a new xml file called data-import.xml , change the obvious variables to suit your DB.  In this example, I am indexing a example DB table called jos_content.</li>
</ol>
<pre style="color:#000000;background:#ffffff;"><span style="color:#004a43;">&lt;?</span><span style="color:#800000;font-weight:bold;">xml</span><span style="color:#074726;">version</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#7d0045;">1.0</span><span style="color:#0000e6;">"</span><span style="color:#074726;">encoding</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">UTF-8</span><span style="color:#0000e6;">"</span><span style="color:#004a43;">?&gt;</span>
<span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">dataConfig</span><span style="color:#a65700;">&gt;</span>
    <span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">dataSource</span> <span style="color:#274796;">type</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">JdbcDataSource</span><span style="color:#0000e6;">"</span>
                  <span style="color:#274796;">driver</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">com.mysql.jdbc.Driver</span><span style="color:#0000e6;">"</span>
                  <span style="color:#274796;">url</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">jdbc:mysql://localhost/database</span><span style="color:#0000e6;">"</span>
                  <span style="color:#274796;">user</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">user</span><span style="color:#0000e6;">"</span>
                  <span style="color:#274796;">password</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">password</span><span style="color:#0000e6;">"</span><span style="color:#a65700;">/&gt;</span>

    <span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">document</span> <span style="color:#274796;">name</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">doc</span><span style="color:#0000e6;">"</span><span style="color:#a65700;">&gt;</span>
        <span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">entity</span> <span style="color:#274796;">name</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">jos_content</span><span style="color:#0000e6;">"</span>
                  <span style="color:#274796;">query</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">select * from jos_content WHERE state=1</span><span style="color:#0000e6;">"</span>
                  <span style="color:#274796;">deltaImportQuery</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">SELECT * FROM `jos_content` WHERE id='${dataimporter.delta.job_jobs_id}'</span><span style="color:#0000e6;">"</span>
          <span style="color:#274796;">deltaQuery</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">SELECT id FROM `jos_content` WHERE modified &gt; '${dataimporter.last_index_time}'</span><span style="color:#0000e6;">"</span><span style="color:#a65700;">&gt;</span>

            <span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">field</span> <span style="color:#274796;">column</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">id</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">name</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">id</span><span style="color:#0000e6;">"</span> <span style="color:#a65700;">/&gt;</span>
            <span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">field</span> <span style="color:#274796;">column</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">title</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">name</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">title</span><span style="color:#0000e6;">"</span> <span style="color:#a65700;">/&gt;</span>
            <span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">field</span> <span style="color:#274796;">column</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">introtext</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">name</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">introtext</span><span style="color:#0000e6;">"</span> <span style="color:#a65700;">/&gt;</span>
            <span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">field</span> <span style="color:#274796;">column</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">fulltext</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">name</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">fulltext</span><span style="color:#0000e6;">"</span> <span style="color:#a65700;">/&gt;</span>
        <span style="color:#a65700;">&lt;/</span><span style="color:#5f5035;">entity</span><span style="color:#a65700;">&gt;</span>
    <span style="color:#a65700;">&lt;/</span><span style="color:#5f5035;">document</span><span style="color:#a65700;">&gt;</span>
<span style="color:#a65700;">&lt;/</span><span style="color:#5f5035;">dataConfig</span><span style="color:#a65700;">&gt;</span></pre>
<p>3.  Edit file solrconfig.xml which is located in solr/example/solr/conf directory. Add the following requestHandler entry if not already existing.</p>
<pre style="color:#000000;background:#ffffff;"><span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">requestHandler</span> <span style="color:#274796;">name</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">/dataimport</span><span style="color:#0000e6;">"</span><span style="color:#a65700;">&gt;</span>
    <span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">lst</span> <span style="color:#274796;">name</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">defaults</span><span style="color:#0000e6;">"</span><span style="color:#a65700;">&gt;</span>
      <span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">str</span> <span style="color:#274796;">name</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">config</span><span style="color:#0000e6;">"</span><span style="color:#a65700;">&gt;</span>/solr/data-config.xml<span style="color:#a65700;">&lt;/</span><span style="color:#5f5035;">str</span><span style="color:#a65700;">&gt;</span>
    <span style="color:#a65700;">&lt;/</span><span style="color:#5f5035;">lst</span><span style="color:#a65700;">&gt;</span>
  <span style="color:#a65700;">&lt;/</span><span style="color:#5f5035;">requestHandler</span><span style="color:#a65700;">&gt;

</span></pre>
<p>4. Now we will configure solr’s schema by editing schema.xml in solr/example/solr/conf directory. Add or edit the following fields as required. The xml format is self explanatory.</p>
<pre style="color:#000000;background:#ffffff;"><span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">fields</span><span style="color:#a65700;">&gt;</span>
        <span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">field</span> <span style="color:#274796;">name</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">id</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">type</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">string</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">indexed</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">true</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">stored</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">true</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">required</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">true</span><span style="color:#0000e6;">"</span><span style="color:#a65700;">/&gt;</span>
    <span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">field</span> <span style="color:#274796;">name</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">title</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">type</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">text</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">indexed</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">true</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">stored</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">true</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">required</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">true</span><span style="color:#0000e6;">"</span><span style="color:#a65700;">/&gt;</span>
    <span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">field</span> <span style="color:#274796;">name</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">introtext</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">type</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">text</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">indexed</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">true</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">stored</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">true</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">required</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">true</span><span style="color:#0000e6;">"</span><span style="color:#a65700;">/&gt;</span>

    <span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">field</span> <span style="color:#274796;">name</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">fulltext</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">type</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">text</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">indexed</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">true</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">stored</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">true</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">required</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">true</span><span style="color:#0000e6;">"</span><span style="color:#a65700;">/&gt;</span>
    <span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">dynamicField</span> <span style="color:#274796;">name</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">*</span><span style="color:#0000e6;">"</span> <span style="color:#274796;">type</span><span style="color:#808030;">=</span><span style="color:#0000e6;">"</span><span style="color:#0000e6;">ignored</span><span style="color:#0000e6;">"</span> <span style="color:#a65700;">/&gt;</span>
 <span style="color:#a65700;">&lt;/</span><span style="color:#5f5035;">fields</span><span style="color:#a65700;">&gt;</span>

 <span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">uniqueKey</span><span style="color:#a65700;">&gt;</span>id<span style="color:#a65700;">&lt;/</span><span style="color:#5f5035;">uniqueKey</span><span style="color:#a65700;">&gt;</span>
 <span style="color:#696969;">&lt;!--</span><span style="color:#696969;"> field for the QueryParser to use when an explicit fieldname is absent </span><span style="color:#696969;">--&gt;</span>
 <span style="color:#a65700;">&lt;</span><span style="color:#5f5035;">defaultSearchField</span><span style="color:#a65700;">&gt;</span>fulltext<span style="color:#a65700;">&lt;/</span><span style="color:#5f5035;">defaultSearchField</span><span style="color:#a65700;">&gt;</span></pre>
<p>5. Stop and restart solr instance. Check if there are any jdbc errors, this could happen if the jdbc drivers are not properly installed.</p>
<h1 style="color:#274c9c;font-size:19px;">Performing full or delta indexing</h1>
<p>If everything works correctly, you can get solr to fully index the configured tables by accessing the following command via your browser. <span style="color:#274c9c;"><a href="http://&lt;your_solr_server&#038;gt" rel="nofollow">http://&lt;your_solr_server&#038;gt</a>;:8983/solr/dataimport?command=full-import</span><br />
You can check the status of the command by accessing<br />
<span style="color:#274c9c;"><a href="http://&lt;your_solr_server&#038;gt" rel="nofollow">http://&lt;your_solr_server&#038;gt</a>;:8983/solr/dataimport</span><br />
If everything works correctly, you can now search for data from <span style="color:#274c9c;"><a href="http://&lt;your_solr_server&#038;gt" rel="nofollow">http://&lt;your_solr_server&#038;gt</a>;:8983/solr/admin/ </span>and you should now have data results in XML format.</p>
<p>To do an incremental or delta indexing of data since the last full or delta, increment, issue the command<br />
<span style="color:#274c9c;"><a href="http://&lt;your_solr_server&#038;gt" rel="nofollow">http://&lt;your_solr_server&#038;gt</a>;:8983/solr/dataimport?command=delta-import</span><br />
You can now access these xml results from your web application.  There are client api’s available for python,RoR, php, java etc.</p>
<h1 style="color:#274c9c;font-size:19px;">References</h1>
<p><a href="http://wiki.apache.org/solr/DataImportHandler#head-df246a3aed0bb38297f3449bc35a0bdf38a272b5">http://wiki.apache.org/solr/DataImportHandler#head-df246a3aed0bb38297f3449bc35a0bdf38a272b5</a></p>
<p><a href="http://lucene.apache.org/solr/tutorial.html">http://lucene.apache.org/solr/tutorial.html</a></p>
<br /> Tagged: <a href='http://aamirhussain.com/tag/dataimporthandler/'>dataimporthandler</a>, <a href='http://aamirhussain.com/tag/dih/'>DIH</a>, <a href='http://aamirhussain.com/tag/lucene/'>lucene</a>, <a href='http://aamirhussain.com/tag/solr/'>solr</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/amirzargar.wordpress.com/295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/amirzargar.wordpress.com/295/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=295&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aamirhussain.com/2012/12/04/using-solr-solrs-dataimporthandler-the-dih-and-mysql-db/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<georss:point>34.080971 74.803757</georss:point>
		<geo:lat>34.080971</geo:lat>
		<geo:long>74.803757</geo:long>
		<media:content url="http://0.gravatar.com/avatar/942a6b34f7b6115469ed71da97ba2d42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aamirhussain055</media:title>
		</media:content>
	</item>
		<item>
		<title>Python code to check either the card number provided is valid or not</title>
		<link>http://aamirhussain.com/2012/09/13/python-code-to-check-either-the-card-number-provided-is-valid-or-not/</link>
		<comments>http://aamirhussain.com/2012/09/13/python-code-to-check-either-the-card-number-provided-is-valid-or-not/#comments</comments>
		<pubDate>Thu, 13 Sep 2012 18:15:28 +0000</pubDate>
		<dc:creator>Aamir Hussain</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://aamirhussain.com/?p=281</guid>
		<description><![CDATA[Python code to check either the card number provided is valid or not can be used in validating the Credit-card &#8230;<p><a href="http://aamirhussain.com/2012/09/13/python-code-to-check-either-the-card-number-provided-is-valid-or-not/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=281&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h1 style="text-align:center;color:#339966;"><strong>Python code to check either the card number provided is valid or not</strong><br />
<strong> can be used in validating the Credit-card while dealing with cards</strong></h1>
<pre style="color:#000000;background:#ffffff;"><span style="color:#696969;"># coding=utf-8</span>

<span style="color:#800000;font-weight:bold;">class</span> Check_Credit_Card<span style="color:#808030;">(</span>object<span style="color:#808030;">)</span><span style="color:#808030;">:</span>
    <span style="color:#696969;">"""</span>
<span style="color:#696969;">    Class to check either the Credit card number is valid or not</span>
<span style="color:#696969;">    """</span>
    cc_patterns <span style="color:#808030;">=</span> <span style="color:#e34adc;">dict</span><span style="color:#808030;">(</span>amex<span style="color:#808030;">=</span><span style="color:#800080;">{</span><span style="color:#008c00;">15</span><span style="color:#808030;">:</span> <span style="color:#808030;">(</span><span style="color:#0000e6;">'34'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'37'</span><span style="color:#808030;">)</span><span style="color:#800080;">}</span><span style="color:#808030;">,</span>
        carteblanche<span style="color:#808030;">=</span><span style="color:#800080;">{</span><span style="color:#008c00;">14</span><span style="color:#808030;">:</span> <span style="color:#808030;">(</span><span style="color:#0000e6;">'300'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'301'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'302'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'303'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'304'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'305'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'36'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'38'</span><span style="color:#808030;">)</span><span style="color:#800080;">}</span><span style="color:#808030;">,</span>
        dinersclub<span style="color:#808030;">=</span><span style="color:#800080;">{</span><span style="color:#008c00;">14</span><span style="color:#808030;">:</span> <span style="color:#808030;">(</span><span style="color:#0000e6;">'300'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'301'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'302'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'303'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'304'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'305'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'36'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'38'</span><span style="color:#808030;">)</span><span style="color:#800080;">}</span><span style="color:#808030;">,</span> discover<span style="color:#808030;">=</span><span style="color:#800080;">{</span><span style="color:#008c00;">16</span><span style="color:#808030;">:</span> <span style="color:#808030;">(</span><span style="color:#0000e6;">'6011'</span><span style="color:#808030;">)</span><span style="color:#800080;">}</span><span style="color:#808030;">,</span>
        enroute<span style="color:#808030;">=</span><span style="color:#800080;">{</span><span style="color:#808030;">(</span><span style="color:#008c00;">15</span><span style="color:#808030;">)</span><span style="color:#808030;">:</span> <span style="color:#808030;">(</span><span style="color:#0000e6;">'2014'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'2149'</span><span style="color:#808030;">)</span><span style="color:#800080;">}</span><span style="color:#808030;">,</span> jcb<span style="color:#808030;">=</span><span style="color:#800080;">{</span><span style="color:#008c00;">15</span><span style="color:#808030;">:</span> <span style="color:#808030;">(</span><span style="color:#0000e6;">'2131'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'1800'</span><span style="color:#808030;">)</span><span style="color:#808030;">,</span> <span style="color:#808030;">(</span><span style="color:#008c00;">16</span><span style="color:#808030;">)</span><span style="color:#808030;">:</span> <span style="color:#808030;">(</span><span style="color:#0000e6;">'3'</span><span style="color:#808030;">)</span><span style="color:#800080;">}</span><span style="color:#808030;">,</span>
        mastercard<span style="color:#808030;">=</span><span style="color:#800080;">{</span><span style="color:#808030;">(</span><span style="color:#008c00;">16</span><span style="color:#808030;">)</span><span style="color:#808030;">:</span> <span style="color:#808030;">(</span><span style="color:#0000e6;">'51'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'52'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'53'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'54'</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'55'</span><span style="color:#808030;">)</span><span style="color:#800080;">}</span><span style="color:#808030;">,</span> visa<span style="color:#808030;">=</span><span style="color:#800080;">{</span><span style="color:#808030;">(</span><span style="color:#008c00;">13</span><span style="color:#808030;">,</span> <span style="color:#008c00;">16</span><span style="color:#808030;">)</span><span style="color:#808030;">:</span> <span style="color:#808030;">(</span><span style="color:#0000e6;">'4'</span><span style="color:#808030;">)</span><span style="color:#800080;">}</span><span style="color:#808030;">)</span>

    <span style="color:#800000;font-weight:bold;">def</span> check<span style="color:#808030;">(</span>self<span style="color:#808030;">,</span>cc_num<span style="color:#808030;">,</span>cc_type<span style="color:#808030;">)</span><span style="color:#808030;">:</span>
        <span style="color:#696969;">"""</span>
<span style="color:#696969;">        Main function to check the card</span>
<span style="color:#696969;">        """</span>
        cc_type <span style="color:#808030;">=</span> cc_type<span style="color:#808030;">.</span>strip<span style="color:#808030;">(</span><span style="color:#808030;">)</span><span style="color:#808030;">.</span>lower<span style="color:#808030;">(</span><span style="color:#808030;">)</span><span style="color:#808030;">.</span>replace<span style="color:#808030;">(</span><span style="color:#0000e6;">' '</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">''</span><span style="color:#808030;">)</span>
        cc_num <span style="color:#808030;">=</span> <span style="color:#e34adc;">str</span><span style="color:#808030;">(</span>cc_num<span style="color:#808030;">)</span>

        <span style="color:#800000;font-weight:bold;">if</span> cc_type <span style="color:#800000;font-weight:bold;">in</span> self<span style="color:#808030;">.</span>cc_patterns<span style="color:#808030;">.</span>keys<span style="color:#808030;">(</span><span style="color:#808030;">)</span><span style="color:#808030;">:</span>
            <span style="color:#696969;"># Perform length check</span>
            valid_lengths <span style="color:#808030;">=</span> self<span style="color:#808030;">.</span>cc_patterns<span style="color:#808030;">[</span>cc_type<span style="color:#808030;">]</span><span style="color:#808030;">.</span>keys<span style="color:#808030;">(</span><span style="color:#808030;">)</span>
            <span style="color:#800000;font-weight:bold;">if</span> <span style="color:#e34adc;">len</span><span style="color:#808030;">(</span>cc_num<span style="color:#808030;">)</span> <span style="color:#800000;font-weight:bold;">not</span> <span style="color:#800000;font-weight:bold;">in</span> valid_lengths<span style="color:#808030;">:</span>
                <span style="color:#800000;font-weight:bold;">return</span> <span style="color:#808030;">(</span><span style="color:#e34adc;">False</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'Invalid length (%s) for %s'</span> <span style="color:#808030;">%</span> \
                    <span style="color:#808030;">(</span><span style="color:#e34adc;">str</span><span style="color:#808030;">(</span>valid_lengths<span style="color:#808030;">)</span><span style="color:#808030;">.</span>strip<span style="color:#808030;">(</span><span style="color:#0000e6;">'[]'</span><span style="color:#808030;">)</span><span style="color:#808030;">,</span> cc_type<span style="color:#808030;">)</span><span style="color:#808030;">)</span>

            <span style="color:#696969;"># Perform prefix check</span>
            valid_prefixes <span style="color:#808030;">=</span> self<span style="color:#808030;">.</span>cc_patterns<span style="color:#808030;">[</span>cc_type<span style="color:#808030;">]</span><span style="color:#808030;">[</span><span style="color:#e34adc;">len</span><span style="color:#808030;">(</span>cc_num<span style="color:#808030;">)</span><span style="color:#808030;">]</span>
            prefix_match <span style="color:#808030;">=</span> <span style="color:#e34adc;">False</span>
            <span style="color:#800000;font-weight:bold;">for</span> prefix <span style="color:#800000;font-weight:bold;">in</span> valid_prefixes<span style="color:#808030;">:</span>
                prefix_length <span style="color:#808030;">=</span> <span style="color:#e34adc;">len</span><span style="color:#808030;">(</span>prefix<span style="color:#808030;">)</span>
                <span style="color:#800000;font-weight:bold;">if</span> cc_num<span style="color:#808030;">[</span><span style="color:#808030;">:</span>prefix_length<span style="color:#808030;">]</span> <span style="color:#808030;">=</span><span style="color:#808030;">=</span> prefix<span style="color:#808030;">:</span>
                    prefix_match <span style="color:#808030;">=</span> <span style="color:#e34adc;">True</span>
            <span style="color:#800000;font-weight:bold;">if</span> <span style="color:#800000;font-weight:bold;">not</span> prefix_match<span style="color:#808030;">:</span>
                <span style="color:#800000;font-weight:bold;">return</span> <span style="color:#808030;">(</span><span style="color:#e34adc;">False</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'Invalid prefix (not in %s)'</span> <span style="color:#808030;">%</span> \
                    <span style="color:#0000e6;">','</span><span style="color:#808030;">.</span>join<span style="color:#808030;">(</span>valid_prefixes<span style="color:#808030;">)</span><span style="color:#808030;">)</span>

            <span style="color:#696969;"># Perform mod 10 check</span>
            <span style="color:#800000;font-weight:bold;">if</span> cc_type <span style="color:#808030;">=</span><span style="color:#808030;">=</span> <span style="color:#0000e6;">'enroute'</span><span style="color:#808030;">:</span>
                <span style="color:#800000;font-weight:bold;">pass</span> <span style="color:#696969;"># Only cc_type that doesn't do a mod 10 check</span>
            <span style="color:#800000;font-weight:bold;">else</span><span style="color:#808030;">:</span>
                check_digits <span style="color:#808030;">=</span> <span style="color:#808030;">[</span><span style="color:#808030;">]</span>
                is_even <span style="color:#808030;">=</span> <span style="color:#e34adc;">False</span>
                <span style="color:#800000;font-weight:bold;">for</span> digit <span style="color:#800000;font-weight:bold;">in</span> cc_num<span style="color:#808030;">:</span>
                    <span style="color:#800000;font-weight:bold;">if</span> <span style="color:#800000;font-weight:bold;">not</span> is_even<span style="color:#808030;">:</span>
                        check_digits<span style="color:#808030;">.</span>append<span style="color:#808030;">(</span><span style="color:#e34adc;">int</span><span style="color:#808030;">(</span>digit<span style="color:#808030;">)</span><span style="color:#808030;">)</span>
                    <span style="color:#800000;font-weight:bold;">else</span><span style="color:#808030;">:</span>
                        mult_digits <span style="color:#808030;">=</span> <span style="color:#e34adc;">str</span><span style="color:#808030;">(</span><span style="color:#e34adc;">int</span><span style="color:#808030;">(</span>digit<span style="color:#808030;">)</span> <span style="color:#808030;">*</span> <span style="color:#008c00;">2</span><span style="color:#808030;">)</span>
                        check_digits<span style="color:#808030;">.</span>append<span style="color:#808030;">(</span>mult_digits<span style="color:#808030;">[</span><span style="color:#008c00;">0</span><span style="color:#808030;">]</span><span style="color:#808030;">)</span>
                        <span style="color:#800000;font-weight:bold;">if</span> <span style="color:#e34adc;">len</span><span style="color:#808030;">(</span>mult_digits <span style="color:#808030;">=</span><span style="color:#808030;">=</span> <span style="color:#008c00;">2</span><span style="color:#808030;">)</span><span style="color:#808030;">:</span>
                            check_digits<span style="color:#808030;">.</span>append<span style="color:#808030;">(</span><span style="color:#e34adc;">int</span><span style="color:#808030;">(</span>mult_digits<span style="color:#808030;">[</span><span style="color:#008c00;">1</span><span style="color:#808030;">]</span><span style="color:#808030;">)</span><span style="color:#808030;">)</span>

                check_sum <span style="color:#808030;">=</span> sum<span style="color:#808030;">(</span>check_digits<span style="color:#808030;">)</span>
                <span style="color:#800000;font-weight:bold;">if</span> check_sum <span style="color:#808030;">%</span> <span style="color:#008c00;">10</span> <span style="color:#808030;">!</span><span style="color:#808030;">=</span> <span style="color:#008c00;">0</span><span style="color:#808030;">:</span>
                    <span style="color:#800000;font-weight:bold;">return</span> <span style="color:#e34adc;">False</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'Invalid check sum (%d)'</span> <span style="color:#808030;">%</span> check_sum

            <span style="color:#800000;font-weight:bold;">return</span> <span style="color:#e34adc;">True</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">''</span>
        <span style="color:#800000;font-weight:bold;">else</span><span style="color:#808030;">:</span>
            <span style="color:#800000;font-weight:bold;">return</span> <span style="color:#e34adc;">False</span><span style="color:#808030;">,</span> <span style="color:#0000e6;">'Invalid card type (%s)'</span> <span style="color:#808030;">%</span> cc_type

<span style="color:#800000;font-weight:bold;">if</span> <span style="color:#e34adc;">__name__</span> <span style="color:#808030;">=</span><span style="color:#808030;">=</span> <span style="color:#0000e6;">"__main__"</span><span style="color:#808030;">: #For testing you can Use the file directly in python console..</span>
    x <span style="color:#808030;">=</span> Check_Credit_Card<span style="color:#808030;">(</span><span style="color:#808030;">)</span>
    x <span style="color:#808030;">=</span> x<span style="color:#808030;">.</span>check<span style="color:#808030;">(</span><span style="color:#0000e6;">'5235235879456682'</span><span style="color:#808030;">,</span><span style="color:#0000e6;">'MASTERCARD'</span><span style="color:#808030;">)</span>
    <span style="color:#800000;font-weight:bold;">print</span> x<span style="color:#808030;">[</span><span style="color:#008c00;">0</span><span style="color:#808030;">]

</span></pre>
<pre style="color:#000000;background:#ffffff;"><span style="color:#696969;">#VIEWS.PY</span>
<span style="color:#800000;font-weight:bold;">def</span> Check_Validity<span style="color:#808030;">(</span>request<span style="color:#808030;">)</span><span style="color:#808030;">:</span>
    <span style="color:#800000;font-weight:bold;">if</span> request<span style="color:#808030;">.</span>method <span style="color:#808030;">=</span><span style="color:#808030;">=</span> <span style="color:#0000e6;">'POST'</span><span style="color:#808030;">:</span>
        Data <span style="color:#808030;">=</span> request<span style="color:#808030;">.</span>POST<span style="color:#808030;">.</span>copy<span style="color:#808030;">(</span><span style="color:#808030;">)</span>
        cc_obj <span style="color:#808030;">=</span> Check_Credit_Card<span style="color:#808030;">(</span><span style="color:#808030;">)</span>
        result <span style="color:#808030;">=</span> cc_obj<span style="color:#808030;">.</span>check<span style="color:#808030;">(</span>Data<span style="color:#808030;">.</span>get<span style="color:#808030;">(</span><span style="color:#0000e6;">'card_no'</span><span style="color:#808030;">,</span><span style="color:#0000e6;">''</span><span style="color:#808030;">)</span><span style="color:#808030;">,</span>Data<span style="color:#808030;">.</span>get<span style="color:#808030;">(</span><span style="color:#0000e6;">'card_type'</span><span style="color:#808030;">,</span><span style="color:#0000e6;">''</span><span style="color:#808030;">)</span><span style="color:#808030;">)</span>
        <span style="color:#800000;font-weight:bold;">return</span> HttpResponse<span style="color:#808030;">(</span>simplejson<span style="color:#808030;">.</span>dumps<span style="color:#808030;">(</span><span style="color:#800080;">{</span><span style="color:#0000e6;">'result'</span><span style="color:#808030;">:</span>result<span style="color:#808030;">[</span><span style="color:#008c00;">0</span><span style="color:#808030;">]</span><span style="color:#808030;">,</span><span style="color:#0000e6;">'reason'</span><span style="color:#808030;">:</span>result<span style="color:#808030;">[</span><span style="color:#008c00;">1</span><span style="color:#808030;">]</span><span style="color:#800080;">}</span><span style="color:#808030;">)</span><span style="color:#808030;">,</span> mimetype<span style="color:#808030;">=</span><span style="color:#0000e6;">"application/json"</span><span style="color:#808030;">)</span>
    <span style="color:#800000;font-weight:bold;">return</span> HttpResponse<span style="color:#808030;">(</span>simplejson<span style="color:#808030;">.</span>dumps<span style="color:#808030;">(</span><span style="color:#800080;">{</span><span style="color:#0000e6;">'result'</span><span style="color:#808030;">:</span><span style="color:#0000e6;">'Not a valid method'</span><span style="color:#800080;">}</span><span style="color:#808030;">)</span><span style="color:#808030;">,</span> mimetype<span style="color:#808030;">=</span><span style="color:#0000e6;">"application/json"</span><span style="color:#808030;">)</span></pre>
<pre style="color:#000000;background:#ffffff;"><span style="color:#696969;">#URLS.PY</span>
url<span style="color:#808030;">(</span><span style="color:#0000e6;">r'^check_validity$'</span><span style="color:#808030;">,</span>Check_Validity<span style="color:#808030;">,</span>name<span style="color:#808030;">=</span><span style="color:#0000e6;">"Check_Validity_View"</span><span style="color:#808030;">)</span></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/amirzargar.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/amirzargar.wordpress.com/281/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=281&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aamirhussain.com/2012/09/13/python-code-to-check-either-the-card-number-provided-is-valid-or-not/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<georss:point>28.459594 77.051530</georss:point>
		<geo:lat>28.459594</geo:lat>
		<geo:long>77.051530</geo:long>
		<media:content url="http://0.gravatar.com/avatar/942a6b34f7b6115469ed71da97ba2d42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aamirhussain055</media:title>
		</media:content>
	</item>
		<item>
		<title>11 line python script acts as a web server</title>
		<link>http://aamirhussain.com/2012/08/24/11-line-python-script-acts-as-a-web-server/</link>
		<comments>http://aamirhussain.com/2012/08/24/11-line-python-script-acts-as-a-web-server/#comments</comments>
		<pubDate>Fri, 24 Aug 2012 08:47:43 +0000</pubDate>
		<dc:creator>Aamir Hussain</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://aamirhussain.com/?p=271</guid>
		<description><![CDATA[A Simple python script that acts as a web server, and handles requests import SimpleHTTPServer import SocketServer PORT = 8000 &#8230;<p><a href="http://aamirhussain.com/2012/08/24/11-line-python-script-acts-as-a-web-server/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=271&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h1 style="padding-left:30px;"><strong><span style="color:#339966;">A Simple python script that acts as a web server, and handles requests</span></strong></h1>
<pre style="color:#000000;background:#ffffff;"><span style="color:#800000;font-weight:bold;">import</span> SimpleHTTPServer
<span style="color:#800000;font-weight:bold;">import</span> SocketServer

PORT <span style="color:#808030;">=</span> <span style="color:#008c00;">8000</span>

<span style="color:#800000;font-weight:bold;">class</span> SimpleServer<span style="color:#808030;">(</span><span style="color:#808030;">)</span><span style="color:#808030;">:</span>

    <span style="color:#696969;">#Simple Http server python usage</span>
    <span style="color:#800000;font-weight:bold;">def</span> start<span style="color:#808030;">(</span>self<span style="color:#808030;">)</span><span style="color:#808030;">:</span>
        Handler <span style="color:#808030;">=</span> SimpleHTTPServer<span style="color:#808030;">.</span>SimpleHTTPRequestHandler
        httpd <span style="color:#808030;">=</span> SocketServer<span style="color:#808030;">.</span>TCPServer<span style="color:#808030;">(</span><span style="color:#808030;">(</span><span style="color:#0000e6;">""</span><span style="color:#808030;">,</span> PORT<span style="color:#808030;">)</span><span style="color:#808030;">,</span> Handler<span style="color:#808030;">)</span>
        <span style="color:#800000;font-weight:bold;">print</span> <span style="color:#0000e6;">"serving at port"</span><span style="color:#808030;">,</span> PORT
        httpd<span style="color:#808030;">.</span>serve_forever<span style="color:#808030;">(</span><span style="color:#808030;">)</span>

<span style="color:#800000;font-weight:bold;">if</span> <span style="color:#e34adc;">__name__</span> <span style="color:#808030;">=</span><span style="color:#808030;">=</span> <span style="color:#0000e6;">"__main__"</span><span style="color:#808030;">:</span>
    SimpleServer<span style="color:#808030;">.</span>start<span style="color:#808030;">(</span><span style="color:#808030;">)</span></pre>
<br /> Tagged: <a href='http://aamirhussain.com/tag/python/'>python</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/amirzargar.wordpress.com/271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/amirzargar.wordpress.com/271/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=271&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aamirhussain.com/2012/08/24/11-line-python-script-acts-as-a-web-server/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<georss:point>28.459594 77.051530</georss:point>
		<geo:lat>28.459594</geo:lat>
		<geo:long>77.051530</geo:long>
		<media:content url="http://0.gravatar.com/avatar/942a6b34f7b6115469ed71da97ba2d42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aamirhussain055</media:title>
		</media:content>
	</item>
		<item>
		<title>Area of Polygon in python</title>
		<link>http://aamirhussain.com/2012/03/13/area-of-polygon-in-python/</link>
		<comments>http://aamirhussain.com/2012/03/13/area-of-polygon-in-python/#comments</comments>
		<pubDate>Tue, 13 Mar 2012 16:48:32 +0000</pubDate>
		<dc:creator>Aamir Hussain</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology/Info]]></category>
		<category><![CDATA[area]]></category>
		<category><![CDATA[code snipet]]></category>
		<category><![CDATA[polygon]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[shoelace]]></category>

		<guid isPermaLink="false">http://aamirhussain.com/?p=261</guid>
		<description><![CDATA[Here is a piece of code which can help you to get the  Area of Polygon using Shoelace formula. # &#8230;<p><a href="http://aamirhussain.com/2012/03/13/area-of-polygon-in-python/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=261&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Here is a piece of code which can help you to get the  Area of Polygon using Shoelace formula.</p>
<pre style="color:#000020;background:#f6f8ff;"><span style="color:#595979;"># Area of Polygon using Shoelace formula</span>
<span style="color:#595979;"># http://en.wikipedia.org/wiki/Shoelace_formula</span>
<span style="color:#595979;"># FB - 20120218</span>
<span style="color:#595979;"># corners must be ordered in clockwise or counter-clockwise direction</span>
<span style="color:#200080;font-weight:bold;">def</span> PolygonArea<span style="color:#308080;">(</span>corners<span style="color:#308080;">)</span><span style="color:#308080;">:</span>
    n <span style="color:#308080;">=</span> <span style="color:#e34adc;">len</span><span style="color:#308080;">(</span>corners<span style="color:#308080;">)</span> <span style="color:#595979;"># of corners</span>
    area <span style="color:#308080;">=</span> <span style="color:#008000;">0.0</span>
    <span style="color:#200080;font-weight:bold;">for</span> i <span style="color:#200080;font-weight:bold;">in</span> <span style="color:#e34adc;">range</span><span style="color:#308080;">(</span>n<span style="color:#308080;">)</span><span style="color:#308080;">:</span>
        j <span style="color:#308080;">=</span> <span style="color:#308080;">(</span>i <span style="color:#308080;">+</span> <span style="color:#008c00;">1</span><span style="color:#308080;">)</span> <span style="color:#308080;">%</span> n
        area <span style="color:#308080;">+</span><span style="color:#308080;">=</span> corners<span style="color:#308080;">[</span>i<span style="color:#308080;">]</span><span style="color:#308080;">[</span><span style="color:#008c00;">0</span><span style="color:#308080;">]</span> <span style="color:#308080;">*</span> corners<span style="color:#308080;">[</span>j<span style="color:#308080;">]</span><span style="color:#308080;">[</span><span style="color:#008c00;">1</span><span style="color:#308080;">]</span>
        area <span style="color:#308080;">-</span><span style="color:#308080;">=</span> corners<span style="color:#308080;">[</span>j<span style="color:#308080;">]</span><span style="color:#308080;">[</span><span style="color:#008c00;">0</span><span style="color:#308080;">]</span> <span style="color:#308080;">*</span> corners<span style="color:#308080;">[</span>i<span style="color:#308080;">]</span><span style="color:#308080;">[</span><span style="color:#008c00;">1</span><span style="color:#308080;">]</span>
    area <span style="color:#308080;">=</span> <span style="color:#e34adc;">abs</span><span style="color:#308080;">(</span>area<span style="color:#308080;">)</span> <span style="color:#308080;">/</span> <span style="color:#008000;">2.0</span>
    <span style="color:#200080;font-weight:bold;">return</span> area

<span style="color:#595979;"># examples</span>
corners <span style="color:#308080;">=</span> <span style="color:#308080;">[</span><span style="color:#308080;">(</span><span style="color:#008000;">2.0</span><span style="color:#308080;">,</span> <span style="color:#008000;">1.0</span><span style="color:#308080;">)</span><span style="color:#308080;">,</span> <span style="color:#308080;">(</span><span style="color:#008000;">4.0</span><span style="color:#308080;">,</span> <span style="color:#008000;">5.0</span><span style="color:#308080;">)</span><span style="color:#308080;">,</span> <span style="color:#308080;">(</span><span style="color:#008000;">7.0</span><span style="color:#308080;">,</span> <span style="color:#008000;">8.0</span><span style="color:#308080;">)</span><span style="color:#308080;">]</span>
<span style="color:#200080;font-weight:bold;">print</span> PolygonArea<span style="color:#308080;">(</span>corners<span style="color:#308080;">)</span>
corners <span style="color:#308080;">=</span> <span style="color:#308080;">[</span><span style="color:#308080;">(</span><span style="color:#008000;">3.0</span><span style="color:#308080;">,</span> <span style="color:#008000;">4.0</span><span style="color:#308080;">)</span><span style="color:#308080;">,</span> <span style="color:#308080;">(</span><span style="color:#008000;">5.0</span><span style="color:#308080;">,</span> <span style="color:#008000;">11.0</span><span style="color:#308080;">)</span><span style="color:#308080;">,</span> <span style="color:#308080;">(</span><span style="color:#008000;">12.0</span><span style="color:#308080;">,</span> <span style="color:#008000;">8.0</span><span style="color:#308080;">)</span><span style="color:#308080;">,</span> <span style="color:#308080;">(</span><span style="color:#008000;">9.0</span><span style="color:#308080;">,</span> <span style="color:#008000;">5.0</span><span style="color:#308080;">)</span><span style="color:#308080;">,</span> <span style="color:#308080;">(</span><span style="color:#008000;">5.0</span><span style="color:#308080;">,</span> <span style="color:#008000;">6.0</span><span style="color:#308080;">)</span><span style="color:#308080;">]</span>
<span style="color:#200080;font-weight:bold;">print</span> PolygonArea<span style="color:#308080;">(</span>corners<span style="color:#308080;">)</span></pre>
<p>Source (Python recipe)</p>
<br /> Tagged: <a href='http://aamirhussain.com/tag/area/'>area</a>, <a href='http://aamirhussain.com/tag/code-snipet/'>code snipet</a>, <a href='http://aamirhussain.com/tag/polygon/'>polygon</a>, <a href='http://aamirhussain.com/tag/python/'>python</a>, <a href='http://aamirhussain.com/tag/shoelace/'>shoelace</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/amirzargar.wordpress.com/261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/amirzargar.wordpress.com/261/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=261&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aamirhussain.com/2012/03/13/area-of-polygon-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>34.091518 74.809429</georss:point>
		<geo:lat>34.091518</geo:lat>
		<geo:long>74.809429</geo:long>
		<media:content url="http://0.gravatar.com/avatar/942a6b34f7b6115469ed71da97ba2d42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aamirhussain055</media:title>
		</media:content>
	</item>
		<item>
		<title>Realtime django by bolsterlabs (djangocon 2011)</title>
		<link>http://aamirhussain.com/2011/09/12/realtime-django-by-bolsterlabs/</link>
		<comments>http://aamirhussain.com/2011/09/12/realtime-django-by-bolsterlabs/#comments</comments>
		<pubDate>Mon, 12 Sep 2011 08:24:23 +0000</pubDate>
		<dc:creator>Aamir Hussain</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology/Info]]></category>
		<category><![CDATA[Api]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[slideshare]]></category>

		<guid isPermaLink="false">http://aamirhussain.com/?p=243</guid>
		<description><![CDATA[Tagged: Api, django, json, python, slideshare<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=243&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<iframe src='http://www.slideshare.net/slideshow/embed_code/9154648' width='529' height='434'></iframe>
<br /> Tagged: <a href='http://aamirhussain.com/tag/api/'>Api</a>, <a href='http://aamirhussain.com/tag/django/'>django</a>, <a href='http://aamirhussain.com/tag/json/'>json</a>, <a href='http://aamirhussain.com/tag/python/'>python</a>, <a href='http://aamirhussain.com/tag/slideshare/'>slideshare</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/amirzargar.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/amirzargar.wordpress.com/243/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=243&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aamirhussain.com/2011/09/12/realtime-django-by-bolsterlabs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<georss:point>28.459594 77.051530</georss:point>
		<geo:lat>28.459594</geo:lat>
		<geo:long>77.051530</geo:long>
		<media:content url="http://0.gravatar.com/avatar/942a6b34f7b6115469ed71da97ba2d42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aamirhussain055</media:title>
		</media:content>
	</item>
		<item>
		<title>Calculating distance between two locations in python/django</title>
		<link>http://aamirhussain.com/2011/09/03/calculating-distance-between-two-locations-in-pythondjango/</link>
		<comments>http://aamirhussain.com/2011/09/03/calculating-distance-between-two-locations-in-pythondjango/#comments</comments>
		<pubDate>Sat, 03 Sep 2011 09:01:41 +0000</pubDate>
		<dc:creator>Aamir Hussain</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology/Info]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[geodjango]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[solr]]></category>

		<guid isPermaLink="false">http://aamirhussain.com/?p=183</guid>
		<description><![CDATA[In my new Searching App i want to simply be able to figure out the distance, geographically, between two points &#8230;<p><a href="http://aamirhussain.com/2011/09/03/calculating-distance-between-two-locations-in-pythondjango/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=183&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In my new <a title="ASK DORY" href="http://dory.trialx.com" target="_blank">Searching App</a> i want to simply be able to figure out the distance, geographically, between two points says user and doctor  based on their zip code and then sort a list based on that. I couldn&#8217;t find  this functionality built-in  Django. then i found a piece of code.</p>
<pre style="color:#000020;background:#f6f8ff;"><span style="color:#200080;font-weight:bold;">from</span> math <span style="color:#200080;font-weight:bold;">import</span> sin<span style="color:#308080;">,</span> cos<span style="color:#308080;">,</span> radians<span style="color:#308080;">,</span> acos

<span style="color:#595979;"># http://en.wikipedia.org/wiki/Earth_radius</span>
<span style="color:#595979;"># """For Earth, the mean radius is 6,371.009 km (˜3,958.761 mi; ˜3,440.069 nmi)"""</span>
EARTH_RADIUS_IN_MILES <span style="color:#308080;">=</span> <span style="color:#008000;">3958.761</span>

<span style="color:#200080;font-weight:bold;">def</span> calculate(lat_a<span style="color:#308080;">,</span> long_a<span style="color:#308080;">,</span> lat_b<span style="color:#308080;">,</span> long_b<span style="color:#308080;">)</span><span style="color:#308080;">:</span>
    <span style="color:#595979;">"""all angles in degrees, result in miles"""</span>
    lat_a <span style="color:#308080;">=</span> radians<span style="color:#308080;">(</span>lat_a<span style="color:#308080;">)</span>
    lat_b <span style="color:#308080;">=</span> radians<span style="color:#308080;">(</span>lat_b<span style="color:#308080;">)</span>
    delta_long <span style="color:#308080;">=</span> radians<span style="color:#308080;">(</span>long_a <span style="color:#308080;">-</span> long_b<span style="color:#308080;">)</span>
    cos_x <span style="color:#308080;">=</span> <span style="color:#308080;">(</span>
        sin<span style="color:#308080;">(</span>lat_a<span style="color:#308080;">)</span> <span style="color:#308080;">*</span> sin<span style="color:#308080;">(</span>lat_b<span style="color:#308080;">)</span> <span style="color:#308080;">+</span>
        cos<span style="color:#308080;">(</span>lat_a<span style="color:#308080;">)</span> <span style="color:#308080;">*</span> cos<span style="color:#308080;">(</span>lat_b<span style="color:#308080;">)</span> <span style="color:#308080;">*</span> cos<span style="color:#308080;">(</span>delta_long<span style="color:#308080;">)</span>
        <span style="color:#308080;">)</span>
    <span style="color:#200080;font-weight:bold;">return</span> acos<span style="color:#308080;">(</span>cos_x<span style="color:#308080;">)</span> <span style="color:#308080;">*</span> EARTH_RADIUS_IN_MILES

#SFO (37.676, -122.433) to NYC (40.733, -73.917)
#calculate -&gt; 2570.9028268899356
#A US government website (http://www.nhc.noaa.gov/gccalc.shtml) -&gt; 2569
#This website (http://www.timeanddate.com/worldclock/distanceresult.html
# ?p1=179&amp;p2=224), from which I got the SFO and NYC coordinates, -&gt; 2577</pre>
<p>but this did&#8217;t solve my whole issue ,in my request i have Zip Codes only so the crack was to get the database of all the zips from <a title="ZIP CODES" href="http://sourceforge.net/projects/zips/files/zips/" target="_blank">ZIP DIR</a> in <a title="CSV ZIP CODES" href="http://sourceforge.net/projects/zips/files/zips/zips.csv.zip/" target="_blank">csv</a> or <a title="SQL DUMP OF ZIPCODES" href="http://sourceforge.net/projects/zips/files/zips/zipcodedb-MySQLdump/" target="_blank">sql</a> format and add to the database of the App e.g the table name is ZipCode.</p>
<pre style="color:#000020;background:#f6f8ff;"><span style="color:#200080;font-weight:bold;">from</span> django <span style="color:#200080;font-weight:bold;">import</span> template
<span style="color:#200080;font-weight:bold;">from</span> models <span style="color:#200080;font-weight:bold;">import</span> ZipCode

register <span style="color:#308080;">=</span> template<span style="color:#308080;">.</span>Library<span style="color:#308080;">(</span><span style="color:#308080;">)</span>

EARTH_RADIUS_IN_MILES <span style="color:#308080;">=</span> <span style="color:#008000;">3958.761</span>

@register<span style="color:#308080;">.</span><span style="color:#e34adc;">filter</span>
<span style="color:#200080;font-weight:bold;">def</span> calculate_distance<span style="color:#308080;">(</span>zip1<span style="color:#308080;">,</span>zip2<span style="color:#308080;">)</span><span style="color:#308080;">:</span>
    coordinate1 <span style="color:#308080;">=</span> ZipCode<span style="color:#308080;">.</span>objects<span style="color:#308080;">.</span>get<span style="color:#308080;">(</span>zip <span style="color:#308080;">=</span> zip1<span style="color:#308080;">)</span>
    coordinate2 <span style="color:#308080;">=</span> ZipCode<span style="color:#308080;">.</span>objects<span style="color:#308080;">.</span>get<span style="color:#308080;">(</span>zip <span style="color:#308080;">=</span> zip2<span style="color:#308080;">)</span>
    distance <span style="color:#308080;">=</span> calculate<span style="color:#308080;">(</span>coordinate1<span style="color:#308080;">.</span>latitude<span style="color:#308080;">,</span>coordinate1<span style="color:#308080;">.</span>longitude<span style="color:#308080;">,</span>coordinate2<span style="color:#308080;">.</span>latitude<span style="color:#308080;">,</span>coordinate2<span style="color:#308080;">.</span>longitude<span style="color:#308080;">)</span>
    <span style="color:#200080;font-weight:bold;">if</span> distance<span style="color:#308080;">&gt;</span><span style="color:#308080;">=</span> <span style="color:#008c00;">0</span><span style="color:#308080;">:</span>
        <span style="color:#200080;font-weight:bold;">return</span> <span style="color:#1060b6;">"%.2f"</span><span style="color:#308080;">%</span><span style="color:#e34adc;">round</span><span style="color:#308080;">(</span>distance<span style="color:#308080;">,</span><span style="color:#008c00;">2</span><span style="color:#308080;">)</span>
    <span style="color:#200080;font-weight:bold;">return</span> <span style="color:#1060b6;">"Sorry can't determine the distance from %s TO %s"</span><span style="color:#308080;">%</span><span style="color:#308080;">(</span>zip1<span style="color:#308080;">,</span>zip2<span style="color:#308080;">)</span></pre>
<p>Usage</p>
<pre style="color:#000020;background:#f6f8ff;"><span style="color:#406080;">{</span><span style="color:#308080;">%</span> <span style="color:#200080;font-weight:bold;">if</span> request<span style="color:#308080;">.</span>GET<span style="color:#308080;">.</span>ZipCode <span style="color:#308080;">%</span><span style="color:#406080;">}</span>
     <span style="color:#406080;">{</span><span style="color:#406080;">{</span> doctor.ZipCode<span style="color:#308080;">|</span>calculate_distance<span style="color:#308080;">:</span>request<span style="color:#308080;">.</span>GET<span style="color:#308080;">.</span>ZipCode <span style="color:#406080;">}</span><span style="color:#406080;">}</span>
<span style="color:#406080;">{</span><span style="color:#308080;">%</span> endif <span style="color:#308080;">%</span><span style="color:#406080;">}</span></pre>
<br /> Tagged: <a href='http://aamirhussain.com/tag/django/'>django</a>, <a href='http://aamirhussain.com/tag/geodjango/'>geodjango</a>, <a href='http://aamirhussain.com/tag/math/'>math</a>, <a href='http://aamirhussain.com/tag/python/'>python</a>, <a href='http://aamirhussain.com/tag/solr/'>solr</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/amirzargar.wordpress.com/183/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/amirzargar.wordpress.com/183/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=183&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aamirhussain.com/2011/09/03/calculating-distance-between-two-locations-in-pythondjango/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<georss:point>28.459594 77.051530</georss:point>
		<geo:lat>28.459594</geo:lat>
		<geo:long>77.051530</geo:long>
		<media:content url="http://0.gravatar.com/avatar/942a6b34f7b6115469ed71da97ba2d42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aamirhussain055</media:title>
		</media:content>
	</item>
		<item>
		<title>Calculating Apache hits per IP</title>
		<link>http://aamirhussain.com/2011/09/02/calculating-apache-hits-per-ip/</link>
		<comments>http://aamirhussain.com/2011/09/02/calculating-apache-hits-per-ip/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 20:04:37 +0000</pubDate>
		<dc:creator>Aamir Hussain</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology/Info]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://aamirhussain.com/?p=176</guid>
		<description><![CDATA[This function returns a dictionary containing the hit counts for each individual IP that has accessed your Apache web server. &#8230;<p><a href="http://aamirhussain.com/2011/09/02/calculating-apache-hits-per-ip/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=176&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This function returns a dictionary containing the hit counts for each individual IP that has accessed your Apache web server.</p>
<pre style="color:#000020;background:#f6f8ff;"><span style="color:#200080;font-weight:bold;">def</span> CalculateApacheIpHits<span style="color:#308080;">(</span>logfile_pathname<span style="color:#308080;">)</span><span style="color:#308080;">:</span>

    <span style="color:#595979;"># make a dictionary to store Ip's and their hit counts and read the</span>
        <span style="color:#595979;"># contents of the logfile line by line</span>

    IpHitListing <span style="color:#308080;">=</span> <span style="color:#406080;">{</span><span style="color:#406080;">}</span>
    Contents <span style="color:#308080;">=</span> <span style="color:#e34adc;">open</span><span style="color:#308080;">(</span>logfile_pathname<span style="color:#308080;">,</span> <span style="color:#1060b6;">"r"</span><span style="color:#308080;">)</span><span style="color:#308080;">.</span>readlines<span style="color:#308080;">(</span><span style="color:#308080;">)</span>

    <span style="color:#595979;"># go through each line of the logfile</span>
    <span style="color:#200080;font-weight:bold;">for</span> line <span style="color:#200080;font-weight:bold;">in</span> Contents<span style="color:#308080;">:</span>

                <span style="color:#595979;">#split the string to isolate the ip</span>
                Ip <span style="color:#308080;">=</span> line<span style="color:#308080;">.</span>split<span style="color:#308080;">(</span><span style="color:#1060b6;">" "</span><span style="color:#308080;">)</span><span style="color:#308080;">[</span><span style="color:#008c00;">0</span><span style="color:#308080;">]</span>

                <span style="color:#595979;"># ensure length of the ip is proper: see discussion</span>
        <span style="color:#200080;font-weight:bold;">if</span> <span style="color:#008c00;">6</span> <span style="color:#308080;">&lt;</span> <span style="color:#e34adc;">len</span><span style="color:#308080;">(</span>Ip<span style="color:#308080;">)</span> <span style="color:#308080;">&lt;</span> <span style="color:#008c00;">15</span><span style="color:#308080;">:</span>
            <span style="color:#595979;"># Increase by 1 if ip exists else hit count = 1</span>
                        IpHitListing<span style="color:#308080;">[</span>Ip<span style="color:#308080;">]</span> <span style="color:#308080;">=</span> IpHitListing<span style="color:#308080;">.</span>get<span style="color:#308080;">(</span>Ip<span style="color:#308080;">,</span> <span style="color:#008c00;">0</span><span style="color:#308080;">)</span> <span style="color:#308080;">+</span> <span style="color:#008c00;">1</span>

    <span style="color:#200080;font-weight:bold;">return</span> IpHitListing

<span style="color:#595979;"># example usage</span>
HitsDictionary <span style="color:#308080;">=</span> CalculateApacheIpHits<span style="color:#308080;">(</span><span style="color:#1060b6;">"/usr/local/nusphere/apache/logs/access_log"</span><span style="color:#308080;">)</span>
<span style="color:#200080;font-weight:bold;">print</span> HitsDictionary<span style="color:#308080;">[</span><span style="color:#1060b6;">"127.0.0.1"</span><span style="color:#308080;">]</span></pre>
<p>This function is quite useful for many things. For one, I often use it in my code to determine how many of my &#8220;hits&#8221; are actually originating from locations other than my local host. This function was also used to chart which IP&#8217;s are most actively viewing pages that are served by a particular installation of Apache.</p>
<p>As for the method of &#8220;validating&#8221; the IP, it as is follows: 1) an IP address will never be longer than 15 digits (4 sets of triplets and 3 periods); 2) an IP address will never be shorter than 6 digits (4 sets of single digits and 3 periods). The whole purpose of this validation is not to enforce stigent validation (for that we could use a regular expression), but rather to avoid the possibility of putting blantently &#8220;garbage&#8221; data into the dictionary.</p>
<br /> Tagged: <a href='http://aamirhussain.com/tag/apache/'>apache</a>, <a href='http://aamirhussain.com/tag/python/'>python</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/amirzargar.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/amirzargar.wordpress.com/176/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=176&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aamirhussain.com/2011/09/02/calculating-apache-hits-per-ip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>28.635308 77.224960</georss:point>
		<geo:lat>28.635308</geo:lat>
		<geo:long>77.224960</geo:long>
		<media:content url="http://0.gravatar.com/avatar/942a6b34f7b6115469ed71da97ba2d42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aamirhussain055</media:title>
		</media:content>
	</item>
		<item>
		<title>Yet another &#8216;enum&#8217; for Python</title>
		<link>http://aamirhussain.com/2011/07/15/yet-another-enum-for-python/</link>
		<comments>http://aamirhussain.com/2011/07/15/yet-another-enum-for-python/#comments</comments>
		<pubDate>Fri, 15 Jul 2011 20:01:37 +0000</pubDate>
		<dc:creator>Aamir Hussain</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[enum]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[recepe]]></category>

		<guid isPermaLink="false">http://aamirhussain.com/?p=169</guid>
		<description><![CDATA[There are quite a few &#8216;enum&#8217; recipes already here. This one is short, cheap, and has neither bells nor whistles &#8230;<p><a href="http://aamirhussain.com/2011/07/15/yet-another-enum-for-python/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=169&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<div id="description">
<p>There are quite a few &#8216;enum&#8217; recipes already here. This one is short, cheap, and has neither bells nor whistles <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="highlight">
<pre><span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">enum</span>(typename, field_names):
    <span style="color:#4070a0;">"Create a new enumeration type"</span>

    <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;">isinstance</span>(field_names, <span style="color:#007020;">str</span>):
        field_names <span style="color:#666666;">=</span> field_names<span style="color:#666666;">.</span>replace(<span style="color:#4070a0;">','</span>, <span style="color:#4070a0;">' '</span>)<span style="color:#666666;">.</span>split()
    d <span style="color:#666666;">=</span> <span style="color:#007020;">dict</span>((reversed(nv) <span style="color:#007020;font-weight:bold;">for</span> nv <span style="color:#007020;font-weight:bold;">in</span> <span style="color:#007020;">enumerate</span>(field_names)), __slots__ <span style="color:#666666;">=</span> ())
    <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#007020;">type</span>(typename, (<span style="color:#007020;">object</span>,), d)()</pre>
</div>
<p>You define an enumeration the same way as collections.namedtuple: with a type name, followed by a sequence of identifiers (it may be a string separated by commas and/or whitespace):</p>
<div class="highlight">
<pre><span style="color:#666666;">&gt;&gt;&gt;</span> STATE <span style="color:#666666;">=</span> enum(<span style="color:#4070a0;">'STATE'</span>, <span style="color:#4070a0;">'GET_QUIZ, GET_VERSE, TEACH'</span>)</pre>
</div>
<p>It returns a (single) instance of a newly created type. The enumeration elements are integers, starting at 0, exposed as attributes. There is no way to change that: if you need specific values, look elsewhere. In fact, it is as if the enum were declared as:</p>
<div class="highlight">
<pre><span style="color:#60a0b0;font-style:italic;"># not actual code</span>
<span style="color:#007020;font-weight:bold;">class</span> <span style="color:#0e84b5;font-weight:bold;">STATE</span>(<span style="color:#007020;">object</span>):
  __slots__ <span style="color:#666666;">=</span> ()
  GET_QUIZ, GET_VERSE, TEACH <span style="color:#666666;">=</span> <span style="color:#007020;">range</span>(<span style="color:#40a070;">3</span>)

STATE <span style="color:#666666;">=</span> STATE()</pre>
</div>
<p>The enumeration elements are read-only:</p>
<div class="highlight">
<pre><span style="color:#666666;">&gt;&gt;&gt;</span> STATE<span style="color:#666666;">.</span>GET_VERSE
<span style="color:#40a070;">1</span>
<span style="color:#666666;">&gt;&gt;&gt;</span> STATE<span style="color:#666666;">.</span>GET_VERSE <span style="color:#666666;">=</span> <span style="color:#40a070;">8</span>
Traceback (most recent call last):
  <span style="color:#666666;">...</span>
<span style="color:#007020;">AttributeError</span>: <span style="color:#4070a0;">'STATE'</span> <span style="color:#007020;">object</span> attribute <span style="color:#4070a0;">'GET_VERSE'</span> <span style="color:#007020;font-weight:bold;">is</span> read<span style="color:#666666;">-</span>only
<span style="color:#666666;">&gt;&gt;&gt;</span> <span style="color:#007020;font-weight:bold;">del</span> STATE<span style="color:#666666;">.</span>GET_VERSE
Traceback (most recent call last):
  <span style="color:#666666;">...</span>
<span style="color:#007020;">AttributeError</span>: <span style="color:#4070a0;">'STATE'</span> <span style="color:#007020;">object</span> attribute <span style="color:#4070a0;">'GET_VERSE'</span> <span style="color:#007020;font-weight:bold;">is</span> read<span style="color:#666666;">-</span>only</pre>
</div>
<p>This recipe has been tested with Python 2.4 ranging up to 3.1</p>
</div>
<br /> Tagged: <a href='http://aamirhussain.com/tag/enum/'>enum</a>, <a href='http://aamirhussain.com/tag/python/'>python</a>, <a href='http://aamirhussain.com/tag/recepe/'>recepe</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/amirzargar.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/amirzargar.wordpress.com/169/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=169&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aamirhussain.com/2011/07/15/yet-another-enum-for-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<georss:point>28.459594 77.051530</georss:point>
		<geo:lat>28.459594</geo:lat>
		<geo:long>77.051530</geo:long>
		<media:content url="http://0.gravatar.com/avatar/942a6b34f7b6115469ed71da97ba2d42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aamirhussain055</media:title>
		</media:content>
	</item>
		<item>
		<title>Sample Pythonic Inversion-of-Control Pseudo-Container.</title>
		<link>http://aamirhussain.com/2011/07/15/sample-pythonic-inversion-of-control-pseudo-container/</link>
		<comments>http://aamirhussain.com/2011/07/15/sample-pythonic-inversion-of-control-pseudo-container/#comments</comments>
		<pubDate>Fri, 15 Jul 2011 19:54:48 +0000</pubDate>
		<dc:creator>Aamir Hussain</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[injection]]></category>
		<category><![CDATA[Inversion-of-Control]]></category>
		<category><![CDATA[psedocode]]></category>
		<category><![CDATA[Pseudo-Container]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://aamirhussain.com/?p=165</guid>
		<description><![CDATA[Sample Pythonic Inversion-of-Control Pseudo-Container. ###################################################################### ## ## Feature Broker ## ###################################################################### class FeatureBroker: def __init__(self, allowReplace=False): self.providers = {} self.allowReplace &#8230;<p><a href="http://aamirhussain.com/2011/07/15/sample-pythonic-inversion-of-control-pseudo-container/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=165&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong>Sample Pythonic Inversion-of-Control Pseudo-Container.</strong></p>
<div class="highlight" style="text-align:justify;">
<pre><span style="color:#60a0b0;font-style:italic;">######################################################################</span>
<span style="color:#60a0b0;font-style:italic;">## </span>
<span style="color:#60a0b0;font-style:italic;">## Feature Broker</span>
<span style="color:#60a0b0;font-style:italic;">## </span>
<span style="color:#60a0b0;font-style:italic;">######################################################################</span>

<span style="color:#007020;font-weight:bold;">class</span> <span style="color:#0e84b5;font-weight:bold;">FeatureBroker</span>:
   <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">__init__</span>(<span style="color:#007020;">self</span>, allowReplace<span style="color:#666666;">=</span><span style="color:#007020;">False</span>):
      <span style="color:#007020;">self</span><span style="color:#666666;">.</span>providers <span style="color:#666666;">=</span> {}
      <span style="color:#007020;">self</span><span style="color:#666666;">.</span>allowReplace <span style="color:#666666;">=</span> allowReplace
   <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">Provide</span>(<span style="color:#007020;">self</span>, feature, provider, <span style="color:#666666;">*</span>args, <span style="color:#666666;">**</span>kwargs):
      <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;font-weight:bold;">not</span> <span style="color:#007020;">self</span><span style="color:#666666;">.</span>allowReplace:
         <span style="color:#007020;font-weight:bold;">assert</span> <span style="color:#007020;font-weight:bold;">not</span> <span style="color:#007020;">self</span><span style="color:#666666;">.</span>providers<span style="color:#666666;">.</span>has_key(feature), <span style="color:#4070a0;">"Duplicate feature: </span><span style="color:#70a0d0;font-style:italic;">%r</span><span style="color:#4070a0;">"</span> <span style="color:#666666;">%</span> feature
      <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;">callable</span>(provider):
         <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">call</span>(): <span style="color:#007020;font-weight:bold;">return</span> provider(<span style="color:#666666;">*</span>args, <span style="color:#666666;">**</span>kwargs)
      <span style="color:#007020;font-weight:bold;">else</span>:
         <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">call</span>(): <span style="color:#007020;font-weight:bold;">return</span> provider
      <span style="color:#007020;">self</span><span style="color:#666666;">.</span>providers[feature] <span style="color:#666666;">=</span> call
   <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">__getitem__</span>(<span style="color:#007020;">self</span>, feature):
      <span style="color:#007020;font-weight:bold;">try</span>:
         provider <span style="color:#666666;">=</span> <span style="color:#007020;">self</span><span style="color:#666666;">.</span>providers[feature]
      <span style="color:#007020;font-weight:bold;">except</span> <span style="color:#007020;">KeyError</span>:
         <span style="color:#007020;font-weight:bold;">raise</span> <span style="color:#007020;">KeyError</span>, <span style="color:#4070a0;">"Unknown feature named </span><span style="color:#70a0d0;font-style:italic;">%r</span><span style="color:#4070a0;">"</span> <span style="color:#666666;">%</span> feature
      <span style="color:#007020;font-weight:bold;">return</span> provider()

features <span style="color:#666666;">=</span> FeatureBroker()

<span style="color:#60a0b0;font-style:italic;">######################################################################</span>
<span style="color:#60a0b0;font-style:italic;">## </span>
<span style="color:#60a0b0;font-style:italic;">## Representation of Required Features and Feature Assertions</span>
<span style="color:#60a0b0;font-style:italic;">## </span>
<span style="color:#60a0b0;font-style:italic;">######################################################################</span>

<span style="color:#60a0b0;font-style:italic;">#</span>
<span style="color:#60a0b0;font-style:italic;"># Some basic assertions to test the suitability of injected features</span>
<span style="color:#60a0b0;font-style:italic;">#</span>

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">NoAssertion</span>(obj): <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#007020;">True</span>

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">IsInstanceOf</span>(<span style="color:#666666;">*</span>classes):
   <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">test</span>(obj): <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#007020;">isinstance</span>(obj, classes)
   <span style="color:#007020;font-weight:bold;">return</span> test

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">HasAttributes</span>(<span style="color:#666666;">*</span>attributes):
   <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">test</span>(obj):
      <span style="color:#007020;font-weight:bold;">for</span> each <span style="color:#007020;font-weight:bold;">in</span> attributes:
         <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;font-weight:bold;">not</span> <span style="color:#007020;">hasattr</span>(obj, each): <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#007020;">False</span>
      <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#007020;">True</span>
   <span style="color:#007020;font-weight:bold;">return</span> test

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">HasMethods</span>(<span style="color:#666666;">*</span>methods):
   <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">test</span>(obj):
      <span style="color:#007020;font-weight:bold;">for</span> each <span style="color:#007020;font-weight:bold;">in</span> methods:
         <span style="color:#007020;font-weight:bold;">try</span>:
            attr <span style="color:#666666;">=</span> <span style="color:#007020;">getattr</span>(obj, each)
         <span style="color:#007020;font-weight:bold;">except</span> <span style="color:#007020;">AttributeError</span>:
            <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#007020;">False</span>
         <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;font-weight:bold;">not</span> <span style="color:#007020;">callable</span>(attr): <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#007020;">False</span>
      <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#007020;">True</span>
   <span style="color:#007020;font-weight:bold;">return</span> test

<span style="color:#60a0b0;font-style:italic;">#</span>
<span style="color:#60a0b0;font-style:italic;"># An attribute descriptor to "declare" required features</span>
<span style="color:#60a0b0;font-style:italic;">#</span>

<span style="color:#007020;font-weight:bold;">class</span> <span style="color:#0e84b5;font-weight:bold;">RequiredFeature</span>(<span style="color:#007020;">object</span>):
   <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">__init__</span>(<span style="color:#007020;">self</span>, feature, assertion<span style="color:#666666;">=</span>NoAssertion):
      <span style="color:#007020;">self</span><span style="color:#666666;">.</span>feature <span style="color:#666666;">=</span> feature
      <span style="color:#007020;">self</span><span style="color:#666666;">.</span>assertion <span style="color:#666666;">=</span> assertion
   <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">__get__</span>(<span style="color:#007020;">self</span>, obj, T):
      <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#007020;">self</span><span style="color:#666666;">.</span>result <span style="color:#60a0b0;font-style:italic;"># &lt;-- will request the feature upon first call</span>
   <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">__getattr__</span>(<span style="color:#007020;">self</span>, name):
      <span style="color:#007020;font-weight:bold;">assert</span> name <span style="color:#666666;">==</span> <span style="color:#4070a0;">'result'</span>, <span style="color:#4070a0;">"Unexpected attribute request other then 'result'"</span>
      <span style="color:#007020;">self</span><span style="color:#666666;">.</span>result <span style="color:#666666;">=</span> <span style="color:#007020;">self</span><span style="color:#666666;">.</span>Request()
      <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#007020;">self</span><span style="color:#666666;">.</span>result
   <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">Request</span>(<span style="color:#007020;">self</span>):
      obj <span style="color:#666666;">=</span> features[<span style="color:#007020;">self</span><span style="color:#666666;">.</span>feature]
      <span style="color:#007020;font-weight:bold;">assert</span> <span style="color:#007020;">self</span><span style="color:#666666;">.</span>assertion(obj), \
             <span style="color:#4070a0;">"The value </span><span style="color:#70a0d0;font-style:italic;">%r</span><span style="color:#4070a0;"> of </span><span style="color:#70a0d0;font-style:italic;">%r</span><span style="color:#4070a0;"> does not match the specified criteria"</span> \
             <span style="color:#666666;">%</span> (obj, <span style="color:#007020;">self</span><span style="color:#666666;">.</span>feature)
      <span style="color:#007020;font-weight:bold;">return</span> obj

<span style="color:#007020;font-weight:bold;">class</span> <span style="color:#0e84b5;font-weight:bold;">Component</span>(<span style="color:#007020;">object</span>):
   <span style="color:#4070a0;">"Symbolic base class for components"</span>

<span style="color:#60a0b0;font-style:italic;">######################################################################</span>
<span style="color:#60a0b0;font-style:italic;">## </span>
<span style="color:#60a0b0;font-style:italic;">## DEMO</span>
<span style="color:#60a0b0;font-style:italic;">## </span>
<span style="color:#60a0b0;font-style:italic;">######################################################################</span>

<span style="color:#60a0b0;font-style:italic;"># ---------------------------------------------------------------------------------</span>
<span style="color:#60a0b0;font-style:italic;"># Some python module defines a Bar component and states the dependencies</span>
<span style="color:#60a0b0;font-style:italic;"># We will assume that</span>
<span style="color:#60a0b0;font-style:italic;"># - Console denotes an object with a method WriteLine(string)</span>
<span style="color:#60a0b0;font-style:italic;"># - AppTitle denotes a string that represents the current application name</span>
<span style="color:#60a0b0;font-style:italic;"># - CurrentUser denotes a string that represents the current user name</span>
<span style="color:#60a0b0;font-style:italic;">#</span>

<span style="color:#007020;font-weight:bold;">class</span> <span style="color:#0e84b5;font-weight:bold;">Bar</span>(Component):
   con   <span style="color:#666666;">=</span> RequiredFeature(<span style="color:#4070a0;">'Console'</span>, HasMethods(<span style="color:#4070a0;">'WriteLine'</span>))
   title <span style="color:#666666;">=</span> RequiredFeature(<span style="color:#4070a0;">'AppTitle'</span>, IsInstanceOf(<span style="color:#007020;">str</span>))
   user  <span style="color:#666666;">=</span> RequiredFeature(<span style="color:#4070a0;">'CurrentUser'</span>, IsInstanceOf(<span style="color:#007020;">str</span>))
   <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">__init__</span>(<span style="color:#007020;">self</span>):
      <span style="color:#007020;">self</span><span style="color:#666666;">.</span>X <span style="color:#666666;">=</span> <span style="color:#40a070;">0</span>
   <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">PrintYourself</span>(<span style="color:#007020;">self</span>):
      <span style="color:#007020;">self</span><span style="color:#666666;">.</span>con<span style="color:#666666;">.</span>WriteLine(<span style="color:#4070a0;">'-- Bar instance --'</span>)
      <span style="color:#007020;">self</span><span style="color:#666666;">.</span>con<span style="color:#666666;">.</span>WriteLine(<span style="color:#4070a0;">'Title: </span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">'</span> <span style="color:#666666;">%</span> <span style="color:#007020;">self</span><span style="color:#666666;">.</span>title)
      <span style="color:#007020;">self</span><span style="color:#666666;">.</span>con<span style="color:#666666;">.</span>WriteLine(<span style="color:#4070a0;">'User: </span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">'</span> <span style="color:#666666;">%</span> <span style="color:#007020;">self</span><span style="color:#666666;">.</span>user)
      <span style="color:#007020;">self</span><span style="color:#666666;">.</span>con<span style="color:#666666;">.</span>WriteLine(<span style="color:#4070a0;">'X: </span><span style="color:#70a0d0;font-style:italic;">%d</span><span style="color:#4070a0;">'</span> <span style="color:#666666;">%</span> <span style="color:#007020;">self</span><span style="color:#666666;">.</span>X)

<span style="color:#60a0b0;font-style:italic;"># ---------------------------------------------------------------------------------</span>
<span style="color:#60a0b0;font-style:italic;"># Some other python module defines a basic Console component</span>
<span style="color:#60a0b0;font-style:italic;">#</span>

<span style="color:#007020;font-weight:bold;">class</span> <span style="color:#0e84b5;font-weight:bold;">SimpleConsole</span>(Component):
   <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">WriteLine</span>(<span style="color:#007020;">self</span>, s):
      <span style="color:#007020;font-weight:bold;">print</span> s

<span style="color:#60a0b0;font-style:italic;"># ---------------------------------------------------------------------------------</span>
<span style="color:#60a0b0;font-style:italic;"># Yet another python module defines a better Console component</span>
<span style="color:#60a0b0;font-style:italic;">#</span>

<span style="color:#007020;font-weight:bold;">class</span> <span style="color:#0e84b5;font-weight:bold;">BetterConsole</span>(Component):
   <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">__init__</span>(<span style="color:#007020;">self</span>, prefix<span style="color:#666666;">=</span><span style="color:#4070a0;">''</span>):
      <span style="color:#007020;">self</span><span style="color:#666666;">.</span>prefix <span style="color:#666666;">=</span> prefix
   <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">WriteLine</span>(<span style="color:#007020;">self</span>, s):
      lines <span style="color:#666666;">=</span> s<span style="color:#666666;">.</span>split(<span style="color:#4070a0;">'</span><span style="color:#4070a0;font-weight:bold;">\n</span><span style="color:#4070a0;">'</span>)
      <span style="color:#007020;font-weight:bold;">for</span> line <span style="color:#007020;font-weight:bold;">in</span> lines:
         <span style="color:#007020;font-weight:bold;">if</span> line:
            <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#007020;">self</span><span style="color:#666666;">.</span>prefix, line
         <span style="color:#007020;font-weight:bold;">else</span>:
            <span style="color:#007020;font-weight:bold;">print</span>

<span style="color:#60a0b0;font-style:italic;"># ---------------------------------------------------------------------------------</span>
<span style="color:#60a0b0;font-style:italic;"># Some third python module knows how to discover the current user's name</span>
<span style="color:#60a0b0;font-style:italic;">#</span>

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">GetCurrentUser</span>():
   <span style="color:#007020;font-weight:bold;">return</span> os<span style="color:#666666;">.</span>getenv(<span style="color:#4070a0;">'USERNAME'</span>) <span style="color:#007020;font-weight:bold;">or</span> <span style="color:#4070a0;">'Some User'</span> <span style="color:#60a0b0;font-style:italic;"># USERNAME is platform-specific</span>

<span style="color:#60a0b0;font-style:italic;"># ---------------------------------------------------------------------------------</span>
<span style="color:#60a0b0;font-style:italic;"># Finally, the main python script specifies the application name,</span>
<span style="color:#60a0b0;font-style:italic;"># decides which components/values to use for what feature,</span>
<span style="color:#60a0b0;font-style:italic;"># and creates an instance of Bar to work with</span>
<span style="color:#60a0b0;font-style:italic;">#</span>
<span style="color:#007020;font-weight:bold;">if</span> __name__ <span style="color:#666666;">==</span> <span style="color:#4070a0;">'__main__'</span>:
   <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">'</span><span style="color:#4070a0;font-weight:bold;">\n</span><span style="color:#4070a0;">*** IoC Demo ***'</span>
   features<span style="color:#666666;">.</span>Provide(<span style="color:#4070a0;">'AppTitle'</span>, <span style="color:#4070a0;">'Inversion of Control ...</span><span style="color:#4070a0;font-weight:bold;">\n\n</span><span style="color:#4070a0;">... The Python Way'</span>)
   features<span style="color:#666666;">.</span>Provide(<span style="color:#4070a0;">'CurrentUser'</span>, GetCurrentUser)
   features<span style="color:#666666;">.</span>Provide(<span style="color:#4070a0;">'Console'</span>, BetterConsole, prefix<span style="color:#666666;">=</span><span style="color:#4070a0;">'--&gt;'</span>) <span style="color:#60a0b0;font-style:italic;"># &lt;-- transient lifestyle</span>
   <span style="color:#60a0b0;font-style:italic;">##features.Provide('Console', BetterConsole(prefix='--&gt;')) # &lt;-- singleton lifestyle</span>

   bar <span style="color:#666666;">=</span> Bar()
   bar<span style="color:#666666;">.</span>PrintYourself()

<span style="color:#60a0b0;font-style:italic;">#</span>
<span style="color:#60a0b0;font-style:italic;"># Evidently, none of the used components needed to know about each other</span>
<span style="color:#60a0b0;font-style:italic;"># =&gt; Loose coupling goal achieved</span>
<span style="color:#60a0b0;font-style:italic;"># ---------------------------------------------------------------------------------</span></pre>
</div>
<p style="text-align:justify;">Inversion of Control (IoC) Containers and the Dependency Injection pattern have drawn some attention in the Java world, and they are increasingly spreading over to .NET, too. (Perhaps we are facing a sort of &#8220;Infection OUT of Control&#8221; &#8211; IooC? <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p style="text-align:justify;">IoC is all about loose coupling between components of an application, about cutting off explicit, direct dependencies, plus some goodies (most of which are useful in statically typed languages only, like automatic type/interface matching). A thorough discussion on the subject can be found at <a href="http://www.martinfowler.com/articles/injection.html" rel="nofollow">http://www.martinfowler.com/articles/injection.html</a> .</p>
<p style="text-align:justify;">In statically typed languages, an IoC container is quite a challenge. But at the heart of it, there are only few key concepts behind it.</p>
<p style="text-align:justify;">Components do not know each other directly<br />
Components specify external dependencies using some sort of a key.<br />
Dependencies are resolved late, preferably just before they are used (JIT dependency resolution).<br />
Dependencies are resolved once for each component.</p>
<p style="text-align:justify;">You guessed it &#8211; it should not be such a big deal to do this in python!</p>
<p style="text-align:justify;">And indeed, a combination of a broker, descriptors and lazy attributes brings about pretty much the same core result as those IoC containers &#8211; effectively in little more then 50 lines (not counting demo code, comments and empy lines).</p>
<p style="text-align:justify;">So what does the code do?</p>
<p style="text-align:justify;">It offers a mechanism to register provided &#8220;features&#8221;.<br />
It offers a mechanism to &#8220;declare&#8221; required features in a readable way as attributes.<br />
The required features are resolved (injected) as late as possible &#8211; at access time.<br />
It provides for reasonable verification of injected dependencies.</p>
<p style="text-align:justify;">The supported injection type is &#8220;Setter Injection&#8221;, which basically means that dependencies are expressed through attributes. There is another type of injection, the &#8220;Constructor Injection&#8221;, but that one builds heavily on static typing and can therefore not be employed in python (or at least I could not think of any elegant way to do it).</p>
<p style="text-align:justify;">Of course, there are a million ways to enhance the code in this recipe. But on the other hand, isn&#8217;t that true for almost any piece of code?</p>
<p style="text-align:justify;">Cheers and happy injecting!</p>
<br /> Tagged: <a href='http://aamirhussain.com/tag/injection/'>injection</a>, <a href='http://aamirhussain.com/tag/inversion-of-control/'>Inversion-of-Control</a>, <a href='http://aamirhussain.com/tag/psedocode/'>psedocode</a>, <a href='http://aamirhussain.com/tag/pseudo-container/'>Pseudo-Container</a>, <a href='http://aamirhussain.com/tag/python/'>python</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/amirzargar.wordpress.com/165/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/amirzargar.wordpress.com/165/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aamirhussain.com&#038;blog=23048992&#038;post=165&#038;subd=amirzargar&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aamirhussain.com/2011/07/15/sample-pythonic-inversion-of-control-pseudo-container/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>28.459594 77.051530</georss:point>
		<geo:lat>28.459594</geo:lat>
		<geo:long>77.051530</geo:long>
		<media:content url="http://0.gravatar.com/avatar/942a6b34f7b6115469ed71da97ba2d42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aamirhussain055</media:title>
		</media:content>
	</item>
	</channel>
</rss>
