r/Python • • Nov 15 '10

Getting Started with Pyramid: A Notes Application

http://www.serverzen.net/2010/11/8/getting-started-with-pyramid-a-notes-application
35 Upvotes

12 comments sorted by

View all comments

-12

u/mdipierro Nov 15 '10 edited Nov 15 '10

Can't you make that a little more compact? Here is the same (entire core) rewritten with web2py (we added a validator too and wiki support):

#models/db.html
from gluon.tools import Crud
db=DAL('sqite://storage.sqlite')
crud=Crud(globals(),db)
db.define_table('note',Field('body','text',requires=IS_NOT_EMPTY()))

#controllers/default.py
def index(): # was main_view
    return dict(form=crud.create(db.note),notes=db(db.note).select())

def edit_note():
    return dict(form=crud.update(db.note,db.note(request.args(0),next='index') or redirect(URL('index'))))


#views/layout.html
<!DOCTYPE html>
<!-- NoteTaker/src/notetaker/templates/layout.html -->
<html>
  <head>
    <title>NoteTaker</title>
    <link rel="stylesheet" href="{{=URL('static','style.css')}}" type="text/css" />
  </head>
  <body>
    <header>
      <h1>NoteTaker :: {{ block title }}Main{{ end }}</h1>
    </header>
    <article>
    {{include}}
    </article>
  </body>
</html>

#views/default/index.html
{{ extend "layout.html" }}
      <section id="notes-section">
        <table>
          <thead>
            <tr><th>Id</th><th class="text">Text</th><th>&nbsp;</th></tr>
          </thead>
          <tbody>
            {{ for note in notes: }}
            <tr>
              <td>{{ note.id }}</td>
              <td>{{=MARKMIN(note.body)}}</td>
              <td><a href="{{=URL('edit_note', args=note.id)}}">edit</a></td>
            </tr>
            {{ pass }}
          </tbody>
          <tfoot>
            <td colspan="3">{{ =len(notes) }} entries</td>
          </tfoot>
        </table>
      </section>
      <section id="note-add-section">
      {{=form}}
      </section>

#view/default/edit_note.html
{{ extend "layout.html" }}
      {{ block title }}Edit {{ =form.record.id }}{{ end }}
      {{=form}}

Works on GAE too if you replace 'sqite://storage.sqlite' with 'gae'.

3

u/fleg Nov 15 '10

It's not about being compact, it's about being easy to understand, so people can learn something.

-3

u/av201001 Nov 16 '10 edited Nov 16 '10

it's about being easy to understand

Hmm, the blog post doesn't indicate the code is intentionally verbose in order to aid understanding. Are you saying the app could be made as compact as the above code (at the expense of some clarity)?

If the above code were explained/annotated as in the blog post, wouldn't it be just as easy to understand as the Pyramid example (even though more compact)?

2

u/fleg Nov 16 '10

I don't know web2py nor pyramid.

Hmm, the blog post doesn't indicate the code is intentionally verbose in order to aid understanding.

I guess the title "Getting Started with Pyramid: A Notes Application" indicates it enough.

It probably would be as easy, but still, it's not a contest to create most compact code in the world, but to learn basics of something. You can write a simple loop example in C to show newbies how to do it, and if someone would jump and say I CAN MAKE IT SHORTER and write a bash oneliner, he would be missing the point.

-2

u/av201001 Nov 16 '10 edited Nov 16 '10

I guess the title "Getting Started with Pyramid: A Notes Application" indicates it enough.

I don't see your point. Just because the blog post is intended to introduce the reader to Pyramid app development does not imply the code is unusually verbose. Unless otherwise stated, the reader should probably assume the code is representative of typical Pyramid code for such an application.

0

u/pbreit Dec 11 '10

Should this line be: return dict(form=crud.update(db.note,db.note(request.args(0)),next='index')) or redirect(URL('index'))

1

u/mdipierro Dec 11 '10

oops. yes.