Suppressing Python Warnings

Posted on November 17th, 2011

Every so often you run into a situation where a piece of code will knowingly raise a warning that you might want to hide from the end user. Good example of this would be a deprecation warning.

Luckily Python provides a handy context manager that can help you catch and filter various warnings.
Here’s a quick example of how to go about doing just that:

# Import warnings module
import warnings

# Initialize context manager
with warnings.catch_warnings ():
    # Suppress warnings
    warnings.simplefilter ('ignore')

    # Put your warning prone code here

Hope this is useful!


No Comments »


Python’s “with” statement

Posted on April 22nd, 2011

Often when writing code I ran into situation where I set things up, do some work and then tear things down and clean up. This task is often achieved by setting up a series of try, except and finally statements within the code. Consider a simple case of retrieving some information from a text file.

try:
    f = open ('test.txt')
    data = f.read ()
finally:
    f.close ()

I’ve been thinking of wrapping this functionality into a decorator until I ran across a snippet of code that went something like this:

with open ('test.txt') as f:
    data = f.read ()

Nowhere in the code did it close the file making me rather curious. Turns out the “with” statement has been introduced in Python version 2.5 (PEP 343) to factor out the standard uses of try / finally logic.

Whats more you can build the necessary logic directly into your classes so that they can be used with the “with” statement. For example:

class Example ():
    def __enter__ (self):
        # Set things up here
        return obj
    def __exit__ (self, type, value, traceback):
        # Clean things up

with Example () as obj:
    # Do some related work

When you execute the code the __enter__ method gets called. If you passed the target along with the with statement the return value from the __enter__ method will be assigned to it. After your code block finishes executing the __exit__ method is called.

Note that if an error occurs in either the __enter__ method or your work code block the __exit__ method will be called and the exception type, value and traceback are passed into it as arguments, which allow you to handle the error intelligently. If no exception occurs then three None values are passed.

Pretty neat stuff!


No Comments »


Source control

Posted on April 7th, 2009

Whether you’re and artist working on a model or a technical director writing a script you undoubtedly have to diligently make sure to save incrementally and back up your work to give you a peace of mind that your work is secure against corruption or user error. While manually backing up your data can be sufficient for a single file project it can quickly become unmanageable and unreliable if the scope of your task begins to grow.

 

Recently I found myself struggling to keep track of revisions I made to several python libraries as I was iterating on a custom plug-in. I wanted to be able to save a working version of my plug-in after every major modification. Naturally I began to look into version control systems like SVN.

 

Thankfully a friend and a colleague of mine, Kevin Hayes, pointed me in a direction of Perforce. I have been using the Perforce software for several years now at work but never considered it as an option for personal use. As Kevin pointed out Perforce Software is kind enough to provide a fully featured version of Perforce for personal use. While it is limited to two users and five workspaces it is more than enough to keep track of your personal work at home.

 

The installation of the Perforce client is really straight forward. I set up my depot database on my portable hard drive which basically gives me a portable server. With the five client workspaces you could set up your local depot on multiple computers and sync those with the ‘server’ as desired.

 

You can find more information about Perforce on their website:
http://www.perforce.com/perforce/demo/testdrive.html

 

It’s been a real time saver for me recently so give it a try if you get a chance.

 

Cheers.


No Comments »


Welcome!

Posted on December 12th, 2008

Welcome to my personal website showcasing my latest artwork as well as development and research projects. I plan to update it regularly with discussions on various topics that I run across during my daily work as well as anything else I find useful to share with cg community. Hopefully this will be a good resource for anyone interested in 3d as well as for those who would like to know more about me and what I do professionally.

 

Thank you and enjoy your stay.


No Comments »