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.)
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
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
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
- django command extensions
- tastypy :: restful web services app
python manage.py inspectdb:: assimilates database, creates model
- vim plugins: nerdtree, miniBufEx
Presentation notes can be found here: http://www.scribd.com/doc/101340711/Django-Bootcamp
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
dir(unittest) # list out methods
– you add methods called test
– supply assert* methods
- (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()
Test fixture: tests + suites + Setup + teardown
pip install distribute
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
- Midwest python workshop for women
- Hack day
- gittip: https://www.gittip.com/
- Built with responsive HTML5
- Only about 1/300 ppl click through Facebook store.
- Idea: by items directly from within FB news feed.
- take Sphinx/restructured docs and turn them into presentation
- python + weather
- weather IRC channel
- “faster than NOAA, weather.com, nbc”
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’
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
- 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
- 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
- Chain of responsibility
- Command: wrap, track history, enable undo
- Interpreter: domain specific languages
- Iterator/generator: “most important addition of the 2000’s”
- State, template, visitor
- most of these turn up in bigger projects
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
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
- HTTP request example. Problem in python: unclear which module to use.
- “HTTP should be as simple as a print statement
- “python needs more pragmatic packages”
- Requests library (http://docs.python-requests.org/en/latest/index.html)
Other python examples:
- Packaging & Dependencies
Hitchhikers guide to python – an opinionated best practices guide to installing and using python.
“There should be one – and preferably only one – obvious way to do something” – more python zen
Presenter: Andrew Schowen – @andrewschoen
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.
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
- django-reversion: lets you version control page data
- django-cms-redirects: lets you manage (303, 410) errors
Lightning Talks 2
- Data Science with Python
- 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
- “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
- 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