[Tutor] help with a class

John washakie at gmail.com
Wed Aug 24 20:34:14 CEST 2011


Hello, I have a class that has an attribute that is a dict which I
fill with more dicts. I've created a function to return those
dictionaries if a key is provide, otherwise, it returns the 'default'
dictionary.

I have the following methods (see below), it seems the update_options
method is fine, but I'm not sure about the override_options method...
and for that matter, I'm not sure this is the best approach overall...
any comments, critiques?

Thank you,
john

class Runner(object):
    """ Initiate with an optional dictionary of settings.

    Defaults are set in the options dictionary, which can be overridden.

    """
    # Simplest default:
    default_options = {
                      'file' : '../data/input.dat',
                      'wavelength' : '310.0 310.0'
                      }


    def __init__(self, **kwargs):

        if 'options' not in kwargs:
            self.options = copy(self.default_options)
        self.run_queue = {}

    def _get_from_queue(self, queue_key):
        """ internal method to set options """

        if queue_key:
            if queue_key not in self.run_queue:
                raise ValueError('queue_key: {0} not in
queue'.format(queue_key))
            else:
                return self.run_queue[queue_key]
        else:
            return self.options


    def update_options(self, options, queue_key=None):
        """ update the options dictionary using a dict """
        assert isinstance(options, dict), "update options requires a dict"
        old_options = self._get_from_queue(queue_key)
        old_options.update(options)


    def overide_options(self, options, queue_key=None):
        """ completely overide the options dict """

        assert isinstance(options, dict), "override options requires a dict"
        old_options = self._get_from_queue(queue_key)
        old_options = options

--


More information about the Tutor mailing list