PyOhio 2012

Last month, I had the great fortune to attend my very first conference: PyOhio. There were many great talks and opportunities to meet interesting people. What follows are some personal notes I took during the conference. (It took awhile because the way I took notes was very incongruous with WordPress.  Lots of reformatting before editing. Lesson for next time friends.)

Django bootcamp

The best thing I got out of this presentation by Raymond Chandler was setting up a basic development environment on Ubuntu and getting comfortable with git and virtualenv in a Linux environment.  Then we progressed to build a basic twitter clone called ‘chirp’.

setting up a development environment:

sudo apt-get install git
pip install django
pip install virtualenv

virtualenv

activate/deactivate
virtualenv [name]
source [name]/bin/activate
pip install django  (installs within the environment)

setting up app

As part of the process of setting up a Django App, Raymond suggested renaming the project app directory to “config” (a rails convention), which I kind of like:

django-admin.py starproject chirp
mv chirp/ config ## rails convention
  • edit manage.py to change os.environ.(…) settings
  • edit chirp/config/settings.py to change [app name] to config

GitHub

Getting a new git repository started for the demo app “chirp”…

python2 manage.py startapp chirp
git init
touch .gitignore
[edit .gitignore]
git add .gitignore
git status
git add --all
git commit -m "commit message"

git remote add origin [location]
git push -u origin master
-u: makes repo you are pushing to the athoritative copy

git checkout -b adding-chirp-model

Miscellaneous

  • django command extensions
  • tastypy :: restful web services app
  • python manage.py inspectdb :: assimilates database, creates model
  • vim plugins: nerdtree, miniBufEx
  • help([module]plorer)

Ray Chandler explaining Django’s Model-View-Controller(template) architecture.

Presentation notes can be found here: http://www.scribd.com/doc/101340711/Django-Bootcamp

Testing

http://drunkenpython.org/

Assertion

assert 1==2, “Reality Check”

python -i myModule.py #provides interactive debugger
python -o #switch to deactivate assertions
python -m unittest [module[.TestClass[.assertion]]] #switch to load module

unittest module

import unittest
dir(unittest)   # list out methods
subclass unittest.TestCase
– you add methods called test
– supply assert* methods

unittest output:

  • (period): successful
  • F: Failed
  • E: Error in assertion

outcomes accumulated in unittest.TestResult instance

can create multiple subclasses and create testsuites
str_suite = unittest.TestSuite()
str_suite.addTest(class(“Descriptor”))

Test fixture: tests + suites + Setup + teardown

pip install distribute

doctest

python -m doctest pycampNG/fibonacci.py

– inspects file for doc strings and constructs and attempts to execute code

doctest.testmod() # runs all test in specified module
doctest.DocTestSuite() converts doctests to a unittest.TestSuite instance.
– reference in setup.py

Lightning talks 1

Brandon Rhodes: Python Patterns 1

1977 Architect Christopher Alexander
– Pattern at one level of detail, apply it to any scale.
“Gang of Four”: Gamma, Helm, Johnson, Vlissides :: Elements of reusable Object design

Not all patterns are OO patterns:

  •  dictionary has a get(key, default) method.
  •  continue: break out an iteration of a for loop, continue on to the next iteration

– Python’s dynamic features makes OO patterns disappear

builder design patterns

  • python doesn’t have separate syntax for instantiating objects
  • most “creational oo patterns” are contortions to avoid making brittle ‘new’ calls in c++ and java
  • python singleton pattern: wrap in method generator.
  • ‘copy’ module can introspect and make a copy of an object
  •  lxml.builder import E :: ‘framework-y’

structural patterns

Adapter: wrapper to add a method.  Examples:

  • sockets send/receive rather than read/write.
  • socket._fileobject adopter
  • crucial pattern for letting us re-use code

Bridge

  • Gary Bernhart: “have modules that do nothing but persist information. Separate your business logic into functions that take data objects”

Composite: “if a module imports 3 major libs or frameworks, refactor into 2 modules each of which only imports 2 libraries”

Facade: an object that hides a structure.

Flyweight pattern: only create single instance of static data

Proxy

  • An obj that wraps another obj, like adapter, but accepts the same methods as the wrapped class.
  • ex: remote procedure calls

Decorator (not like the python decorators)

  • tend to appear in ‘glue’ code

behavioral patterns

  • Chain of responsibility
  • Command: wrap, track history, enable undo
  • Interpreter: domain specific languages
  • Iterator/generator:  “most important addition of the 2000’s”
  • Mediator
  • Memento
  • Observer
  • State, template, visitor
  • most of these turn up in bigger projects

Conclusion

Python programmers rarely meet the bigger patterns because python programmers tend to write smaller programs and scripts.  Frameworks have already implemented them.

Python for humans

presenter: @kennethreitz
works for Heroku

“open source encourages best practices”

zen of python:

  • Explicit is better than implicit
  • Simple is better than complex
  • Complex is better than complicated
  • If the implementation is hard to explain, it’s a bad idea

Barriers to entry

Other python examples:

  • Packaging & Dependencies
  • Date/Times
  • Unicode
  • Testing
  • Dependencies

Hitchhikers guide to python – an opinionated best practices guide to installing and using python.

Slides: https://speakerdeck.com/u/kennethreitz/p/python-for-humans

“There should be one – and preferably only one – obvious way to do something” – more python zen

Django-CMS

Presenter: Andrew Schowen – @andrewschoen

@django_CMS: https://www.django-cms.org/

Andrew’s demo: https://github.com/andrewschoen/django-cms-presentation

You can edit content (templates/plugins) within the context of your page

  • create the pages in the menus at the same time.
  • internationalized

Structure: templates, placeholders, menus

Plugins: Yes! Plugins are available.  You can also easily write custom plugins using other Django app/model

apphooks: direct through djangocms to the app. templates make for abstract skinning.  CMS app puts ‘polls’ in the menu.

Extensions and 3rd party apps

  • django-filer: image manager
  • cmsplugin-filer
  • django-reversion: lets you version control page data
  • cmsplugin-blog
  • django-cms-redirects: lets you manage (303, 410) errors

Lightning Talks 2

  • Bookie
  • Data Science with Python
  • MaximinProject
    • people know poverty exists, but don’t know much about it or where it is.
    • human wealth index
    • non profit to build website to connect funders with groups on the ground
  • Dependency Injection – A Python Patterns Encore
    • Brandon Rhodes
    • @brandon_rhodes
    • “if i define it, i can’t be wrong”
    • ex: urlopen – add a parameter to pass real object or some simple test function
    • pass in the callable function rather than chaining function calls.
  • From Python Import Podcast
  • Dynamic DNS
  • HTSQL
    • @catherinedevlin
    • htsql.org
    • URL:- SQL access to a healthcare database
    • API for python

I’m looking forward to catching the other amazing presentations I missed on pyvideo.org

Update: The videos have been posted to pyvideo.org

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s