Python contextlib2.ExitStack() Examples

The following are 30 code examples of contextlib2.ExitStack(). 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 contextlib2 , or try the search function .
Example #1
Source File: merge_records.py    From tfrecord_utility with MIT License 6 votes vote down vote up
def main(_):
    record_files = list_records(FLAGS.path_to_records)
    logging.debug(
        "Number of records to be processed: {}".format(len(record_files)))
    next_example = read_records(record_files, shuffle=FLAGS.shuffle)

    # To maximize file I/O throughout, split the training data into pieces.
    with contextlib2.ExitStack() as tf_record_close_stack:
        output_records = tf_record_creation_util.open_sharded_output_tfrecords(
            tf_record_close_stack, FLAGS.output_file, FLAGS.num_shards)

        with tf.Session() as sess:
            index = 0
            while True:
                try:
                    serialized_example = sess.run(next_example)
                    index += 1
                    output_shard_index = index % FLAGS.num_shards
                    output_records[output_shard_index].write(
                        serialized_example)

                    logging.debug("Samples processed: {}".format(index))

                except tf.errors.OutOfRangeError:
                    break 
Example #2
Source File: test_show_loaders.py    From dtale with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_show_csv():
    import dtale

    csv_path = "/../".join([os.path.dirname(__file__), "data/test_df.csv"])

    with mock.patch("dtale.app.show", mock.Mock()):
        dtale.show_csv(path=csv_path)

    with open(csv_path, "r") as f:
        csv_txt = f.read()
        with ExitStack() as stack:
            stack.enter_context(mock.patch("dtale.app.show", mock.Mock()))

            class MockRequest(object):
                def __init__(self):
                    self.content = csv_txt.encode() if PY3 else csv_txt
                    self.status_code = 200

            stack.enter_context(
                mock.patch("requests.get", mock.Mock(return_value=MockRequest()))
            )
            dtale.show_csv(path="http://test-csv")
            dtale.show_csv(path="http://test-csv", proxy="http://test-proxy") 
Example #3
Source File: test_dash.py    From dtale with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_query_changes():
    import dtale.views as views

    df = pd.DataFrame(dict(a=[1, 2, 3], b=[4, 5, 6], c=[7, 8, 9]))
    with app.test_client() as c:
        with ExitStack() as stack:
            df, _ = views.format_data(df)
            stack.enter_context(mock.patch("dtale.global_state.DATA", {c.port: df}))
            pathname = path_builder(c.port)
            params = {
                "output": "..query-data.data...query-input.style...query-input.title..",
                "changedPropIds": ["query-input.value"],
                "inputs": [{"id": "query-input", "property": "value", "value": "d"}],
                "state": [pathname, {"id": "query-data", "property": "data"}],
            }
            response = c.post("/charts/_dash-update-component", json=params)
            resp_data = response.get_json()["response"]
            assert resp_data["query-data"]["data"] is None
            assert resp_data["query-input"]["title"] == "name 'd' is not defined"

            params["inputs"][0]["value"] = "a == 1"
            response = c.post("/charts/_dash-update-component", json=params)
            resp_data = response.get_json()["response"]
            assert resp_data["query-data"]["data"] == "a == 1" 
Example #4
Source File: shadow_demo.py    From ratcave with MIT License 6 votes vote down vote up
def on_draw():
    window.clear()
    with ExitStack() as stack:
        for shader in [rc.resources.shadow_shader, rc.default_shader]:
            with shader, rc.default_states, light, rc.default_camera:
                if shader == rc.resources.shadow_shader:
                    stack.enter_context(fbo_shadow)
                    window.clear()
                else:
                    stack.close()

                for x, y in it.product([-2, -1, 0, 1, 2], [-2, -1, 0, 1, 2]):
                    monkey.position.x = x
                    monkey.position.y = y
                    monkey.drawmode = rc.GL_POINTS if x % 2 and y % 2 else rc.GL_TRIANGLES
                    monkey.uniforms['diffuse'][0] = (x + 1) / 4.
                    monkey.uniforms['diffuse'][1:] = (y + 1) / 4.
                    monkey.scale.z = np.linalg.norm((x, y)) / 10. + .03
                    monkey.draw()

                plane.draw()

    fps_display.draw() 
