Skip navigation

Tag Archives: python

Related objects

When you define a relationship in a model (i.e., a ForeignKey, OneToOneField, or ManyToManyField), instances of that model will have a convenient API to access the related object(s).

Using the models at the top of this page, for example, an Entry object e can get its associated Blog object by accessing the blog attribute: e.blog.

(Behind the scenes, this functionality is implemented by Python descriptors. This shouldn’t really matter to you, but we point it out here for the curious.)

Django also creates API accessors for the “other” side of the relationship — the link from the related model to the model that defines the relationship. For example, a Blog object b has access to a list of all related Entry objects via the entry_set attribute: b.entry_set.all().

All this underscore business seems like identifier abuse to me, but who am I to argue?

Posted via web from Apphacker’s learning curve

I have been using bash for many years but I haven’t really done too much with it except some simple setup scripts. I learned some things today and I just wanted to write this down to help me remember:

Separate multiple paths in a bash environment variable using a colon, not a semicolon:

PYTHONPATH='/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/:/Users/bjorn/projects/python/orsaur.us/trunk'
export PYTHONPATH

PYTHONPATH can be viewed in os.environ[ “PYTHONPATH”] ( not os.environ[ “PATH” ] ) and any of those paths should be visible in sys.path. If the paths added to PYTHONPATH in your .bash_profile on Mac OS X do not show up in sys.path then something is wrong.

Use the “source” command to reload your .bash_profile settings in your existing terminal:

source ~/.bash_profile

I also found this “Bash by Example” article helpful.

I updated my Python Oauth Client Library so that it actually works now!

Here’s an example of how to use it:

EXAMPLE

(using Twitter)

def myCallback ( data ):
  print "data: %s" % data

c = oauthConsumer.Client( consumerKey, consumerSecret, requestTokenURL, accessTokenURL, authorizeURL, "http://example.com/check" )
c.requestAuth( myCallback )
#data: https://twitter.com/oauth/authorize?oaut..
c.requestSession( oauth_token, oauth_verifier, myCallback )
#data: oauth_token=20687908-snoIXOqT7StBdYtyeatV0fAzxLTB0DYklIgBh8klx&o...
print c._sessionSecret
#'hPxakZ3tztpiFaxulcHK9VfkCiJ4PIsL40mc9tfw'
print c._sessionToken
#'20687908-snoIXOqT7StBdYtyeatV0fAzxLTB0DYklIgBh8klx'

Now make requests via createRequest

update = c.createRequest( path="/statuses/update.json", callback=myCallback)
update.post(params = { "status":"testing pyoauthconsumer oauth client python library I'm working on" } )
#data: {"truncated":false, ... }

You can see the post made with this example here.