In a recent Django class one of my students posed the problem she was learning Django to tackle - she would be responsible for writing a web-based administrative interface to a database whose structure she wasn't allowed to modify. Can Django do that?
Absolutely - and Django even comes with a management command to bootstrap working with a legacy database. Let's create a brand new project, use a popular sample database as our target, and using Django's multi-db support to store Django's built-in model data in a separate database.
Posted August 13th, 2012 in Django Python (Comment)
Ok, perhaps I jest. As a Python instructor decorators are a topic I find students consistently struggle with upon first exposure. That’s because decorators are hard to understand! Getting decorators requires understanding several functional programming concepts as well as feeling comfortable with some unique features of Python’s function definition and function calling syntax.
I can’t make decorators easy - but maybe by walking through each piece of the puzzle one step at a time I can help you feel more confident in understanding decorators. Because decorators are complex this is going to be a long article - but stick with it! I promise to make each piece as simple as possible - and if you understand each piece, you’ll understand how decorators work!
Posted July 1st, 2012 in Python (Comment)
Modesto Scripting Languages Meetup is up to 22 members and 4 consecutive meetups!
Posted June 4th, 2012 in Programming (Comment)
I teach Python classes and enjoy exploring language features from the perspective of newbie's to the language. Usually I can explain the rationale for Python language features by showing a compelling use case. But what about generator functions?
Posted May 22nd, 2012 in Programming Python (Comment)
I filmed a short tutorial on setting up a developer config of Celery with Django. Celery lets you call Python functions as asynchronous tasks, to be run by a separate process. Production usage of Celery involves installing a queue service of some kind (RabbitMQ, etc) but for development use it is possible to just use the Django ORM to store tasks in the database and run the celery daemon as a program in the foreground ala Django's runserver command. Video after the jump...
Posted April 26th, 2012 in Django Python (Comment)
Posted April 26th, 2012 in Programming (Comment)
Fernando Perez will be presenting at Baypiggies this coming Thursday (7:30 at Symantec Mountain View on Apr 19th, 2012). I'm a big fan of IPython and really enjoyed the IPython sessions I sat in @ PyCon2012 - come by and pick up IPython tips that will make you a more productive programmer daily!
Posted April 12th, 2012 in Python (Comment)
A student in my Python Fundamentals class asked me to demonstrate how to get started with Jython - this is what I could figure out in 30 minutes...
Posted April 4th, 2012 in Python (Comment)
PyCon 2012 rocks. Dancing robots! Great tutorials, great talks, and 2,200+ Pythonistas!
Posted March 9th, 2012 in Python (Comment)
Monday night we had our second Modesto Scripting Languages Meetup. This was our first presentations-style meeting and all the presenters did a great job - James did a live demo of building a simple app in web.py, Nathan covered the MVC pattern in web applications and I talked about list processing generally and list comprehensions in Python specifically.
Posted March 2nd, 2012 in Python (Comment)
Last Thursday I presented at Baypiggies. I have to say this was not one of my most polished presentations ever - I volunteered at the last minute when we didn't have a speaker. I had started a talk on the port of Processing to Python for PyCon and hadn't finished my sample programs or the talk structure when I didn't make the cut for PyCon. But taking my own advice not to waste a rejection I (sorta) finished up my talk in the couple of days before Baypiggies and filled in. Read More to see the slides and source code for the talk.
Posted February 28th, 2012 in Programming Python (Comment)
This is actually an announcement after the fact, but I am now a full time Python instructor for Marakana. Yes, my official job title is now Expert and instead of working as a freelance programmer I teach other programmers how to transition to Python. This has been a huge but welcome shift for me! Let me explain how I got here.
Posted February 14th, 2012 in Programming Python Technology (Comment)
Announcing the Modesto Scripting Languages Meetup. Central Valley Pythonistas, Rubyists, PHPers, etc all welcome.
Posted January 25th, 2012 in Programming Python (Comment)
I recently received a very polite email from the PyCon program chair telling me what I already knew: my talk proposal didn't get accepted. My talk was rejected.
Let me explain how I already knew, why being rejected is not a bad thing, and why I'm challenging you to put your rejection to good use both personally and for the Python community.
Posted December 23rd, 2011 in Programming Python (Comment)
Tonight's Baypiggies topic is going to be What is Pythonic - Marilyn Davis is doing the presentation. I had a few thoughts but they're all in the opposite direction. I don't feel authoritative enough to define Pythonicness but as an instructor I frequently get to see code written by students new to Python that is obviously un-pythonic, for lack of a better term.
Posted December 15th, 2011 in Programming Python (Comment)
I was recently asked about using AJAX via JQuery with Django and mentioned that I frequently use html fragments and a decorator to add Ajax functionality to existing views. Let's see how that works. (more after the jump).
Posted August 22nd, 2011 in Django Programming Python (Comment)
Cool! See Max's awesome video editing skills render my pdb howto relatively stumble free! If you're curious about how to use the built in Python debugger this brief video tutorial should get you going.
Posted August 11th, 2011 in Programming Python (Comment)
A co-worker asked me today about the best way to OR together a list of Q objects in the Django ORM. Usually you have a specific number of conditions and you can use Q objects and the bitwise OR operator to logical OR the query conditions together. The Q object is necessary because multiple arguments or successive calls to .filter are ANDed. But what if you have an arbitrary number of Q conditions?
One suggestion is to use the undocumented .add method of the Q object in a loop to add multiple query conditions together. I thought this might be a good use case for reduce and the operator module:
# Normal Usage with the | operator from django.db.models import Q qs = MyModel.objects.filter(Q(cond1=1) | Q(cond2="Y")) #but given a list of Q conditions from operator import __or__ as OR lst = [Q(...), Q(...), Q(...)] qs = MyModel.objects.filter(reduce(OR, lst))
Is this the most Pythonic approach?
Posted June 14th, 2011 in Programming (Comment)
As you can probably figure out from the last post, I have a new gig.
This was my first time teaching for Marakana and I enjoyed the experience immensely - as did my students judging by their class reviews at the end! I had a blast and am looking forwards to more python classes, including an under-development Pro Django course. More details to come in this space - and thanks to Jas, Brenda, Mike, Chris, and Robert for being great first-time students. I hope you all go on to Pythonic success.
Posted June 14th, 2011 in Programming (Comment)
Deprecated: see the Python Fundamentals Page instead of this post
This will just be a grab bag of extra resources and notes for my students taking the Python Fundamentals Course at Marakana. Don't forget to grab the labs.
- Python in a Nutshell
- Dive into Python Online Book - somewhat dated but still good.
- The Official Python Tutorial
- Code like a Pythonista: read this and your code will be pythonic.
- Doug Hellman's Python Module of the Week - a great way to get to know Python's vast stdlib.
Important Basics You Should Know
Python builtin functions: see the http://docs.python.org/library/functions.html. Or
import __builtin__ dir(__builtin__)
Keywords: see the docs http://docs.python.org/reference/lexical_analysis.html#keywords. Or
import keyword print(keyword.kwlist)
- IPython - a better python shell
- Python Regex Testing Tool
- virtualenv and virtualenvwrapper - tools for managing python environments. If you find yourself installing 3rd party python code you should be using these tools.
- Stuck on windows? Use a replacement console
Additional Documentation Referred to in Class
- Simple String Formatting
- Built In Exception Types
- Quick Doctest Overview - but I like to use
$ python -m doctest -v myfile.py
- Don't forget the first dunderscore hack you'll need:
if __name__ == '__main__': main()
- A great walkthrough of the stdlib logging module
- JSON in stdlib
- List of dunderscore methods
Don't forget the old labs - new labs and samples coming soon!
Posted May 30th, 2011 in Programming (Comment)