Example #5
Source File: pyro_utils.py    From DL4MT with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def setup_remotes(remote_metadata_list, pyro_port, pyro_key):
    """
    :param remote_metadata_list: list of dictionaries, each containing:
        "script" -  python script to run
        "name" - name used to register with Pyro nameserver
        "gpu_id"  - GPU ID to run on
    """
    with NameServerSP(key=pyro_key, port=pyro_port) as _:
        with contextlib2.ExitStack() as stack:
            for metadata in remote_metadata_list:
                stack.enter_context(remoteSP(metadata['script'],
                                            key=pyro_key,
                                            name=metadata['name'],
                                            port=pyro_port,
                                            gpu_id=metadata['gpu_id']))

            print 'sleeping extra...'
            time.sleep(20) #TODO: why do I need this now?
            print 'assuming everything is ready to use now' # This is a bad assumption. Can we check this?

            yield 
Example #6
Source File: conftest.py    From okcupyd with MIT License 6 votes vote down vote up
def patch(option, *patches, **kwargs):
    negate = kwargs.get('negate', False)

    @pytest.yield_fixture(autouse=True, scope='session')
    def patch_conditionally(request):
        condition = bool(request.config.getoption(option))
        if negate:
            condition = not condition
        if condition:
            with contextlib.ExitStack() as exit_stack:
                for patch in patches:
                    exit_stack.enter_context(patch)
                yield
        else:
            yield

    return patch_conditionally 
Example #7
Source File: test_views.py    From dtale with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_302():
    import dtale.views as views

    df = pd.DataFrame([1, 2, 3])
    df, _ = views.format_data(df)
    with app.test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(mock.patch("dtale.global_state.DATA", {c.port: df}))
            stack.enter_context(
                mock.patch(
                    "dtale.global_state.DTYPES", {c.port: views.build_dtypes_state(df)}
                )
            )
            for path in [
                "/",
                "/dtale",
                "/dtale/main",
                "/dtale/iframe",
                "/dtale/popup/test",
                "/favicon.ico",
            ]:
                response = c.get(path)
                assert (
                    response.status_code == 302
                ), "{} should return 302 response".format(path) 
Example #8
Source File: test_xarray.py    From dtale with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_convert():
    from dtale.views import startup
    from tests.dtale.test_replacements import replacements_data

    with app.test_client() as c:
        with ExitStack() as stack:
            data, dtypes, datasets, dataset_dim, settings = {}, {}, {}, {}, {}
            stack.enter_context(mock.patch("dtale.global_state.DATA", data))
            stack.enter_context(mock.patch("dtale.global_state.DTYPES", dtypes))
            stack.enter_context(mock.patch("dtale.global_state.DATASETS", datasets))
            stack.enter_context(
                mock.patch("dtale.global_state.DATASET_DIM", dataset_dim)
            )
            stack.enter_context(mock.patch("dtale.global_state.SETTINGS", settings))
            startup(URL, data=replacements_data(), data_id=c.port)

            resp = c.get(
                "/dtale/to-xarray/{}".format(c.port),
                query_string=dict(index=json.dumps(["a"])),
            )
            assert resp.status_code == 200
            assert c.port in datasets
            assert settings[c.port]["locked"] == ["a"] 
Example #9
Source File: test_replacements.py    From dtale with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_spaces(unittest):
    df = replacements_data()
    data_id, replacement_type = "1", "spaces"
    with ExitStack() as stack:
        stack.enter_context(mock.patch("dtale.global_state.DATA", {data_id: df}))

        builder = ColumnReplacement(data_id, "b", replacement_type, {})
        verify_builder(
            builder,
            lambda col: unittest.assertEqual(list(col.values), ["", np.nan, " - "]),
        )

        builder = ColumnReplacement(data_id, "b", replacement_type, {"value": "blah"})
        verify_builder(
            builder,
            lambda col: unittest.assertEqual(list(col.values), ["", "blah", " - "]),
        ) 
