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, […]
Continue reading...13. October 2009
Comments Off on Dynamically Importing and Instantiating Modules in Python
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 […]
Continue reading...12. October 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 […]
Continue reading...11. October 2009
Comments Off on Resolving DJANGO Error: “ValueError: invalid literal for int() with base 10: ‘None'”
If you receive this error via email while viewing one of your admin pages, here are some steps to help resolve it. First note the problem URL. It should be in the detailed error email. Try the url in your browser and verify you receive the same error. Now you have to find the page […]
Continue reading...10. October 2009
Comments Off on Tracking SWReg pages with Google Analytics
Here are the steps to add the Google Analytics tracking code to a SWReg shopping basket. 1) Find the link(s) you use for your shopping basket. Modify the pages that include the links and the links themselves using Google’s instructions: “How do I use Google Analytics to track a 3rd-party shopping cart?“. 2) Now you […]
Continue reading...
19. October 2009
2 Comments