Changeset b6a3161


Ignore:
Timestamp:
05/09/2013 06:13:20 PM (12 years ago)
Author:
Calum Lind <calumlind+deluge@gmail.com>
Branches:
2.0.x, develop, master
Children:
7424cf
Parents:
daba92
git-author:
Calum Lind <calumlind+deluge@gmail.com> (05/09/2013 05:41:30 PM)
git-committer:
Calum Lind <calumlind+deluge@gmail.com> (05/09/2013 06:13:20 PM)
Message:

Add a get method to config so a default value can be returned

Location:
deluge
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • deluge/config.py

    rdaba92 rb6a3161  
    268268        else:
    269269            return self.__config[key]
     270
     271
     272    def get(self, key, default=None):
     273        """
     274        Gets the value of item 'key' if key is in the config, else default.
     275        If default is not given, it defaults to None, so that this method
     276        never raises a KeyError.
     277
     278        :param key: the item for which you want it's value
     279        :param default: the default value if key is missing
     280        :return: the value of item 'key' or default
     281
     282        **Usage**
     283
     284        >>> config = Config("test.conf", defaults={"test": 5})
     285        >>> config.get("test", 10)
     286        5
     287        >>> config.get("bad_key", 10)
     288        10
     289
     290        """
     291        try:
     292            return self.get_item(key)
     293        except KeyError:
     294            return default
     295
    270296
    271297    def __delitem__(self, key):
  • deluge/core/core.py

    rdaba92 rb6a3161  
    481481    def get_config_value(self, key):
    482482        """Get the config value for key"""
    483         try:
    484             value = self.config[key]
    485         except KeyError:
    486             return None
    487 
    488         return value
     483        return self.config.get(key)
    489484
    490485    @export
    491486    def get_config_values(self, keys):
    492487        """Get the config values for the entered keys"""
    493         config = {}
    494         for key in keys:
    495             try:
    496                 config[key] = self.config[key]
    497             except KeyError:
    498                 pass
    499         return config
     488        return {(key, self.config.get(key)) for key in keys}
    500489
    501490    @export
  • deluge/tests/test_config.py

    rdaba92 rb6a3161  
    3232        config["unicode"] = u"ВИДЕОЀИЛЬМЫ"
    3333        self.assertEquals(config["unicode"], u"ВИДЕОЀИЛЬМЫ")
    34        
     34
    3535        config._save_timer.cancel()
     36
     37    def test_get(self):
     38        config = Config("test.conf", config_dir=self.config_dir)
     39        config["foo"] = 1
     40        self.assertEquals(config.get("foo"), 1)
     41        self.assertEquals(config.get("foobar"), None)
     42        self.assertEquals(config.get("foobar", 2), 2)
     43        config["foobar"] = 5
     44        self.assertEquals(config.get("foobar", 2), 5)
    3645
    3746    def test_load(self):
Note: See TracChangeset for help on using the changeset viewer.