<?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>Bryan Helmig</title>
	<atom:link href="http://bryanhelmig.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://bryanhelmig.com</link>
	<description>...does nerdy things.</description>
	<lastBuildDate>Thu, 29 Mar 2012 16:18:25 +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>Simple Finite State Machine Decorator for Python</title>
		<link>http://bryanhelmig.com/simple-finite-state-machine-decorator-for-python/</link>
		<comments>http://bryanhelmig.com/simple-finite-state-machine-decorator-for-python/#comments</comments>
		<pubDate>Thu, 29 Mar 2012 16:18:25 +0000</pubDate>
		<dc:creator>Bryan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://bryanhelmig.com/?p=423</guid>
		<description><![CDATA[Working on a fun side project at HackComo, I needed a finite state machine. Well, this is a simple decorator for limiting when a method can be ran: def fsm&#40;target, whence='*', attr='state'&#41;: &#34;&#34;&#34; `target` -&#62; (str): REQUIRED should be the target state. &#160; `whence` -&#62; (list): '*' should be a *list* of states that you [...]]]></description>
			<content:encoded><![CDATA[<p>Working on a fun side project at HackComo, I needed a finite state machine. Well, this is a simple decorator for limiting when a method can be ran:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> fsm<span style="color: black;">&#40;</span>target, whence=<span style="color: #483d8b;">'*'</span>, attr=<span style="color: #483d8b;">'state'</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
    `target` -&gt; (str): REQUIRED
        should be the target state.
&nbsp;
    `whence` -&gt; (list): '*'
        should be a *list* of states that you can change from.
&nbsp;
    `attr` -&gt; (str): 'state'
        is the that of the attribute that represents state.
&nbsp;
&nbsp;
    Example:
&nbsp;
        class Car(object):
            state = 'off'
&nbsp;
            @fsm('on', whence=['off'])
            def turn_on(self):
                pass
&nbsp;
            @fsm('off', whence=['on'])
            def turn_off(self):
                pass
&nbsp;
        car = Car()
        # state is 'off'
&nbsp;
        car.turn_off()              # raise Exception
        car.turn_off(silent=True)   # fails silently
        # state is still 'off'
&nbsp;
        car.turn_on()
        # state is 'on
&nbsp;
    &quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> dec<span style="color: black;">&#40;</span>method<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">def</span> inner<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
            silent = kwargs.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'silent'</span>, <span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #808080; font-style: italic;"># check that whence == curent state</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> whence == <span style="color: #483d8b;">'*'</span>:
                <span style="color: #ff7700;font-weight:bold;">pass</span>
            <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, attr<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> whence:
                <span style="color: #ff7700;font-weight:bold;">pass</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> silent:
                    <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'You shall not pass!'</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;">#LOTR</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
&nbsp;
            result = method<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
            <span style="color: #008000;">setattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, attr, target<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># set state to target</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">return</span> result
        <span style="color: #ff7700;font-weight:bold;">return</span> inner
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> dec</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://bryanhelmig.com/simple-finite-state-machine-decorator-for-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Large Django Migrations with Celery</title>
		<link>http://bryanhelmig.com/large-django-migrations-utilizing-celery/</link>
		<comments>http://bryanhelmig.com/large-django-migrations-utilizing-celery/#comments</comments>
		<pubDate>Tue, 06 Mar 2012 00:19:34 +0000</pubDate>
		<dc:creator>Bryan</dc:creator>
				<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://bryanhelmig.com/?p=404</guid>
		<description><![CDATA[Let me preface this post by saying South is awesome. It greatly simplifies schema changes when working with databases with Django. However, if you&#8217;ve ever had to do a large data migration, you likely will see South bite the dust. It&#8217;s not really made for that. At that point you really need something a little [...]]]></description>
			<content:encoded><![CDATA[<p>Let me preface this post by saying South is awesome. It greatly simplifies schema changes when working with databases with Django. However, if you&#8217;ve ever had to do a large data migration, you likely will see South bite the dust. It&#8217;s not really made for that. At that point you really need something a little more robust at chewing through large amounts of data.</p>
<p>This is where I like to use Celery. Do your setup schema migrations like normal and write a new task for handling the migration from the old to new table, and then write another migration for running after your Celery tasks complete. Here&#8217;s a little better workflow:</p>
<ol>
<li>Create migration that insert new column(s) or table(s).</li>
<li>Create separate migration that removes old column(s) or tables(s).</li>
<li>Write a task to migrate a discrete chunk of rows.</li>
<li>Run the first migration.</li>
<li>Iterate over discrete chunks of rows (think id range 1-1000, 1001-2000, etc&#8230;) and launch tasks.</li>
<li>Wait for tasks to complete.</li>
<li>Run the second migration.</li>
</ol>
<p>You migrations and tasks will obviously be implementation specific, but I thought I&#8217;d share the chunking of data sets that I&#8217;ve used.</p>
<p>Basically, I write a task that accepts both `begin` and `end` arguments, filter for those ID ranges, and then generate a bunch of `begin` and `end` pairs. Like so:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> celery.<span style="color: black;">task</span> <span style="color: #ff7700;font-weight:bold;">import</span> task
&nbsp;
@task<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> sweep_migrate<span style="color: black;">&#40;</span>begin, end<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">from</span> app.<span style="color: black;">models</span> <span style="color: #ff7700;font-weight:bold;">import</span> Model
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> instance <span style="color: #ff7700;font-weight:bold;">in</span> Model.<span style="color: black;">objects</span>.<span style="color: #008000;">filter</span><span style="color: black;">&#40;</span>id__gt=begin, id__lte=end<span style="color: black;">&#41;</span>.<span style="color: black;">iterator</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #808080; font-style: italic;"># migrate instance</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> gen_pairs<span style="color: black;">&#40;</span>count, cut<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
    Generates a list of [begin, end] pairs for appropriate slicing in
    over massive lists. (mainly for Django QS).
&nbsp;
    &gt;&gt;&gt; gen_pairs(42, 10)
    &gt;&gt;&gt; [[0, 10], [10, 20], [20, 30], [30, 40], [40, 42]]
    &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        _pairs = <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>count<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>cut::cut<span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span><span style="color: black;">&#91;</span>x-cut, x<span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> _pairs<span style="color: black;">&#93;</span> + <span style="color: black;">&#91;</span><span style="color: black;">&#91;</span>_pairs<span style="color: black;">&#91;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, count<span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">IndexError</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span>, count<span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> start_tasks<span style="color: black;">&#40;</span>final_id<span style="color: black;">&#41;</span>:
    pairs = gen_pairs<span style="color: black;">&#40;</span>final_id, <span style="color: #ff4500;">1000</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> begin, end <span style="color: #ff7700;font-weight:bold;">in</span> pairs:
        task = sweep_migrate.<span style="color: black;">apply_async</span><span style="color: black;">&#40;</span>args=<span style="color: black;">&#91;</span>begin, end<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>And to launch, I simply find the highest auto increment ID of the set I want to migrate and launch a shell and do something like so:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> app.<span style="color: black;">task</span> <span style="color: #ff7700;font-weight:bold;">import</span> start_tasks
start_tasks<span style="color: black;">&#40;</span><span style="color: #ff4500;">12345678</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Go grab a cup of coffee and wait&#8230; <img src='http://bryanhelmig.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://bryanhelmig.com/large-django-migrations-utilizing-celery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Poor Man&#8217;s Continuous Integration With Django/Python</title>
		<link>http://bryanhelmig.com/poor-mans-continuous-integration-with-djangopython/</link>
		<comments>http://bryanhelmig.com/poor-mans-continuous-integration-with-djangopython/#comments</comments>
		<pubDate>Tue, 20 Dec 2011 21:32:07 +0000</pubDate>
		<dc:creator>Bryan</dc:creator>
				<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://bryanhelmig.com/?p=385</guid>
		<description><![CDATA[Don&#8217;t feel like setting up Jenkins you lazy bum? Fine. Try this on for size: use a Github service hook to ping a Django view which runs a bash script out of process. Sound like a bad idea? Probably, but bad is a relative thing you see&#8230; here&#8217;s how: Gonna need the at command. Do [...]]]></description>
			<content:encoded><![CDATA[<p>Don&#8217;t feel like setting up Jenkins you lazy bum? Fine. Try this on for size: use a Github service hook to ping a Django view which runs a bash script out of process. Sound like a bad idea? Probably, but bad is a relative thing you see&#8230; here&#8217;s how:</p>
<ul>
<li>Gonna need the <span style="text-decoration: underline;">at</span> command. Do that now: <strong><strong>apt-get install at</strong></strong></li>
<li>Gonna need two bash scripts: <em>delay_deploy.sh</em> and <em>deploy.sh</em></li>
<li>Gonna need a <span style="text-decoration: underline;">view</span> to receive the hook</li>
</ul>
<p>
<strong>First</strong>, let&#8217;s do the <em>deploy.sh</em> script:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>repo
<span style="color: #c20cb9; font-weight: bold;">git</span> pull origin master
service apache2 restart
<span style="color: #666666; font-style: italic;"># or anything else you might need</span></pre></div></div>

<p><strong>Second</strong>, let&#8217;s do the <em>delay_deploy.sh</em> script:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
at <span style="color: #660033;">-f</span> <span style="color: #ff0000;">'/path/to/deploy.sh'</span> now</pre></div></div>

<p>If you have a standard Django/Apache/mod_wsgi setup, we will have a problem running this script from a view because Apache is running under a non-root user (as it should be). Usually it is under the user www-data, but you can double check with <strong>ps aux | grep apache</strong>. The same holds for Nginx, lighttpd, etc&#8230;</p>
<p><strong>Third</strong>, let&#8217;s permit www-data (or whoever) to sudo <em>delay_deploy.sh</em> (which will running a static, out of process command via <strong>at</strong>). This requires editing the <em>/etc/sudoers</em> file, so <strong>nano /etc/sudoers</strong> and add this line:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">www-data <span style="color: #007800;">ALL</span>= NOPASSWD: <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>delay_deploy.sh</pre></div></div>

<p>Now, www-data can run <em>delay_deploy.sh</em> as root, but only <em>delay_deploy.sh</em> and nothing else. This is much safer that allowing www-data to run <strong>/usr/bin/at</strong> or something more generic. Also, don&#8217;t forget to run <strong>chmod +x</strong> on both scripts.</p>
<p>The reason you can&#8217;t just hit the <em>deploy.sh</em> script is pretty simple: the server will wait on the command to finish, but the command will kill the server. Not good. So our two script method fixes this: the delay script triggers an out of process deploy script. Also, its good to keep sudo power very, very specific.</p>
<p><strong>Fourth</strong>, let&#8217;s work on that view. I&#8217;ll leave it up to the reader to decide where to put it and how to secure it (<strong>hint:</strong> maybe check for a regularly changed secret GET param key and restrict to Github&#8217;s IP address):</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">views</span>.<span style="color: black;">decorators</span>.<span style="color: black;">csrf</span> <span style="color: #ff7700;font-weight:bold;">import</span> csrf_exempt
&nbsp;
@csrf_exempt
<span style="color: #ff7700;font-weight:bold;">def</span> do_deploy<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
    Smile. Life's just gotten easier.
    &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">import</span> simplejson
    <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">subprocess</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">http</span> <span style="color: #ff7700;font-weight:bold;">import</span> HttpResponse, Http404
&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: #ff7700;font-weight:bold;">raise</span> Http404
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #483d8b;">'payload'</span> <span style="color: #ff7700;font-weight:bold;">in</span> request.<span style="color: black;">POST</span>.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">raise</span> Http404
&nbsp;
    <span style="color: #808080; font-style: italic;"># might raise 404 if secret GET key isn't good </span>
    <span style="color: #808080; font-style: italic;"># or requesting IP isn't whitelisted </span>
&nbsp;
    payload = simplejson.<span style="color: black;">loads</span><span style="color: black;">&#40;</span>request.<span style="color: black;">POST</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'payload'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
    out_json = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'status'</span>: <span style="color: #483d8b;">'failed'</span><span style="color: black;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># only trigger if master branch receives a commit</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> payload<span style="color: black;">&#91;</span><span style="color: #483d8b;">'ref'</span><span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'refs/heads/master'</span>:
        <span style="color: #808080; font-style: italic;"># DO NOT use any user input when calling scripts, as</span>
        <span style="color: #808080; font-style: italic;"># this is naughty enough already</span>
&nbsp;
        <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">call</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'sudo /path/to/delay_deploy.sh'</span>, shell=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
        out_json<span style="color: black;">&#91;</span><span style="color: #483d8b;">'status'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">'success'</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponse<span style="color: black;">&#40;</span>simplejson.<span style="color: black;">dumps</span><span style="color: black;">&#40;</span>out_json<span style="color: black;">&#41;</span>, content_type=<span style="color: #483d8b;">'application/json'</span><span style="color: black;">&#41;</span></pre></div></div>

<p><strong>Fifth</strong>, and finally, set up your Post Receive Hook or custom Service Hook with Github to hit the URL for the above view. If you need to debug, check out <a href="http://hurl.it/">hurl.it</a> for some handy dandy POST testing action.</p>
<p>Another warning: letting HTTP requests trigger sudo&#8217;d events is very, very dangerous. While as far as I know, this particular method is mostly safe, it most certainly isn&#8217;t best practices. Light me up in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://bryanhelmig.com/poor-mans-continuous-integration-with-djangopython/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python and the DocuSign API</title>
		<link>http://bryanhelmig.com/python-and-the-docusign-api/</link>
		<comments>http://bryanhelmig.com/python-and-the-docusign-api/#comments</comments>
		<pubDate>Fri, 04 Nov 2011 15:36:32 +0000</pubDate>
		<dc:creator>Bryan</dc:creator>
				<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://bryanhelmig.com/?p=369</guid>
		<description><![CDATA[SOAP is a bit foreign to me (JSON + good documentation seems so much easier), but I finally managed to authenticate DocuSign with a SOAP client in Python. The code below assumes you have a developer account all set up and have suds, the Python SOAP library, installed: from suds.client import Client &#160; class DocuSign&#40;Client&#41;: [...]]]></description>
			<content:encoded><![CDATA[<p>SOAP is a bit foreign to me (JSON + good documentation seems so much easier), but I finally managed to authenticate DocuSign with a SOAP client in Python. The code below assumes you have a developer account all set up and have suds, the <a href="https://fedorahosted.org/suds/">Python SOAP library</a>, installed:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> suds.<span style="color: black;">client</span> <span style="color: #ff7700;font-weight:bold;">import</span> Client
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> DocuSign<span style="color: black;">&#40;</span>Client<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
    Create a preloaded suds client.
    &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, username, password, integrator_key, demo=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>:
&nbsp;
        url = <span style="color: #483d8b;">'https://%s.docusign.net/api/3.0/schema/dsapi.wsdl'</span> <span style="color: #66cc66;">%</span> <span style="color: #483d8b;">'demo'</span> <span style="color: #ff7700;font-weight:bold;">if</span> demo <span style="color: #ff7700;font-weight:bold;">else</span> <span style="color: #483d8b;">'www'</span>
        location = <span style="color: #483d8b;">'https://%s.docusign.net/api/3.0/dsapi.asmx'</span> <span style="color: #66cc66;">%</span> <span style="color: #483d8b;">'demo'</span> <span style="color: #ff7700;font-weight:bold;">if</span> demo <span style="color: #ff7700;font-weight:bold;">else</span> <span style="color: #483d8b;">'www'</span>
        auth = <span style="color: black;">&#123;</span>
            <span style="color: #483d8b;">'X-DocuSign-Authentication'</span>: <span style="color: #483d8b;">'&lt;DocuSignCredentials&gt;'</span> +
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">'&lt;Username&gt;%s&lt;/Username&gt;'</span> <span style="color: #66cc66;">%</span> username<span style="color: black;">&#41;</span> +
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">'&lt;Password&gt;%s&lt;/Password&gt;'</span> <span style="color: #66cc66;">%</span> password<span style="color: black;">&#41;</span> +
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">'&lt;IntegratorKey&gt;%s&lt;/IntegratorKey&gt;'</span> <span style="color: #66cc66;">%</span> integrator_key<span style="color: black;">&#41;</span> +
            <span style="color: #483d8b;">'&lt;/DocuSignCredentials&gt;'</span><span style="color: black;">&#125;</span>
&nbsp;
        <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>DocuSign, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span>url, location=location, headers=auth<span style="color: black;">&#41;</span>
&nbsp;
client = DocuSign<span style="color: black;">&#40;</span>
    username=<span style="color: #483d8b;">'me@example.com'</span>, 
    password=<span style="color: #483d8b;">'secret'</span>, 
    integrator_key=<span style="color: #483d8b;">'FOOO-6deb3ff9-5479-1241-9287-11f4919c7417'</span>, 
    demo=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> client.<span style="color: black;">service</span>.<span style="color: black;">Ping</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Just follow the <a rel="nofollow" href="http://www.docusign.com/p/APIGuide/APIGuide.htm">DocuSign documentation</a> and <a rel="nofollow" href="https://fedorahosted.org/suds/wiki/Documentation">suds documentation</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://bryanhelmig.com/python-and-the-docusign-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Chasing Those Long Tail Keywords</title>
		<link>http://bryanhelmig.com/chasing-those-long-tail-keywords/</link>
		<comments>http://bryanhelmig.com/chasing-those-long-tail-keywords/#comments</comments>
		<pubDate>Sun, 23 Oct 2011 23:34:49 +0000</pubDate>
		<dc:creator>Bryan</dc:creator>
				<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://bryanhelmig.com/?p=362</guid>
		<description><![CDATA[With Snapier.com, my early stage startup, we&#8217;re betting that a large fraction of our future customers will find us from organic searches. However, we know better than to bet the house on a single, high value keyword like API integration. So, we&#8217;re trying to think a little like Hacker News&#8217; resident SEO expert Patrick McKenzie [...]]]></description>
			<content:encoded><![CDATA[<p>With Snapier.com, my early stage startup, we&#8217;re betting that a large fraction of our future customers will find us from organic searches. However, we know better than to bet the house on a single, high value keyword like <strong>API integration</strong>. So, we&#8217;re trying to think a little like Hacker News&#8217; resident SEO expert Patrick McKenzie (patio11):</p>
<p style="padding-left: 30px;"><em>&#8230;go for long tailed keywords, and make sure you have a scalable system to create the content.</em></p>
<p>The solution is our <a href="https://zapier.com/zapbook/">directory of API integrations</a>, the <em>Zapbook</em>. We&#8217;ve built a quick and dirty system of categorizing what we are calling &#8220;snaps&#8221;, little pieces of content that fall into categories that encompass popular web services. The hope is that we can build up a powerhouse of extremely useful (if narrow) solutions that will rank highly across very specific long tail searches.</p>
<p>Some things we spent some time thinking about:</p>
<h3>Common sense URL structure:</h3>
<p>We wanted to be sure that we wouldn&#8217;t be shooting ourselves in the foot if we decide to make changes later on down the road. So we kept URL structure really simple and obvious. Ideally, we should be able to restructure the way a user searches and navigates the Zapbook without altering the URLs Google has indexed.</p>
<h3>Simplified content creatation:</h3>
<p>Our Snapbook is no good without content, so we made it super simple to manually add small pieces of content as we go along the path of building our company. Convenience is key. Eventually, it should be trivial to let users augment content as they use the site, or have services like Textbroker automatically handle this.</p>
<h3>Measurable value of content:</h3>
<p>We can track every user that signs up after landing on a piece of deep content. From that, we can calculate the value of an average (or specific) piece of content, allowing us to justify spending more or less time or money on content creation.</p>
]]></content:encoded>
			<wfw:commentRss>http://bryanhelmig.com/chasing-those-long-tail-keywords/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>20 Minutes With Stripe and Django</title>
		<link>http://bryanhelmig.com/20-minutes-with-stripe-and-django/</link>
		<comments>http://bryanhelmig.com/20-minutes-with-stripe-and-django/#comments</comments>
		<pubDate>Tue, 11 Oct 2011 05:14:30 +0000</pubDate>
		<dc:creator>Bryan</dc:creator>
				<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://bryanhelmig.com/?p=340</guid>
		<description><![CDATA[If you use this, make sure you are PCI compliant, otherwise explore stripe.js&#8230; In case you haven&#8217;t heard, payment gateways, merchant accounts and all that jazz are now obsolete thanks to Stripe. Stripe offers a simple to set up payment service with an absolutely wonderful API. Instead of comparing and contrasting dozens of merchant accounts [...]]]></description>
			<content:encoded><![CDATA[<p><strong>If you use this, make sure you are PCI compliant, otherwise explore stripe.js&#8230;</span></p>
<p>In case you haven&#8217;t heard, payment gateways, merchant accounts and all that jazz are now obsolete thanks to Stripe. Stripe offers a simple to set up payment service with an absolutely wonderful API. Instead of comparing and contrasting dozens of merchant accounts and struggling with arcane API&#8217;s, with Stripe you input your name, address, SSN, and bank account information (among a few other things) and&#8230; you&#8217;re done.</p>
<p>In addition to a sane API, they offer <a href="https://stripe.com/api/bindings">great libraries</a> for PHP, Python and Ruby along with <a href="https://stripe.com/api/docs?lang=python#top">wonderful documentation</a>. Kudos to the developer team at Stripe, they&#8217;ve really outdone themselves.</p>
<p>Enough praising Stripe, let&#8217;s learn how to integrate it with Django. This should only take 20 minutes or so, and you shouldn&#8217;t have a problem bending this method to your will.</p>
<p><strong>Things we need:</strong></p>
<ol>
<li>Some way to store the sale in our database.</li>
<li>A form that will validate the card details.</li>
<li>A template to display the form with proper, human readable errors.</li>
<li>The Stripe Python library:
<pre><code>sudo pip install --index-url https://code.stripe.com --upgrade stripe</code></pre>
</li>
</ol>
<p>Luckily, we need not start from scratch on everything, as there is a very useful <a href="http://djangosnippets.org/snippets/907/">snippet for a credit card form</a> we can start with. With that in mind, let&#8217;s lay out our project. I&#8217;ve decided to create a <strong>sales</strong> app that will contain: a Sale model, a SalePaymentForm, and a single view for displaying and parsing the form. First up, the <strong>Sale</strong> model in <em>mystore/sales/models.py</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">db</span> <span style="color: #ff7700;font-weight:bold;">import</span> models
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> settings
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Sale<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>Sale, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># bring in stripe, and get the api key from settings.py</span>
        <span style="color: #ff7700;font-weight:bold;">import</span> stripe
        stripe.<span style="color: black;">api_key</span> = settings.<span style="color: black;">STRIPE_API_KEY</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">stripe</span> = stripe
&nbsp;
    <span style="color: #808080; font-style: italic;"># store the stripe charge id for this sale</span>
    charge_id = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">32</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># you could also store other information about the sale</span>
    <span style="color: #808080; font-style: italic;"># but I'll leave that to you!</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> charge<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, price_in_cents, number, exp_month, exp_year, cvc<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;
        Takes a the price and credit card details: number, exp_month,
        exp_year, cvc.
&nbsp;
        Returns a tuple: (Boolean, Class) where the boolean is if
        the charge was successful, and the class is response (or error)
        instance.
        &quot;&quot;&quot;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">charge_id</span>: <span style="color: #808080; font-style: italic;"># don't let this be charged twice!</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>, <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span>message=<span style="color: #483d8b;">&quot;Already charged.&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            response = <span style="color: #008000;">self</span>.<span style="color: black;">stripe</span>.<span style="color: black;">Charge</span>.<span style="color: black;">create</span><span style="color: black;">&#40;</span>
                amount = price_in_cents,
                currency = <span style="color: #483d8b;">&quot;usd&quot;</span>,
                card = <span style="color: black;">&#123;</span>
                    <span style="color: #483d8b;">&quot;number&quot;</span> : number,
                    <span style="color: #483d8b;">&quot;exp_month&quot;</span> : exp_month,
                    <span style="color: #483d8b;">&quot;exp_year&quot;</span> : exp_year,
                    <span style="color: #483d8b;">&quot;cvc&quot;</span> : cvc,
&nbsp;
                    <span style="color: #808080; font-style: italic;">#### it is recommended to include the address!</span>
                    <span style="color: #808080; font-style: italic;">#&quot;address_line1&quot; : self.address1,</span>
                    <span style="color: #808080; font-style: italic;">#&quot;address_line2&quot; : self.address2,</span>
                    <span style="color: #808080; font-style: italic;">#&quot;daddress_zip&quot; : self.zip_code,</span>
                    <span style="color: #808080; font-style: italic;">#&quot;address_state&quot; : self.state,</span>
                <span style="color: black;">&#125;</span>,
                description=<span style="color: #483d8b;">'Thank you for your purchase!'</span><span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #008000;">self</span>.<span style="color: black;">charge_id</span> = response.<span style="color: #008000;">id</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">self</span>.<span style="color: black;">stripe</span>.<span style="color: black;">CardError</span>, ce:
            <span style="color: #808080; font-style: italic;"># charge failed</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>, ce
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>, response</pre></div></div>

<p>A couple things to point out before moving on. <strong>One</strong>, we initialize the Stripe API in the __init__ method and use the API key you should set in <em>settings.py</em> as <em>STRIPE_API_KEY = &#8220;somelongstripefromstripe&#8221;</em>. <strong>Two</strong>, we wrap the actual charge API call in a try/except block so we can catch any errors Stripe might throw. Notice that we return that error as it is important to show the real, human readable error to the end user. You&#8217;ll see how in a second. <strong>Three</strong>, we make sure to set the <em>charge_id</em> on the mode, but we&#8217;ll need to remember to call the <strong>save()</strong> method.</p>
<p>Next, we need a form to handle the user&#8217;s data. Luckily, that snippet from before will come in handy, as the only thing we need to do is switch out the payment implementation. So, without further ado, here is the <strong>SalePaymentForm</strong> in <em>mystore/sales/forms.py</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">datetime</span> <span style="color: #ff7700;font-weight:bold;">import</span> date, <span style="color: #dc143c;">datetime</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">calendar</span> <span style="color: #ff7700;font-weight:bold;">import</span> monthrange
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> django <span style="color: #ff7700;font-weight:bold;">import</span> forms
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> sales.<span style="color: black;">models</span> <span style="color: #ff7700;font-weight:bold;">import</span> Sale
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> CreditCardField<span style="color: black;">&#40;</span>forms.<span style="color: black;">IntegerField</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> clean<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, value<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;Check if given CC number is valid and one of the
           card types we accept&quot;&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> value <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">13</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">16</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">raise</span> forms.<span style="color: black;">ValidationError</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Please enter in a valid &quot;</span>+\
                <span style="color: #483d8b;">&quot;credit card number.&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CreditCardField, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">clean</span><span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> CCExpWidget<span style="color: black;">&#40;</span>forms.<span style="color: black;">MultiWidget</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot; Widget containing two select boxes for selecting the month and year&quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> decompress<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, value<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span>value.<span style="color: black;">month</span>, value.<span style="color: black;">year</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">if</span> value <span style="color: #ff7700;font-weight:bold;">else</span> <span style="color: black;">&#91;</span><span style="color: #008000;">None</span>, <span style="color: #008000;">None</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> format_output<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, rendered_widgets<span style="color: black;">&#41;</span>:
        html = u<span style="color: #483d8b;">' / '</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>rendered_widgets<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> u<span style="color: #483d8b;">'&lt;span style=&quot;white-space: nowrap;&quot;&gt;%s&lt;/span&gt;'</span> <span style="color: #66cc66;">%</span> html
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> CCExpField<span style="color: black;">&#40;</span>forms.<span style="color: black;">MultiValueField</span><span style="color: black;">&#41;</span>:
    EXP_MONTH = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>x, x<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">13</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    EXP_YEAR = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>x, x<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>date.<span style="color: black;">today</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">year</span>,
                                       date.<span style="color: black;">today</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">year</span> + <span style="color: #ff4500;">15</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    default_error_messages = <span style="color: black;">&#123;</span>
        <span style="color: #483d8b;">'invalid_month'</span>: u<span style="color: #483d8b;">'Enter a valid month.'</span>,
        <span style="color: #483d8b;">'invalid_year'</span>: u<span style="color: #483d8b;">'Enter a valid year.'</span>,
    <span style="color: black;">&#125;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
        errors = <span style="color: #008000;">self</span>.<span style="color: black;">default_error_messages</span>.<span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">'error_messages'</span> <span style="color: #ff7700;font-weight:bold;">in</span> kwargs:
            errors.<span style="color: black;">update</span><span style="color: black;">&#40;</span>kwargs<span style="color: black;">&#91;</span><span style="color: #483d8b;">'error_messages'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        fields = <span style="color: black;">&#40;</span>
            forms.<span style="color: black;">ChoiceField</span><span style="color: black;">&#40;</span>choices=<span style="color: #008000;">self</span>.<span style="color: black;">EXP_MONTH</span>,
                error_messages=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'invalid'</span>: errors<span style="color: black;">&#91;</span><span style="color: #483d8b;">'invalid_month'</span><span style="color: black;">&#93;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>,
            forms.<span style="color: black;">ChoiceField</span><span style="color: black;">&#40;</span>choices=<span style="color: #008000;">self</span>.<span style="color: black;">EXP_YEAR</span>,
                error_messages=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'invalid'</span>: errors<span style="color: black;">&#91;</span><span style="color: #483d8b;">'invalid_year'</span><span style="color: black;">&#93;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>,
        <span style="color: black;">&#41;</span>
        <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CCExpField, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span>fields, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">widget</span> = CCExpWidget<span style="color: black;">&#40;</span>widgets =
            <span style="color: black;">&#91;</span>fields<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">widget</span>, fields<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>.<span style="color: black;">widget</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> clean<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, value<span style="color: black;">&#41;</span>:
        exp = <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CCExpField, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">clean</span><span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> date.<span style="color: black;">today</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span> exp:
            <span style="color: #ff7700;font-weight:bold;">raise</span> forms.<span style="color: black;">ValidationError</span><span style="color: black;">&#40;</span>
            <span style="color: #483d8b;">&quot;The expiration date you entered is in the past.&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> exp
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> compress<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, data_list<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> data_list:
            <span style="color: #ff7700;font-weight:bold;">if</span> data_list<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">in</span> forms.<span style="color: black;">fields</span>.<span style="color: black;">EMPTY_VALUES</span>:
                error = <span style="color: #008000;">self</span>.<span style="color: black;">error_messages</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'invalid_year'</span><span style="color: black;">&#93;</span>
                <span style="color: #ff7700;font-weight:bold;">raise</span> forms.<span style="color: black;">ValidationError</span><span style="color: black;">&#40;</span>error<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> data_list<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">in</span> forms.<span style="color: black;">fields</span>.<span style="color: black;">EMPTY_VALUES</span>:
                error = <span style="color: #008000;">self</span>.<span style="color: black;">error_messages</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'invalid_month'</span><span style="color: black;">&#93;</span>
                <span style="color: #ff7700;font-weight:bold;">raise</span> forms.<span style="color: black;">ValidationError</span><span style="color: black;">&#40;</span>error<span style="color: black;">&#41;</span>
            year = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>data_list<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
            month = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>data_list<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
            <span style="color: #808080; font-style: italic;"># find last day of the month</span>
            day = monthrange<span style="color: black;">&#40;</span>year, month<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> date<span style="color: black;">&#40;</span>year, month, day<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> SalePaymentForm<span style="color: black;">&#40;</span>forms.<span style="color: black;">Form</span><span style="color: black;">&#41;</span>:
    number = CreditCardField<span style="color: black;">&#40;</span>required=<span style="color: #008000;">True</span>, label=<span style="color: #483d8b;">&quot;Card Number&quot;</span><span style="color: black;">&#41;</span>
    expiration = CCExpField<span style="color: black;">&#40;</span>required=<span style="color: #008000;">True</span>, label=<span style="color: #483d8b;">&quot;Expiration&quot;</span><span style="color: black;">&#41;</span>
    cvc = forms.<span style="color: black;">IntegerField</span><span style="color: black;">&#40;</span>required=<span style="color: #008000;">True</span>, label=<span style="color: #483d8b;">&quot;CCV Number&quot;</span>,
        max_value=<span style="color: #ff4500;">9999</span>, widget=forms.<span style="color: black;">TextInput</span><span style="color: black;">&#40;</span>attrs=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'size'</span>: <span style="color: #483d8b;">'4'</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> clean<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;
        The clean method will effectively charge the card and create a new
        Sale instance. If it fails, it simply raises the error given from
        Stripe's library as a standard ValidationError for proper feedback.
        &quot;&quot;&quot;</span>
        cleaned = <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>SalePaymentForm, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">clean</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">errors</span>:
            number = <span style="color: #008000;">self</span>.<span style="color: black;">cleaned_data</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;number&quot;</span><span style="color: black;">&#93;</span>
            exp_month = <span style="color: #008000;">self</span>.<span style="color: black;">cleaned_data</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;expiration&quot;</span><span style="color: black;">&#93;</span>.<span style="color: black;">month</span>
            exp_year = <span style="color: #008000;">self</span>.<span style="color: black;">cleaned_data</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;expiration&quot;</span><span style="color: black;">&#93;</span>.<span style="color: black;">year</span>
            cvc = <span style="color: #008000;">self</span>.<span style="color: black;">cleaned_data</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;cvc&quot;</span><span style="color: black;">&#93;</span>
&nbsp;
            sale = Sale<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #808080; font-style: italic;"># let's charge $10.00 for this particular item</span>
            success, instance = sale.<span style="color: black;">charge</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1000</span>, number, exp_month,
                                                exp_year, cvc<span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> success:
                <span style="color: #ff7700;font-weight:bold;">raise</span> forms.<span style="color: black;">ValidationError</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Error: %s&quot;</span> <span style="color: #66cc66;">%</span> instance.<span style="color: black;">message</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                instance.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                <span style="color: #808080; font-style: italic;"># we were successful! do whatever you will here...</span>
                <span style="color: #808080; font-style: italic;"># perhaps you'd like to send an email...</span>
                <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> cleaned</pre></div></div>

<p>I&#8217;ve removed a few of the bits from the snippet, mainly to simplify things, as we don&#8217;t need to filter out Discover cards or the like. The primary thing to notice is how if the charge isn&#8217;t successful, we raise a <strong>ValidationError</strong> and pass in the message from Stripe&#8217;s exception. This will allow us to display it as a normal error on the form. But before we we move on to the view, notice how we initialize a blank <strong>Sale</strong>? The charge() method doesn&#8217;t require a saved model instance, but we make sure to save it if it is successful. It might be more appropriate to make this a ModelForm, but for now, it works.</p>
<p>Let&#8217;s take a look at <em>mystore/sales/urls.py</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span>.<span style="color: black;">urls</span>.<span style="color: black;">defaults</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">from</span> sales <span style="color: #ff7700;font-weight:bold;">import</span> views
&nbsp;
urlpatterns = patterns<span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span>,
    url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^charge/$'</span>, views.<span style="color: black;">charge</span>, name=<span style="color: #483d8b;">&quot;charge&quot;</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#41;</span></pre></div></div>

<p>Nothing fancy here. Let&#8217;s take a look at and <em>mystore/sales/views.py</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">shortcuts</span> <span style="color: #ff7700;font-weight:bold;">import</span> render_to_response
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">http</span> <span style="color: #ff7700;font-weight:bold;">import</span> HttpResponse
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">template</span> <span style="color: #ff7700;font-weight:bold;">import</span> RequestContext
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> sales.<span style="color: black;">models</span> <span style="color: #ff7700;font-weight:bold;">import</span> Sale
<span style="color: #ff7700;font-weight:bold;">from</span> sales.<span style="color: black;">forms</span> <span style="color: #ff7700;font-weight:bold;">import</span> SalePaymentForm
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> charge<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> request.<span style="color: black;">method</span> == <span style="color: #483d8b;">&quot;POST&quot;</span>:
        form = SalePaymentForm<span style="color: black;">&#40;</span>request.<span style="color: black;">POST</span><span style="color: black;">&#41;</span>
&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;"># charges the card</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponse<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Success! We've charged your card!&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        form = SalePaymentForm<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> render_to_response<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;sales/charge.html&quot;</span>,
                        RequestContext<span style="color: black;">&#40;</span> request, <span style="color: black;">&#123;</span><span style="color: #483d8b;">'form'</span>: form<span style="color: black;">&#125;</span> <span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span></pre></div></div>

<p>Once again, nothing very fancy here either. Let&#8217;s take a look at <em>templates/sales/charge.html</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Stripe Example<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;wrapper&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
  {% for key, value in form.errors.items %}
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>{{ value }}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  {% endfor %}
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;post&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>{% csrf_token %}
&nbsp;
    {% for field in form %}
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;field-wrapper&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;field-label&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          {{ field.label_tag }}:
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;field-field&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          {{ field }}
          {{ field.errors }}
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    {% endfor %}
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Charge Me!&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This should come as no surprise to you, but there isn&#8217;t much going on here. Probably the only slightly odd thing is the for loop over the <em>form.errors</em> dictionary, as this is where we will grab the ValidationError placed on the form itself as we caught it during the charge phase.</p>
<p>Besides for setting up the standard Django stuff like the database and admin bits (<em>mystore/sales/admin.py)</em>, that pretty much covers it.</p>
<p>This is a method similar to what we&#8217;re using on our new product <a href="http://zapier.com/">Zapier</a>, so <a href="https://github.com/bryanhelmig/django-stripe-example">go grab the code off of GitHub!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://bryanhelmig.com/20-minutes-with-stripe-and-django/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Setting up Ubuntu 10.04 with Apache, memcached, ufw, MySQL, and Django 1.2 on Linode</title>
		<link>http://bryanhelmig.com/setting-up-ubuntu-10-04-with-apache-memcached-ufw-mysql-and-django-1-2-on-linode/</link>
		<comments>http://bryanhelmig.com/setting-up-ubuntu-10-04-with-apache-memcached-ufw-mysql-and-django-1-2-on-linode/#comments</comments>
		<pubDate>Mon, 24 May 2010 08:40:03 +0000</pubDate>
		<dc:creator>Bryan</dc:creator>
				<category><![CDATA[Work]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[linode]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[ufw]]></category>

		<guid isPermaLink="false">http://bryanhelmig.com/?p=286</guid>
		<description><![CDATA[Call me a sucker, but I love a good server setup as much as the next guy, I just have a little trouble setting it up sometimes. So I thought I&#8217;d walk through my process for setting up all this Django goodness on what is basically a LAMP setup (where &#8220;P&#8221; stands for Python!) with [...]]]></description>
			<content:encoded><![CDATA[<p>Call me a sucker, but I love a good server setup as much as the next guy, I just have a little trouble setting it up sometimes. So I thought I&#8217;d walk through my process for setting up all this Django goodness on what is basically a LAMP setup (where &#8220;P&#8221; stands for Python!) with a few extras like memcached and ufw. We&#8217;ll get to a level of general security, but not prefect security.</p>
<p>Also, we&#8217;ll keep this well within the 360 megabytes allotted for the cheapest Linode. Before we get underway, just create a new Linode with Ubuntu 10.04 (32-bit), set your password. Alright, let&#8217;s get going!</p>
<p><strong>Initial Setup</strong><br />
First thing to do is log in via the standard SSH on your Linode&#8217;s IP, port 22, and with <em>root</em> as your username. We&#8217;ll change the login user away from root, but for now, this will do (plus we can skip all that sudo stuff). First, let&#8217;s update and upgrade the system.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> update <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #c20cb9; font-weight: bold;">apt-get</span> upgrade</pre></div></div>

<p>Awesome, you should be 100% up-to-date. Now its time to get to the fun part, let&#8217;s <strong>install</strong> some of the software we&#8217;ll be using! Below are the commands to install Apache, MySQL, mod_wsgi and the python MySQL bindings, as well as memcached and ufw. If you have any prompts for passwords, you know what to do! Just remember what you set them as.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> build-essentials
<span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> apache2 apache2.2-common apache2-mpm-worker apache2-threaded-dev libapache2-mod-wsgi python-dev python-setuptools
<span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> mysql-server python-mysqldb
<span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> memcached libmemcache-dev
<span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> ufw</pre></div></div>

<p>Really quickly, let&#8217;s get <strong>python-memcached</strong> installed (alternatively, if you need some raw speed, look up cmemcached or python-libmemcached). This will take a few steps&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># create and enter a temp dir in /home</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>home <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">mkdir</span> downloads <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #7a0874; font-weight: bold;">cd</span> downloads
<span style="color: #666666; font-style: italic;"># get django 1.2 tar and untar it</span>
<span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #660033;">-O</span> pymem.tar.gz <span style="color: #c20cb9; font-weight: bold;">ftp</span>:<span style="color: #000000; font-weight: bold;">//</span>ftp.tummy.com<span style="color: #000000; font-weight: bold;">/</span>pub<span style="color: #000000; font-weight: bold;">/</span>python-memcached<span style="color: #000000; font-weight: bold;">/</span>python-memcached-<span style="color: #000000;">1.47</span>.tar.gz <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zxvf</span> pymem.tar.gz
<span style="color: #666666; font-style: italic;"># enter the directory and install</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> python-memcached-<span style="color: #000000;">1.47</span> <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; python setup.py <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>Now its time for Django! <strong>Django 1.1.1</strong> is a lot easier to install than Django 1.2.4:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># install django 1.1.1</span>
<span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> python-django</pre></div></div>

<p>But let&#8217;s say we need <strong>Django 1.2.4</strong>: its gonna take a few more commands to make this happen. Watch out for that last command, anytime you use <code>rm -r</code> you can run into real trouble if you mistype (but we&#8217;re not production yet so no worries, right?).</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># enter the downloads directory in /home</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>downloads
<span style="color: #666666; font-style: italic;"># get django 1.2 tar and untar it</span>
<span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #660033;">-O</span> django124.tar.gz http:<span style="color: #000000; font-weight: bold;">//</span>www.djangoproject.com<span style="color: #000000; font-weight: bold;">/</span>download<span style="color: #000000; font-weight: bold;">/</span>1.2.4<span style="color: #000000; font-weight: bold;">/</span>tarball<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zxvf</span> django124.tar.gz
<span style="color: #666666; font-style: italic;"># enter the directory and install django 1.2</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> Django-1.2.4 <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; python setup.py <span style="color: #c20cb9; font-weight: bold;">install</span>
<span style="color: #666666; font-style: italic;"># strictly optional delete of downloads directory, be careful with rm -r</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>home <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-r</span> downloads</pre></div></div>

<p><strong>Actual Configuration</strong><br />
Let&#8217;s work backwards, we&#8217;ll start with the easy stuff and work our way to the more complicated things. Let&#8217;s get <strong>ufw</strong> and the <strong>SSH port</strong> out of the way first. Go ahead and pick a number between 1024-8000ish for the port we will eventually; I chose <strong>5555</strong> but you <del>can</del> should use something else.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># turn on ufw</span>
ufw <span style="color: #7a0874; font-weight: bold;">enable</span>
<span style="color: #666666; font-style: italic;"># log all activity (you'll be glad you have this later)</span>
ufw logging on
<span style="color: #666666; font-style: italic;"># allow port 80 for tcp (web stuff)</span>
ufw allow <span style="color: #000000;">80</span><span style="color: #000000; font-weight: bold;">/</span>tcp
<span style="color: #666666; font-style: italic;"># allow our ssh port</span>
ufw allow <span style="color: #000000;">5555</span>
<span style="color: #666666; font-style: italic;"># deny everything else</span>
ufw default deny
<span style="color: #666666; font-style: italic;"># open the ssh config file and edit the port number from 22 to 5555, ctrl-x to exit</span>
<span style="color: #c20cb9; font-weight: bold;">nano</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ssh<span style="color: #000000; font-weight: bold;">/</span>sshd_config
<span style="color: #666666; font-style: italic;"># restart ssh (don't forget to ssh with port 5555, not 22 from now on)</span>
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">ssh</span> reload</pre></div></div>

<p>Now that you have ufw and SSH locked down, its time to move onto setting up <strong>memcached</strong> (which is super easy). We&#8217;ll just run it as root and be done with it (you will need to repeat this command on each boot):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># replace 24 with however many megabytes of cache is appropriate</span>
memcached <span style="color: #660033;">-u</span> root <span style="color: #660033;">-d</span> <span style="color: #660033;">-m</span> <span style="color: #000000;">24</span> <span style="color: #660033;">-l</span> 127.0.0.1 <span style="color: #660033;">-p</span> <span style="color: #000000;">11211</span></pre></div></div>

<p>Alright, with that out of the way, let&#8217;s get MySQL nice and tight. The standard install of MySQL can suck up a lot of memory, so we&#8217;ll suggest a few ways to lighten the load:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># open mysql conf and set these settings:</span>
<span style="color: #666666; font-style: italic;">#    key_buffer = 16k</span>
<span style="color: #666666; font-style: italic;">#    max_allowed_packet = 1M</span>
<span style="color: #666666; font-style: italic;">#    thread_stack = 64K</span>
<span style="color: #c20cb9; font-weight: bold;">nano</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>my.cnf
<span style="color: #666666; font-style: italic;"># restart mysql</span>
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>mysql restart</pre></div></div>

<p>Now let&#8217;s get a new user setup and leave behind this <strong>root</strong> nonsense for safety&#8217;s sake. Your username is going to be <strong>bobby</strong> for this example. Replace <strong>bobby</strong> everywhere if you want something different.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># create bobby, you'll be asked to set the password and such</span>
adduser bobby
<span style="color: #666666; font-style: italic;"># edit the ssh file and add the line: AllowUsers bobby</span>
<span style="color: #666666; font-style: italic;"># ctrl-x to exit and save</span>
<span style="color: #c20cb9; font-weight: bold;">nano</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ssh<span style="color: #000000; font-weight: bold;">/</span>sshd_config
<span style="color: #666666; font-style: italic;"># restart ssh</span>
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">ssh</span> reload
<span style="color: #666666; font-style: italic;"># log out and login as bobby from now on!</span></pre></div></div>

<p>It&#8217;s time for the nitty gritty stuff: setting up <strong>Apache</strong> and <strong>mod_wsgi</strong> with Django for the domain you own called <em>examplesite.com</em> (creative, I know). We need to make a <strong>public_html</strong> folder in bobby&#8217;s <strong>home</strong> folder and place a folder called <strong>examplesite.com</strong> (as well as a few more). We&#8217;ll do that first.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>bobby<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> public_html
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> public_html<span style="color: #000000; font-weight: bold;">/</span>examplesite.com
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> public_html<span style="color: #000000; font-weight: bold;">/</span>examplesite.com<span style="color: #000000; font-weight: bold;">/</span>logs
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> public_html<span style="color: #000000; font-weight: bold;">/</span>examplesite.com<span style="color: #000000; font-weight: bold;">/</span>private</pre></div></div>

<p>Right now you should place your Django project into the <strong>public_html/examplesite.com</strong> folder. For example, if the project is housed in <strong>demoproject</strong> (eg: demoproject/manage.py, demoproject/urls.py, etc.) you&#8217;ll want it placed ALA <strong>public_html/examplesite.com/demoproject</strong>. Time to get the Apache config files up and running. Here we go!</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>bobby<span style="color: #000000; font-weight: bold;">/</span>public_html<span style="color: #000000; font-weight: bold;">/</span>examplesite.com<span style="color: #000000; font-weight: bold;">/</span>demoproject<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> apache
<span style="color: #c20cb9; font-weight: bold;">nano</span> apache<span style="color: #000000; font-weight: bold;">/</span>demoproject.wsgi</pre></div></div>

<p>First, in the <strong>demoproject.wsgi</strong> file you should paste and save:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">import os, sys
&nbsp;
<span style="color: #000099;">apache_configuration</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> os.path.dirname<span style="">&#40;</span>__file__<span style="">&#41;</span></span>
<span style="color: #000099;">project</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> os.path.dirname<span style="">&#40;</span>apache_configuration<span style="">&#41;</span></span>
<span style="color: #000099;">workspace</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> os.path.dirname<span style="">&#40;</span>project<span style="">&#41;</span></span>
sys.path.append<span style="">&#40;</span>workspace<span style="">&#41;</span>
&nbsp;
sys.path.append<span style="">&#40;</span>'/usr/lib/python2.5/site-packages/django/'<span style="">&#41;</span>
sys.path.append<span style="">&#40;</span>'/home/bobby/public_html/examplesite.com/demoproject'<span style="">&#41;</span>
&nbsp;
os.environ<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>'DJANGO_SETTINGS_MODULE'<span style="">&#93;</span></span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 'demoproject.settings'</span>
import django.core.handlers.wsgi
<span style="color: #000099;">application</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> django.core.handlers.wsgi.WSGIHandler<span style="">&#40;</span><span style="">&#41;</span></span></pre></div></div>

<p>We&#8217;re so close, let&#8217;s get the other Apache files setup. Oh, and don&#8217;t worry about that <strong>www-data</strong> thing just yet, we&#8217;ll get to that in a second.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># give the www-data user/group permission on public_html</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chown</span> <span style="color: #660033;">-R</span> www-data:www-data <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>bobby<span style="color: #000000; font-weight: bold;">/</span>public_html
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">nano</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>sites-available<span style="color: #000000; font-weight: bold;">/</span>examplesite.com</pre></div></div>

<p>Place the text below in the <strong>examplesite.com</strong> config file (remember the www-data part from the last command?). You can modify the threads and processes numbers to suit your moods and load.</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">    #Basic setup
    ServerAdmin your@email.com
    ServerName www.examplesite.com
    ServerAlias examplesite.com
&nbsp;
        Order deny,allow
        Allow from all
&nbsp;
    LogLevel warn
    ErrorLog  /home/bobby/public_html/examplesite.com/logs/apache_error.log
    CustomLog /home/bobby/public_html/examplesite.com/logs/apache_access.log combined
&nbsp;
    WSGIDaemonProcess examplesite.com user<span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">www-data group=www-data threads=20 processes=2</span>
    WSGIProcessGroup examplesite.com
&nbsp;
    WSGIScriptAlias / /home/bobby/public_html/examplesite.com/demoproject/apache/demoproject.wsgi</pre></div></div>

<p>A few more final things:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> a2ensite examplesite.com
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart</pre></div></div>

<p><strong>Time to admire your handiwork.</strong></p>
<p>Congrats! You&#8217;re all set up and ready to roll! Nothing can stop you now! Here&#8217;s a neat command to measure your memory usage as mine rarely gets over 160mb. This gives lots of room for growth as you can always increase the memcached size, MySQL settings, and Apache/mod_wsgi instances/threads.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># measure your memory usage in kb</span>
<span style="color: #c20cb9; font-weight: bold;">ps</span> aux <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $3&quot;\t&quot;$6&quot;\t&quot;$11;sum+=$6;cpu+=$3} END {print &quot;Total RSS&quot;, sum, &quot;\nTotal CPU&quot;, cpu}'</span></pre></div></div>

<p>Also, under Ubuntu 10.04 (Lucid), Python&#8217;s site-packages is now called dist-packages. Just a FYI.</p>
]]></content:encoded>
			<wfw:commentRss>http://bryanhelmig.com/setting-up-ubuntu-10-04-with-apache-memcached-ufw-mysql-and-django-1-2-on-linode/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Python Crossword Puzzle Generator</title>
		<link>http://bryanhelmig.com/python-crossword-puzzle-generator/</link>
		<comments>http://bryanhelmig.com/python-crossword-puzzle-generator/#comments</comments>
		<pubDate>Sat, 10 Apr 2010 21:18:31 +0000</pubDate>
		<dc:creator>Bryan</dc:creator>
				<category><![CDATA[Boring Stuff]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[crosswords]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://bryanhelmig.com/?p=275</guid>
		<description><![CDATA[As my next miniature project will be a crossword puzzle maker for teachers that will make random generation of crossword puzzles and word search puzzles, I thought I&#8217;d share the code I developed to create these puzzles on the fly. While I was working on it, I ran across many different scripts to accomplish this, [...]]]></description>
			<content:encoded><![CDATA[<p>As my next miniature project will be a <a href="http://http://crosswordpuzzlemaker.org/">crossword puzzle maker</a> for teachers that will make random generation of crossword puzzles and word search puzzles, I thought I&#8217;d share the code I developed to create these puzzles on the fly. While I was working on it, I ran across many different scripts to accomplish this, but none of them were in my most favorite of languages: Python. Besides, I&#8217;d like the code to fit snugly in my web framework of choice: Django; the popular PHP version just wouldn&#8217;t cut it. Anyways, scroll down to see the code, or read on for a little primer about the process behind it.</p>
<p><strong>Puzzles like these:</strong></p>
<pre>
p u m p e r n i c k e l -    p u m p e r n i c k e l v
a - - - - - - - a - - e -    a w j m p c a y a w r e s
l - s n i c k e r - - a -    l f s n i c k e r b z a x
a - a - - - - - a - - v -    a f a z k e u i a b f v k
d - f - c - - - m - - e -    d x f v c j f d m c n e x
i - f j o r d - e - - n -    i d f j o r d z e j g n z
n - r - d - - - l i p - -    n r r x d j a o l i p d j
- c o r a l - - - - i - -    i c o r a l u s t o i x w
- - n - - i - - - - s - -    m r n u e i i h o t s y w
- - - - - m i s t - t - -    m w e x s m i s t r t u j
p l a g u e - - - - o - -    p l a g u e b n h k o m s
- - - - - - - d a w n - -    f m n v j f p d a w n c q
- - - - - - - - - - - - -    m h j a e d p p r g t p j
</pre>
<p><strong>Behind the Scenes</strong></p>
<p>This program is actually very simple and creates completely random crosswords on the fly. Naturally, the more words you have, the better it will be at placing the most possible on a board. However, increasing the number of words will increase computation time. Additionally, increasing the board size will severely increase computation time. To counteract the fact that sometimes it will randomly generate a sub-par board, we will generate many different boards in an allotted time and only keep the &#8220;best&#8221; board (in this case, the board with the most words placed). So, as the board and word list gets bigger, the number of prospective boards created decreases within a fixed time.</p>
<p>The code first randomizes the word list and then sorts by word length. The idea here is that longer words are more difficult to place, so get them placed when the board is the most open. Next, we place the longest word on the <strong>1, 1</strong> coordinate of the grid as the <em>seed</em>. In tests, the placement of the first word at <strong>1, 1</strong> yielded by far the best results on average. Then we go to the next longest word and loop over its letters and each cell in the grid. When we find a match, we back it up and suggest a coordinate placement for that word. Once we&#8217;ve checked every letter against every cell, we chose the best (the word best is used very loosely here) coordinate and apply the word to the grid. Now we move on the next word and so forth. Once we&#8217;ve made it through once, we can loop over the unplaced words and looks for any lucky chances for a second placement.</p>
<p>This suggested coordinate system allows for a much faster fit than some methods I&#8217;ve seen that will randomly place a word to see if it works. Additionally, it requires the word cross other words which is the point of well, a crossword puzzle.</p>
<p><strong>Operation</strong></p>
<p>Be mindful when you create a word list to exclude words like &#8220;an&#8221; or  &#8220;or&#8221; because these have a tendency to be placed <em>inside </em>other  already placed words. This can be confusing. Simply run the code below.</p>
<p>You can feed the Crossword class a list of Word classes, or a list of tuples or lists with the word and clue. Either way works.</p>
<p>When you call the compute_crossword(<em>seconds</em>) method, it does all the work of computing the best crossword in however many seconds you passed. 1 second is probably enough for crossword grids of less that 20&#215;20 and 2 seconds is fine for 25&#215;25 and 3 seconds is good for 30&#215;30. Additionally, if you have a massive word list, you may want to double the time alloyed. Finally, if you can&#8217;t run psycho, quadruple these times for similar quality.</p>
<p><strong>The Code:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">random</span>, <span style="color: #dc143c;">re</span>, <span style="color: #dc143c;">time</span>, <span style="color: #dc143c;">string</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">copy</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">copy</span> <span style="color: #ff7700;font-weight:bold;">as</span> duplicate
&nbsp;
<span style="color: #808080; font-style: italic;"># optional, speeds up by a factor of 4</span>
<span style="color: #ff7700;font-weight:bold;">import</span> psyco
psyco.<span style="color: black;">full</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Crossword<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, cols, rows, empty = <span style="color: #483d8b;">'-'</span>, maxloops = <span style="color: #ff4500;">2000</span>, available_words=<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">cols</span> = cols
        <span style="color: #008000;">self</span>.<span style="color: black;">rows</span> = rows
        <span style="color: #008000;">self</span>.<span style="color: black;">empty</span> = empty
        <span style="color: #008000;">self</span>.<span style="color: black;">maxloops</span> = maxloops
        <span style="color: #008000;">self</span>.<span style="color: black;">available_words</span> = available_words
        <span style="color: #008000;">self</span>.<span style="color: black;">randomize_word_list</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">current_word_list</span> = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">debug</span> = <span style="color: #ff4500;">0</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">clear_grid</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> clear_grid<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># initialize grid and fill with empty character</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">grid</span> = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">rows</span><span style="color: black;">&#41;</span>:
            ea_row = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
            <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">cols</span><span style="color: black;">&#41;</span>:
                ea_row.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">empty</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">grid</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>ea_row<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> randomize_word_list<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># also resets words and sorts by length</span>
        temp_list = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> word <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">available_words</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">isinstance</span><span style="color: black;">&#40;</span>word, Word<span style="color: black;">&#41;</span>:
                temp_list.<span style="color: black;">append</span><span style="color: black;">&#40;</span>Word<span style="color: black;">&#40;</span>word.<span style="color: black;">word</span>, word.<span style="color: black;">clue</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                temp_list.<span style="color: black;">append</span><span style="color: black;">&#40;</span>Word<span style="color: black;">&#40;</span>word<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, word<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">random</span>.<span style="color: black;">shuffle</span><span style="color: black;">&#40;</span>temp_list<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># randomize word list</span>
        temp_list.<span style="color: black;">sort</span><span style="color: black;">&#40;</span>key=<span style="color: #ff7700;font-weight:bold;">lambda</span> i: <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>i.<span style="color: black;">word</span><span style="color: black;">&#41;</span>, reverse=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># sort by length</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">available_words</span> = temp_list
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> compute_crossword<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, time_permitted = <span style="color: #ff4500;">1.00</span>, spins=<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
        time_permitted = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>time_permitted<span style="color: black;">&#41;</span>
&nbsp;
        count = <span style="color: #ff4500;">0</span>
        <span style="color: #dc143c;">copy</span> = Crossword<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">cols</span>, <span style="color: #008000;">self</span>.<span style="color: black;">rows</span>, <span style="color: #008000;">self</span>.<span style="color: black;">empty</span>, <span style="color: #008000;">self</span>.<span style="color: black;">maxloops</span>, <span style="color: #008000;">self</span>.<span style="color: black;">available_words</span><span style="color: black;">&#41;</span>
&nbsp;
        start_full = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: black;">&#40;</span><span style="color: #008000;">float</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> - start_full<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> time_permitted <span style="color: #ff7700;font-weight:bold;">or</span> count == <span style="color: #ff4500;">0</span>: <span style="color: #808080; font-style: italic;"># only run for x seconds</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">debug</span> += <span style="color: #ff4500;">1</span>
            <span style="color: #dc143c;">copy</span>.<span style="color: black;">current_word_list</span> = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
            <span style="color: #dc143c;">copy</span>.<span style="color: black;">clear_grid</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">copy</span>.<span style="color: black;">randomize_word_list</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            x = <span style="color: #ff4500;">0</span>
            <span style="color: #ff7700;font-weight:bold;">while</span> x <span style="color: #66cc66;">&lt;</span> spins: <span style="color: #808080; font-style: italic;"># spins; 2 seems to be plenty</span>
                <span style="color: #ff7700;font-weight:bold;">for</span> word <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">copy</span>.<span style="color: black;">available_words</span>:
                    <span style="color: #ff7700;font-weight:bold;">if</span> word <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">copy</span>.<span style="color: black;">current_word_list</span>:
                        <span style="color: #dc143c;">copy</span>.<span style="color: black;">fit_and_add</span><span style="color: black;">&#40;</span>word<span style="color: black;">&#41;</span>
                x += <span style="color: #ff4500;">1</span>
            <span style="color: #808080; font-style: italic;">#print copy.solution()</span>
            <span style="color: #808080; font-style: italic;">#print len(copy.current_word_list), len(self.current_word_list), self.debug</span>
            <span style="color: #808080; font-style: italic;"># buffer the best crossword by comparing placed words</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">copy</span>.<span style="color: black;">current_word_list</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">current_word_list</span><span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">current_word_list</span> = <span style="color: #dc143c;">copy</span>.<span style="color: black;">current_word_list</span>
                <span style="color: #008000;">self</span>.<span style="color: black;">grid</span> = <span style="color: #dc143c;">copy</span>.<span style="color: black;">grid</span>
            count += <span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> suggest_coord<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, word<span style="color: black;">&#41;</span>:
        count = <span style="color: #ff4500;">0</span>
        coordlist = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        glc = -<span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> given_letter <span style="color: #ff7700;font-weight:bold;">in</span> word.<span style="color: black;">word</span>: <span style="color: #808080; font-style: italic;"># cycle through letters in word</span>
            glc += <span style="color: #ff4500;">1</span>
            rowc = <span style="color: #ff4500;">0</span>
            <span style="color: #ff7700;font-weight:bold;">for</span> row <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">grid</span>: <span style="color: #808080; font-style: italic;"># cycle through rows</span>
                rowc += <span style="color: #ff4500;">1</span>
                colc = <span style="color: #ff4500;">0</span>
                <span style="color: #ff7700;font-weight:bold;">for</span> cell <span style="color: #ff7700;font-weight:bold;">in</span> row: <span style="color: #808080; font-style: italic;"># cycle through  letters in rows</span>
                    colc += <span style="color: #ff4500;">1</span>
                    <span style="color: #ff7700;font-weight:bold;">if</span> given_letter == cell: <span style="color: #808080; font-style: italic;"># check match letter in word to letters in row</span>
                        <span style="color: #ff7700;font-weight:bold;">try</span>: <span style="color: #808080; font-style: italic;"># suggest vertical placement </span>
                            <span style="color: #ff7700;font-weight:bold;">if</span> rowc - glc <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>: <span style="color: #808080; font-style: italic;"># make sure we're not suggesting a starting point off the grid</span>
                                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>rowc - glc<span style="color: black;">&#41;</span> + word.<span style="color: black;">length</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #008000;">self</span>.<span style="color: black;">rows</span>: <span style="color: #808080; font-style: italic;"># make sure word doesn't go off of grid</span>
                                    coordlist.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>colc, rowc - glc, <span style="color: #ff4500;">1</span>, colc + <span style="color: black;">&#40;</span>rowc - glc<span style="color: black;">&#41;</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
                        <span style="color: #ff7700;font-weight:bold;">except</span>: <span style="color: #ff7700;font-weight:bold;">pass</span>
                        <span style="color: #ff7700;font-weight:bold;">try</span>: <span style="color: #808080; font-style: italic;"># suggest horizontal placement </span>
                            <span style="color: #ff7700;font-weight:bold;">if</span> colc - glc <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>: <span style="color: #808080; font-style: italic;"># make sure we're not suggesting a starting point off the grid</span>
                                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>colc - glc<span style="color: black;">&#41;</span> + word.<span style="color: black;">length</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #008000;">self</span>.<span style="color: black;">cols</span>: <span style="color: #808080; font-style: italic;"># make sure word doesn't go off of grid</span>
                                    coordlist.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>colc - glc, rowc, <span style="color: #ff4500;">0</span>, rowc + <span style="color: black;">&#40;</span>colc - glc<span style="color: black;">&#41;</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
                        <span style="color: #ff7700;font-weight:bold;">except</span>: <span style="color: #ff7700;font-weight:bold;">pass</span>
        <span style="color: #808080; font-style: italic;"># example: coordlist[0] = [col, row, vertical, col + row, score]</span>
        <span style="color: #808080; font-style: italic;">#print word.word</span>
        <span style="color: #808080; font-style: italic;">#print coordlist</span>
        new_coordlist = <span style="color: #008000;">self</span>.<span style="color: black;">sort_coordlist</span><span style="color: black;">&#40;</span>coordlist, word<span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;">#print new_coordlist</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> new_coordlist
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> sort_coordlist<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, coordlist, word<span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># give each coordinate a score, then sort</span>
        new_coordlist = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> coord <span style="color: #ff7700;font-weight:bold;">in</span> coordlist:
            col, row, vertical = coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>
            coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">self</span>.<span style="color: black;">check_fit_score</span><span style="color: black;">&#40;</span>col, row, vertical, word<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># checking scores</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span>: <span style="color: #808080; font-style: italic;"># 0 scores are filtered</span>
                new_coordlist.<span style="color: black;">append</span><span style="color: black;">&#40;</span>coord<span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">random</span>.<span style="color: black;">shuffle</span><span style="color: black;">&#40;</span>new_coordlist<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># randomize coord list; why not?</span>
        new_coordlist.<span style="color: black;">sort</span><span style="color: black;">&#40;</span>key=<span style="color: #ff7700;font-weight:bold;">lambda</span> i: i<span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span>, reverse=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># put the best scores first</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> new_coordlist
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> fit_and_add<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, word<span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># doesn't really check fit except for the first word; otherwise just adds if score is good</span>
        fit = <span style="color: #008000;">False</span>
        count = <span style="color: #ff4500;">0</span>
        coordlist = <span style="color: #008000;">self</span>.<span style="color: black;">suggest_coord</span><span style="color: black;">&#40;</span>word<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff7700;font-weight:bold;">not</span> fit <span style="color: #ff7700;font-weight:bold;">and</span> count <span style="color: #66cc66;">&lt;</span> <span style="color: #008000;">self</span>.<span style="color: black;">maxloops</span>:
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">current_word_list</span><span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>: <span style="color: #808080; font-style: italic;"># this is the first word: the seed</span>
                <span style="color: #808080; font-style: italic;"># top left seed of longest word yields best results (maybe override)</span>
                vertical, col, row = <span style="color: #dc143c;">random</span>.<span style="color: black;">randrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">1</span>
                <span style="color: #483d8b;">''</span><span style="color: #483d8b;">' 
                # optional center seed method, slower and less keyword placement
                if vertical:
                    col = int(round((self.cols + 1)/2, 0))
                    row = int(round((self.rows + 1)/2, 0)) - int(round((word.length + 1)/2, 0))
                else:
                    col = int(round((self.cols + 1)/2, 0)) - int(round((word.length + 1)/2, 0))
                    row = int(round((self.rows + 1)/2, 0))
                # completely random seed method
                col = random.randrange(1, self.cols + 1)
                row = random.randrange(1, self.rows + 1)
                '</span><span style="color: #483d8b;">''</span>
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">check_fit_score</span><span style="color: black;">&#40;</span>col, row, vertical, word<span style="color: black;">&#41;</span>: 
                    fit = <span style="color: #008000;">True</span>
                    <span style="color: #008000;">self</span>.<span style="color: black;">set_word</span><span style="color: black;">&#40;</span>col, row, vertical, word, force=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>: <span style="color: #808080; font-style: italic;"># a subsquent words have scores calculated</span>
                <span style="color: #ff7700;font-weight:bold;">try</span>: 
                    col, row, vertical = coordlist<span style="color: black;">&#91;</span>count<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, coordlist<span style="color: black;">&#91;</span>count<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, coordlist<span style="color: black;">&#91;</span>count<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>
                <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">IndexError</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #808080; font-style: italic;"># no more cordinates, stop trying to fit</span>
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">if</span> coordlist<span style="color: black;">&#91;</span>count<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span>: <span style="color: #808080; font-style: italic;"># already filtered these out, but double check</span>
                    fit = <span style="color: #008000;">True</span> 
                    <span style="color: #008000;">self</span>.<span style="color: black;">set_word</span><span style="color: black;">&#40;</span>col, row, vertical, word, force=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
            count += <span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> check_fit_score<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, col, row, vertical, word<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        And return score (0 signifies no fit). 1 means a fit, 2+ means a cross.
&nbsp;
        The more crosses the better.
        '</span><span style="color: #483d8b;">''</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> col <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">or</span> row <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">1</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>
&nbsp;
        count, score = <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">1</span> <span style="color: #808080; font-style: italic;"># give score a standard value of 1, will override with 0 if collisions detected</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> letter <span style="color: #ff7700;font-weight:bold;">in</span> word.<span style="color: black;">word</span>:            
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                active_cell = <span style="color: #008000;">self</span>.<span style="color: black;">get_cell</span><span style="color: black;">&#40;</span>col, row<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">IndexError</span>:
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">if</span> active_cell == <span style="color: #008000;">self</span>.<span style="color: black;">empty</span> <span style="color: #ff7700;font-weight:bold;">or</span> active_cell == letter:
                <span style="color: #ff7700;font-weight:bold;">pass</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">if</span> active_cell == letter:
                score += <span style="color: #ff4500;">1</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">if</span> vertical:
                <span style="color: #808080; font-style: italic;"># check surroundings</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> active_cell <span style="color: #66cc66;">!</span>= letter: <span style="color: #808080; font-style: italic;"># don't check surroundings if cross point</span>
                    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">check_if_cell_clear</span><span style="color: black;">&#40;</span>col+<span style="color: #ff4500;">1</span>, row<span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># check right cell</span>
                        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>
&nbsp;
                    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">check_if_cell_clear</span><span style="color: black;">&#40;</span>col-<span style="color: #ff4500;">1</span>, row<span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># check left cell</span>
                        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">if</span> count == <span style="color: #ff4500;">1</span>: <span style="color: #808080; font-style: italic;"># check top cell only on first letter</span>
                    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">check_if_cell_clear</span><span style="color: black;">&#40;</span>col, row-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
                        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">if</span> count == <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>word.<span style="color: black;">word</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># check bottom cell only on last letter</span>
                    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">check_if_cell_clear</span><span style="color: black;">&#40;</span>col, row+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>: 
                        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>: <span style="color: #808080; font-style: italic;"># else horizontal</span>
                <span style="color: #808080; font-style: italic;"># check surroundings</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> active_cell <span style="color: #66cc66;">!</span>= letter: <span style="color: #808080; font-style: italic;"># don't check surroundings if cross point</span>
                    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">check_if_cell_clear</span><span style="color: black;">&#40;</span>col, row-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># check top cell</span>
                        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>
&nbsp;
                    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">check_if_cell_clear</span><span style="color: black;">&#40;</span>col, row+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># check bottom cell</span>
                        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">if</span> count == <span style="color: #ff4500;">1</span>: <span style="color: #808080; font-style: italic;"># check left cell only on first letter</span>
                    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">check_if_cell_clear</span><span style="color: black;">&#40;</span>col-<span style="color: #ff4500;">1</span>, row<span style="color: black;">&#41;</span>:
                        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">if</span> count == <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>word.<span style="color: black;">word</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># check right cell only on last letter</span>
                    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">check_if_cell_clear</span><span style="color: black;">&#40;</span>col+<span style="color: #ff4500;">1</span>, row<span style="color: black;">&#41;</span>:
                        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>
&nbsp;
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">if</span> vertical: <span style="color: #808080; font-style: italic;"># progress to next letter and position</span>
                row += <span style="color: #ff4500;">1</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>: <span style="color: #808080; font-style: italic;"># else horizontal</span>
                col += <span style="color: #ff4500;">1</span>
&nbsp;
            count += <span style="color: #ff4500;">1</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> score
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> set_word<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, col, row, vertical, word, force=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># also adds word to word list</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> force:
            word.<span style="color: black;">col</span> = col
            word.<span style="color: black;">row</span> = row
            word.<span style="color: black;">vertical</span> = vertical
            <span style="color: #008000;">self</span>.<span style="color: black;">current_word_list</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>word<span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">for</span> letter <span style="color: #ff7700;font-weight:bold;">in</span> word.<span style="color: black;">word</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">set_cell</span><span style="color: black;">&#40;</span>col, row, letter<span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> vertical:
                    row += <span style="color: #ff4500;">1</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    col += <span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> set_cell<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, col, row, value<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">grid</span><span style="color: black;">&#91;</span>row-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>col-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> = value
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get_cell<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, col, row<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">grid</span><span style="color: black;">&#91;</span>row-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>col-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> check_if_cell_clear<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, col, row<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            cell = <span style="color: #008000;">self</span>.<span style="color: black;">get_cell</span><span style="color: black;">&#40;</span>col, row<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> cell == <span style="color: #008000;">self</span>.<span style="color: black;">empty</span>: 
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">IndexError</span>:
            <span style="color: #ff7700;font-weight:bold;">pass</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> solution<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># return solution grid</span>
        outStr = <span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> r <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">rows</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">grid</span><span style="color: black;">&#91;</span>r<span style="color: black;">&#93;</span>:
                outStr += <span style="color: #483d8b;">'%s '</span> <span style="color: #66cc66;">%</span> c
            outStr += <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> outStr
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> word_find<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># return solution grid</span>
        outStr = <span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> r <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">rows</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">grid</span><span style="color: black;">&#91;</span>r<span style="color: black;">&#93;</span>:
                <span style="color: #ff7700;font-weight:bold;">if</span> c == <span style="color: #008000;">self</span>.<span style="color: black;">empty</span>:
                    outStr += <span style="color: #483d8b;">'%s '</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">string</span>.<span style="color: black;">lowercase</span><span style="color: black;">&#91;</span><span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">string</span>.<span style="color: black;">lowercase</span><span style="color: black;">&#41;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    outStr += <span style="color: #483d8b;">'%s '</span> <span style="color: #66cc66;">%</span> c
            outStr += <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> outStr
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> order_number_words<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># orders words and applies numbering system to them</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">current_word_list</span>.<span style="color: black;">sort</span><span style="color: black;">&#40;</span>key=<span style="color: #ff7700;font-weight:bold;">lambda</span> i: <span style="color: black;">&#40;</span>i.<span style="color: black;">col</span> + i.<span style="color: black;">row</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        count, icount = <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> word <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">current_word_list</span>:
            word.<span style="color: black;">number</span> = count
            <span style="color: #ff7700;font-weight:bold;">if</span> icount <span style="color: #66cc66;">&lt;</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">current_word_list</span><span style="color: black;">&#41;</span>:
                <span style="color: #ff7700;font-weight:bold;">if</span> word.<span style="color: black;">col</span> == <span style="color: #008000;">self</span>.<span style="color: black;">current_word_list</span><span style="color: black;">&#91;</span>icount<span style="color: black;">&#93;</span>.<span style="color: black;">col</span> <span style="color: #ff7700;font-weight:bold;">and</span> word.<span style="color: black;">row</span> == <span style="color: #008000;">self</span>.<span style="color: black;">current_word_list</span><span style="color: black;">&#91;</span>icount<span style="color: black;">&#93;</span>.<span style="color: black;">row</span>:
                    <span style="color: #ff7700;font-weight:bold;">pass</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    count += <span style="color: #ff4500;">1</span>
            icount += <span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> display<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, order=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># return (and order/number wordlist) the grid minus the words adding the numbers</span>
        outStr = <span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> order:
            <span style="color: #008000;">self</span>.<span style="color: black;">order_number_words</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #dc143c;">copy</span> = <span style="color: #008000;">self</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">for</span> word <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">current_word_list</span>:
            <span style="color: #dc143c;">copy</span>.<span style="color: black;">set_cell</span><span style="color: black;">&#40;</span>word.<span style="color: black;">col</span>, word.<span style="color: black;">row</span>, word.<span style="color: black;">number</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">for</span> r <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">copy</span>.<span style="color: black;">rows</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">copy</span>.<span style="color: black;">grid</span><span style="color: black;">&#91;</span>r<span style="color: black;">&#93;</span>:
                outStr += <span style="color: #483d8b;">'%s '</span> <span style="color: #66cc66;">%</span> c
            outStr += <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span>
&nbsp;
        outStr = <span style="color: #dc143c;">re</span>.<span style="color: black;">sub</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'[a-z]'</span>, <span style="color: #483d8b;">' '</span>, outStr<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> outStr
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> word_bank<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>: 
        outStr = <span style="color: #483d8b;">''</span>
        temp_list = duplicate<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">current_word_list</span><span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">random</span>.<span style="color: black;">shuffle</span><span style="color: black;">&#40;</span>temp_list<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># randomize word list</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> word <span style="color: #ff7700;font-weight:bold;">in</span> temp_list:
            outStr += <span style="color: #483d8b;">'%s<span style="color: #000099; font-weight: bold;">\n</span>'</span> <span style="color: #66cc66;">%</span> word.<span style="color: black;">word</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> outStr
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> legend<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># must order first</span>
        outStr = <span style="color: #483d8b;">''</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> word <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">current_word_list</span>:
            outStr += <span style="color: #483d8b;">'%d. (%d,%d) %s: %s<span style="color: #000099; font-weight: bold;">\n</span>'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>word.<span style="color: black;">number</span>, word.<span style="color: black;">col</span>, word.<span style="color: black;">row</span>, word.<span style="color: black;">down_across</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, word.<span style="color: black;">clue</span> <span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> outStr
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Word<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, word=<span style="color: #008000;">None</span>, clue=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">word</span> = <span style="color: #dc143c;">re</span>.<span style="color: black;">sub</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\s</span>'</span>, <span style="color: #483d8b;">''</span>, word.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">clue</span> = clue
        <span style="color: #008000;">self</span>.<span style="color: black;">length</span> = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">word</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># the below are set when placed on board</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">row</span> = <span style="color: #008000;">None</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">col</span> = <span style="color: #008000;">None</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">vertical</span> = <span style="color: #008000;">None</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">number</span> = <span style="color: #008000;">None</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> down_across<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># return down or across</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">vertical</span>: 
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'down'</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>: 
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'across'</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__repr__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">word</span>
&nbsp;
<span style="color: #808080; font-style: italic;">### end class, start execution</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#start_full = float(time.time())</span>
&nbsp;
word_list = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'saffron'</span>, <span style="color: #483d8b;">'The dried, orange yellow plant used to as dye and as a cooking spice.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'pumpernickel'</span>, <span style="color: #483d8b;">'Dark, sour bread made from coarse ground rye.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'leaven'</span>, <span style="color: #483d8b;">'An agent, such as yeast, that cause batter or dough to rise..'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'coda'</span>, <span style="color: #483d8b;">'Musical conclusion of a movement or composition.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'paladin'</span>, <span style="color: #483d8b;">'A heroic champion or paragon of chivalry.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'syncopation'</span>, <span style="color: #483d8b;">'Shifting the emphasis of a beat to the normally weak beat.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'albatross'</span>, <span style="color: #483d8b;">'A large bird of the ocean having a hooked beek and long, narrow wings.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'harp'</span>, <span style="color: #483d8b;">'Musical instrument with 46 or more open strings played by plucking.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'piston'</span>, <span style="color: #483d8b;">'A solid cylinder or disk that fits snugly in a larger cylinder and moves under pressure as in an engine.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'caramel'</span>, <span style="color: #483d8b;">'A smooth chery candy made from suger, butter, cream or milk with flavoring.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'coral'</span>, <span style="color: #483d8b;">'A rock-like deposit of organism skeletons that make up reefs.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'dawn'</span>, <span style="color: #483d8b;">'The time of each morning at which daylight begins.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'pitch'</span>, <span style="color: #483d8b;">'A resin derived from the sap of various pine trees.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'fjord'</span>, <span style="color: #483d8b;">'A long, narrow, deep inlet of the sea between steep slopes.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'lip'</span>, <span style="color: #483d8b;">'Either of two fleshy folds surrounding the mouth.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'lime'</span>, <span style="color: #483d8b;">'The egg-shaped citrus fruit having a green coloring and acidic juice.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'mist'</span>, <span style="color: #483d8b;">'A mass of fine water droplets in the air near or in contact with the ground.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'plague'</span>, <span style="color: #483d8b;">'A widespread affliction or calamity.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'yarn'</span>, <span style="color: #483d8b;">'A strand of twisted threads or a long elaborate narrative.'</span><span style="color: black;">&#93;</span>, \
    <span style="color: black;">&#91;</span><span style="color: #483d8b;">'snicker'</span>, <span style="color: #483d8b;">'A snide, slightly stifled laugh.'</span><span style="color: black;">&#93;</span>
&nbsp;
a = Crossword<span style="color: black;">&#40;</span><span style="color: #ff4500;">13</span>, <span style="color: #ff4500;">13</span>, <span style="color: #483d8b;">'-'</span>, <span style="color: #ff4500;">5000</span>, word_list<span style="color: black;">&#41;</span>
a.<span style="color: black;">compute_crossword</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> a.<span style="color: black;">word_bank</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> a.<span style="color: black;">solution</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> a.<span style="color: black;">word_find</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> a.<span style="color: black;">display</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> a.<span style="color: black;">legend</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>a.<span style="color: black;">current_word_list</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'out of'</span>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>word_list<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> a.<span style="color: black;">debug</span>
<span style="color: #808080; font-style: italic;">#end_full = float(time.time())</span>
<span style="color: #808080; font-style: italic;">#print end_full - start_full</span></pre></div></div>

<p><strong>Sample output:</strong></p>
<p>You should be able to see the associated methods lining up with the output. A side note: you must run the display() method before the legend() method can be ran.</p>
<pre>
mist
lime
snicker
paladin
caramel
leaven
pumpernickel
coral
fjord
plague
piston
lip
dawn
saffron
coda

p u m p e r n i c k e l -
a - - - - - - - a - - e -
l - s n i c k e r - - a -
a - a - - - - - a - - v -
d - f - c - - - m - - e -
i - f j o r d - e - - n -
n - r - d - - - l i p - -
- c o r a l - - - - i - -
- - n - - i - - - - s - -
- - - - - m i s t - t - -
p l a g u e - - - - o - -
- - - - - - - d a w n - -
- - - - - - - - - - - - -

p u m p e r n i c k e l v
a w j m p c a y a w r e s
l f s n i c k e r b z a x
a f a z k e u i a b f v k
d x f v c j f d m c n e x
i d f j o r d z e j g n z
n r r x d j a o l i p d j
i c o r a l u s t o i x w
m r n u e i i h o t s y w
m w e x s m i s t r t u j
p l a g u e b n h k o m s
f m n v j f p d a w n c q
m h j a e d p p r g t p j

1               4     8 -
  - - - - - - -   - -   -
  - 2             - -   -
  -   - - - - -   - -   -
  -   - 6 - - -   - -   -
  - 3         -   - -   -
  -   -   - - - 10   12 - -
- 5       9 - - - -   - -
- -   - -   - - - -   - -
- - - - - 11       -   - -
7           - - - -   - -
- - - - - - - 13       - -
- - - - - - - - - - - - -

1. (1,1) across: Dark, sour bread made from coarse ground rye.
1. (1,1) down: A heroic champion or paragon of chivalry.
2. (3,3) across: A snide, slightly stifled laugh.
2. (3,3) down: The dried, orange yellow plant used to as dye and as a cooking spice.
3. (3,6) across: A long, narrow, deep inlet of the sea between steep slopes.
4. (9,1) down: A smooth chery candy made from suger, butter, cream or milk with flavoring.
5. (2,8) across: A rock-like deposit of organism skeletons that make up reefs.
6. (5,5) down: Musical conclusion of a movement or composition.
7. (1,11) across: A widespread affliction or calamity.
8. (12,1) down: An agent, such as yeast, that cause batter or dough to rise..
9. (6,8) down: The egg-shaped citrus fruit having a green coloring and acidic juice.
10. (9,7) across: Either of two fleshy folds surrounding the mouth.
11. (6,10) across: A mass of fine water droplets in the air near or in contact with the ground.
12. (11,7) down: A solid cylinder or disk that fits snugly in a larger cylinder and moves under pressure as in an engine.
13. (8,12) across: The time of each morning at which daylight begins.

15 out of 20
811
</pre>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://bryanhelmig.com/python-crossword-puzzle-generator/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>My Favorite Way To Sell Files Online</title>
		<link>http://bryanhelmig.com/my-favorite-way-to-sell-files-online/</link>
		<comments>http://bryanhelmig.com/my-favorite-way-to-sell-files-online/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 22:48:42 +0000</pubDate>
		<dc:creator>Bryan</dc:creator>
				<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://bryanhelmig.com/?p=254</guid>
		<description><![CDATA[There are quite a few services out there that provide a mechanism for digital downloads, most of them are cart based or even store based (their store on their site). This puts you at the mercy of their approval process and can shut down your income in a flash if they don&#8217;t like what your [...]]]></description>
			<content:encoded><![CDATA[<p>There are quite a few services out there that provide a mechanism for digital downloads, most of them are cart based or even store based (<em>their </em>store on <em>their </em>site). This puts you at the mercy of their approval process and can shut down your income in a flash if they don&#8217;t like what your are selling. Wouldn&#8217;t it be awesome if you could just <strong>sell some digital file on your site</strong>, receive the money <strong>directly in your PayPal account</strong> and <strong>automate the file delivery</strong>?</p>
<p><strong>Try BitBuffet.com if you want to <a href="http://bitbuffet.com/">sell digital goods</a>. I promise you&#8217;ll be impressed (because I built it myself to be a simple and effective solution for delivering digital files).</strong></p>
<p>I&#8217;ve been using BitBuffet.com to sell my WordPress themes at GazelleThemes.com and haven&#8217;t hit any snags at all. I just upload the zip file containing my theme and copy the button code onto my website. I even get email notifications when I receive a sale and nice flash charts showing my past sales (including a massive database for searching past sales).</p>
<p>I can set how long the download links are active and how many times they can download. After the time is up, the link no longer works. I can send freebie to friends and resend lost download links. Each download link is unique and expires according to <em>your </em>settings.</p>
<p><strong>Sell Albums or MP3&#8242;s On Your MySpace/Band Website</strong></p>
<p>Since my band <strong>Glass Cannon </strong>is getting ready to release our debut album, I can also use BitBuffet.com to host and sell an album. Again, all I&#8217;ll need to do is make a zip of the file, upload it to BitBuffet.com and copy the button to my bands site (or MySpace). All they have to do is click, pay through PayPal and check their email!</p>
<p><strong>Sell Photos From Your Online Portfolio/Flickr</strong></p>
<p>Although I&#8217;m not a photographer (I just play one on TV), I can see photographers selling their high resolution files to interested buyers. Since the files are securely hosted, you don&#8217;t have to worry about people stealing your work by sharing a download link (each is unique and expires according to your settings).</p>
<p><strong>Sell <em>Any </em>Digital File From Your Site</strong></p>
<p>It doesn&#8217;t matter what it is! Software, images, designs, HTML/CSS themes, WordPress Themes, Joomla! Themes, Woopra Themes, MP3&#8242;s, Albums, eBooks, vector images&#8230; you name it! If it is digital, BitBuffet.com offers the easiest way to deliver the file with after PayPal payments!</p>
<p><em><strong>Some of the Features:</strong></em></p>
<ul>
<li>Unlimited Bandwidth</li>
<li>Unlimited Sales</li>
<li>Keep Your Profits</li>
<li>Pay Flat-Fee Month-to-Month or Yearly</li>
<li>Payment Directly Into Seller&#8217;s Personal PayPal Account</li>
</ul>
<p>Check out <a href="http://bitbuffet.com/">BitBuffet.com</a> today and let me know in the comments what you think!</p>
]]></content:encoded>
			<wfw:commentRss>http://bryanhelmig.com/my-favorite-way-to-sell-files-online/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Notorious B.I.G&#8217;s Crack Commandments In Business</title>
		<link>http://bryanhelmig.com/notorious-b-i-g-crack-commandments-in-business/</link>
		<comments>http://bryanhelmig.com/notorious-b-i-g-crack-commandments-in-business/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 22:22:53 +0000</pubDate>
		<dc:creator>Bryan</dc:creator>
				<category><![CDATA[Interesting]]></category>

		<guid isPermaLink="false">http://bryanhelmig.com/?p=240</guid>
		<description><![CDATA[In case you need a refresher, check out the tune here. While some are a stretch, a few are really quite relevant. 1. Never let no one know how much dough you hold. Keep your finances (good or bad) to yourself. Don&#8217;t make the mistake of bragging about how well or mentioning how badly you&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<p>In case you need a refresher, <a href="http://www.youtube.com/watch?v=6ihPOTDxMfE">check out the tune here</a>. While some are a stretch, a few are really quite relevant.</p>
<p><strong>1. Never let no one know how much dough you hold.</strong></p>
<p><em>Keep your finances (good or bad) to yourself.</em></p>
<p>Don&#8217;t make the mistake of bragging about how well or mentioning how badly you&#8217;re doing unless you have a very good reason for it. What you think of as idle talk amongst friends can get around very quickly and can affect future deals or relationships. When it comes to finances, its just better to keep your mouth shut.</p>
<p><strong>2. Never let &#8216;em know your next move.</strong></p>
<p><em>Keep your core strategies/opportunities under wraps.</em></p>
<p>I know its tempting to talk about your plans or techniques, but just like <strong>#1</strong>, sometimes it&#8217;s just best to shut up. Biggie elaborates on this with <em>&#8220;don&#8217;t you know Bad Boys move in silence or violence&#8221;</em> which is just another way of letting you know that the big dogs don&#8217;t over-plan and discuss, and they <strong>act</strong>.</p>
<p><strong>3. Never trust nobody.</strong></p>
<p><em>Words are words. Get a contract.</p>
<p></em></p>
<p>Trust is a funny and terribly fragile thing. While your business partners or clients may not want to ruin you from the outset, who knows what the future will bring? You need to protect yourself. Hire a lawyer, get a contract. Live by this motto:<strong> &#8220;Everybody signs something.&#8221;</strong></p>
<p><strong>4. Never get high, on your own supply.</strong></p>
<p><em>Discover the customers&#8217; needs; don&#8217;t substitute your own.</p>
<p></em></p>
<p>While you may think you have it under control, your customer should come first. They are the ones controlling your paycheck. Don&#8217;t forget that. If you think you have all the answers, be prepared to fail. Badly.</p>
<p><strong>5. Never sell no crack where you rest at.</strong></p>
<p><em>Don&#8217;t mix business with personal life.</p>
<p></em></p>
<p>It&#8217;s easy to bring your personal life into business, and some people have no problems maintaining the difference. But when you become a friend to all, you may have trouble making necessary decisions in the face of emotion. Just know that if you do mix the two, you may need to break the connection to make the right decision.</p>
<p><strong>6. That God damn credit, dead it.</strong></p>
<p><em>Get cash upfront unless you don&#8217;t care about being paid.</p>
<p></em></p>
<p>This goes back to <strong>#3</strong>, don&#8217;t trust anyone. Get a contract and get the cash upfront. While Biggie was dealing with unreliable crackheads, you&#8217;ll still run across unreliable or dishonest businessmen. When in doubt, get it in cash.</p>
<p><strong>7. Keep your family and business completely separated.</strong></p>
<p><em>Don&#8217;t work with family for family&#8217;s sake.</em></p>
<p>This is an elaboration on <strong>#5</strong>, but don&#8217;t hire friends or family just because they are who they are. Do they have a strong skill-set? Can they contribute to your bottom line? If you can&#8217;t be honest here, you won&#8217;t make it far.</p>
<p><strong>8. Never keep no weight on you.</strong></p>
<p><em>Learn to delegate effectively.</p>
<p></em></p>
<p>Biggie clarifies this with the line: <em>&#8220;them cats that squeeze your guns can hold jumps too.&#8221;</em> In Biggie&#8217;s case, he doesn&#8217;t want to get nailed with possession. In your case, hire someone to do your dirty work for you. Learn to delegate and get on with more important things.</p>
<p><strong>9. If you ain’t gettin bags stay the fuck from police.</strong></p>
<p><em>Watch who you are perceived as working with.</p>
<p></em></p>
<p>There are probably a lot of people who in hindsight would have taken a different route when dealing with unsavory people. Biggie had the right idea, your colleagues will form their own assumption, some of them negative.</p>
<p><strong>10. A strong word called consignment; if you ain’t got the clientele say hell no.</strong></p>
<p><em>Don&#8217;t take credit without a means to repay.</p>
<p></em></p>
<p>This is the flip-side of <strong>#6</strong>, don&#8217;t take obligations you can&#8217;t repay. This is one very quick way of run yourself into the ground. If you already have revenue and need to grow, then by all means.</p>
<p>What do you think? Did I interpret one wrong?</p>
]]></content:encoded>
			<wfw:commentRss>http://bryanhelmig.com/notorious-b-i-g-crack-commandments-in-business/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