Example #10
Source File: test_views.py    From dtale with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_update_visibility(unittest):
    from dtale.views import build_dtypes_state

    df = pd.DataFrame([dict(a=1, b=2, c=3)])
    with app.test_client() as c:
        dtypes = {c.port: build_dtypes_state(df)}
        with ExitStack() as stack:
            stack.enter_context(mock.patch("dtale.global_state.DATA", {c.port: df}))
            stack.enter_context(mock.patch("dtale.global_state.DTYPES", dtypes))
            c.post(
                "/dtale/update-visibility/{}".format(c.port),
                data=dict(visibility=json.dumps({"a": True, "b": True, "c": False})),
            )
            unittest.assertEqual(
                [True, True, False], [col["visible"] for col in dtypes[c.port]]
            )
            c.post("/dtale/update-visibility/{}".format(c.port), data=dict(toggle="c"))
            unittest.assertEqual(
                [True, True, True], [col["visible"] for col in dtypes[c.port]]
            )

            resp = c.post("/dtale/update-visibility/-1", data=dict(toggle="foo"))
            assert "error" in json.loads(resp.data) 
Example #11
Source File: test_views.py    From dtale with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_delete_col():
    from dtale.views import build_dtypes_state

    df = pd.DataFrame([dict(a=1, b=2, c=3)])
    with app.test_client() as c:
        with ExitStack() as stack:
            data = {c.port: df}
            stack.enter_context(mock.patch("dtale.global_state.DATA", data))
            settings = {c.port: {"locked": ["a"]}}
            stack.enter_context(mock.patch("dtale.global_state.SETTINGS", settings))
            dtypes = {c.port: build_dtypes_state(df)}
            stack.enter_context(mock.patch("dtale.global_state.DTYPES", dtypes))
            c.get("/dtale/delete-col/{}/a".format(c.port))
            assert "a" not in data[c.port].columns
            assert (
                next((dt for dt in dtypes[c.port] if dt["name"] == "a"), None) is None
            )
            assert len(settings[c.port]["locked"]) == 0

            resp = c.get("/dtale/delete-col/-1/d")
            assert "error" in json.loads(resp.data) 
Example #12
Source File: test_app.py    From dtale with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_build_startup_url_and_app_root():
    from dtale.app import build_startup_url_and_app_root

    with ExitStack() as stack:
        stack.enter_context(mock.patch("dtale.app.JUPYTER_SERVER_PROXY", True))
        stack.enter_context(mock.patch("dtale.app.ACTIVE_PORT", 40000))
        stack.enter_context(mock.patch("dtale.app.ACTIVE_HOST", "localhost"))
        url, app_root = build_startup_url_and_app_root()

        assert url == "/user/{}/proxy/40000".format(getpass.getuser())
        assert app_root == "/user/{}/proxy/40000".format(getpass.getuser())
        url, app_root = build_startup_url_and_app_root("/test_route/")
        assert url == "/test_route/40000"
        assert app_root == "/test_route/40000"

    with ExitStack() as stack:
        stack.enter_context(mock.patch("dtale.app.JUPYTER_SERVER_PROXY", False))
        stack.enter_context(mock.patch("dtale.app.ACTIVE_PORT", 40000))
        stack.enter_context(mock.patch("dtale.app.ACTIVE_HOST", "localhost"))
        url, app_root = build_startup_url_and_app_root()
        assert url == "http://localhost:40000"
        assert app_root is None
        url, app_root = build_startup_url_and_app_root("/test_route/")
        assert url == "http:/localhost:40000/test_route/"
        assert app_root == "/test_route/" 
Example #13
Source File: test_views.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_rename_col():
    from dtale.views import build_dtypes_state

    df = pd.DataFrame([dict(a=1, b=2, c=3)])
    with app.test_client() as c:
        with ExitStack() as stack:
            data = {c.port: df}
            stack.enter_context(mock.patch("dtale.global_state.DATA", data))
            settings = {c.port: {"locked": ["a"]}}
            stack.enter_context(mock.patch("dtale.global_state.SETTINGS", settings))
            dtypes = {c.port: build_dtypes_state(df)}
            stack.enter_context(mock.patch("dtale.global_state.DTYPES", dtypes))
            c.get(
                "/dtale/rename-col/{}/a".format(c.port), query_string=dict(rename="d")
            )
            assert "a" not in data[c.port].columns
            assert (
                next((dt for dt in dtypes[c.port] if dt["name"] == "a"), None) is None
            )
            assert len(settings[c.port]["locked"]) == 1

            resp = c.get(
                "/dtale/rename-col/{}/d".format(c.port), query_string=dict(rename="b")
            )
            assert "error" in json.loads(resp.data)

            resp = c.get("/dtale/rename-col/-1/d", query_string=dict(rename="b"))
            assert "error" in json.loads(resp.data) 
