<?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>Standard Deviations &#187; Python</title>
	<atom:link href="http://parand.com/say/index.php/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://parand.com/say</link>
	<description>Parand Tony Darugar: A Cruel and Petty Dictator</description>
	<lastBuildDate>Sun, 25 Jul 2010 02:36:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Python-Oauth2 To Access OAuth Protected Resources</title>
		<link>http://parand.com/say/index.php/2010/06/13/using-python-oauth2-to-access-oauth-protected-resources/</link>
		<comments>http://parand.com/say/index.php/2010/06/13/using-python-oauth2-to-access-oauth-protected-resources/#comments</comments>
		<pubDate>Sun, 13 Jun 2010 23:06:15 +0000</pubDate>
		<dc:creator>Parand</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://parand.com/say/?p=1058</guid>
		<description><![CDATA[Most of the examples I found for python-oauth2 show how to use the library to request and approve tokens, but not how to use the access token to access a protected resource (ie. how to actually make a call to the service you&#8217;re trying to access). Here&#8217;s an example:

import oauth2 as oauth
consumer = oauth.Consumer('consumer-key-here','consumer-secret-here')
token = [...]]]></description>
			<content:encoded><![CDATA[<p>Most of the examples I found for <a href="http://github.com/simplegeo/python-oauth2">python-oauth2</a> show how to use the library to request and approve tokens, but not how to use the access token to access a protected resource (ie. how to actually make a call to the service you&#8217;re trying to access). Here&#8217;s an example:</p>
<pre><code lang="python">
import oauth2 as oauth
consumer = oauth.Consumer('consumer-key-here','consumer-secret-here')
token = oauth.Token('access-key-here','access-key-secret-here')
client = oauth.Client(consumer, token)
response = client.request('http://someservice.com/api/something/')
</code></pre>
<p>And here&#8217;s how you make a POST call:</p>
<pre><code lang="python">
import urllib
response, content = myclient.request("http://someservice.com/api/something/", \
    method="POST", body=urllib.urlencode({'name': 'value', 'another_name': 'another value'}) )
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://parand.com/say/index.php/2010/06/13/using-python-oauth2-to-access-oauth-protected-resources/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django: Using The Permission System</title>
		<link>http://parand.com/say/index.php/2010/02/19/django-using-the-permission-system/</link>
		<comments>http://parand.com/say/index.php/2010/02/19/django-using-the-permission-system/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 21:37:03 +0000</pubDate>
		<dc:creator>Parand</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Django]]></category>

		<guid isPermaLink="false">http://parand.com/say/?p=1002</guid>
		<description><![CDATA[I was surprised at how little information I found on making use of Django&#8217;s permission system. Here are some quick notes on one way to use it:
Groups are groups of users. For example, you could define a group of users who have premium accounts, or have been verified in some way, or are somehow special:

from [...]]]></description>
			<content:encoded><![CDATA[<p>I was surprised at how little information I found on making use of Django&#8217;s permission system. Here are some quick notes on one way to use it:</p>
<p><em>Groups</em> are groups of users. For example, you could define a group of users who have premium accounts, or have been verified in some way, or are somehow special:</p>
<pre><code lang="python">
from django.contrib.auth.models import Group, Permission
special_users = Group(name='Special Users')
special_users.save()
really_special_users = Group(name='Super Special Users')
really_special_users.save()
</code></pre>
<p>Now you have two groups defined and can define permissions for them. Django associates permissions with models (note: not model instances, but models). You&#8217;ll need to select a model to apply the permissions to, and do a small dance with &#8220;ContentType&#8221; to find that model&#8217;s content type:</p>
<pre><code lang="python">
from django.contrib.contenttypes.models import ContentType
somemodel_ct = ContentType.objects.get(app_label='myapp', model='somemodel')

can_view = Permission(name='Can View', codename='can_view_something',
                       content_type=somemodel_ct)
can_view.save()

can_modify = Permission(name='Can Modify', codename='can_modify_something',
                       content_type=somemodel_ct)
can_modify.save()
</code></pre>
<p>You&#8217;ve now defined two permissions and can associate them with your Groups:</p>
<pre><code lang="python">
special_users.permissions.add(can_view)
really_special_users.permissions = [can_view, can_modify]
</code></pre>
<p>Our groups and their associated permissions are ready to go. Now we just have to associate these permissions with users:</p>
<pre><code lang="python">
jack=User.objects.get(email='jack@test.com')
jack.groups.add(special_users)

jill=User.objects.get(email='jill@test.com')
jill.groups.add(really_special_users)
</code></pre>
<p>We&#8217;re all done. Now we can check the users&#8217; permissions:</p>
<pre><code lang="python">
&gt;&gt;&gt; jack.has_perm('myapp.can_view_something')
True
&gt;&gt;&gt; jack.has_perm('myapp.can_modify_something')
False

&gt;&gt;&gt; jill.has_perm('myapp.can_view_something')
True
&gt;&gt;&gt; jill.has_perm('myapp.can_modify_something')
True
</code></pre>
<p>And to use it in your templates:</p>
<pre><code lang="html">
{% if perms.myapp.can_view_something %}
Here is something for you to see.
{% else %}
Can't show you!
{% endif %}
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://parand.com/say/index.php/2010/02/19/django-using-the-permission-system/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Django-mptt: Tree Storage in Django: A Brief Overview</title>
		<link>http://parand.com/say/index.php/2010/01/24/django-mptt-tree-storage-in-django-a-brief-overview/</link>
		<comments>http://parand.com/say/index.php/2010/01/24/django-mptt-tree-storage-in-django-a-brief-overview/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 00:01:16 +0000</pubDate>
		<dc:creator>Parand</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[mptt]]></category>

		<guid isPermaLink="false">http://parand.com/say/?p=976</guid>
		<description><![CDATA[django-mptt is a library for storing tree oriented data using the Django ORM. It allows you to place your model instances into a tree structure and efficiently query for ancestors and children.
Here&#8217;s a brief tutorial on how to use it:
After installing, you&#8217;ll need to modify your model to include a &#8220;parent&#8221; field, and register it [...]]]></description>
			<content:encoded><![CDATA[<p><a target="blank" href="http://code.google.com/p/django-mptt/">django-mptt</a> is a library for storing tree oriented data using the Django ORM. It allows you to place your model instances into a tree structure and efficiently query for ancestors and children.</p>
<p>Here&#8217;s a brief tutorial on how to use it:</p>
<p>After installing, you&#8217;ll need to modify your model to include a &#8220;parent&#8221; field, and register it with mptt:</p>
<pre><code lang="python">class Person(models.Model):
    contact   = models.ForeignKey( Contact, db_index=True )
    role      = models.CharField(max_length=20, blank=True)
    parent    = models.ForeignKey('self', null=True, blank=True, related_name='children')

    def __unicode__(self):
        return "Person: &lt;%s&gt;" % (self.contact.email, )

mptt.register(Person)
</code></pre>
<p>mptt dynamically adds fields to your model, so you&#8217;ll need to syncdb after you&#8217;ve added the parent attribute and the mptt.register call to your model.</p>
<p>The basics are fairly easy to use:</p>
<p>To move a node to the root of the tree, use move_to with a targe of None:</p>
<pre><code lang="python">person1.move_to(None)
person1.save()
</code></pre>
<p>To make a node the child of another, set its parent:</p>
<pre><code lang="python">person2.parent = person1
person2.save()
</code></pre>
<p>To find the children of a node, use the children field:</p>
<pre><code lang="python">&gt;&gt;&gt;person1.children.all()
[&lt;Person: Person: &lt;test2@testing.com&gt;&gt;, &lt;Person: Person: &lt;test3@testing.com&gt;&gt;]
</code></pre>
<p>
Here&#8217;s a little snippet of code to setup a 15 node tree where each node has two child nodes:<br />
<br /><font color="red">[UPDATE] The code in this snippet is not correct &#8211; you have to save each node as you update it, then look it up again. You can&#8217;t modify a node, save it, then use the reference you already have for it. I&#8217;ll update the code when I get a chance</font>
</p>
<pre><code lang="python">contacts = []
people = []
for n in range(15):
    c = mod.Contact(email="test" + str(n) + "@testing.com")
    c.save()
    contacts.append(c)
    p = mod.Person(contact=c)
    p.save()
    people.append(p)

people[0].move_to(None)  # Root
people[0].save()
for n in range(1,15):
    people[n].parent = people[(n-1)/2]
    people[n].save()
</code></pre>
<p>Now let&#8217;s take a look around:</p>
<pre><code lang="python">&gt;&gt;&gt;people[7].parent
&lt;Person: Person: &lt;test3@testing.com&gt;&gt;

&gt;&gt;&gt;people[3].children.all()
[&lt;Person: Person: &lt;test7@testing.com&gt;&gt;, &lt;Person: Person: &lt;test8@testing.com&gt;&gt;]
</code></pre>
<p>Now let&#8217;s move things around a bit; we&#8217;ll take person3, which is 2 levels down from the root, and make it a direct child of the root:</p>
<pre><code lang="python">&gt;&gt;&gt;people[3].parent = people[0]
&gt;&gt;&gt;people[3].save()

&gt;&gt;&gt;people[0].children.all()
[&lt;Person: Person: &lt;test1@testing.com&gt;&gt; &lt;Person: Person: &lt;test2@testing.com&gt;&gt;, &lt;Person: Person: &lt;test3@testing.com&gt;&gt;]
</code></pre>
<p>And we can look at the ancestors of a given node:</p>
<pre><code lang="python">people[14].get_ancestors()</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://parand.com/say/index.php/2010/01/24/django-mptt-tree-storage-in-django-a-brief-overview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Eclipse + PyDev : I Recommend It</title>
		<link>http://parand.com/say/index.php/2009/10/11/eclipse-pydev-i-recommend-it/</link>
		<comments>http://parand.com/say/index.php/2009/10/11/eclipse-pydev-i-recommend-it/#comments</comments>
		<pubDate>Sun, 11 Oct 2009 18:46:42 +0000</pubDate>
		<dc:creator>Parand</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[pydev]]></category>

		<guid isPermaLink="false">http://parand.com/say/?p=882</guid>
		<description><![CDATA[I used to be a vi guy who finally made the move to graphical editors. I looked for the simplest, lightest possible solutions, using ConTEXT for quite a while.
Some years ago I was forced into using Eclipse for reasons I can&#8217;t quite recall; probably Java development. I didn&#8217;t like it &#8211; the forced Project concept, [...]]]></description>
			<content:encoded><![CDATA[<p>I used to be a vi guy who finally made the move to graphical editors. I looked for the simplest, lightest possible solutions, using <a href="http://www.contexteditor.org/" target="_blank">ConTEXT</a> for quite a while.</p>
<p>Some years ago I was forced into using Eclipse for reasons I can&#8217;t quite recall; probably Java development. I didn&#8217;t like it &#8211; the forced Project concept, the bloat, the general slowness.</p>
<p>Eventually I got comfortable with it, got <a href="http://pydev.org/" target="_blank">PyDev</a> installed, and made it my primary development environment. These days most of my development lives in Eclipse.</p>
<p><img class="alignleft" title="PyDev Logo" src="http://pydev.org/images/pydev_banner2.gif" alt="" width="272" height="112" /></p>
<p>With the 1.5 release PyDev included quite a few previously pay-only features in the free / open source version. Since then I&#8217;ve found I&#8217;ve become even more productive in the environment, and now actually enjoy it.</p>
<p>In particular, the code analysis is very useful. I love the fact that it points out unused imports and variables as well as syntax errors. Going through old code I was surprised at how many spurious imports I had, as well as a few actual errors in code that had been in production for several years in rarely exercised branches.</p>
<p>If you&#8217;re doing python development I recommend you take a look at Eclipse+PyDev. I was surprised at the level of increased productivity it brought me.</p>
]]></content:encoded>
			<wfw:commentRss>http://parand.com/say/index.php/2009/10/11/eclipse-pydev-i-recommend-it/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Python Simple Mock Object</title>
		<link>http://parand.com/say/index.php/2009/06/19/python-simple-mock-object/</link>
		<comments>http://parand.com/say/index.php/2009/06/19/python-simple-mock-object/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 19:24:58 +0000</pubDate>
		<dc:creator>Parand</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://parand.com/say/?p=808</guid>
		<description><![CDATA[Another one of those that&#8217;s mainly for my own notes. I needed to create a mock object that always a returns an empty list no matter what method is called on it. That is, you can instantiate one of these and call any made up method on it and get an empty list back. Here [...]]]></description>
			<content:encoded><![CDATA[<p>Another one of those that&#8217;s mainly for my own notes. I needed to create a mock object that always a returns an empty list no matter what method is called on it. That is, you can instantiate one of these and call any made up method on it and get an empty list back. Here is it:</p>
<pre><code lang="python">
class Dummy:
	def __getattr__(self, name): return lambda *args: []
d = Dummy()
d.something()   # returns []
d.something_else("a",1,"b")   # returns []
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://parand.com/say/index.php/2009/06/19/python-simple-mock-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python SMTP Debugging Server</title>
		<link>http://parand.com/say/index.php/2009/05/29/python-smtp-debugging-server/</link>
		<comments>http://parand.com/say/index.php/2009/05/29/python-smtp-debugging-server/#comments</comments>
		<pubDate>Sat, 30 May 2009 07:57:27 +0000</pubDate>
		<dc:creator>Parand</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://parand.com/say/?p=805</guid>
		<description><![CDATA[Note to self: this is how you start a python SMTP debugging server:
python -m smtpd -n -c DebuggingServer localhost:25
]]></description>
			<content:encoded><![CDATA[<p>Note to self: this is how you start a python SMTP debugging server:</p>
<pre>python -m smtpd -n -c DebuggingServer localhost:25</pre>
]]></content:encoded>
			<wfw:commentRss>http://parand.com/say/index.php/2009/05/29/python-smtp-debugging-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django-Piston: REST Framework for Django</title>
		<link>http://parand.com/say/index.php/2009/04/30/django-piston-rest-framework-for-django/</link>
		<comments>http://parand.com/say/index.php/2009/04/30/django-piston-rest-framework-for-django/#comments</comments>
		<pubDate>Fri, 01 May 2009 01:38:51 +0000</pubDate>
		<dc:creator>Parand</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://parand.com/say/?p=798</guid>
		<description><![CDATA[Django-Piston in a promising looking REST frameowork for Django. On first inspection it seems to have all the right attributes and setup. I hope to give it a try soon.
[Update] By the way, I&#8217;ve been using django-piston in a real project, like it quite a bit. I recommend it.
One question I have &#8211; while I [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://bitbucket.org/jespern/django-piston/wiki/Home" target="_blank">Django-Piston</a> in a promising looking REST frameowork for Django. On first inspection it seems to have all the right attributes and setup. I hope to give it a try soon.</p>
<p>[Update] By the way, I&#8217;ve been using django-piston in a real project, like it quite a bit. I recommend it.</p>
<p>One question I have &#8211; while I agree <a href="http://bitbucket.org/jespern/django-piston/wiki/Documentation#resources" target="_blank">HTTP PUT and DELETE are the right verbs to use for Update and Delete</a>, in practice they&#8217;re not well supported and can cause confusion. I&#8217;m wondering if there&#8217;s a way to change the mapping to the following:</p>
<pre><code>POST /resource  -- Create
POST /resource/id -- Update
POST /resource/id?action=delete -- Delete
GET / resource/id -- Read
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://parand.com/say/index.php/2009/04/30/django-piston-rest-framework-for-django/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Python Simple Inheritance Example</title>
		<link>http://parand.com/say/index.php/2009/04/20/python-simple-inheritance-example/</link>
		<comments>http://parand.com/say/index.php/2009/04/20/python-simple-inheritance-example/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 20:21:09 +0000</pubDate>
		<dc:creator>Parand</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://parand.com/say/?p=793</guid>
		<description><![CDATA[Another one of those things I tend to forget the syntax of, noting for easy future lookup:
 

class Base():
    def __init__(self, param):
        print "Base:", param
    def method(self, param):
        print "Base.method:", param

class Derived(Base):
    def [...]]]></description>
			<content:encoded><![CDATA[<p>Another one of those things I tend to forget the syntax of, noting for easy future lookup:</p>
<p> 
<pre><code long="python">
class Base():
    def __init__(self, param):
        print "Base:", param
    def method(self, param):
        print "Base.method:", param

class Derived(Base):
    def __init__(self, param):
        Base.__init__(self, param)
        print "Derived:", param
    def method(self, param):
        Base.method(self, param)
        print "Derived.method:", param

>>> d = Derived("me")
Base: me
Derived: me
>>> d.method("you")
Base.method: you
Derived.method: you
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://parand.com/say/index.php/2009/04/20/python-simple-inheritance-example/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Python S3 Library For Chunked / Streaming Download</title>
		<link>http://parand.com/say/index.php/2009/03/13/python-s3-library-for-chunked-streaming-download/</link>
		<comments>http://parand.com/say/index.php/2009/03/13/python-s3-library-for-chunked-streaming-download/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 20:18:39 +0000</pubDate>
		<dc:creator>Parand</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[s3]]></category>

		<guid isPermaLink="false">http://parand.com/say/?p=772</guid>
		<description><![CDATA[Born of a need to deal with multi-gig files on S3, I&#8217;ve modified the Python Amazon S3 library to allow you to read data in chunks, as well as a simple file-like object that lets you to read the file one line at a time (ala for line in f ).
The plan was to create this [...]]]></description>
			<content:encoded><![CDATA[<p>Born of a need to deal with multi-gig files on S3, I&#8217;ve modified the Python Amazon S3 library to allow you to read data in chunks, as well as a simple file-like object that lets you to read the file one line at a time (ala <em>for line in f </em>).</p>
<p>The plan was to create this as a patch against <a href="http://developer.amazonwebservices.com/connect/entry.jspa?externalID=134" target="_blank">the official Python S3 library from Amazon</a> (it&#8217;s only a small change), and possibly even do a github thing, but it&#8217;s evident I&#8217;ll never get around to it, so I&#8217;m simply <a href="/projects/s3/S3T.py.txt">uploading it here</a>. </p>
<p>The small change is the addition of an optional <em>readbody</em> argument to <em>AWSAuthConnection.get</em> that tells the library not to read the body of the message, and a S3File class that provides the line interface. Here&#8217;s an example of using the S3File class:</p>
<pre><code lang="python">
if use_S3:
    f = S3.S3File(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, BUCKET, FILE)
else:
    f = file(conf.local_location + '/' + FILE)

for line in f:
    # do something
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://parand.com/say/index.php/2009/03/13/python-s3-library-for-chunked-streaming-download/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Use Python&#8217;s Marshal For Faster Serialization</title>
		<link>http://parand.com/say/index.php/2009/03/11/use-pythons-marshal-for-faster-serialization/</link>
		<comments>http://parand.com/say/index.php/2009/03/11/use-pythons-marshal-for-faster-serialization/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 00:49:23 +0000</pubDate>
		<dc:creator>Parand</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://parand.com/say/?p=769</guid>
		<description><![CDATA[I&#8217;d been using Python&#8217;s cPickle module for my serializing data structures to disk. As my data size got larger I started to care about performance, and after a few searches ended up at the marshal module. It comes with a few caveats, but is working great, much faster for me than cPickle. So if you&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d been using Python&#8217;s cPickle module for my serializing data structures to disk. As my data size got larger I started to care about performance, and after a few searches ended up at the <a href="http://docs.python.org/library/marshal.html" target="_blank">marshal</a> module. It comes with a few caveats, but is working great, much faster for me than cPickle. So if you&#8217;re looking for performance and can live with the caveats, give marshal a try.</p>
]]></content:encoded>
			<wfw:commentRss>http://parand.com/say/index.php/2009/03/11/use-pythons-marshal-for-faster-serialization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
