<?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>Joe Lanman &#187; python</title>
	<atom:link href="http://www.joelanman.com/by_tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.joelanman.com</link>
	<description>Web design and development when not playing computer games</description>
	<lastBuildDate>Sat, 18 Dec 2010 19:10:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Uploading images and making thumbnails with web.py and PIL</title>
		<link>http://www.joelanman.com/2008/09/uploading-images-and-making-thumbnails-with-webpy-and-pil/</link>
		<comments>http://www.joelanman.com/2008/09/uploading-images-and-making-thumbnails-with-webpy-and-pil/#comments</comments>
		<pubDate>Sat, 27 Sep 2008 18:02:26 +0000</pubDate>
		<dc:creator>Joe Lanman</dc:creator>
				<category><![CDATA[Back-end]]></category>
		<category><![CDATA[pil]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[thumbnail]]></category>
		<category><![CDATA[upload]]></category>
		<category><![CDATA[web.py]]></category>

		<guid isPermaLink="false">http://www.joelanman.com/?p=10</guid>
		<description><![CDATA[I ran into an issue today while trying to implement image uploads. It&#8217;s a very basic implementation: taking files from a POST request, copying them to a local folder, and then making thumbnails of them. I started with the file upload tutorial in the web.py cookbook. The problem I was getting was that the Python [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into an issue today while trying to implement image uploads. It&#8217;s a very basic implementation: taking files from a POST request, copying them to a local folder, and then making thumbnails of them.</p>
<p>I started with the <a href="http://webpy.org/cookbook/fileupload" class="aga aga_2">file upload tutorial</a> in the web.py cookbook.</p>
<p>The problem I was getting was that the Python Image library (PIL) was failing to make the thumbnails, with the error &#8216;image file is truncated&#8217;.</p>
<p><span id="more-10"></span></p>
<p>Guessing that the upload was not yet complete when the thumbnail was initiated, I googled around for some help and came across the idea of &#8216;chunking&#8217;. This method copies the file upload to the destination folder in chunks as it arrives.</p>
<p>From <a href="http://www.aurigma.com/Support/DocViewer/28/WritingServerSideUploadCode.htm.aspx" class="aga aga_3">this tutorial</a>:</p>
<pre>
<code>
while 1:
        chunk = sourceFile.file.read( 10000 )
        if not chunk:
            break
        destFile.write( chunk )
    destFile.close()
</code>
</pre>
<p>I tried this method in web.py, using web.input(file = {}) to get the file field from the POST form, and then file.value.read(10000) in the code above. This fails because file.value does not have a .read() method &#8211; this is a file object method. Using file.file.read(10000) instead seems to work fine.</p>
<p>I&#8217;ve ended up with this:</p>
<pre>
<code>
    def POST(self, path = ''):

        self.path = path

        input = web.input(file = {})

        action = input['action']

        if action == 'upload':

            file = input['file']

            f = open(config.root + 'static/uploads/' + file.filename, "wb")

            while 1:
                chunk = file.file.read(10000 )
                if not chunk:
                    break
                f.write( chunk )
            f.close()

            self.thumbnail(path, file.filename)

        self.output()

    def thumbnail(self, path, filename):

        size = 128, 128

        outfile = open(config.root + 'static/resized/' + filename, "wb")

        im = Image.open(config.root + 'static/uploads/' + filename)
        im.thumbnail(size)
        im.save(outfile, "JPEG")
</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.joelanman.com/2008/09/uploading-images-and-making-thumbnails-with-webpy-and-pil/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Passing variables from a child template to a parent in Mako</title>
		<link>http://www.joelanman.com/2008/09/passing-variables-from-a-child-template-to-a-parent-in-mako/</link>
		<comments>http://www.joelanman.com/2008/09/passing-variables-from-a-child-template-to-a-parent-in-mako/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 15:35:40 +0000</pubDate>
		<dc:creator>Joe Lanman</dc:creator>
				<category><![CDATA[Back-end]]></category>
		<category><![CDATA[bayer]]></category>
		<category><![CDATA[mako]]></category>
		<category><![CDATA[michael]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[templating]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://www.joelanman.com/?p=9</guid>
		<description><![CDATA[After looking at Web.py&#8217;s own templating system, and considering Cheetah, I&#8217;ve ended up going with Mako for now. So far I&#8217;ve found it very straightforward, with some powerful-looking features to expand into in the future. One of the great features is template inheritance &#8211; but I hit an issue recently with passing a variable from [...]]]></description>
			<content:encoded><![CDATA[<p>After looking at Web.py&#8217;s own templating system, and considering Cheetah, I&#8217;ve ended up going with <a href="http://www.makotemplates.org/" class="aga aga_7">Mako</a> for now. So far I&#8217;ve found it very straightforward, with some powerful-looking features to expand into in the future.</p>
<p>One of the great features is template inheritance &#8211; but I hit an issue recently with passing a variable from a child template to a parent. It&#8217;s not immediately obvious how to do this, but I found the answer on the Mako Google Groups archive (<a href="http://markmail.org/message/wjbo3wvmw3zygmxj#query:mako%20pass%20variable%20to%20parent%20template+page:1+mid:wjbo3wvmw3zygmxj+state:results" class="aga aga_8">link here</a>):</p>
<p>Child:</p>
<pre>
<code>
&lt;%inherit file="meta.html"/&gt;

&lt;%def name="vars()"&gt;

	&lt;% return (meta.id,) %&gt;

&lt;/%def&gt;
</code>
</pre>
<p>Parent:</p>
<pre>
<code>
&lt;% (id,) = self.vars() %&gt;
</code>
</pre>
<p>On a side note, I found out today that two of the Python libraries I&#8217;m using, Mako and SQLAlchemy, were coded by the same guy &#8211; Michael Bayer. Clever chap &#8211; not only are the libraries pretty impressive, but they are both well documented and he regularly contributes to the mailing lists for both projects. <em>And</em> he doesn&#8217;t even have a big internet head like some developers &#8211; though I tracked his blog down here:</p>
<p><a href="http://techspot.zzzeek.org/" class="aga aga_9">http://techspot.zzzeek.org/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.joelanman.com/2008/09/passing-variables-from-a-child-template-to-a-parent-in-mako/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making a copy of a SQLAlchemy object</title>
		<link>http://www.joelanman.com/2008/09/making-a-copy-of-a-sqlalchemy-object/</link>
		<comments>http://www.joelanman.com/2008/09/making-a-copy-of-a-sqlalchemy-object/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 17:12:47 +0000</pubDate>
		<dc:creator>Joe Lanman</dc:creator>
				<category><![CDATA[Back-end]]></category>
		<category><![CDATA[copy]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sqlalchemy]]></category>

		<guid isPermaLink="false">http://www.joelanman.com/?p=8</guid>
		<description><![CDATA[Should that be &#8216;a&#8217; or &#8216;an&#8217; SQLAlchemy object? My current CMS project is based on creating templates in the back-end, and then using these to produce &#8216;inherited&#8217; objects with the same attributes. I quickly found that it&#8217;s surprisingly tricky to grab a template object using SQLAlchemy, and copy it into a similar, but separate, entity. [...]]]></description>
			<content:encoded><![CDATA[<p>Should that be &#8216;a&#8217; or &#8216;an&#8217; SQLAlchemy object?</p>
<p>My current CMS project is based on creating templates in the back-end, and then using these to produce &#8216;inherited&#8217; objects with the same attributes.</p>
<p><img src="http://gonet.cz/~tri65dnigalerie/porad/250/3/385fe0f0f6919d0c88db26dac9ff1fb1.jpg" alt="multiplicity" /></p>
<p>I quickly found that it&#8217;s surprisingly tricky to grab a template object using SQLAlchemy, and copy it into a similar, but separate, entity. Using the Python &#8216;copy&#8217; function confuses SQLAlchemy &#8211; when you try to save it, it thinks it is merely a representation of the original (template) object.</p>
<p>Here&#8217;s how I create separate &#8216;clones&#8217; from my template objects, defining a function on the object itself:</p>
<p><span id="more-8"></span></p>
<pre>
<code>
def clone (self, source, table):

    for c in table.c:

        if not c.name.endswith('id'):

            setattr(self, c.name, getattr(source, c.name))
</code>
</pre>
<p>Here&#8217;s a usage example:</p>
<pre>
<code>
template_meta = self.session.query(Meta).filter(and_(Meta.is_template == 1, Meta.id == id)).one()

new_meta = Meta(new_object.id)

new_meta.clone(template_meta, table_meta)
</code>
</pre>
<p>The clone function takes the object&#8217;s table as an argument, so that only the basic column values are copied. The table object has an attribute called c &#8211; these are the table columns in long form (eg. meta.id, meta.title, etc). Using c.name gives us just the name of the column.</p>
<p>Looping through the source object&#8217;s attributes would result in SQLA attributes and any child objects being copied too, which is not what we want.</p>
<p>Notice too that anything ending in &#8216;id&#8217; is ignored &#8211; otherwise we would get the original object&#8217;s relationships &#8211; again this is not what I want in this situation. The new object should have its own relationships.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joelanman.com/2008/09/making-a-copy-of-a-sqlalchemy-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python-style startswith and endswith string functions in Javascript</title>
		<link>http://www.joelanman.com/2008/07/python-style-startswith-and-endswith-string-functions-in-javascript/</link>
		<comments>http://www.joelanman.com/2008/07/python-style-startswith-and-endswith-string-functions-in-javascript/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 17:06:49 +0000</pubDate>
		<dc:creator>Joe Lanman</dc:creator>
				<category><![CDATA[Front-end]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[strings]]></category>

		<guid isPermaLink="false">http://www.joelanman.com/?p=4</guid>
		<description><![CDATA[The Python language has useful string methods to search for prefixes and suffixes: my_string.startswith('pre') The same thing can be achieved in Javascript using the rather powerful search and replace methods. These methods can either accept strings or regular expressions.  To search for a plain string, use quote marks, for example: my_string.search('tom'); This will return the [...]]]></description>
			<content:encoded><![CDATA[<p>The Python language has useful string methods to search for prefixes and suffixes:</p>
<pre><code>
my_string.startswith('pre')</code></pre>
<p>The same thing can be achieved in Javascript using the rather powerful search and replace methods.</p>
<p><span id="more-4"></span></p>
<p>These methods can either accept strings or regular expressions.  To search for a plain string, use quote marks, for example:</p>
<pre><code>
my_string.search('tom');</code></pre>
<p>This will return the index of the first occurence of &#8216;tom&#8217; in my_string.</p>
<p>To search for a regular expression, use slashes. Regular expressions use special characters to search for particular patterns in strings &#8211; we&#8217;ll just be using the &#8216;starts with&#8217; (^)  and &#8216;ends with&#8217; ($) characters. Therefore, our Python-style startswith and endswith tests looks like this:</p>
<pre><code>
my_string.search(/^prefix/) ;

my_string.search(/suffix$/);</code></pre>
<p><strong>Note from <a href="http://x13n.com/alex" class="aga aga_11">Alex Macmillan</a></strong> (honorary proof reader)</p>
<p>While the Python functions return a boolean value depending on the match, the Javascript versions return the index of the matched string. If they are not found, the return value will be -1, not false.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joelanman.com/2008/07/python-style-startswith-and-endswith-string-functions-in-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.592 seconds -->

