<?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/"
	>

<channel>
	<title>Search Nuggets &#187; python</title>
	<atom:link href="http://blog.comperiosearch.com/blog/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.comperiosearch.com</link>
	<description>A blog about Search as THE solution</description>
	<lastBuildDate>Mon, 13 Jun 2016 08:59:45 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>Using iPython notebooks and Pycharm together</title>
		<link>http://blog.comperiosearch.com/blog/2015/05/11/using-ipython-notebooks-and-pycharm-together/</link>
		<comments>http://blog.comperiosearch.com/blog/2015/05/11/using-ipython-notebooks-and-pycharm-together/#comments</comments>
		<pubDate>Mon, 11 May 2015 11:12:24 +0000</pubDate>
		<dc:creator><![CDATA[André Lynum]]></dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[autoreload]]></category>
		<category><![CDATA[ipython notebook]]></category>
		<category><![CDATA[pycharm]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.comperiosearch.com/?p=3626</guid>
		<description><![CDATA[IPython notebooks have become an indispensable tool for many Python developers. They are a reasonably good environment for interactive computing, can contain inline data visualisations and can be hosted remotely for sharing results or working together with other developers. In many academic environments and increasingly in industry IPython notebooks are used for data visualisation work [...]]]></description>
				<content:encoded><![CDATA[<p><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-7.png" alt="ipython-blog-7" width="282" height="70" class="aligncenter size-large wp-image-3628" /><br />
<a href="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-6.png"><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-6.png" alt="ipython-blog-6" width="600" class="aligncenter size-full wp-image-3627" /></a></p>
<p />
IPython notebooks have become an indispensable tool for many Python developers. They are a reasonably good environment for interactive computing, can contain inline data visualisations and can be hosted remotely for sharing results or working together with other developers. In many academic environments and increasingly in industry IPython notebooks are used for data visualisation work and exploratory programming, depending on the IPython interactive environment for fast prototyping of ideas.</p>
<p />
As nice an environment we have in IPython, I often wish for the features of a full-fledged IDE. Here at Comperio we use PyCharm a lot which has excellent code editing, semantic completion, a graphical debugger and efficient code navigaton capabilities. In this blog post I’m going to show how you can simultaneously work on code in both the IDE and IPython notebook or interactive shell while keeping the running notebook and IDE project in sync.</p>
<p />
Hey, PyCharm already have IPython notebook integration. What about that? Personally I find that the IPython notebook integration in the latest PyCharm (version 4.0.6)  still isn’t adequate for serious work. You get the the completion and code navigation from PyCharm, but editing and navigation is reduced to half a dozen buttons. Further some functionality such as debugging appears to be plainly non-functional. Regardless there are other very nice IDEs for Python such as Wing or Eclipse, and the approach here will work equally well with them.</p>
<p />
This cunning recipe consists of two spicy ingredients, Both are neat tricks on their own, but together they form a smooth workflow bridging exploratory programming and more structured software engineering. We are going to:</p>
<p />
<ul>
<li>Install our code as an editable Pip package.</li>
<li>Use the IPython autoreload extension to dynamically reload code.</li>
</ul>
<p />
So let’s get cooking!</p>
<p />
<h2>Editable Pip packages</h2>
<p />
We are going to organise our code in a Python package and install it with Pip using the <code>-e</code> or <code>—editable</code> option. This installs the package as it is pointing to our project directory and that we are always importing the code that we are editing. We could also accomplish this with some hacking on <code>sys.path</code> or <code>PYTHONPATH</code>, but having our code available as a package is a lot more seamless. It makes sense to use <code>virtualenv</code> (or EPD/Anaconda environments) to isolate your system Python from your development packages.</p>
<p />
<p><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-1.png"><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-1-300x190.png" alt="ipython-blog-1" width="300" height="190" class="aligncenter size-medium wp-image-3620" /></a></p>
<p />
First we create Python project in PyCharm, add source folder with <code>setup.py</code> defining a basic python package.</p>
<p><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-2.png"><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-2-300x194.png" alt="ipython-blog-2" width="300" height="194" class="aligncenter size-medium wp-image-3621" /></a></p>
<p />
Then we create stub file with the following code in our python module and a folder for our notebooks.</p>
<p />
<p></p><pre class="crayon-plain-tag">def get_page():
    print "Don't know how to do this yet."</pre><p> </p>
<p><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-3.png"><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-3-300x158.png" alt="ipython-blog-3" width="300" height="158" class="aligncenter size-medium wp-image-3622" /></a></p>
<p />
And we activate our <code>virtualenv</code>/Conda environment and run <code>pip install -e </code>.</p>
<p />
<strong>Pip and Git: </strong>If you install your package with <code>-e</code> from a Git repository it may think that you want to install from Git even if you&#8217;re giving it a file path. This is usually not what you want when you&#8217;re developing since you would have to commit your code for the package to update itself. An ugly but practical way to avoid this behavior is to move the .git folder out of the way when installing the package.</p>
<p />
<h2>%autoreloading code</h2>
<p />
Now to the important part which is dynamically loading the IDE project into our IPython notebook. Let’s first fire up the notebook.</p>
<p><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-4.png"><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-4-300x71.png" alt="ipython-blog-4" width="300" height="71" class="aligncenter size-medium wp-image-3623" /></a></p>
<p />
Start iPython and create a notebook.</p>
<p />
You have probably used reload(module) to update the Python environment at runtime. This hardly ever works for more than five minutes and results in an inscrutable mess of old new stuff in your modules and classes.There are however a bag of neat tricks taking care of at least the majority of the problems around reloading Python code or modules (see http://pyunit.sourceforge.net/notes/reloading.html),  and the IPython developers has collected these into their autoreload extension. Let’s look at it in action.</p>
<p><a href="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-5.png"><img src="http://blog.comperiosearch.com/wp-content/uploads/2015/05/ipython-blog-5-300x137.png" alt="ipython-blog-5" width="300" height="137" class="aligncenter size-medium wp-image-3624" /></a></p>
<p />
Here we set up the autoreload module and import our stub function in the first two cells. In the third we run our function. We then change the function definition in the IDE and save the file.</p>
<p></p><pre class="crayon-plain-tag">def get_page():
    print "Hey I'm updated."</pre><p> </p>
<p />
<p>And when we run the function again in cell four the updated code is run.</p>
<p />
<h2>From notebook to project and back</h2>
<p />
Combining editable Pip packages and the autoreload module we have a way to seamlessly load our project code in the notebook. When we are ready to move our exploratory programming back to project we can move our code over, import any new definitions and refine our implementation while using it in our notebook. In this way we can quickly move from noodling around in the notebook to developing and testing in the IDE and move back to the notebook to use our project code in further unstructured meanderings.</p>
<p />
In the next post we will demonstrate this in more detail.</p>
<p />
]]></content:encoded>
			<wfw:commentRss>http://blog.comperiosearch.com/blog/2015/05/11/using-ipython-notebooks-and-pycharm-together/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Crafting elasticsearch queries in Python.</title>
		<link>http://blog.comperiosearch.com/blog/2014/12/17/crafting-elasticsearch-queries-python-hassle-free-way/</link>
		<comments>http://blog.comperiosearch.com/blog/2014/12/17/crafting-elasticsearch-queries-python-hassle-free-way/#comments</comments>
		<pubDate>Wed, 17 Dec 2014 12:36:39 +0000</pubDate>
		<dc:creator><![CDATA[Mats Julian Olsen]]></dc:creator>
				<category><![CDATA[Elasticsearch]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Json]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.comperiosearch.com/?p=3120</guid>
		<description><![CDATA[Creating JSON-like structures in Python (or any other programming language), can be a cumbersome experience. Consider this snippet from the elasticsearch-py library, taken from the example/query.py file: I would argue that 33 lines for creating the facets above is too much. To save you from the dreaded hassle of writing JSON in your programs, I [...]]]></description>
				<content:encoded><![CDATA[<p>Creating JSON-like structures in Python (or any other programming language), can be a cumbersome experience. Consider this snippet from the <a title="elasticsearch-py" href="https://github.com/elasticsearch/elasticsearch-py">elasticsearch-py</a> library, taken from the <a href="https://github.com/elasticsearch/elasticsearch-py/blob/master/example/queries.py">example/query.py </a>file:</p>
<script src="https://gist.github.com/6a30b86a010f361b9256.js"></script>
<p>I would argue that 33 lines for creating the facets above is too much.</p>
<p>To save you from the dreaded hassle of writing JSON in your programs, I created <a href="https://github.com/mewwts/addict"><em>addict,</em></a> a Python dictionary that let&#8217;s you create nested dictionaries using get- and set attribute syntax.</p>
<p>Here&#8217;s how the same code looks with addict<em>:</em></p>
<script src="https://gist.github.com/49e0fdf5bff3aeda521a.js"></script>
<p><a href="https://github.com/mewwts/addict"><em>addict</em></a> is open-source and MIT-licensed. It is actively developed and anyone interested is encouraged to submit issues and pull requests, as both are more than welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.comperiosearch.com/blog/2014/12/17/crafting-elasticsearch-queries-python-hassle-free-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
