<?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>Kangas Bros. Innovations Blog</title>
	<atom:link href="http://blog.kangasbros.fi/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://blog.kangasbros.fi</link>
	<description>Blog about software development and life-style entrepreneurship</description>
	<lastBuildDate>Wed, 09 May 2012 05:39:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Tutorial: creating a bitcoin instawallet clone with python and django</title>
		<link>http://blog.kangasbros.fi/?p=85</link>
		<comments>http://blog.kangasbros.fi/?p=85#comments</comments>
		<pubDate>Sun, 25 Dec 2011 17:40:10 +0000</pubDate>
		<dc:creator>Jeremias Kangas</dc:creator>
				<category><![CDATA[bitcoin]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.kangasbros.fi/?p=85</guid>
		<description><![CDATA[Advertisement: if you want a feature-rich web-based bitcoin wallet, check out EasyWallet.org for Bitcoin. Bitcoin is great and crazy innovation. Ability to transfer electronic money around the world, with low transaction costs might be a revolutionary concept. However, bitcoin is a very young and only time will tell if it succeeds or not. In every [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Advertisement: if you want a feature-rich web-based bitcoin wallet, check out <a href="https://easywallet.org">EasyWallet.org for Bitcoin</a>.</strong></p>
<p><a href="http://www.bitcoin.org">Bitcoin</a> is great and crazy innovation. Ability to transfer electronic money around the world, with low transaction costs might be a revolutionary concept. However, bitcoin is a very young and only time will tell if it succeeds or not. In every case, Bitcoin needs huge amount of inrastructure around it to be successful. That is why there is <a href="https://github.com/kangasbros/django-bitcoin">django-bitcoin</a> library, to make development of bitcoin web apps easier.</p>
<p>From a developer perspective bitcoin is great because bitcoins are so easy to move around. You can automate all aspects of money transfers. In this tutorial I will show you how to create a simple <a href="http://www.instawallet.org">instawallet</a> clone using <a href="http://www.python.org">python language</a>, <a href="http://www.djangoproject.org">django web framework</a> and <a href="https://github.com/kangasbros/django-bitcoin">django-bitcoin</a> library. The resulting code of this tutorial is in <a href="https://github.com/kangasbros/django-bitcoin-instawallet">github</a>.</p>
<p>Instawallet is a very simple bitcoin web app. It allows anyone to set up a simple bitcoin wallet, where you can store bitcoins and move them easily around without installing the bitcoin client. If you are using this kind of service, you are putting a huge trust on the service owner. The benefits of using instawallet is that it makes bitcoin transfers more anonymous, and also makes using bitcoins easier.</p>
<p>Now let&#8217;s start coding! First we need to setup the app from django. We start a new django application, start a new &#8220;wallets&#8221; django app, and clone django_bitcoin from github.</p>
<p>Note that it is usually a good practice to use <a href="http://pypi.python.org/pypi/virtualenv">virtualenv</a>, which we are not using now.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">pip <span style="color: #c20cb9; font-weight: bold;">install</span> django<br />
django-admin.py startproject instawallet<br />
<span style="color: #7a0874; font-weight: bold;">cd</span> instawallet<br />
<br />
python manage.py startapp wallets<br />
<br />
<span style="color: #c20cb9; font-weight: bold;">git clone</span> git:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>kangasbros<span style="color: #000000; font-weight: bold;">/</span>django-bitcoin.git<br />
<span style="color: #7a0874; font-weight: bold;">cd</span> django-bitcoin<br />
python setup.py <span style="color: #c20cb9; font-weight: bold;">install</span></div></div>
<p>Next we need to setup some settings for our django project to file &#8220;settings.py&#8221;. For this project we are using sqlite, which we first install via command line.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">pip <span style="color: #c20cb9; font-weight: bold;">install</span> pysqlite</div></div>
<p>We set up the sqlite database as a file &#8220;dev.db&#8221;. After that we add &#8220;django_bitcoin&#8221; and  &#8220;wallets&#8221; to INSTALLED_APPS so that django can find our apps. Also we specify a templates directory. Create a folder &#8220;templates&#8221; inside the project root.</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">DATABASES <span style="color: #66cc66;">=</span> <span style="color: black;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #483d8b;">'default'</span>: <span style="color: black;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'ENGINE'</span>: <span style="color: #483d8b;">'django.db.backends.sqlite3'</span><span style="color: #66cc66;">,</span> <span style="color: #808080; font-style: italic;"># Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'NAME'</span>: <span style="color: #483d8b;">'dev.db'</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;"># Or path to database file if using sqlite3.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'USER'</span>: <span style="color: #483d8b;">''</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;"># Not used with sqlite3.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'PASSWORD'</span>: <span style="color: #483d8b;">''</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;"># Not used with sqlite3.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'HOST'</span>: <span style="color: #483d8b;">''</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;"># Set to empty string for localhost. Not used with sqlite3.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'PORT'</span>: <span style="color: #483d8b;">''</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;"># Set to empty string for default. Not used with sqlite3.</span><br />
&nbsp; &nbsp; <span style="color: black;">&#125;</span><br />
<span style="color: black;">&#125;</span><br />
<br />
...<br />
<br />
<span style="color: black;">INSTALLED_APPS</span> <span style="color: #66cc66;">=</span> <span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; ….<br />
&nbsp; &nbsp; <span style="color: #483d8b;">'django_bitcoin'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; <span style="color: #483d8b;">'wallets'</span><span style="color: #66cc66;">,</span><br />
<span style="color: black;">&#41;</span><br />
<br />
...<br />
<br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span><br />
<br />
PROJECT_ROOT <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">abspath</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>__file__<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<br />
TEMPLATE_DIRS <span style="color: #66cc66;">=</span> <span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>PROJECT_ROOT<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;templates&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span><br />
<span style="color: black;">&#41;</span></div></div>
<p>And finally, we need to setup the bitcoin daemon. For this, you must download the official bitcoin client, and edit the bitcoin.conf. Example bitcoin.conf for development purposes:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">testnet=1<br />
server=1<br />
rpcuser=admin<br />
rpcpassword=1234<br />
rpcallowip=*</div></div>
<p>Then you need to configure your bitcoin daemon to django settings.py file. Here we use 0 confirmations for testing purposes.</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">BITCOIND_CONNECTION_STRING <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;http://login:password@localhost:8332&quot;</span><br />
BITCOIN_MINIMUM_CONFIRMATIONS <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span></div></div>
<p>Now everything is set up! Now we start the real coding. We start with models. We will use only one model in this application. That is the online wallet. It will have only two fields, one is the random and unique identifier (uuid) of the wallet, and second is a foreign key to the django_bitcoin Wallet object, which faciliates the actual transactions. The following model goes to &#8220;wallets/models.py&#8221; file.</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">class</span> InstaWallet<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #483d8b;">&quot;&quot;&quot;A wallet model&quot;&quot;&quot;</span><br />
<br />
&nbsp; &nbsp; uuid <span style="color: #66cc66;">=</span> models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>blank<span style="color: #66cc66;">=</span><span style="color: #008000;">True</span><span style="color: #66cc66;">,</span> max_length<span style="color: #66cc66;">=</span><span style="color: #ff4500;">50</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; wallet <span style="color: #66cc66;">=</span> models.<span style="color: black;">ForeignKey</span><span style="color: black;">&#40;</span>Wallet<span style="color: #66cc66;">,</span> null<span style="color: #66cc66;">=</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__unicode__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> u<span style="color: #483d8b;">&quot;Wallet &quot;</span>+<span style="color: #008000;">self</span>.<span style="color: black;">uuid</span>+<span style="color: #483d8b;">&quot; &quot;</span>+<span style="color: #008000;">self</span>.<span style="color: black;">name</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> url<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;/wallet/&quot;</span>+<span style="color: #008000;">self</span>.<span style="color: black;">uuid</span></div></div>
<p>We need also one form for this app, which resides in the &#8220;wallets/forms.py&#8221; file. The form is used to send transactions from a specific instawallet.</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">class</span> SendForm<span style="color: black;">&#40;</span>forms.<span style="color: black;">Form</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; address_to <span style="color: #66cc66;">=</span> forms.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>min_length<span style="color: #66cc66;">=</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> max_length<span style="color: #66cc66;">=</span><span style="color: #ff4500;">40</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; amount <span style="color: #66cc66;">=</span> forms.<span style="color: black;">DecimalField</span><span style="color: black;">&#40;</span>min_value<span style="color: #66cc66;">=</span>Decimal<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;0.0001&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div></div>
<p>Now we have the database structure defined in models.py, and we can sync the initial database.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">python manage.py syncdb</div></div>
<p>Now the logic part of this application. This resides in the &#8220;wallets/views.py&#8221; file. Before we implement the logic, we have to define the &#8220;urls.py&#8221; file, which defines what kind of urls we can use.</p>
<p>We define two addresses for this app: the &#8220;home&#8221; address, which always just generates a new wallet and redirects to it. The other is address for each invidual instawallet.</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">urlpatterns <span style="color: #66cc66;">=</span> patterns<span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^$'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'wallets.views.home'</span><span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'home'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^wallet/(?P&lt;uuid&gt;.*?)$'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'wallets.views.wallet'</span><span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'wallet'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span><br />
<span style="color: black;">&#41;</span></div></div>
<p>Now for the actual views. First the home view. In this view, we generate a new wallet with a random uuid. For a better random, we use system random function os.urandom(), which is better than the pseudorandom function provided by python. We redirect to the wallet address right away.</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">def</span> home<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># just generate a new wallet</span><br />
&nbsp; &nbsp; new_wallet<span style="color: #66cc66;">=</span>InstaWallet.<span style="color: black;">objects</span>.<span style="color: black;">create</span><span style="color: black;">&#40;</span>uuid<span style="color: #66cc66;">=</span>generateuniquehash<span style="color: black;">&#40;</span>extradata<span style="color: #66cc66;">=</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">urandom</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">32</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span>\<br />
&nbsp; &nbsp; &nbsp; &nbsp; wallet<span style="color: #66cc66;">=</span>Wallet.<span style="color: black;">objects</span>.<span style="color: black;">create</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponseRedirect<span style="color: black;">&#40;</span>new_wallet.<span style="color: black;">url</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div></div>
<p>Now the actual wallet view, which is the core functionality of our instawallet. We get the wallet uuid as a parameter. If the InstaWallet object isn&#8217;t found with that uuid, we redirect to 404.</p>
<p>If the request is GET, there is nothing to do. Otherwise we have received an transaction request.</p>
<p>With our instawallet, we can generate two types of transactions. If the address provided is between 30-37 characters, we assume that the address is an actual bitcoin address, and we generate a bitcoin transaction. This is done by using the send_to_address() -function of the django_bitcoin &#8220;Wallet&#8221; object.</p>
<p>Otherwise, we assume that the user tries to send the funds to an another instawallet app. We try to search if there are any InstaWallet objects, whose uuid start with the address provided. Then if a right InstaWallet object is found, we send bitcoins to it with the send_to_wallet() function. No actual bitcoin transaction is generated, and the funds are sent between our web app users.</p>
<p>Why is this useful? This way the users of this web app can send instant transactions between each other, without a need for actual bitcoin transactions.</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">def</span> wallet<span style="color: black;">&#40;</span>request<span style="color: #66cc66;">,</span> uuid<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">try</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; instawallet<span style="color: #66cc66;">=</span>InstaWallet.<span style="color: black;">objects</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span>uuid<span style="color: #66cc66;">=</span>uuid<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">except</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponseRedirect<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/404'</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> request.<span style="color: black;">method</span> <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'POST'</span>: <span style="color: #808080; font-style: italic;"># If the form has been submitted...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; form <span style="color: #66cc66;">=</span> SendForm<span style="color: black;">&#40;</span>request.<span style="color: black;">POST</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># A form bound to the POST data</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> form.<span style="color: black;">is_valid</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># All validation rules pass</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># check if </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>form.<span style="color: black;">cleaned_data</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;address_to&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">30</span> \<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>form.<span style="color: black;">cleaned_data</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;address_to&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">&lt;</span><span style="color: #ff4500;">37</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">try</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; instawallet.<span style="color: black;">wallet</span>.<span style="color: black;">send_to_address</span><span style="color: black;">&#40;</span>form.<span style="color: black;">cleaned_data</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;address_to&quot;</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> form.<span style="color: black;">cleaned_data</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;amount&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; messages.<span style="color: black;">add_message</span><span style="color: black;">&#40;</span>request<span style="color: #66cc66;">,</span> messages.<span style="color: black;">SUCCESS</span><span style="color: #66cc66;">,</span> \<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _<span style="color: black;">&#40;</span>u<span style="color: #483d8b;">&quot;Transaction successfully sent to bitcoin address &quot;</span>+form.<span style="color: black;">cleaned_data</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;address_to&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">except</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; messages.<span style="color: black;">add_message</span><span style="color: black;">&#40;</span>request<span style="color: #66cc66;">,</span> messages.<span style="color: black;">ERROR</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">exc_info</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; otherwallets<span style="color: #66cc66;">=</span>InstaWallet.<span style="color: black;">objects</span>.<span style="color: #008000;">filter</span><span style="color: black;">&#40;</span>uuid__startswith<span style="color: #66cc66;">=</span>form.<span style="color: black;">cleaned_data</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;address_to&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>otherwallets<span style="color: black;">&#41;</span><span style="color: #66cc66;">==</span><span style="color: #ff4500;">1</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; instawallet.<span style="color: black;">wallet</span>.<span style="color: black;">send_to_wallet</span><span style="color: black;">&#40;</span>otherwallets<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">wallet</span><span style="color: #66cc66;">,</span> form.<span style="color: black;">cleaned_data</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;amount&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; messages.<span style="color: black;">add_message</span><span style="color: black;">&#40;</span>request<span style="color: #66cc66;">,</span> messages.<span style="color: black;">SUCCESS</span><span style="color: #66cc66;">,</span> \<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _<span style="color: black;">&#40;</span>u<span style="color: #483d8b;">&quot;Transaction successfully sent to another instawallet &quot;</span>+form.<span style="color: black;">cleaned_data</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;address_to&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; messages.<span style="color: black;">add_message</span><span style="color: black;">&#40;</span>request<span style="color: #66cc66;">,</span> messages.<span style="color: black;">ERROR</span><span style="color: #66cc66;">,</span> _<span style="color: black;">&#40;</span>u<span style="color: #483d8b;">&quot;Address not an valid bitcoin address or wallet uuid not found.&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; form <span style="color: #66cc66;">=</span> SendForm<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># An unbound form</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> render_to_response<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;instawallet.html&quot;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">&quot;instawallet&quot;</span>: instawallet<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">&quot;form&quot;</span>: form<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">&#125;</span><span style="color: #66cc66;">,</span> context_instance<span style="color: #66cc66;">=</span>RequestContext<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div></div>
<p>Almost done! Now we only need to do the HTML template for the app. This file is &#8220;templates/instawallet.html&#8221;.</p>
<p>We are using several templatetags provided by django_bitcoin templatetag file &#8220;currency_conversions&#8221;. The first is wallet_tagline, which displays the wallet balance, money received and sent, and the balance approximation in USD.</p>
<p>The second is bitcoin_payment_qr, which displays a simple qr code for our bitcoin receiving address. The Wallet.receiving_address() function shows the wallet receiving address, where we can send bitcoins.</p>
<p>The final one is wallet_history tag, which shows all of the transactions of our bitcoin wallet as a table.</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">{% load i18n %}<br />
{% load currency_conversions %}<br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">title</span></a>&gt;</span>{% trans &quot;Bitcoin Instawallet&quot; %}: {{ instawallet.uuid }}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">title</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/h1.html"><span style="color: #000000; font-weight: bold;">h1</span></a>&gt;</span>{% trans &quot;Your instawallet&quot; %}: {{ instawallet.uuid }}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/h1.html"><span style="color: #000000; font-weight: bold;">h1</span></a>&gt;</span><br />
<br />
{% if messages %}<br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;messages&quot;</span>&gt;</span><br />
&nbsp; &nbsp; {% for message in messages %}<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;li<span style="color: #66cc66;">&#123;</span>% if message.tags %<span style="color: #66cc66;">&#125;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;{{ message.tags }}&quot;</span><span style="color: #66cc66;">&#123;</span>% endif %<span style="color: #66cc66;">&#125;</span>&gt;</span>{{ message }}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; {% endfor %}<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span><br />
{% endif %}<br />
<br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/b.html"><span style="color: #000000; font-weight: bold;">b</span></a>&gt;</span>{% wallet_tagline instawallet.wallet %}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/b.html"><span style="color: #000000; font-weight: bold;">b</span></a>&gt;&lt;<a href="http://december.com/html/4/element/br.html"><span style="color: #000000; font-weight: bold;">br</span></a><span style="color: #66cc66;">/</span>&gt;</span><br />
<br />
{% trans &quot;Send bitcoins to following address to receive coins&quot; %}:<br />
<br />
{% bitcoin_payment_qr instawallet.wallet.receiving_address %}<br />
<br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/h2.html"><span style="color: #000000; font-weight: bold;">h2</span></a>&gt;</span>{% trans &quot;Send payments&quot; %}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/h2.html"><span style="color: #000000; font-weight: bold;">h2</span></a>&gt;</span><br />
<br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/form.html"><span style="color: #000000; font-weight: bold;">form</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;newform&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;{{ instawallet.url }}&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;POST&quot;</span>&gt;</span>{% csrf_token %}<br />
{{ form.as_p }}<br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">input</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Send!&quot;</span><span style="color: #66cc66;">/</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/form.html"><span style="color: #000000; font-weight: bold;">form</span></a>&gt;</span><br />
<br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/h2.html"><span style="color: #000000; font-weight: bold;">h2</span></a>&gt;</span>{% trans &quot;Transaction log&quot; %}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/h2.html"><span style="color: #000000; font-weight: bold;">h2</span></a>&gt;</span><br />
<br />
{% wallet_history instawallet.wallet %}</div></div>
<p>Now all the code is created, and you can run the application and test it at &#8220;http://localhost:8000/&#8221;.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">python manage.py runserver</div></div>
<p>Now we can see that the instawallet is really working, and can test sending coins back and forth.</p>
<p><img src="http://blog.kangasbros.fi/wp-content/uploads/2011/12/Screen-shot-2011-12-25-at-5.50.31-PM.png" alt="Main View and QR code" /></p>
<p><img src="http://blog.kangasbros.fi/wp-content/uploads/2011/12/Screen-shot-2011-12-25-at-5.57.09-PM.png" alt="The transaction log of our instawallet." /></p>
<p>As you can see, creating an instawallet-style app doesn&#8217;t require truckloads of code, as most of the functionality is provided by the django_bitcoin library. Feel free to develop the project further and explore the django_bitcoin library.</p>
<p>If you plan to run django_bitcoin based app in the production, make sure to change the confirmation count setting to something higher, and do some serious testing before you deploy anything!</p>
<p>The code is at <a href="https://github.com/kangasbros/django-bitcoin-instawallet">https://github.com/kangasbros/django-bitcoin-instawallet</a></p>
<p>And finally, if you liked this tutorial, you can send a small donation to bitcoin address 1DaVAZ1kytYPqaDVse9HUcrCQfD6NpNamp <img src='http://blog.kangasbros.fi/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kangasbros.fi/?feed=rss2&#038;p=85</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Is javascript cryptography bad for your health?</title>
		<link>http://blog.kangasbros.fi/?p=72</link>
		<comments>http://blog.kangasbros.fi/?p=72#comments</comments>
		<pubDate>Wed, 16 Nov 2011 05:58:20 +0000</pubDate>
		<dc:creator>Jeremias Kangas</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.kangasbros.fi/?p=72</guid>
		<description><![CDATA[Recently I launched two little web apps in my spare time. Those both involved heavy use of javascript cryptography. After spreading the word about these services, I received some heavy criticism about the usage of javascript cryptography. The reasons were that it creates a false sense of security, is un-trustable and the persons who would [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I launched two little web apps in my spare time. Those both involved heavy use of javascript cryptography.</p>
<p>After spreading the word about these services, I received some heavy criticism about the usage of javascript cryptography. The reasons were that it creates a false sense of security, is un-trustable and the persons who would use or need cryptography prefer to use more traditional and time-tested software.</p>
<p>Cryptographic javascript libraries such as <a href="http://crypto.stanford.edu/sjcl/">Stanford Javascript Crypto Library</a> are relatively new. However, at the very basic level it is pretty easy to validate these libraries by comparing the results to more stable software. Hashes are gonna hash and encryption is gonna encrypt.</p>
<p>Also using javascript-enabled encryption is probably safer than not using an encryption at all. Javascript cryptography allows very easy and accessible cryptography-enabled web services, which makes cryptography more usable for common people.</p>
<p>Some ways to sign &#038; verify javascript code could be implemented, so that it is easier for users to trust cryptographically-enabled web services. This could be achieved somewhat user-friendly way via browser plugins, which could cache or automatically compare different javascript source files.</p>
<p>After all I think javascript cryptography is a good thing. Cryptography should be available for everyone, and common developers shouldn&#8217;t be afraid to use cryptography in their apps. </p>
<p>The services I created: first was <a href="https://cryptedmemo.com/">cryptedmemo.com</a>, which let&#8217;s you store secret notes online, and the second one was <a href="https://cryptedmemo.com/SHA1_Pass.js">SHA1_Pass.js</a>, which is an implementation handy sentence-based password-system.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kangasbros.fi/?feed=rss2&#038;p=72</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why I don&#8217;t use TDD or BDD</title>
		<link>http://blog.kangasbros.fi/?p=30</link>
		<comments>http://blog.kangasbros.fi/?p=30#comments</comments>
		<pubDate>Sat, 04 Sep 2010 20:56:21 +0000</pubDate>
		<dc:creator>Jeremias Kangas</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.kangasbros.fi/?p=30</guid>
		<description><![CDATA[Everyone nowadays is writing tests like crazy, and even using TDD so that tests are always written before actual code. Right? No. I have used TDD for one project and I wasn&#8217;t happy, it was waste of time. Nowadays I write unit tests very seldomly (in my case they are more integration-tests than unit tests) [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone nowadays is writing tests like crazy, and even using TDD so that tests are always written before actual code. Right? No.</p>
<p>I have used TDD for one project and I wasn&#8217;t happy, it was waste of time. Nowadays I write unit tests very seldomly (in my case they are more integration-tests than unit tests) and never follow BDD-style development. That is mostly because I&#8217;m a lazy-ass bastard, but there are other reasons as well, such as:</p>
<ul>
<li><strong>&#8220;Examples&#8221; of unit tests suck donkey balls every time.</strong> If you are going to keep a presentation about how cool unit tests are, try to show me the practical benefits <strong>with a code example</strong>, not some lame-ass &#8220;Hello world&#8221; example or hand-waving. I haven&#8217;t seen any convincing real-world cases.</li>
<li><strong>They seem to be a great way to spend some extra time doing time consuming, not very hard work.</strong>. And if you are a sub-contractor/consultant, that is pretty good because it means more billable hours. So good for consultants, not for me.</li>
<li><b>You have to think about the &#8220;evil&#8221; business side too.</b> OK, I believe how TDD/BDD will make my project ultra-maintainable, and unit tests will alert me if something gets broken. But what if my project does not need maintaining because it wasn&#8217;t commercially successful? Often I just want to get something out of the door quickly, and I want to minimize my time developing the software.</li>
<li><b>Unit tests=more code.</b> This is just a personal belief; I think the less code, the better. I&#8217;m a minimalist.</li>
</ul>
<p>Don&#8217;t get this wrong, use BDD/TDD if you really believe in them and think they benefit your project. I just personally haven&#8217;t found any use for them in any of my projects or my clients&#8217; projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kangasbros.fi/?feed=rss2&#038;p=30</wfw:commentRss>
		<slash:comments>39</slash:comments>
		</item>
		<item>
		<title>Python/Django unicode/utf-8 usage, how hard can it be?</title>
		<link>http://blog.kangasbros.fi/?p=12</link>
		<comments>http://blog.kangasbros.fi/?p=12#comments</comments>
		<pubDate>Thu, 17 Sep 2009 06:36:07 +0000</pubDate>
		<dc:creator>Jeremias Kangas</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.kangasbros.fi/?p=12</guid>
		<description><![CDATA[Regarding character encodings, big boys have always told me a rule of thumb: use utf-8 for everything. In my last project, I tried to follow this simple guideline, but still managed to get several headaches with utf-8/unicode problems. First step: Set your text editor/IDE to use always utf-8. I guess most editors (eg. textmate, which [...]]]></description>
			<content:encoded><![CDATA[<p>Regarding character encodings, big boys have always told me a rule of thumb: <strong>use utf-8 for everything</strong>. In my last project, I tried to follow this simple guideline, but still managed to get several headaches with utf-8/unicode problems.</p>
<p>First step: <strong>Set your text editor/IDE to use always utf-8</strong>. I guess most editors (eg. textmate, which I mostly use) do this automatically nowadays. For vim I have used following line in my .vimrc:</p>
<blockquote><p>set encoding=utf8</p></blockquote>
<p>If you have old files lying around in other encodings, it is recommended to convert them to utf-8. This can be easily accomplished with <a href="http://www.gnu.org/software/libiconv/documentation/libiconv/iconv.1.html">iconv</a> or other similar tools.</p>
<p>OK, now the text editor should default to utf-8 when creating new files. However, don&#8217;t be surprised if django throws an error looking something like this:</p>
<blockquote><p>(&#8220;Non-ASCII character &#8216;\\xc3&#8242; in file views.py on line 303, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details&#8221;, (&#8216;views.py&#8217;, 303, 0, None))</p></blockquote>
<p>That is because python doesn&#8217;t get that your files are utf-8, unless your editor places a byte-order-mark automatically in each file. Textmate doesn&#8217;t seem to, and that&#8217;s why I need to place following in each .py file.</p>
<div class="codecolorer-container vim default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vim codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"># coding=utf8</div></div>
<p>That fixed the problem! Another common error I caught was:</p>
<blockquote><p>UnicodeDecodeError: &#8216;ascii&#8217; codec can&#8217;t decode byte 0xc3 in position 4: ordinal not in range(128)</p></blockquote>
<p>Usually it turned out that I had done something like &#8220;str&#8221;+unicodeObject. This can be solved by marking strings as u&#8221;this is a unicode object&#8221; or using the unicode(&#8220;string&#8221;) object.</p>
<p>Reading &amp; writing files with unicode turned out to be problematic too. For example, the common way to read files is:</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: #008000;">file</span>.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>filename<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;r&quot;</span> <span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> fin:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> fin:</div></div>
<p>However, with this method every line variable is a str object and we need to convert them to unicode object separately. Easier method is to use the codecs library (assuming that input file is utf-8):</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">codecs</span><br />
<span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: #dc143c;">codecs</span>.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>filename<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;r&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;utf-8&quot;</span> <span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> fin:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> fin:</div></div>
<p>That way, every input line is automatically an unicode object, and we don&#8217;t need to convert the strings to unicode later.</p>
<p>As a last thing, when you create python script which outputs utf-8 like this:</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span><br />
<span style="color: #ff7700;font-weight:bold;">print</span> u<span style="color: #483d8b;">&quot;unicode object äöäö&quot;</span></div></div>
<p>I works normally when the output is printed to terminal, but when you do file redirect like this</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span>python script.py <span style="color: #000000; font-weight: bold;">&gt;</span> test.txt</div></div>
<p>the familiar UnicodeEncodeError pops out:</p>
<blockquote><p>Traceback (most recent call last):<br />
  File &#8220;script.py&#8221;, line 2, in <module><br />
    print u&#8221;unicode object äöäö&#8221;<br />
UnicodeEncodeError: &#8216;ascii&#8217; codec can&#8217;t encode characters in position 15-18: ordinal not in range(128)</p></blockquote>
<p>This is fixed by manually setting stdout encoding:</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">codecs</span><br />
<span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span> <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">codecs</span>.<span style="color: black;">getwriter</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'utf-8'</span><span style="color: black;">&#41;</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">print</span> u<span style="color: #483d8b;">&quot;unicode object äöäö&quot;</span></div></div>
<p>This is because python doesn&#8217;t define output encoding when redirecting to a file.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kangasbros.fi/?feed=rss2&#038;p=12</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

