<?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>Pressing the Red Button &#187; python</title>
	<atom:link href="http://www.pressthered.com/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pressthered.com</link>
	<description>Adventures in Software Development</description>
	<lastBuildDate>Sun, 08 Jan 2012 21:28:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Importing a List with BulkLoader</title>
		<link>http://www.pressthered.com/importing_a_list_with_bulkloader/</link>
		<comments>http://www.pressthered.com/importing_a_list_with_bulkloader/#comments</comments>
		<pubDate>Sat, 19 Jun 2010 06:16:59 +0000</pubDate>
		<dc:creator>jim.richmond</dc:creator>
				<category><![CDATA[Tech Tips]]></category>
		<category><![CDATA[google app engine]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.pressthered.com/?p=814</guid>
		<description><![CDATA[The latest bulkloader script (api version 1.3.4) does a much better job of exporting data when you auto generate a .yaml file. However it doesn&#8217;t handle the re-import of lists by default (it just treats them as strings). You can add an inline lambda function to the .yaml file to fix this. This handles lists [...]]]></description>
			<content:encoded><![CDATA[<p>The latest bulkloader script (api version 1.3.4) does a much better job of exporting data when you auto generate a .yaml file. However it doesn&#8217;t handle the re-import of lists by default (it just treats them as strings). You can add an inline lambda function to the .yaml file to fix this. This handles lists of strings, but it can probably be modified to handle other types.</p>
<p>- property: mylist<br />
      external_name: mylist<br />
      <strong>import_transform: &#8220;lambda x: len(x) > 0 and eval(x) or &#8221;&#8221;</strong></p>
<p><strong>Why it works:</strong><br />
In your csv, your list should export like so:<br />
&#8220;[u'item1', u'item2', u'item3']&#8220;<br />
Running an eval on this string, converts it to a list. The length check is like if/else statement, if len(x) > 0 convert it else use empty string.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pressthered.com/importing_a_list_with_bulkloader/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Delete an Index in an AppEngine Datastore</title>
		<link>http://www.pressthered.com/how_to_delete_an_index_in_an_appengine_datastore/</link>
		<comments>http://www.pressthered.com/how_to_delete_an_index_in_an_appengine_datastore/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 03:23:49 +0000</pubDate>
		<dc:creator>jim.richmond</dc:creator>
				<category><![CDATA[Tech Tips]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Google AppEngine]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.pressthered.com/?p=804</guid>
		<description><![CDATA[If using Python, you can delete an index by first removing the entry in your index.yaml file, and then updating your site. Then you must issue this command: appcfg.py vacuum_indexes myapp/ With Django (what I am using), you use this command: manage.py vacuum_indexes What is nice about the process is that, for each index that [...]]]></description>
			<content:encoded><![CDATA[<p>If using Python, you can delete an index by first removing the entry in your index.yaml file, and then updating your site. Then you must issue this command:<br />
<strong>appcfg.py vacuum_indexes myapp/</strong></p>
<p>With Django (what I am using), you use this command:<br />
<strong>manage.py vacuum_indexes</strong></p>
<p>What is nice about the process is that, for each index that has been removed from your index.yaml, it will confirm whether you want to delete it or not. So you get a second chance to make sure you are right. If you delete the wrong index, it&#8217;s going to take some <a href="http://code.google.com/appengine/articles/index_building.html">time for it to rebuild</a>, and portions of your site could be down.</p>
<p><strong>Why do I need this:</strong><br />
I don&#8217;t think you would see a noticeable speed improvement to your data inserts, updates, or deletes by having a few less unused indexes, but it couldn&#8217;t hurt. Also you are limited to 100 indexes (as of April 2010) so this how you free them up.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pressthered.com/how_to_delete_an_index_in_an_appengine_datastore/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google AppEngine: appcfg.py update check failed</title>
		<link>http://www.pressthered.com/google_appengine_appcfgpy_update_check_failed/</link>
		<comments>http://www.pressthered.com/google_appengine_appcfgpy_update_check_failed/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 04:55:18 +0000</pubDate>
		<dc:creator>jim.richmond</dc:creator>
				<category><![CDATA[Tech Tips]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Google AppEngine]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.pressthered.com/?p=800</guid>
		<description><![CDATA[I started getting this error in Windows when running &#8216;manage runserver&#8217; in django on Google AppEngine: appcfg.py update check failed: EOF occurred in violation of protocol This error is related to not having the SSL module for python installed. The easy fix is to install it by running &#8220;easy_install ssl&#8221; from the command prompt. However, [...]]]></description>
			<content:encoded><![CDATA[<p>I started getting this error in Windows when running &#8216;manage runserver&#8217; in django on Google AppEngine:</p>
<p><strong>appcfg.py update check failed: EOF occurred in violation of protocol</strong></p>
<p>This error is related to not having the SSL module for python installed. The easy fix is to install it by running &#8220;easy_install ssl&#8221; from the command prompt. However, using Python 2.5, I received errors and it did not work. </p>
<p>The longer, more involved fix is to compile the SSL module for python. This was a hassle but after about an hour I finally got it working.</p>
<p>Here is the tutorial I used to install the ssl module:<br />
<a href="http://beautifulisbetterthanugly.com/posts/2009/aug/19/compile-ssl-115-python-25-or-lower/">http://beautifulisbetterthanugly.com/posts/2009/aug/19/compile-ssl-115-python-25-or-lower/</a></p>
<p>I also had to follow the troubleshooting section on this link:<br />
<a href="http://wiki.cython.org/InstallingOnWindows">http://wiki.cython.org/InstallingOnWindows</a><br />
because I did not have the python library file (libpython25.a).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pressthered.com/google_appengine_appcfgpy_update_check_failed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Solving &#8220;InvalidSenderError: Unauthorized sender&#8221; in AppEngine</title>
		<link>http://www.pressthered.com/solving_invalidsendererror_unauthorized_sender_in_appengine/</link>
		<comments>http://www.pressthered.com/solving_invalidsendererror_unauthorized_sender_in_appengine/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 06:12:19 +0000</pubDate>
		<dc:creator>jim.richmond</dc:creator>
				<category><![CDATA[Tech Tips]]></category>
		<category><![CDATA[Google AppEngine]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.pressthered.com/?p=794</guid>
		<description><![CDATA[You might get this message in Google AppEngine when sending an email message and specifying a custom from address. To get it to work you must add the address as a developer for your application (or use the email address you signed up with). Steps to Add a Developer Email Address: Log in to your [...]]]></description>
			<content:encoded><![CDATA[<p>You might get this message in Google AppEngine when sending an email message and specifying a custom from address. To get it to work you must add the address as a developer for your application (or use the email address you signed up with). </p>
<p><strong>Steps to Add a Developer Email Address:</strong></p>
<ol>
<li>Log in to your Google App Engine Account.</li>
<li>Under Administration, click Permissions, and add the email address.</li>
<li>Log out, and check for the validation email.</li>
</ol>
<p><strong>More Info:</strong><br />
This is quite a hassle. I understand the reason is to cut back on spam but not only do you have to have a valid email account but you must create a google account for it.<br />
In my case, I just wanted to send notifications out but I don&#8217;t monitor the sender address (but I do include a contact link in the email).<br />
Darn spammers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pressthered.com/solving_invalidsendererror_unauthorized_sender_in_appengine/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>UrlEncode in Python</title>
		<link>http://www.pressthered.com/urlencode_in_python/</link>
		<comments>http://www.pressthered.com/urlencode_in_python/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 05:36:55 +0000</pubDate>
		<dc:creator>jim.richmond</dc:creator>
				<category><![CDATA[Tech Tips]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.pressthered.com/?p=719</guid>
		<description><![CDATA[To urlencode a querystring or form data in python you can use the urllib module: use urllib # you have to pass in a dictionary print urllib.urlencode({'id':'100', 'name':'john smith'}) # then you get 'id=100&#038;name=john+smith' Urlencoding Unicode and UTF8 Values #this works fine print urllib.urlencode({name:'José'}) #you have to be careful with unicode strings # this will [...]]]></description>
			<content:encoded><![CDATA[<p><strong>To urlencode a querystring or form data in python you can use the urllib module:</strong></p>
<pre name="code" class="python">
use urllib
# you have to pass in a dictionary
print urllib.urlencode({'id':'100', 'name':'john smith'})

# then you get 'id=100&#038;name=john+smith'
</pre>
<p><strong>Urlencoding Unicode and UTF8 Values</strong></p>
<pre name="code" class="python">
#this works fine
print urllib.urlencode({name:'José'})

#you have to be careful with unicode strings
# this will throw an error:
print urllib.urlencode({name:u'José'})
# UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in
# position 11: ordinal not in range

#since the default ascii conversion can't handle the 'é',
# you have to be explicit in what charset you are using
print urllib.urlencode({'name':u'José'.encode('utf8')})
</pre>
<p><strong>Additional Info:</strong><br />
<a href="http://docs.python.org/library/urllib.html#urllib.urlencode">Python Doc for urllib module</a><br />
<a href="http://www.joelonsoftware.com/articles/Unicode.html">Unicode Primer</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.pressthered.com/urlencode_in_python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding Dates and Times in Python</title>
		<link>http://www.pressthered.com/adding_dates_and_times_in_python/</link>
		<comments>http://www.pressthered.com/adding_dates_and_times_in_python/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 06:11:03 +0000</pubDate>
		<dc:creator>jim.richmond</dc:creator>
				<category><![CDATA[Tech Tips]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.pressthered.com/?p=645</guid>
		<description><![CDATA[Using the built-in modules datetime and timedelta, you can perform date and time addition/subtraction in python: from datetime import datetime from datetime import timedelta #Add 1 day print datetime.now() + timedelta(days=1) #Subtract 60 seconds print datetime.now() - timedelta(seconds=60) #Add 2 years print datetime.now() + timedelta(days=730) #Other Parameters you can pass in to timedelta: # days, [...]]]></description>
			<content:encoded><![CDATA[<p>Using the built-in modules <strong>datetime</strong> and <strong>timedelta</strong>, you can perform date and time addition/subtraction in <strong>python</strong>:</p>
<pre name="code" class="python">
from datetime import datetime
from datetime import timedelta

#Add 1 day
print datetime.now() + timedelta(days=1)

#Subtract 60 seconds
print datetime.now() - timedelta(seconds=60)

#Add 2 years
print datetime.now() + timedelta(days=730)

#Other Parameters you can pass in to timedelta:
# days, seconds, microseconds,
# milliseconds, minutes, hours, weeks

#Pass multiple parameters (1 day and 5 minutes)
print datetime.now() + timedelta(days=1,minutes=5)
</pre>
<p>Here is a python reference that gives more examples and advanced features:<br />
<a href="http://docs.python.org/library/datetime.html">http://docs.python.org/library/datetime.html</a></p>
<p>If you are coming from a .net or sql environment, here are the above examples in C# and SQL (Microsoft) for comparison:<br />
<strong>C#</strong></p>
<pre name="code" class="c#">
DateTime myTime = new DateTime();

--Add 1 day
myTime.AddDays(1);

--Subtract 60 seconds
myTime.AddSeconds(-60);

--Add 2 years
myTime.AddYears(2);
</pre>
<p><strong>SQL</strong></p>
<pre name="code" class="sql">
--Add 1 day
select DATEADD(day, 1, getdate())

--Subtract 60 seconds
select DATEADD(second, -60, getdate())

--Add 2 years
select DATEADD(Year, 2, getdate())
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.pressthered.com/adding_dates_and_times_in_python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Dynamically Importing and Instantiating Modules in Python</title>
		<link>http://www.pressthered.com/dynamically_importing_and_instantiating_modules_in_python/</link>
		<comments>http://www.pressthered.com/dynamically_importing_and_instantiating_modules_in_python/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 05:43:20 +0000</pubDate>
		<dc:creator>jim.richmond</dc:creator>
				<category><![CDATA[Tech Tips]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.pressthered.com/?p=634</guid>
		<description><![CDATA[Also called reflection, here is one method to import modules and instantiate objects using strings: moduleName = 'myApp.models' className = 'Blog' #import the module by saying 'from myApp.models import Blog' module = __import__(moduleName, {}, {}, className) #now you can instantiate the class obj = getattr(module, className )() #set a property of the object using a [...]]]></description>
			<content:encoded><![CDATA[<p>Also called reflection, here is one method to import modules and instantiate objects using strings:</p>
<pre name="code" class="python">

moduleName = 'myApp.models'
className = 'Blog'

#import the module by saying 'from myApp.models import Blog'
module = __import__(moduleName, {}, {}, className)

#now you can instantiate the class
obj = getattr(module, className )()

#set a property of the object using a string
setattr('Title','my first entry')

#call an existing method you already know
obj.save()
</pre>
<p>I was using Python 2.5, so it is probably safe to say the code above is at least Python 2.5+ compatible.</p>
<p>Some additional references:<br />
<a href="http://diveintopython.org/power_of_introspection/getattr.html">Dive Into Python &#8211; The Power of Introspection</a><br />
<a href="http://www.python.org/doc/2.5.1/lib/built-in-funcs.html">Python Manual &#8211; Built In Functions</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.pressthered.com/dynamically_importing_and_instantiating_modules_in_python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Convert None to Empty String in Python</title>
		<link>http://www.pressthered.com/convert_none_to_empty_string_in_python/</link>
		<comments>http://www.pressthered.com/convert_none_to_empty_string_in_python/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 06:12:56 +0000</pubDate>
		<dc:creator>jim.richmond</dc:creator>
				<category><![CDATA[Tech Tips]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.pressthered.com/?p=626</guid>
		<description><![CDATA[If you need to check for and convert the &#8220;None&#8221; string when returning the value of an object, here is a quick and readable method in python: myObj = None print myObj or '' This brought back a reference to a blurb in Dive Into Python regarding ternary operators, although at the time I didn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>If you need to check for and convert the &#8220;None&#8221; string when returning the value of an object, here is a quick and readable method in python:</p>
<pre name="code" class="python">
myObj = None
print myObj or ''
</pre>
<p>This brought back a reference to a blurb in <a href="http://diveintopython.org/toc/index.html">Dive Into Python</a> regarding <a href="http://diveintopython.org/power_of_introspection/and_or.html">ternary operators</a>, although at the time I didn&#8217;t think of using it this way.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pressthered.com/convert_none_to_empty_string_in_python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Resolving DJANGO Error: &#8220;ValueError: invalid literal for int() with base 10: &#8216;None&#8217;&#8221;</title>
		<link>http://www.pressthered.com/resolving_django_error_valueerror_invalid_literal_for_int_with_base_10_none/</link>
		<comments>http://www.pressthered.com/resolving_django_error_valueerror_invalid_literal_for_int_with_base_10_none/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 05:39:55 +0000</pubDate>
		<dc:creator>jim.richmond</dc:creator>
				<category><![CDATA[Tech Tips]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.pressthered.com/?p=614</guid>
		<description><![CDATA[If you receive this error via email while viewing one of your admin pages, here are some steps to help resolve it. First note the problem URL. It should be in the detailed error email. Try the url in your browser and verify you receive the same error. Now you have to find the page [...]]]></description>
			<content:encoded><![CDATA[<p>If you receive this error via email while viewing one of your admin pages, here are some steps to help resolve it. First note the problem URL. It should be in the detailed error email. Try the url in your browser and verify you receive the same error. Now you have to find the page that is linking to this invalid URL. </p>
<p>In my case, it was an image tag that I included in the admin <a href="http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display">list display</a>. In my custom field code, I did not handle the &#8220;None&#8221; case. So instead of displaying a broken link it was trying to load the None image in my admin directory. Generating the URL that was causing the issue:<br />
/admin/myApp/myModel/None/</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pressthered.com/resolving_django_error_valueerror_invalid_literal_for_int_with_base_10_none/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reloading Modules in a Python Interpreter</title>
		<link>http://www.pressthered.com/reloading_modules_in_a_python_interpreter/</link>
		<comments>http://www.pressthered.com/reloading_modules_in_a_python_interpreter/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 05:29:37 +0000</pubDate>
		<dc:creator>jim.richmond</dc:creator>
				<category><![CDATA[Tech Tips]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.pressthered.com/?p=591</guid>
		<description><![CDATA[If you have edited an external module (or .py file), you can reload the module from within the python interpreter by using the following command: reload(myModule) This assumes that you have imported the module at some point within your interpreter session. When Is This Useful It&#8217;s helpful if you are debugging code in a IDE [...]]]></description>
			<content:encoded><![CDATA[<p>If you have edited an external module (or .py file), you can reload the module from within the python interpreter by using the following command:</p>
<pre name="code" class="sql">
reload(myModule)
</pre>
<p>This assumes that you have imported the module at some point within your interpreter session.</p>
<p><strong>When Is This Useful</strong><br />
It&#8217;s helpful if you are debugging code in a IDE like <a href="http://code.google.com/p/pyscripter/">PyScripter</a>. It&#8217;s quicker to test your programs within the IDE&#8217;s interpreter (it&#8217;s also the default). However if you have multiple files open and are making changes to them, your changes to files that have been imported already will not take affect. Unless you use the reload() command.</p>
<p><strong>An Alternate Method</strong><br />
You can also <a href="http://afulay.blogspot.com/2006/12/reload-modules-in-python-environment.html">programmatically remove the module from the cache</a>. Then the next time your import is called, it will reload it:</p>
<pre name="code" class="sql">
if  'myModule' in sys.modules:
    del(sys.modules["myModule"])
import myModule
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.pressthered.com/reloading_modules_in_a_python_interpreter/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