Example #14
Source File: test_views.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_update_locked(unittest):
    from dtale.views import build_dtypes_state

    df = pd.DataFrame([dict(a=1, b=2, c=3)])
    with app.test_client() as c:
        data = {c.port: df}
        dtypes = {c.port: build_dtypes_state(df)}
        settings = {c.port: dict(locked=[])}
        with ExitStack() as stack:
            stack.enter_context(mock.patch("dtale.global_state.DATA", data))
            stack.enter_context(mock.patch("dtale.global_state.DTYPES", dtypes))
            stack.enter_context(mock.patch("dtale.global_state.SETTINGS", settings))

            c.get(
                "/dtale/update-locked/{}".format(c.port),
                query_string=dict(action="lock", col="c"),
            )
            unittest.assertEqual(["c"], settings[c.port]["locked"])
            assert data[c.port].columns[0] == "c"
            assert dtypes[c.port][0]["name"] == "c"

            c.get(
                "/dtale/update-locked/{}".format(c.port),
                query_string=dict(action="lock", col="c"),
            )
            unittest.assertEqual(["c"], settings[c.port]["locked"])
            assert data[c.port].columns[0] == "c"
            assert dtypes[c.port][0]["name"] == "c"

            c.get(
                "/dtale/update-locked/{}".format(c.port),
                query_string=dict(action="unlock", col="c"),
            )
            unittest.assertEqual([], settings[c.port]["locked"])
            assert data[c.port].columns[0] == "c"
            assert dtypes[c.port][0]["name"] == "c"

            resp = c.get("/dtale/update-locked/-1")
            assert "error" in json.loads(resp.data) 
Example #15
Source File: test_views.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_get_filter_info(unittest):
    with app.test_client() as c:
        with ExitStack() as stack:
            data_id = "1"
            context_vars = {
                "1": ["cat", "dog"],
                "2": 420346,
                "3": pd.Series(range(1000)),
                "4": "A" * 2000,
            }
            expected_return_value = [
                dict(name=k, value=str(v)[:1000]) for k, v in context_vars.items()
            ]
            stack.enter_context(mock.patch("dtale.global_state.DATA", {data_id: None}))
            stack.enter_context(
                mock.patch(
                    "dtale.global_state.CONTEXT_VARIABLES", {data_id: context_vars}
                )
            )
            response = c.get("/dtale/filter-info/{}".format(data_id))
            response_data = json.loads(response.data)
            assert response_data["success"]
            unittest.assertEqual(
                response_data["contextVars"],
                expected_return_value,
                "should match expected",
            )

    with app.test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(
                mock.patch("dtale.global_state.CONTEXT_VARIABLES", None)
            )
            response = c.get("dtale/filter-info/1")
            response_data = json.loads(response.data)
            assert response_data[
                "error"
            ], "An error should be returned since CONTEXT_VARIABLES is None" 
Example #16
Source File: test_views.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_jinja_output():
    import dtale.views as views

    df = pd.DataFrame([1, 2, 3])
    df, _ = views.format_data(df)
    url = "http://localhost.localdomain:40000"
    with build_app(url=url).test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(mock.patch("dtale.global_state.DATA", {c.port: df}))
            stack.enter_context(
                mock.patch(
                    "dtale.global_state.DTYPES", {c.port: views.build_dtypes_state(df)}
                )
            )
            stack.enter_context(mock.patch("dtale.global_state.DATA", {c.port: df}))
            response = c.get("/dtale/main/{}".format(c.port))
            assert 'span id="forkongithub"' not in str(response.data)
            response = c.get("/charts/{}".format(c.port))
            assert 'span id="forkongithub"' not in str(response.data)

    with build_app(url=url, github_fork=True).test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(mock.patch("dtale.global_state.DATA", {c.port: df}))
            stack.enter_context(
                mock.patch(
                    "dtale.global_state.DTYPES", {c.port: views.build_dtypes_state(df)}
                )
            )
            stack.enter_context(mock.patch("dtale.global_state.DATA", {c.port: df}))
            response = c.get("/dtale/main/{}".format(c.port))
            assert 'span id="forkongithub"' in str(response.data)
            response = c.get("/charts/{}".format(c.port))
            assert 'span id="forkongithub"' in str(response.data) 
