Archive | Tech Tips RSS feed for this section

Dynamically Importing and Instantiating Modules in Python

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

Convert None to Empty String in Python

12. October 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'”

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

Tracking SWReg pages with Google Analytics

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

Reloading Modules in a Python Interpreter

3. October 2009

3 Comments

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