+ All Categories
Home > Documents > Developing reusable apps - James...

Developing reusable apps - James...

Date post: 26-May-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
60
Developing reusable apps James Bennett PyCon Chicago, March 15, 2008
Transcript
Page 1: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Developing reusable apps

James Bennett

PyCon Chicago, March 15, 2008

Page 2: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

The fourfold path

Do one thing, and do it well.

Don’t be afraid of multiple apps.

Write for flexibility.

Build to distribute.

Page 3: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

1

Page 4: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

“”

Do one thing, and do it well.

-- The UNIX philosophy

Page 5: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Application == encapsulation

Page 6: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Case study: user registration

Page 7: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,
Page 8: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Features

User fills out form, inactive account created.

User gets email with link, clicks to activate.

And that’s it.

Page 9: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Some “simple” things aren’t so simple.

Page 10: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Approach features skeptically

Page 11: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Should I add this feature?

What does the application do?

Does this feature have anything to do with that?

No? Guess I shouldn’t add it, then.

Page 12: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Top feature request in django-registration:

User profiles

Page 13: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

“”

What does that have to do with user registration?

-- Me

Page 14: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,
Page 15: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

The solution?

Page 16: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

django-profiles

Add profile

Edit profile

View profile

And that’s it.

Page 17: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,
Page 18: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

2

Page 19: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Don’t be afraid of multiple apps

Page 20: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,
Page 21: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

The monolith mindset

The “application” is the whole site

Re-use is often an afterthought

Tend to develop plugins that hook into the “main” application

Page 22: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

The Django mindset

Application == some bit of functionality

Site == several applications

Tend to spin off new applications liberally

Page 23: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Should this be its own application?

Is it orthogonal to whatever else I’m doing?

Will I need similar functionality on other sites?

Yes? Then I should break it out into a separate application.

Page 24: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Case study: blogging

Page 25: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

I wanted a blog

Entries and links

Tagging

Comments with moderation

Contact form

“About” page

Etc., etc.

Page 26: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

I ended up with

A blog app (entries and links)

A third-party tagging app

contrib.comments + moderation app

A contact-form app

contrib.flatpages

Etc., etc.

Page 27: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Advantages

Don’t keep rewriting features

Drop things into other sites easily

Page 28: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

urlpatterns += (‘’, (r’^contact/’, include(‘contact_form.urls’)),)

Page 29: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

But what about...

Page 30: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Site-specific needs

Site A wants a contact form that just collects a message.

Site B’s marketing department wants a bunch of info.

Site C wants to use Akismet to filter automated spam.

Page 31: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

3

Page 32: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Write for flexibility

Page 33: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,
Page 34: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Common sense

Sane defaults

Easy overrides

Don’t set anything in stone

Page 35: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Form processing

Supply a form class

But let people specify their own if they want

Page 36: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

class SomeForm(forms.Form): ...

def process_form(request, form_class=SomeForm): if request.method == ‘POST’: form = form_class(request.POST) ... else: form = form_class() ...

Page 37: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Specify a default template

But let people specify their own if they want

Templates

Page 38: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

def process_form(request, form_class=SomeForm, template_name=’do_form.html’): ... return render_to_response(template_name, ...

Page 39: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

You want to redirect after successful submission

Supply a default URL

But let people specify their own if they want

Form processing

Page 40: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

def process_form(request, form_class=SomeForm, template_name=’do_form.html’, success_url=’/foo/’): ... return HttpResponseRedirect(success_url)

Page 41: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Provide a URLConf in the application

Use named URL patterns

Use reverse lookups: reverse(), permalink, {% url %}

URL best practices

Page 42: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

4

Page 43: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Build to distribute

Page 44: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

So you did the tutorial

from mysite.polls.models import Poll

mysite.polls.views.vote

include(‘mysite.polls.urls’)

mysite.mysite.bork.bork.bork

Page 45: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Project coupling kills re-use

Page 46: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,
Page 47: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

What is a “project”?

A settings module

A root URLConf module

And that’s it.

Page 48: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

ljworld.com

worldonline.settings.ljworld

worldonline.urls.ljworld

And a whole bunch of reused apps

Page 49: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

What reusable apps look like

Single module directly on Python path (registration, tagging, etc.)

Related modules under a package (ellington.events, ellington.podcasts, etc.)

No project cruft whatsoever

Page 50: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

And now it’s easy

You can build a package with distutils or setuptools

Put it on the Cheese Shop

People can download and install

Page 51: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

General best practices

Be up-front about dependencies

Write for Python 2.3 when possible

Pick a release or pick trunk, and document that

But if you pick trunk, update frequently

Page 52: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Templates are hard

Providing templates is a big “out of the box” win

But templates are hard to make portable (block structure/inheritance, tag libraries, etc.)

Page 53: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

I usually don’t do default templates

Page 54: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Either way

Document template names

Document template contexts

Page 55: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Be obsessive about documentation

It’s Python: give stuff docstrings

If you do, Django will generate documentation for you

And users will love you forever

Page 56: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Recap:

Do one thing, and do it well.

Don’t be afraid of multiple apps.

Write for flexibility.

Build to distribute.

Page 57: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Good examples

django-tagging (Jonathan Buchanan, http://code.google.com/p/django-tagging/)

django-atompub (James Tauber, http://code.google.com/p/django-atompub/)

Search for “django” on code hosting sites

Page 59: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Questions?

Page 60: Developing reusable apps - James Bennettmedia.b-list.org/presentations/2008/pycon/reusable_apps.pdf · What reusable apps look like Single module directly on Python path (registration,

Photo credits

"Purple Sparkly Pony Ride" by ninjapoodles, http://www.flickr.com/photos/ninjapoodles/285048576/

“Stonehenge #2” by severecci, http://www.flickr.com/photos/severecci/129553243/

“sookiepose” by 416style, http://www.flickr.com/photos/sookie/41561946/

"The Happy Couple" by galapogos, http://www.flickr.com/photos/galapogos/343592116/


Recommended