Example #17
Source File: test_views.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_missing_js():
    with app.test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(
                mock.patch("dtale.global_state.DATA", {c.port: pd.DataFrame([1, 2, 3])})
            )
            stack.enter_context(mock.patch("os.listdir", mock.Mock(return_value=[])))
            response = c.get("/")
            assert response.status_code == 302 
Example #18
Source File: test_views.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_cleanup_error(unittest):
    with app.test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(
                mock.patch(
                    "dtale.global_state.cleanup", mock.Mock(side_effect=Exception)
                )
            )
            resp = c.get("/dtale/cleanup/1")
            assert "error" in json.loads(resp.data) 
Example #19
Source File: test_views.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_in_ipython_frontend(builtin_pkg):
    import dtale.views as views

    orig_import = __import__

    mock_ipython = mock.Mock()

    class zmq(object):
        __name__ = "zmq"

        def __init__(self):
            pass

    mock_ipython.get_ipython = lambda: zmq()

    def import_mock(name, *args, **kwargs):
        if name == "IPython":
            return mock_ipython
        return orig_import(name, *args, **kwargs)

    with ExitStack() as stack:
        stack.enter_context(
            mock.patch("{}.__import__".format(builtin_pkg), side_effect=import_mock)
        )
        assert views.in_ipython_frontend()

    def import_mock(name, *args, **kwargs):
        if name == "IPython":
            raise ImportError()
        return orig_import(name, *args, **kwargs)

    with ExitStack() as stack:
        stack.enter_context(
            mock.patch("{}.__import__".format(builtin_pkg), side_effect=import_mock)
        )
        assert not views.in_ipython_frontend() 
Example #20
Source File: test_views.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_update_formats():
    from dtale.views import build_dtypes_state

    settings = dict()
    df = pd.DataFrame([dict(a=1, b=2)])
    with app.test_client() as c:
        data = {c.port: df}
        dtypes = {c.port: build_dtypes_state(df)}
        with ExitStack() as stack:
            stack.enter_context(mock.patch("dtale.global_state.DATA", data))
            stack.enter_context(mock.patch("dtale.global_state.DTYPES", dtypes))
            stack.enter_context(mock.patch("dtale.global_state.SETTINGS", settings))
            response = c.get(
                "/dtale/update-formats/{}".format(c.port),
                query_string=dict(
                    all=False, col="a", format=json.dumps(dict(fmt="", style={}))
                ),
            )
            assert response.status_code == 200, "should return 200 response"
            assert "a" in settings[c.port]["formats"]

            c.get(
                "/dtale/update-formats/{}".format(c.port),
                query_string=dict(
                    all=True, col="a", format=json.dumps(dict(fmt="", style={}))
                ),
            )
            assert "b" in settings[c.port]["formats"]

    with app.test_client() as c:
        with ExitStack() as stack:
            stack.enter_context(mock.patch("dtale.global_state.DTYPES", {c.port: None}))
            response = c.get(
                "/dtale/update-formats/{}".format(c.port),
                query_string=dict(
                    all=True, col="a", format=json.dumps(dict(fmt="", style={}))
                ),
            )
            assert "error" in response.json 
Example #21
Source File: test_views.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_update_column_position():
    from dtale.views import build_dtypes_state

    df = pd.DataFrame([dict(a=1, b=2, c=3)])
    tests = [
        ("front", 0),
        ("front", 0),
        ("left", 0),
        ("back", -1),
        ("back", -1),
        ("left", -2),
        ("right", -1),
        ("right", -1),
    ]
    with app.test_client() as c:
        data = {c.port: df}
        dtypes = {c.port: build_dtypes_state(df)}
        with ExitStack() as stack:
            stack.enter_context(mock.patch("dtale.global_state.DATA", data))
            stack.enter_context(mock.patch("dtale.global_state.DTYPES", dtypes))
            for action, col_idx in tests:
                c.get(
                    "/dtale/update-column-position/{}".format(c.port),
                    query_string=dict(action=action, col="c"),
                )
                assert data[c.port].columns[col_idx] == "c"
                assert dtypes[c.port][col_idx]["name"] == "c"

            resp = c.get("/dtale/update-column-position/-1")
            assert "error" in json.loads(resp.data) 
