Python traitlets.Integer() Examples

The following are 2 code examples of traitlets.Integer(). 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 traitlets , or try the search function .
Example #1
Source File: test_db_backend.py    From dask-gateway with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cluster_options():
    config = Config()
    config.InProcessBackend.cluster_config_class = MyClusterConfig
    config.InProcessBackend.cluster_options = options.Options(
        options.Integer("option_one", default=1, min=1, max=4, target="option_one_b"),
        options.Select("option_two", options=[("small", 1.5), ("large", 15)]),
    )
    async with temp_gateway(config=config) as g:
        async with g.gateway_client() as gateway:
            # Create with no parameters
            async with gateway.new_cluster() as cluster:
                report = await gateway.get_cluster(cluster.name)
                assert report.options == {"option_one": 1, "option_two": "small"}

            # Create with parameters
            async with gateway.new_cluster(option_two="large") as cluster:
                report = await gateway.get_cluster(cluster.name)
                assert report.options == {"option_one": 1, "option_two": "large"}

            # With options object
            opts = await gateway.cluster_options()
            opts.option_one = 2
            async with gateway.new_cluster(opts, option_two="large") as cluster:
                report = await gateway.get_cluster(cluster.name)
                assert report.options == {"option_one": 2, "option_two": "large"}

            # Bad parameters
            with pytest.raises(TypeError):
                await gateway.new_cluster(cluster_options=10)

            with pytest.raises(ValueError) as exc:
                await gateway.new_cluster(option_two="medium")
            assert "option_two" in str(exc.value) 
Example #2
Source File: test_db_backend.py    From dask-gateway with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def test_cluster_options_client_config(monkeypatch):
    monkeypatch.setenv("TEST_OPTION_TWO", "large")

    config = Config()
    config.InProcessBackend.cluster_config_class = MyClusterConfig
    config.InProcessBackend.cluster_options = options.Options(
        options.Integer("option_one", default=1, min=1, max=4, target="option_one_b"),
        options.Select("option_two", options=[("small", 1.5), ("large", 15)]),
    )

    async with temp_gateway(config=config) as g:
        async with g.gateway_client() as gateway:
            with dask.config.set(gateway__cluster__options={"option_one": 2}):
                # Local config can override
                opts = await gateway.cluster_options()
                assert opts.option_one == 2
                assert opts.option_two == "small"
                # Without local config, uses server-side defaults
                opts = await gateway.cluster_options(use_local_defaults=False)
                assert opts.option_one == 1
                assert opts.option_two == "small"

            with dask.config.set(
                gateway__cluster__options={"option_two": "{TEST_OPTION_TWO}"}
            ):
                # Values can format from environment variables
                opts = await gateway.cluster_options()
                assert opts.option_one == 1
                assert opts.option_two == "large"

            with dask.config.set(
                gateway__cluster__options={
                    "option_two": "{TEST_OPTION_TWO}",
                    "option_one": 3,
                }
            ):
                # Defaults are merged with kwargs to new_cluster
                async with gateway.new_cluster(option_one=2) as cluster:
                    report = await gateway.get_cluster(cluster.name)
                    assert report.options == {"option_one": 2, "option_two": "large"}

                # If passing `cluster_options`, defaults are assumed already applied
                opts = await gateway.cluster_options()
                opts.option_two = "small"
                async with gateway.new_cluster(opts) as cluster:
                    report = await gateway.get_cluster(cluster.name)
                    assert report.options == {"option_one": 3, "option_two": "small"}