Repository: tornadoweb/tornado

Issue:
Options should support setitem
From http://stackoverflow.com/questions/33411269/attributeerror-in-python-tornado-to-configure-log-into-a-file/33417289

Options currently override getattr, setattr, and getitem. Item and attribute syntax should be symmetric so we need a setitem override to match setattr.

Discussion:

Patch:
diff --git a/tornado/options.py b/tornado/options.py
index ba16b1a7..bdb5baa0 100644
--- a/tornado/options.py
+++ b/tornado/options.py
@@ -132,8 +132,10 @@ class OptionParser(object):
         return name in self._options
 
     def __getitem__(self, name):
-        name = self._normalize_name(name)
-        return self._options[name].value()
+        return self.__getattr__(name)
+
+    def __setitem__(self, name, value):
+        return self.__setattr__(name, value)
 
     def items(self):
         """A sequence of (name, value) pairs.


Test patch:
diff --git a/tornado/test/options_test.py b/tornado/test/options_test.py
index c32184bb..2f2384b2 100644
--- a/tornado/test/options_test.py
+++ b/tornado/test/options_test.py
@@ -131,6 +131,12 @@ class OptionsTest(unittest.TestCase):
         options = self._sample_options()
         self.assertEqual(1, options['a'])
 
+    def test_setitem(self):
+        options = OptionParser()
+        options.define('foo', default=1, type=int)
+        options['foo'] = 2
+        self.assertEqual(options['foo'], 2)
+
     def test_items(self):
         options = self._sample_options()
         # OptionParsers always define 'help'.