Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PyCon India: Snakes on the Web (2012)

PyCon India: Snakes on the Web (2012)

My keynote at PyCon India 2012.

Jacob Kaplan-Moss

September 30, 2012
Tweet

More Decks by Jacob Kaplan-Moss

Other Decks in Technology

Transcript

  1. SNAKES ON THE WEB Jacob Kaplan-Moss [email protected] The history and

    future of Python on the web http://www.flickr.com/photos/kejhu/3751877257 2012
  2. 1. What sucks now? 2. How will we fix it?

    3. Can we fix it with Python?
  3. Hand-rolled HTML “The Stone Age” <h1>Appendix A: Model Definition Reference</h1>

    <p>Chapter 5 explains the basics of defining models, and we use them throughout the rest of the book. There is, however, a <em>huge</em> range of model options available not covered elsewhere. This appendix explains each possible model definition option.</p> <p>Note that although these APIs are considered stable, the Django developers consistently add new shortcuts and conveniences to the model definition. It&#8217;s a good idea to always check the latest documentation online at <a class="reference external" href="http://docs.djangoproject.com/">http://docs.djangoproject.com/</a>.</p> <div class="section" id="fields"> <h2>Fields</h2> <p>The most important part of a model &#8211; and the only required part of a model &#8211; is the list of database fields it defines.</p> <div class="admonition-field-name-restrictions admonition"> <p class="first admonition-title">Field Name Restrictions</p> <p>Django places only two restrictions on model field names:</p> <ol class="arabic"> <li><p class="first">A field name cannot be a Python reserved word, because that would result in a Python syntax error. For example:</p> <div class="highlight-python"><pre>class Example(models.Model): pass = models.IntegerField() # 'pass' is a reserved word!</pre> </div> </li> <li><p class="first">A field name cannot contain more than one underscore in a row, due to the way Django&#8217;s query lookup syntax works. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Example</span><span class="p">(</ span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span> <span class="n">foo__bar</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">IntegerField</span><span class="p">()</span> <span class="c"># &#39;foo__bar&#39; has two underscores!</span> </pre></div> </div> </li> </ol> <p>These limitations can be worked around, though, because your field name doesn&#8217;t necessarily have to match your database column name. See &#8220;db_column&#8221;, below.</p> <p class="last">SQL reserved words, such as <tt class="docutils literal"><span class="pre">join</span></tt>, <tt class="docutils literal"><span class="pre">where</span></tt>, or <tt class="docutils literal"><span class="pre">select</span></tt>, <em>are</em> allowed as model field names, because Django escapes all database table names and column names in every underlying SQL query. It uses the quoting syntax of your particular database engine.</p> </div> <p>Each field in your model should be an instance of the appropriate <tt class="docutils literal"><span class="pre">Field</span></tt> class. Django uses the field class types to determine a few things:</p> <ul class="simple"> <li>The database column type (e.g., <tt class="docutils literal"><span class="pre">INTEGER</span></tt>, <tt class="docutils literal"><span class="pre">VARCHAR</span></tt>).</li> <li>The widget to use in Django&#8217;s forms and admin site, if you care to use it (e.g., <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type=&quot;text&quot;&gt;</span></tt>, <tt class="docutils literal"><span class="pre">&lt;select&gt;</span></tt>).</li> <li>The minimal validation requirements, which are used in Django&#8217;s admin interface and by forms.</li> </ul> <p>A complete list of field classes follows, sorted alphabetically. Note that relationship fields (<tt class="docutils literal"><span class="pre">ForeignKey</span></tt>, etc.) are handled in the next section.</p>
  4. SUCK. <h1>Appendix A: Model Definition Reference</h1> <p>Chapter 5 explains the

    basics of defining models, and we use them throughout the rest of the book. There is, however, a <em>huge</em> range of model options available not covered elsewhere. This appendix explains each possible model definition option.</p> <p>Note that although these APIs are considered stable, the Django developers consistently add new shortcuts and conveniences to the model definition. It&#8217;s a good idea to always check the latest documentation online at <a class="reference external" href="http://docs.djangoproject.com/">http://docs.djangoproject.com/</a>.</p> <div class="section" id="fields"> <h2>Fields</h2> <p>The most important part of a model &#8211; and the only required part of a model &#8211; is the list of database fields it defines.</p> <div class="admonition-field-name-restrictions admonition"> <p class="first admonition-title">Field Name Restrictions</p> <p>Django places only two restrictions on model field names:</p> <ol class="arabic"> <li><p class="first">A field name cannot be a Python reserved word, because that would result in a Python syntax error. For example:</p> <div class="highlight-python"><pre>class Example(models.Model): pass = models.IntegerField() # 'pass' is a reserved word!</pre> </div> </li> <li><p class="first">A field name cannot contain more than one underscore in a row, due to the way Django&#8217;s query lookup syntax works. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Example</span><span class="p">(</ span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span> <span class="n">foo__bar</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">IntegerField</span><span class="p">()</span> <span class="c"># &#39;foo__bar&#39; has two underscores!</span> </pre></div> </div> </li> </ol> <p>These limitations can be worked around, though, because your field name doesn&#8217;t necessarily have to match your database column name. See &#8220;db_column&#8221;, below.</p> <p class="last">SQL reserved words, such as <tt class="docutils literal"><span class="pre">join</span></tt>, <tt class="docutils literal"><span class="pre">where</span></tt>, or <tt class="docutils literal"><span class="pre">select</span></tt>, <em>are</em> allowed as model field names, because Django escapes all database table names and column names in every underlying SQL query. It uses the quoting syntax of your particular database engine.</p> </div> <p>Each field in your model should be an instance of the appropriate <tt class="docutils literal"><span class="pre">Field</span></tt> class. Django uses the field class types to determine a few things:</p> <ul class="simple"> <li>The database column type (e.g., <tt class="docutils literal"><span class="pre">INTEGER</span></tt>, <tt class="docutils literal"><span class="pre">VARCHAR</span></tt>).</li> <li>The widget to use in Django&#8217;s forms and admin site, if you care to use it (e.g., <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type=&quot;text&quot;&gt;</span></tt>, <tt class="docutils literal"><span class="pre">&lt;select&gt;</span></tt>).</li> <li>The minimal validation requirements, which are used in Django&#8217;s admin interface and by forms.</li> </ul> <p>A complete list of field classes follows, sorted alphabetically. Note that relationship fields (<tt class="docutils literal"><span class="pre">ForeignKey</span></tt>, etc.) are handled in the next section.</p>
  5. CGI “The Bronze Age” use CGI; my $q = CGI->new;

    # Process an HTTP request @values = $q->param('form_field'); $fh = $q->upload('file_field'); $riddle = $query->cookie('riddle_name'); %answers = $query->cookie('answers'); # Prepare various HTTP responses print $q->header(); print $q->header('application/json'); $cookie1 = $q->cookie(-name=>'riddle_name', -value=>"The Sphynx's Question"); $cookie2 = $q->cookie(-name=>'answers', -value=>\%answers); print $q->header( -type => 'image/gif', -expires => '+3d', -cookie => [$cookie1,$cookie2] ); print $q->redirect('http://somewhere.else/in/movie/land');
  6. SUCK. use CGI; my $q = CGI->new; # Process an

    HTTP request @values = $q->param('form_field'); $fh = $q->upload('file_field'); $riddle = $query->cookie('riddle_name'); %answers = $query->cookie('answers'); # Prepare various HTTP responses print $q->header(); print $q->header('application/json'); $cookie1 = $q->cookie(-name=>'riddle_name', -value=>"The Sphynx's Question"); $cookie2 = $q->cookie(-name=>'answers', -value=>\%answers); print $q->header( -type => 'image/gif', -expires => '+3d', -cookie => [$cookie1,$cookie2] ); print $q->redirect('http://somewhere.else/in/movie/land');
  7. Django 750 lines of code 18 external libraries 4 running

    services Meteor 12 lines of code 0 external libraries
  8. Django 750 lines of code 18 external libraries 4 running

    services Meteor 12 lines of code 0 external libraries 1 running service
  9. Actors STM PDS Dataflow Threads Events Tuple spaces Ted Leung,

    A survey of concurrency constructs: http://tinyurl.com/mmbqe6
  10. “ ” Good software takes ten years. Get used to

    it. — Joel Spolsky http://tinyurl.com/ca4pr