Example #22
Source File: test_cli.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_loader_retrievers(builtin_pkg):
    from dtale.cli.loaders import get_py35_loader, get_py33_loader, get_py2_loader

    orig_import = __import__
    mock_importlib_util = mock.Mock()
    mock_importlib_machinery = mock.Mock()
    mock_imp = mock.Mock()

    def import_mock(name, *args, **kwargs):
        if name == "importlib.util":
            return mock_importlib_util
        if name == "importlib.machinery":
            return mock_importlib_machinery
        if name == "imp":
            return mock_imp
        return orig_import(name, *args, **kwargs)

    with ExitStack() as stack:
        stack.enter_context(
            mock.patch("{}.__import__".format(builtin_pkg), side_effect=import_mock)
        )

        assert get_py35_loader("custom.loaders", "loader.py") is not None
        assert mock_importlib_util.spec_from_file_location.called_once()
        mock_spec = mock_importlib_util.spec_from_file_location.return_value
        assert mock_importlib_util.module_from_spec.called_once()
        mock_loader = mock_spec.loader.return_value
        assert mock_loader.exec_module.called_once()
        assert get_py33_loader("custom.loaders", "loader.py") is not None
        assert mock_importlib_machinery.SourceFileLoader.called_once()
        mock_sourcefileloader = mock_importlib_machinery.SourceFileLoader.return_value
        assert mock_sourcefileloader.load_module.called_once()
        assert get_py2_loader("custom.loaders", "loader.py") is not None
        assert mock_imp.load_source.called_once() 
Example #23
Source File: test_replacements.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_iterative_imputers(unittest):
    df = replacements_data()
    data_id, replacement_type = "1", "imputer"
    with ExitStack() as stack:
        stack.enter_context(mock.patch("dtale.global_state.DATA", {data_id: df}))

        cfg = {"type": "iterative"}
        builder = ColumnReplacement(data_id, "d", replacement_type, cfg)
        verify_builder(
            builder, lambda col: unittest.assertEqual(list(col.values), [1.1, 2.05, 3])
        ) 
Example #24
Source File: test_replacements.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_simple_imputers(unittest):
    df = replacements_data()
    data_id, replacement_type = "1", "imputer"
    with ExitStack() as stack:
        stack.enter_context(mock.patch("dtale.global_state.DATA", {data_id: df}))

        cfg = {"type": "simple"}
        builder = ColumnReplacement(data_id, "d", replacement_type, cfg)
        verify_builder(
            builder, lambda col: unittest.assertEqual(list(col.values), [1.1, 2.05, 3])
        ) 
Example #25
Source File: test_cli.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_arctic_import_error(builtin_pkg):
    orig_import = __import__

    def import_mock(name, *args, **kwargs):
        if name.startswith("arctic"):
            raise ImportError()
        return orig_import(name, *args, **kwargs)

    with ExitStack() as stack:
        stack.enter_context(mock.patch("dtale.app.build_app", mock.Mock()))
        stack.enter_context(
            mock.patch("{}.__import__".format(builtin_pkg), side_effect=import_mock)
        )
        args = [
            "--port",
            "9999",
            "--arctic-host",
            "arctic_host",
            "--arctic-library",
            "arctic_lib",
            "--arctic-node",
            "arctic_node",
            "--arctic-start",
            "20000101",
            "--arctic-end",
            "20000102",
        ]
        with pytest.raises(ImportError) as error:
            script.main(args, standalone_mode=False)
        assert "In order to use the arctic loader you must install arctic!" in str(
            error
        ) 
Example #26
Source File: test_ipython.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_show(unittest):
    from dtale.app import show
    import dtale.global_state as global_state

    test_data = pd.DataFrame([dict(a=1, b=2)])
    with ExitStack() as stack:
        stack.enter_context(mock.patch("dtale.app.DtaleFlask.run", mock.Mock()))
        stack.enter_context(
            mock.patch("dtale.app.find_free_port", mock.Mock(return_value=9999))
        )
        stack.enter_context(
            mock.patch("socket.gethostname", mock.Mock(return_value="localhost"))
        )
        stack.enter_context(mock.patch("dtale.app.logger", mock.Mock()))
        stack.enter_context(
            mock.patch("dtale.views.in_ipython_frontend", mock.Mock(return_value=True))
        )

        get_calls = {"ct": 0}
        getter = namedtuple("get", "ok")

        def mock_requests_get(url, verify=True):
            if url.endswith("/health"):
                is_ok = get_calls["ct"] > 0
                get_calls["ct"] += 1
                return getter(is_ok)
            return getter(True)

        stack.enter_context(mock.patch("requests.get", mock_requests_get))
        instance = show(data=test_data, subprocess=False, name="foo")
        assert instance.__repr__() == ""
        assert instance.__str__() == ""
        instance.adjust_cell_dimensions(height=600)

    # cleanup
    global_state.cleanup() 
