You might get this message in Google AppEngine when sending an email message and specifying a custom from address. To get it to work you must add the address as a developer for your application (or use the email address you signed up with). Steps to Add a Developer Email Address: Log in to your Google [...]
Continue reading...Tuesday, November 3, 2009
To urlencode a querystring or form data in python you can use the urllib module: use urllib # you have to pass in a dictionary print urllib.urlencode({'id':'100', 'name':'john smith'}) # then you get 'id=100&name=john+smith' Urlencoding Unicode and UTF8 Values #this works fine print urllib.urlencode({name:'José'}) #you have to be careful with unicode strings # this will throw an error: print urllib.urlencode({name:u'José'}) # UnicodeDecodeError: 'ascii' codec can't decode [...]
Continue reading...Monday, October 19, 2009
Using the built-in modules datetime and timedelta, you can perform date and time addition/subtraction in python: from datetime import datetime from datetime import timedelta #Add 1 day print datetime.now() + timedelta(days=1) #Subtract 60 seconds print datetime.now() - timedelta(seconds=60) #Add 2 years print datetime.now() + timedelta(days=730) #Other Parameters you can pass in to timedelta: # days, seconds, microseconds, # milliseconds, minutes, hours, weeks #Pass multiple parameters (1 day [...]
Continue reading...Tuesday, October 13, 2009
Also called reflection, here is one method to import modules and instantiate objects using strings: moduleName = 'myApp.models' className = 'Blog' #import the module by saying 'from myApp.models import Blog' module = __import__(moduleName, {}, {}, className) #now you can instantiate the class obj = getattr(module, className )() #set a property of the object using a string setattr('Title','my first entry') #call an existing method you [...]
Continue reading...Monday, October 12, 2009
If you need to check for and convert the “None” string when returning the value of an object, here is a quick and readable method in python: myObj = None print myObj or '' This brought back a reference to a blurb in Dive Into Python regarding ternary operators, although at the time I didn’t think of using [...]
Continue reading...
Monday, March 8, 2010
0 Comments