instance_id stringlengths 13 45 | pull_number int64 7 30.1k | repo stringclasses 83
values | version stringclasses 68
values | base_commit stringlengths 40 40 | created_at stringdate 2013-05-16 18:15:55 2025-01-08 15:12:50 | patch stringlengths 347 35.2k | test_patch stringlengths 432 113k | non_py_patch stringlengths 0 18.3k | new_components listlengths 0 40 | FAIL_TO_PASS listlengths 1 2.53k | PASS_TO_PASS listlengths 0 1.7k | problem_statement stringlengths 607 52.7k | hints_text stringlengths 0 57.4k | environment_setup_commit stringclasses 167
values |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
tornadoweb__tornado-789 | 789 | tornadoweb/tornado | null | 833d6975c320836ddc3a20fde681fdccbdcb82ed | 2013-05-16T18:15:55Z | diff --git a/tornado/options.py b/tornado/options.py
index faf0e164cc..04692f7a83 100644
--- a/tornado/options.py
+++ b/tornado/options.py
@@ -101,6 +101,44 @@ def __setattr__(self, name, value):
return self._options[name].set(value)
raise AttributeError("Unrecognized option %r" % name)
+ def __iter__(self):
+ return iter(self._options)
+
+ def __getitem__(self, item):
+ return self._options[item].value()
+
+ def items(self):
+ """A sequence of (name, value) pairs."""
+ return [(name, opt.value()) for name, opt in self._options.items()]
+
+ def groups(self):
+ """The set of option-groups created by ``define``."""
+ return set(opt.group_name for opt in self._options.values())
+
+ def group_dict(self, group):
+ """The names and values of options in a group.
+
+ Useful for copying options into Application settings::
+
+ from tornado.options import define, parse_command_line, options
+
+ define('template_path', group='application')
+ define('static_path', group='application')
+
+ parse_command_line()
+
+ application = Application(
+ handlers, **options.group_dict('application'))
+ """
+ return dict(
+ (name, opt.value()) for name, opt in self._options.items()
+ if not group or group == opt.group_name)
+
+ def as_dict(self):
+ """The names and values of all options."""
+ return dict(
+ (name, opt.value()) for name, opt in self._options.items())
+
def define(self, name, default=None, type=None, help=None, metavar=None,
multiple=False, group=None, callback=None):
"""Defines a new command line option.
| diff --git a/tornado/test/options_test.py b/tornado/test/options_test.py
index dc52a82a49..23ce0fe458 100644
--- a/tornado/test/options_test.py
+++ b/tornado/test/options_test.py
@@ -113,6 +113,47 @@ def test_setattr_with_callback(self):
options.foo = 2
self.assertEqual(values, [2])
+ def _sample_options(self):
+ options = OptionParser()
+ options.define('a', default=1)
+ options.define('b', default=2)
+ return options
+
+ def test_iter(self):
+ options = self._sample_options()
+ # OptionParsers always define 'help'.
+ self.assertEqual(set(['a', 'b', 'help']), set(iter(options)))
+
+ def test_getitem(self):
+ options = self._sample_options()
+ self.assertEqual(1, options['a'])
+
+ def test_items(self):
+ options = self._sample_options()
+ # OptionParsers always define 'help'.
+ expected = [('a', 1), ('b', 2), ('help', options.help)]
+ actual = sorted(options.items())
+ self.assertEqual(expected, actual)
+
+ def test_as_dict(self):
+ options = self._sample_options()
+ expected = {'a': 1, 'b': 2, 'help': options.help}
+ self.assertEqual(expected, options.as_dict())
+
+ def test_group_dict(self):
+ options = OptionParser()
+ options.define('a', default=1)
+ options.define('b', group='b_group', default=2)
+
+ frame = sys._getframe(0)
+ this_file = frame.f_code.co_filename
+ self.assertEqual(set(['b_group', '', this_file]), options.groups())
+
+ b_group_dict = options.group_dict('b_group')
+ self.assertEqual({'b': 2}, b_group_dict)
+
+ self.assertEqual({}, options.group_dict('nonexistent'))
+
@unittest.skipIf(mock is None, 'mock package not present')
def test_mock_patch(self):
# ensure that our setattr hooks don't interfere with mock.patch
| [
{
"components": [
{
"doc": "",
"lines": [
104,
105
],
"name": "OptionParser.__iter__",
"signature": "def __iter__(self):",
"type": "function"
},
{
"doc": "",
"lines": [
107,
108
],
... | [
"tornado/test/options_test.py::OptionsTest::test_as_dict",
"tornado/test/options_test.py::OptionsTest::test_getitem",
"tornado/test/options_test.py::OptionsTest::test_group_dict",
"tornado/test/options_test.py::OptionsTest::test_items",
"tornado/test/options_test.py::OptionsTest::test_iter"
] | [
"tornado/test/options_test.py::OptionsTest::test_help",
"tornado/test/options_test.py::OptionsTest::test_mock_patch",
"tornado/test/options_test.py::OptionsTest::test_multiple_int",
"tornado/test/options_test.py::OptionsTest::test_multiple_string",
"tornado/test/options_test.py::OptionsTest::test_parse_call... | This is a feature request which requires a new feature to add in the code repository.
<<NEW FEATURE REQUEST>>
<request>
Make options instance more dict-like.
In my Motor-Blog application, I find myself copying a lot of options from tornado.options into Application(). I thought this would be nice:
application = Application(handler, **options.options.as_dict())
... so I added as_dict() and some dict-like features to OptionParser.
----------
</request>
There are several new functions or classes that need to be implemented, using the definitions below:
<<NEW DEFINITIONS>>
There are several new functions or classes that need to be implemented, using the definitions below:
<definitions>
[start of new definitions in tornado/options.py]
(definition of OptionParser.__iter__:)
def __iter__(self):
(definition of OptionParser.__getitem__:)
def __getitem__(self, item):
(definition of OptionParser.items:)
def items(self):
"""A sequence of (name, value) pairs."""
(definition of OptionParser.groups:)
def groups(self):
"""The set of option-groups created by ``define``."""
(definition of OptionParser.group_dict:)
def group_dict(self, group):
"""The names and values of options in a group.
Useful for copying options into Application settings::
from tornado.options import define, parse_command_line, options
define('template_path', group='application')
define('static_path', group='application')
parse_command_line()
application = Application(
handlers, **options.group_dict('application'))"""
(definition of OptionParser.as_dict:)
def as_dict(self):
"""The names and values of all options."""
[end of new definitions in tornado/options.py]
</definitions>
Please note that in addition to the newly added components mentioned above, you also need to make other code changes to ensure that the new feature can be executed properly.
<<END>> | b5dad636aaba94f86a3c00ca6ec49c79ff4313b2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.