Example #27
Source File: test_column_builders.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_transform(unittest):
    def _data():
        for i in range(100):
            a = i % 5
            b = i % 3
            c = i % 4
            yield dict(a=a, b=b, c=c, i=i)

    df = pd.DataFrame(list(_data()))

    aggs = [
        "count",
        "nunique",
        "sum",
        "first",
        "last",
        "median",
        "min",
        "max",
        "std",
        "var",
        "mad",
        "prod",
    ]
    data_id, column_type = "1", "transform"
    i = 0
    with ExitStack() as stack:
        stack.enter_context(mock.patch("dtale.global_state.DATA", {data_id: df}))

        cfg = {"col": "i", "group": ["a"], "agg": "mean"}
        builder = ColumnBuilder(data_id, column_type, "Col{}".format(++i), cfg)
        verify_builder(
            builder, lambda col: col.values[0] == 47.5 and col.values[-1] == 51.5
        )

        for agg in aggs:
            cfg["agg"] = agg
            builder = ColumnBuilder(data_id, column_type, "Col{}".format(++i), cfg)
            verify_builder(builder, lambda col: len(col[col.isnull()]) == 0) 
Example #28
Source File: test_colab.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_failing_show_colab():
    from dtale.app import show

    with ExitStack() as stack:
        stack.enter_context(mock.patch("dtale.app.USE_COLAB", True))
        stack.enter_context(mock.patch("dtale.app.DtaleFlask.run", mock.Mock()))
        stack.enter_context(mock.patch("dtale.app.ACTIVE_PORT", 40000))
        stack.enter_context(mock.patch("dtale.app.ACTIVE_HOST", "localhost"))
        stack.enter_context(
            mock.patch("dtale.app.is_up", mock.Mock(return_value=False))
        )
        stack.enter_context(mock.patch("requests.get", mock.Mock()))
        instance = show(data=pd.DataFrame([1, 2, 3]))
        assert "http://localhost:40000" == instance._url 
Example #29
Source File: test_show_loaders.py    From dtale with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_show_json():
    import dtale

    json_path = "/../".join([os.path.dirname(__file__), "data/test_df.json"])

    with mock.patch("dtale.app.show", mock.Mock()):
        dtale.show_json(path=json_path)

    with open(json_path, "r") as f:
        json_txt = f.read()
        with ExitStack() as stack:
            stack.enter_context(mock.patch("dtale.app.show", mock.Mock()))

            class MockRequest(object):
                def __init__(self):
                    self.text = json_txt.encode() if PY3 else json_txt
                    self.status_code = 200

                def json(self):
                    return json.loads(json_txt)

            stack.enter_context(
                mock.patch("requests.get", mock.Mock(return_value=MockRequest()))
            )
            dtale.show_json(path="http://test-json", normalize=True)
            dtale.show_json(
                path="http://test-json", proxy="http://test-proxy", normalize=True
            )
            dtale.show_json(path="http://test-json") 
Example #30
Source File: oid_tfrecord_creation_test.py    From ros_tensorflow with Apache License 2.0 5 votes vote down vote up
def test_sharded_tfrecord_writes(self):
    with contextlib2.ExitStack() as tf_record_close_stack:
      output_tfrecords = oid_tfrecord_creation.open_sharded_output_tfrecords(
          tf_record_close_stack,
          os.path.join(tf.test.get_temp_dir(), 'test.tfrec'), 10)
      for idx in range(10):
        output_tfrecords[idx].write('test_{}'.format(idx))

    for idx in range(10):
      tf_record_path = '{}-{:05d}-of-00010'.format(
          os.path.join(tf.test.get_temp_dir(), 'test.tfrec'), idx)
      records = list(tf.python_io.tf_record_iterator(tf_record_path))
      self.assertAllEqual(records, ['test_{}'.format(idx)])