Python contextlib.nested() Examples

The following are 30 code examples of contextlib.nested(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module contextlib , or try the search function .
Example #1
Source File: interactiveshell.py    From Computable with MIT License 6 votes vote down vote up
def mainloop(self, display_banner=None):
        """Start the mainloop.

        If an optional banner argument is given, it will override the
        internally created default banner.
        """

        with nested(self.builtin_trap, self.display_trap):

            while 1:
                try:
                    self.interact(display_banner=display_banner)
                    #self.interact_with_readline()
                    # XXX for testing of a readline-decoupled repl loop, call
                    # interact_with_readline above
                    break
                except KeyboardInterrupt:
                    # this should not be necessary, but KeyboardInterrupt
                    # handling seems rather unpredictable...
                    self.write("\nKeyboardInterrupt in interact()\n") 
Example #2
Source File: train_cifar.py    From multilabel-image-classification-tensorflow with MIT License 6 votes vote down vote up
def build_model(inputs, num_classes, is_training, hparams):
  """Constructs the vision model being trained/evaled.

  Args:
    inputs: input features/images being fed to the image model build built.
    num_classes: number of output classes being predicted.
    is_training: is the model training or not.
    hparams: additional hyperparameters associated with the image model.

  Returns:
    The logits of the image model.
  """
  scopes = setup_arg_scopes(is_training)
  with contextlib.nested(*scopes):
    if hparams.model_name == 'pyramid_net':
      logits = build_shake_drop_model(
          inputs, num_classes, is_training)
    elif hparams.model_name == 'wrn':
      logits = build_wrn_model(
          inputs, num_classes, hparams.wrn_size)
    elif hparams.model_name == 'shake_shake':
      logits = build_shake_shake_model(
          inputs, num_classes, hparams, is_training)
  return logits 
Example #3
Source File: train_cifar.py    From models with Apache License 2.0 6 votes vote down vote up
def build_model(inputs, num_classes, is_training, hparams):
  """Constructs the vision model being trained/evaled.

  Args:
    inputs: input features/images being fed to the image model build built.
    num_classes: number of output classes being predicted.
    is_training: is the model training or not.
    hparams: additional hyperparameters associated with the image model.

  Returns:
    The logits of the image model.
  """
  scopes = setup_arg_scopes(is_training)
  with contextlib.nested(*scopes):
    if hparams.model_name == 'pyramid_net':
      logits = build_shake_drop_model(
          inputs, num_classes, is_training)
    elif hparams.model_name == 'wrn':
      logits = build_wrn_model(
          inputs, num_classes, hparams.wrn_size)
    elif hparams.model_name == 'shake_shake':
      logits = build_shake_shake_model(
          inputs, num_classes, hparams, is_training)
  return logits 
Example #4
Source File: train_cifar.py    From g-tensorflow-models with Apache License 2.0 6 votes vote down vote up
def build_model(inputs, num_classes, is_training, hparams):
  """Constructs the vision model being trained/evaled.

  Args:
    inputs: input features/images being fed to the image model build built.
    num_classes: number of output classes being predicted.
    is_training: is the model training or not.
    hparams: additional hyperparameters associated with the image model.

  Returns:
    The logits of the image model.
  """
  scopes = setup_arg_scopes(is_training)
  with contextlib.nested(*scopes):
    if hparams.model_name == 'pyramid_net':
      logits = build_shake_drop_model(
          inputs, num_classes, is_training)
    elif hparams.model_name == 'wrn':
      logits = build_wrn_model(
          inputs, num_classes, hparams.wrn_size)
    elif hparams.model_name == 'shake_shake':
      logits = build_shake_shake_model(
          inputs, num_classes, hparams, is_training)
  return logits 
Example #5
Source File: compat.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
def nested(*managers):
        exits = []
        vars = []
        exc = (None, None, None)
        try:
            for mgr in managers:
                exit = mgr.__exit__
                enter = mgr.__enter__
                vars.append(enter())
                exits.append(exit)
            yield vars
        except:
            exc = sys.exc_info()
        finally:
            while exits:
                exit = exits.pop()
                try:
                    if exit(*exc):
                        exc = (None, None, None)
                except:
                    exc = sys.exc_info()
            if exc != (None, None, None):
                reraise(exc[0], exc[1], exc[2]) 
Example #6
Source File: test_sh.py    From cider with MIT License 6 votes vote down vote up
def test_read_config(path, contents, fallback):
    def test_case(assertion, msg, mock=None, read_data=None):
        with patch("__builtin__.open", mock_open(mock, read_data)):
            assert assertion(), msg

    test_case(
        lambda: sh.read_config(path, fallback) == contents,
        "Should read valid YAML",
        read_data=yaml.dump(contents, default_flow_style=False)
    )

    with pytest.raises(IOError) if fallback is None else empty():
        test_case(
            lambda: sh.read_config(path, fallback) == fallback,
            "Should use fallback if file is missing",
            MagicMock(side_effect=IOError(errno.ENOENT, ""))
        )

    with pytest.raises(ParserError):
        test_case(
            lambda: sh.read_config(path, fallback) is None,
            "Should raise error if data is invalid",
            read_data="[garbage}",
        ) 
Example #7
Source File: 错误和异常概念.py    From Python_code with MIT License 6 votes vote down vote up
def close(self):
        print("资源释放")

    # def __enter__(self):
    #     return self
    #
    # def __exit__(self, exc_type, exc_val, exc_tb):
    #     self.close()
# import contextlib
# with contextlib.closing(Test()) as t_obj:
#     t_obj.t()


# with open("xx.jpg", "rb") as from_file:
#     with open("xx2.jpg", "wb") as to_file:
#         from_contents = from_file.read()
#         to_file.write(from_contents)
# with open("xx.jpg", "rb") as from_file, open("xx2.jpg", "wb") as to_file:
#     from_contents = from_file.read()
#     to_file.write(from_contents)
# import contextlib
# with contextlib.nested(open("xx.jpg", "rb"), open("xx2.jpg", "wb")) as (from_file, to_file):
#     from_contents = from_file.read()
#     to_file.write(from_contents) 
Example #8
Source File: test_log_source_action.py    From mycroft with MIT License 6 votes vote down vote up
def mock_response(self):
        with nested(
            mock.patch('mycroft.logic.log_source_action.staticconf.read_bool',
                       autospec=True),
            mock.patch('mycroft.logic.log_source_action.staticconf.read_string',
                       autospec=True),
            mock.patch('mycroft.logic.log_source_action.requests.post',
                       autospec=True)
        ) as (
            read_bool,
            read_string,
            mock_requests_post
        ):
            read_bool.return_value = False
            mock_response = Response()
            mock_requests_post.return_value = mock_response
            yield mock_response 
Example #9
Source File: test_driver.py    From openstack-omni with Apache License 2.0 6 votes vote down vote up
def test_destory_instance_terminated_on_aws(self):
        self._create_vm_in_aws_nova()
        fake_instances = self.fake_ec2_conn.get_only_instances()
        self.fake_ec2_conn.stop_instances(instance_ids=[fake_instances[0].id])
        self.fake_ec2_conn.terminate_instances(
            instance_ids=[fake_instances[0].id])
        with contextlib.nested(
            mock.patch.object(boto.ec2.EC2Connection, 'stop_instances'),
            mock.patch.object(boto.ec2.EC2Connection, 'terminate_instances'),
            mock.patch.object(EC2Driver, '_wait_for_state'),
        ) as (fake_stop, fake_terminate, fake_wait):
            self.conn.destroy(self.context, self.instance, None, None)
            fake_stop.assert_not_called()
            fake_terminate.assert_not_called()
            fake_wait.assert_not_called()
        self.reset() 
Example #10
Source File: test_driver.py    From openstack-omni with Apache License 2.0 6 votes vote down vote up
def test_spawn_with_key(self):
        self._create_instance(key_name='fake_key', key_data='fake_key_data')
        self._create_network()
        with contextlib.nested(
            mock.patch.object(EC2Driver, '_get_image_ami_id_from_meta'),
            mock.patch.object(EC2Driver, '_process_network_info'),
            mock.patch.object(EC2Driver, '_get_instance_sec_grps'),
        ) as (mock_image, mock_network, mock_secgrp):
            mock_image.return_value = 'ami-1234abc'
            mock_network.return_value = (self.subnet_id, '192.168.10.5', None,
                                         None)
            mock_secgrp.return_value = []
            self._create_nova_vm()
            fake_instances = self.fake_ec2_conn.get_only_instances()
            self.assertEqual(len(fake_instances), 1)
            inst = fake_instances[0]
            self.assertEqual(inst.key_name, 'fake_key')
        self.reset() 
Example #11
Source File: test_driver.py    From openstack-omni with Apache License 2.0 6 votes vote down vote up
def test_spawn(self):
        self._create_instance()
        self._create_network()
        with contextlib.nested(
            mock.patch.object(EC2Driver, '_get_image_ami_id_from_meta'),
            mock.patch.object(EC2Driver, '_process_network_info'),
            mock.patch.object(EC2Driver, '_get_instance_sec_grps'),
        ) as (mock_image, mock_network, mock_secgrp):
            mock_image.return_value = 'ami-1234abc'
            mock_network.return_value = (self.subnet_id, '192.168.10.5', None,
                                         None)
            mock_secgrp.return_value = []
            self._create_nova_vm()
            fake_instances = self.fake_ec2_conn.get_only_instances()
            self.assertEqual(len(fake_instances), 1)
            inst = fake_instances[0]
            self.assertEqual(inst.vpc_id, self.vpc.id)
            self.assertEqual(self.subnet_id, inst.subnet_id)
            self.assertEqual(inst.tags['Name'], 'fake_instance')
            self.assertEqual(inst.tags['openstack_id'], self.uuid)
            self.assertEqual(inst.image_id, 'ami-1234abc')
            self.assertEqual(inst.region.name, self.region_name)
            self.assertEqual(inst.key_name, 'None')
            self.assertEqual(inst.instance_type, 't2.small')
        self.reset() 
Example #12
Source File: compat.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def nested(*managers):
        exits = []
        vars = []
        exc = (None, None, None)
        try:
            for mgr in managers:
                exit = mgr.__exit__
                enter = mgr.__enter__
                vars.append(enter())
                exits.append(exit)
            yield vars
        except:
            exc = sys.exc_info()
        finally:
            while exits:
                exit = exits.pop()
                try:
                    if exit(*exc):
                        exc = (None, None, None)
                except:
                    exc = sys.exc_info()
            if exc != (None, None, None):
                reraise(exc[0], exc[1], exc[2]) 
Example #13
Source File: atrace_from_file_agent_unittest.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_from_file(self):
    update_systrace_trace_viewer.update(force_update=True)
    self.assertTrue(os.path.exists(
        update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE))
    output_file_name = util.generate_random_filename_for_test()
    try:
      # use from-file to create a specific expected output
      run_systrace.main_impl(['./run_systrace.py',
                              '--from-file',
                              COMPRESSED_ATRACE_DATA,
                              '-o',
                              output_file_name])
      # and verify file contents
      with contextlib.nested(open(output_file_name, 'r'),
                             open(DECOMPRESSED_ATRACE_DATA, 'r')) as (f1, f2):
        full_trace = f1.read()
        expected_contents = f2.read()
        self.assertTrue(expected_contents in full_trace)
    except:
      raise
    finally:
      os.remove(update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE)
      if os.path.exists(output_file_name):
        os.remove(output_file_name) 
Example #14
Source File: atrace_from_file_agent_unittest.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_default_output_filename(self):
    update_systrace_trace_viewer.update(force_update=True)
    self.assertTrue(os.path.exists(
        update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE))
    output_file_name = os.path.join(TEST_DIR, 'compressed_atrace_data.html')
    try:
      # use from-file to create a specific expected output
      run_systrace.main_impl(['./run_systrace.py',
                              '--from-file',
                              COMPRESSED_ATRACE_DATA])
      # and verify file contents
      with contextlib.nested(open(output_file_name, 'r'),
                             open(DECOMPRESSED_ATRACE_DATA, 'r')) as (f1, f2):
        full_trace = f1.read()
        expected_contents = f2.read()
        self.assertTrue(expected_contents in full_trace)
    except:
      raise
    finally:
      os.remove(update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE)
      if os.path.exists(output_file_name):
        os.remove(output_file_name) 
Example #15
Source File: compat.py    From jbox with MIT License 6 votes vote down vote up
def nested(*managers):
        exits = []
        vars = []
        exc = (None, None, None)
        try:
            for mgr in managers:
                exit = mgr.__exit__
                enter = mgr.__enter__
                vars.append(enter())
                exits.append(exit)
            yield vars
        except:
            exc = sys.exc_info()
        finally:
            while exits:
                exit = exits.pop()
                try:
                    if exit(*exc):
                        exc = (None, None, None)
                except:
                    exc = sys.exc_info()
            if exc != (None, None, None):
                reraise(exc[0], exc[1], exc[2]) 
Example #16
Source File: test_query_device_capacity.py    From bitmath with MIT License 6 votes vote down vote up
def test_query_device_capacity_linux_everything_is_wonderful(self):
        """query device capacity works on a happy Linux host"""
        with nested(
            mock.patch('os.stat'),
            mock.patch('stat.S_ISBLK'),
            mock.patch('platform.system'),
            mock.patch('fcntl.ioctl'),
        ) as (os_stat, stat_is_block, plat_system, ioctl):
            os_stat.return_value = mock.Mock(st_mode=25008)
            stat_is_block.return_value = True
            plat_system.return_value = 'Linux'
            ioctl.return_value = struct.pack('L', 244140625)
            # = 'QJ\x8d\x0e\x00\x00\x00\x00'
            # = 244140625 ~= 244.140625 MB (in SI)
            buffer_test = ' ' * struct.calcsize('L')

            bytes = bitmath.query_device_capacity(device)
            self.assertEqual(bytes, 244140625)
            self.assertEqual(ioctl.call_count, 1)
            ioctl.assert_called_once_with(4, 0x80081272, buffer_test) 
Example #17
Source File: atrace_from_file_agent_unittest.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_from_file(self):
    update_systrace_trace_viewer.update(force_update=True)
    self.assertTrue(os.path.exists(
        update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE))
    output_file_name = util.generate_random_filename_for_test()
    try:
      # use from-file to create a specific expected output
      run_systrace.main_impl(['./run_systrace.py',
                              '--from-file',
                              COMPRESSED_ATRACE_DATA,
                              '-o',
                              output_file_name])
      # and verify file contents
      with contextlib.nested(open(output_file_name, 'r'),
                             open(DECOMPRESSED_ATRACE_DATA, 'r')) as (f1, f2):
        full_trace = f1.read()
        expected_contents = f2.read()
        self.assertTrue(expected_contents in full_trace)
    except:
      raise
    finally:
      os.remove(update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE)
      if os.path.exists(output_file_name):
        os.remove(output_file_name) 
Example #18
Source File: atrace_from_file_agent_unittest.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_default_output_filename(self):
    update_systrace_trace_viewer.update(force_update=True)
    self.assertTrue(os.path.exists(
        update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE))
    output_file_name = os.path.join(TEST_DIR, 'compressed_atrace_data.html')
    try:
      # use from-file to create a specific expected output
      run_systrace.main_impl(['./run_systrace.py',
                              '--from-file',
                              COMPRESSED_ATRACE_DATA])
      # and verify file contents
      with contextlib.nested(open(output_file_name, 'r'),
                             open(DECOMPRESSED_ATRACE_DATA, 'r')) as (f1, f2):
        full_trace = f1.read()
        expected_contents = f2.read()
        self.assertTrue(expected_contents in full_trace)
    except:
      raise
    finally:
      os.remove(update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE)
      if os.path.exists(output_file_name):
        os.remove(output_file_name) 
Example #19
Source File: test_sh.py    From cider with MIT License 5 votes vote down vote up
def test_tap(self, cask, debug, verbose, tap, use_tap):
        with pytest.raises(AssertionError) if cask else empty():
            brew = Brew(cask, debug, verbose)
            tap = tap if use_tap else None
            args = self.__cmd() + ["tap"] + ([tap] if tap is not None else [])
            args += self.__flags(debug, verbose)

            brew.tap(tap)
            sh.spawn.assert_called_with(args, debug=debug,
                                        check_output=not use_tap,
                                        env=brew.env) 
Example #20
Source File: test_query_device_capacity.py    From bitmath with MIT License 5 votes vote down vote up
def test_query_device_capacity_mac_everything_is_wonderful(self):
        """query device capacity works on a happy Mac OS X host"""
        with nested(
            mock.patch('os.stat'),
            mock.patch('stat.S_ISBLK'),
            mock.patch('platform.system'),
            mock.patch('fcntl.ioctl'),
        ) as (os_stat, stat_is_block, plat_system, ioctl):
            # These are the struct.pack() equivalents of 244140625
            # (type: u64) and 4096 (type: u32). Multiplied together
            # they equal the number of bytes in 1 TB.
            returns = [
                struct.pack('L', 244140625),  # 'QJ\x8d\x0e\x00\x00\x00\x00'
                struct.pack('I', 4096)  # , '\x00\x10\x00\x00'
            ]

            def side_effect(*args, **kwargs):
                return returns.pop(0)

            os_stat.return_value = mock.Mock(st_mode=25008)
            stat_is_block.return_value = True
            plat_system.return_value = 'Darwin'
            ioctl.side_effect = side_effect

            bytes = bitmath.query_device_capacity(device)
            # The result should be 1 TB
            self.assertEqual(bytes, 1000000000000)
            self.assertEqual(ioctl.call_count, 2) 
Example #21
Source File: test_sh.py    From cider with MIT License 5 votes vote down vote up
def test_untap(self, cask, debug, verbose, tap):
        with pytest.raises(AssertionError) if cask else empty():
            brew = Brew(cask, debug, verbose)
            args = self.__cmd() + ["untap", tap]
            args += self.__flags(debug, verbose)

            brew.untap(tap)
            sh.spawn.assert_called_with(args, debug=debug, check_output=False,
                                        env=brew.env) 
Example #22
Source File: test_core.py    From cider with MIT License 5 votes vote down vote up
def test_unlink(self, tmpdir, debug, verbose, name, links):
        """
        Tests that:
        1. Each symlink is moved back to its original location.
        2. Symlinks are removed from bootstrap.
        3. Cache is updated with targets removed.
        4. Symlink directory is removed if empty.

        Expected errors:
        - StowError is raised if no symlink was found.
        - SymlinkError is raised if target already exists.
        """
        cider = Cider(
            False, debug, verbose,
            cider_dir=str(tmpdir.join("cider")),
            support_dir=str(tmpdir.join("cider", ".cache"))
        )
        cider.remove_symlink = MagicMock()

        stow_dir = os.path.abspath(os.path.join(cider.symlink_dir, name))
        os.makedirs(stow_dir)
        for link in links:
            source = os.path.join(stow_dir, link)
            target = str(tmpdir.join(link))
            touch(source)
            os.symlink(source, target)
            cider.add_symlink(name, target)

        cider.unlink(name)

        new_cache = cider._cached_targets()  # pylint:disable=W0212
        for link in links:
            source = os.path.join(stow_dir, link)
            target = str(tmpdir.join(link))
            assert os.path.exists(target)
            assert not os.path.islink(target)
            assert not os.path.exists(source)
            assert target not in new_cache

        cider.remove_symlink.assert_called_with(name)
        assert not os.path.exists(stow_dir) 
Example #23
Source File: fabfile.py    From volontulo with MIT License 5 votes vote down vote up
def update():
    u"""Function defining all steps required to properly update application."""

    # Django app refresh:
    with contextlib.nested(
        cd('/var/www/volontuloapp_org'),
        prefix('workon volontuloapp_org')
    ):
        run('git checkout master')
        run('git pull')
        run('pip install -r requirements/production.txt')

    # Gulp frontend refresh:
    with contextlib.nested(
        cd('/var/www/volontuloapp_org/apps/volontulo')
    ):
        run('npm install .')
        run('./node_modules/.bin/gulp build')

    # Django site refresh:
    with contextlib.nested(
        cd('/var/www/volontuloapp_org'),
        prefix('workon volontuloapp_org')
    ):
        run('python manage.py migrate --traceback'
            ' --settings=volontulo_org.settings.production')
        run('service apache2 restart') 
Example #24
Source File: test_writers_gstorage.py    From exporters with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_init_from_resource(self):
        options = {
            'name': 'exporters.writers.gstorage_writer.GStorageWriter',
            'options': {
                'project': 'some-project-666',
                'bucket': 'bucket-777',
                'filebase': 'tests/',
            }
        }
        env = {'EXPORTERS_GSTORAGE_CREDS_RESOURCE': 'a:b'}
        with nested(mock.patch.dict('os.environ', env),
                    mock.patch('pkg_resources.resource_string', return_value='{}'),
                    mock.patch('gcloud.storage.Client.from_service_account_json')):
            with closing(GStorageWriter(options, meta())):
                pass 
Example #25
Source File: test_writers_gstorage.py    From exporters with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_init_fails_with_bad_resource(self):
        options = {
            'name': 'exporters.writers.gstorage_writer.GStorageWriter',
            'options': {
                'project': 'some-project-666',
                'bucket': 'bucket-777',
                'filebase': 'tests/',
            }
        }
        env = {'EXPORTERS_GSTORAGE_CREDS_RESOURCE': 'a:b'}
        with nested(self.assertRaisesRegexp(ImportError, 'No module named a'),
                    mock.patch.dict('os.environ', env)):
            GStorageWriter(options, meta()) 
Example #26
Source File: main.py    From uda with Apache License 2.0 5 votes vote down vote up
def build_model(inputs, num_classes, is_training, update_bn, hparams):
  """Constructs the vision model being trained/evaled.

  Args:
    inputs: input features/images being fed to the image model build built.
    num_classes: number of output classes being predicted.
    is_training: is the model training or not.
    hparams: additional hyperparameters associated with the image model.

  Returns:
    The logits of the image model.
  """
  scopes = setup_arg_scopes(is_training)

  try:
      from contextlib import nested
  except ImportError:
      from contextlib import ExitStack, contextmanager

      @contextmanager
      def nested(*contexts):
          with ExitStack() as stack:
              for ctx in contexts:
                  stack.enter_context(ctx)
              yield contexts

  with nested(*scopes):
    if hparams.model_name == "pyramid_net":
      logits = build_shake_drop_model(
          inputs, num_classes, is_training)
    elif hparams.model_name == "wrn":
      logits = build_wrn_model(
          inputs, num_classes, hparams.wrn_size, update_bn)
    elif hparams.model_name == "shake_shake":
      logits = build_shake_shake_model(
          inputs, num_classes, hparams, is_training)

  return logits 
Example #27
Source File: test.py    From masakari with Apache License 2.0 5 votes vote down vote up
def nested(*contexts):
        with contextlib.ExitStack() as stack:
            yield [stack.enter_context(c) for c in contexts] 
Example #28
Source File: io.py    From lang2program with Apache License 2.0 5 votes vote down vote up
def read_files(*file_paths):
    files = []
    for i, p in enumerate(file_paths):
        if p:
            files.append(open(p, mode="r"))
            print 'Opened:', p
        else:
            files.append(EmptyFile())
            print 'WARNING: no path provided for file {} in list.'.format(i)

    with contextlib.nested(*files) as entered_files:
        for lines in izip(*entered_files):
            yield lines 
Example #29
Source File: io.py    From lang2program with Apache License 2.0 5 votes vote down vote up
def read_files(*file_paths):
    files = []
    for i, p in enumerate(file_paths):
        if p:
            files.append(open(p, mode="r"))
            print 'Opened:', p
        else:
            files.append(EmptyFile())
            print 'WARNING: no path provided for file {} in list.'.format(i)

    with contextlib.nested(*files) as entered_files:
        for lines in izip(*entered_files):
            yield lines 
Example #30
Source File: test_query_device_capacity.py    From bitmath with MIT License 5 votes vote down vote up
def nested(*contexts):
        """Emulation of contextlib.nested in terms of ExitStack

        Has the problems for which "nested" was removed from Python; see:
            https://docs.python.org/2/library/contextlib.html#contextlib.nested
        But for mock.patch, these do not matter.
        """
        with ExitStack() as stack:
            yield tuple(stack.enter_context(c) for c in contexts)