Tag Archive | "python"

Adding Dates and Times in Python

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, […]

Continue reading...

Dynamically Importing and Instantiating Modules in Python

Tuesday, October 13, 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...

Convert None to Empty String in Python

Monday, October 12, 2009

1 Comment

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...

Resolving DJANGO Error: “ValueError: invalid literal for int() with base 10: ‘None'”

Sunday, October 11, 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...

Reloading Modules in a Python Interpreter

Saturday, October 3, 2009


If you have edited an external module (or .py file), you can reload the module from within the python interpreter by using the following command: reload(myModule) This assumes that you have imported the module at some point within your interpreter session. When Is This Useful It’s helpful if you are debugging code in a IDE […]

Continue reading...