ronantakizawa/codereview-bench huggingface dataset:
Repository: Avaiga/taipy
File: taipy/_cli/_scaffold_cli.py (Python)

Code before:
    _COMMAND_NAME = "create"
    _ARGUMENTS = ["--template"]

    @classmethod
    def create_parser(cls):
        create_parser = _TaipyParser._add_subparser(
            cls._COMMAND_NAME,
            help="Create a new Taipy application using pre-defined templates.",
        )
        create_parser.add_argument(
            "--template",
            choices=list(cls._TEMPLATE_MAP.keys()),
            default="default",
            help="The Taipy template to create new application.",
        )

    @classmethod
    def handle_command(cls):
        args = cls._parse_arguments()
        if not args:
            return
        try:
            cookiecutter(cls._TEMPLATE_MAP[args.template])
        except OutputDirExistsException as err:
            error_msg = f"{str(err)[7:]}. Please remove the existing directory or provide a new folder name."
            cls._logger.error(error_msg)
            sys.exit(1)
        sys.exit(0)


Review comment (suggestion):
Can't we simply get rid of the cookie-cutter exception?

Plus I believe we don't need to use the taipy logger here. There is no application running. We just exit the command line with a message.

Code after:
    _COMMAND_NAME = "create"
    _ARGUMENTS = ["--template"]

    @classmethod
    def create_parser(cls):
        create_parser = _TaipyParser._add_subparser(
            cls._COMMAND_NAME,
            help="Create a new Taipy application using pre-defined templates.",
        )
        create_parser.add_argument(
            "--template",
            choices=list(cls._TEMPLATE_MAP.keys()),
            default="default",
            help="The Taipy template to create new application.",
        )

    @classmethod
    def handle_command(cls):
        args = cls._parse_arguments()
        if not args:
            return
        try:
            cookiecutter(cls._TEMPLATE_MAP[args.template])
        except OutputDirExistsException as err:
            error_msg = f"{str(err)}. Please remove the existing directory or provide a new folder name."
            print(error_msg)  # noqa: T201
            sys.exit(1)
        sys.exit(0)
Repository: Avaiga/taipy
File: taipy/_cli/_scaffold_cli.py (Python)

Code before:
    _ARGUMENTS = ["--template"]

    @classmethod
    def create_parser(cls):
        create_parser = _TaipyParser._add_subparser(
            cls._COMMAND_NAME,
            help="Create a new Taipy application using pre-defined templates.",
        )
        create_parser.add_argument(
            "--template",
            choices=list(cls._TEMPLATE_MAP.keys()),
            default="default",
            help="The Taipy template to create new application.",
        )

    @classmethod
    def handle_command(cls):
        args = cls._parse_arguments()
        if not args:
            return
        try:
            cookiecutter(cls._TEMPLATE_MAP[args.template])
        except OutputDirExistsException as err:
           error_msg = f"{str(err)}. Please remove the existing directory or provide a new folder name."
           print(error_msg)
           sys.exit(1)
        sys.exit(0)


Review comment (suggestion):
```suggestion
            error_msg = f"{str(err)}. Please remove the existing directory or provide a new folder name."
            print(error_msg)
            sys.exit(1)
```

Code after:
    _ARGUMENTS = ["--template"]

    @classmethod
    def create_parser(cls):
        create_parser = _TaipyParser._add_subparser(
            cls._COMMAND_NAME,
            help="Create a new Taipy application using pre-defined templates.",
        )
        create_parser.add_argument(
            "--template",
            choices=list(cls._TEMPLATE_MAP.keys()),
            default="default",
            help="The Taipy template to create new application.",
        )

    @classmethod
    def handle_command(cls):
        args = cls._parse_arguments()
        if not args:
            return
        try:
            cookiecutter(cls._TEMPLATE_MAP[args.template])
        except OutputDirExistsException as err:
            error_msg = f"{str(err)}. Please remove the existing directory or provide a new folder name."
            print(error_msg)  # noqa: T201
            sys.exit(1)
        sys.exit(0)
Repository: Avaiga/taipy
File: doc/gui/examples/controls/metric-value-format.py (Python)

Code before:
# Copyright 2021-2024 Avaiga Private Limited
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
# -----------------------------------------------------------------------------------------
# To execute this script, make sure that the taipy-gui package is installed in your
# Python environment and run:
#     python <script>
# -----------------------------------------------------------------------------------------
from taipy.gui import Gui

delta_value = 20

# format & delta_format are used to format the value and delta value respectively.
# They use the printf syntax.

page = """
<|90|metric|delta={delta_value}|format=%d km/h|delta_format=%dkm/h|>
"""


Gui(page).run()



Review comment (suggestion):
```suggestion
<|90|metric|delta={delta_value}|format=%d km/h|delta_format=%d km/h|>
```

Code after:
# Copyright 2021-2024 Avaiga Private Limited
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
# -----------------------------------------------------------------------------------------
# To execute this script, make sure that the taipy-gui package is installed in your
# Python environment and run:
#     python <script>
# -----------------------------------------------------------------------------------------
from taipy.gui import Gui

value = 50
delta_value = 20

# format & delta_format are used to format the value and delta value respectively.
# They use the printf syntax.

page = """
<|{value}|metric|delta={delta_value}|format=%d km/h|delta_format=%d km/h|>
"""


Gui(page).run()
Repository: Avaiga/taipy
File: taipy/core/reason/_reason_factory.py (Python)

Code before:
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

from ..data.data_node import DataNodeId


def _build_data_node_is_being_edited_reason(dn_id: DataNodeId) -> str:
    return f"DataNode {dn_id} is being edited"


def _build_data_node_is_not_written(dn_id: DataNodeId) -> str:
    return f"DataNode {dn_id} is not written"


def _build_not_submittable_entity_reason(entity_id: str) -> str:
    return f"Entity {entity_id} is not a submittable entity"


def _build_config_can_not_create_reason(config_id: str) -> str:
    return f'Object "{config_id}" is not a valid config to be created'


def _build_not_global_datanode_config_reason(config_id: str) -> str:
    return f'Data node config "{config_id}" does not have GLOBAL scope'


Review comment (bug):
```suggestion
def _build_wrong_config_type_reason(config_id: str) -> str:
```

Code after:
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

from typing import Optional

from ..data.data_node import DataNodeId


def _build_data_node_is_being_edited_reason(dn_id: DataNodeId) -> str:
    return f"DataNode {dn_id} is being edited"


def _build_data_node_is_not_written(dn_id: DataNodeId) -> str:
    return f"DataNode {dn_id} is not written"


def _build_not_submittable_entity_reason(entity_id: str) -> str:
    return f"Entity {entity_id} is not a submittable entity"


def _build_wrong_config_type_reason(config_id: str, config_type: Optional[str]) -> str:
    if config_type:
        return f'Object "{config_id}" must be a valid {config_type}'

    return f'Object "{config_id}" is not a valid config to be created'


def _build_not_global_scope_reason(config_id: str) -> str:
    return f'Data node config "{config_id}" does not have GLOBAL scope'
Repository: Avaiga/taipy
File: taipy/core/reason/_reason_factory.py (Python)

Code before:
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

from ..data.data_node import DataNodeId


def _build_data_node_is_being_edited_reason(dn_id: DataNodeId) -> str:
    return f"DataNode {dn_id} is being edited"


def _build_data_node_is_not_written(dn_id: DataNodeId) -> str:
    return f"DataNode {dn_id} is not written"


def _build_not_submittable_entity_reason(entity_id: str) -> str:
    return f"Entity {entity_id} is not a submittable entity"


def _build_config_can_not_create_reason(config_id: str) -> str:
    return f'Object "{config_id}" is not a valid config to be created'


def _build_not_global_datanode_config_reason(config_id: str) -> str:
    return f'Data node config "{config_id}" does not have GLOBAL scope'


Review comment (suggestion):
```suggestion
    return f'Object "{config_id}" is not a valid config.'
```

Code after:
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

from typing import Optional

from ..data.data_node import DataNodeId


def _build_data_node_is_being_edited_reason(dn_id: DataNodeId) -> str:
    return f"DataNode {dn_id} is being edited"


def _build_data_node_is_not_written(dn_id: DataNodeId) -> str:
    return f"DataNode {dn_id} is not written"


def _build_not_submittable_entity_reason(entity_id: str) -> str:
    return f"Entity {entity_id} is not a submittable entity"


def _build_wrong_config_type_reason(config_id: str, config_type: Optional[str]) -> str:
    if config_type:
        return f'Object "{config_id}" must be a valid {config_type}'

    return f'Object "{config_id}" is not a valid config to be created'


def _build_not_global_scope_reason(config_id: str) -> str:
    return f'Data node config "{config_id}" does not have GLOBAL scope'
Repository: Avaiga/taipy
File: taipy/core/reason/_reason_factory.py (Python)

Code before:
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

from ..data.data_node import DataNodeId


def _build_data_node_is_being_edited_reason(dn_id: DataNodeId) -> str:
    return f"DataNode {dn_id} is being edited"


def _build_data_node_is_not_written(dn_id: DataNodeId) -> str:
    return f"DataNode {dn_id} is not written"


def _build_not_submittable_entity_reason(entity_id: str) -> str:
    return f"Entity {entity_id} is not a submittable entity"


def _build_config_can_not_create_reason(config_id: str) -> str:
    return f'Object "{config_id}" is not a valid config to be created'


def _build_not_global_datanode_config_reason(config_id: str) -> str:
    return f'Data node config "{config_id}" does not have GLOBAL scope'


Review comment (suggestion):
```suggestion
def _build_not_global_scope_reason(config_id: str) -> str:
```

Code after:
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

from typing import Optional

from ..data.data_node import DataNodeId


def _build_data_node_is_being_edited_reason(dn_id: DataNodeId) -> str:
    return f"DataNode {dn_id} is being edited"


def _build_data_node_is_not_written(dn_id: DataNodeId) -> str:
    return f"DataNode {dn_id} is not written"


def _build_not_submittable_entity_reason(entity_id: str) -> str:
    return f"Entity {entity_id} is not a submittable entity"


def _build_wrong_config_type_reason(config_id: str, config_type: Optional[str]) -> str:
    if config_type:
        return f'Object "{config_id}" must be a valid {config_type}'

    return f'Object "{config_id}" is not a valid config to be created'


def _build_not_global_scope_reason(config_id: str) -> str:
    return f'Data node config "{config_id}" does not have GLOBAL scope'
Repository: Avaiga/taipy
File: taipy/gui/state.py (Python)

Code before:
        _attrsetter(self, name, val)

    def broadcast(self, name: str, value: t.Any):
        """Update a variable on all clients.

        All connected clients will receive an update of the variable called *name* with the
        provided value, even if it is not shared.

        Arguments:
            name (str): The variable name to update.
            value (Any): The new variable value.
        """
        gui: "Gui" = super().__getattribute__(State.__gui_attr)
        with self._set_context(gui):
            encoded_name = gui._bind_var(name)
            gui._broadcast_all_clients(encoded_name, value)

    def __enter__(self):
        super().__getattribute__(State.__attrs[0]).__enter__()
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        return super().__getattribute__(State.__attrs[0]).__exit__(exc_type, exc_value, traceback)

    def set_favicon(self, favicon_path: t.Union[str, Path]):
        """Change the favicon for a specific client.

        This function dynamically changes the favicon of Taipy GUI pages for a specific client.
        favicon_path can be an URL (relative or not) or a file path.
        The *favicon* parameter to `(Gui.)run()^` can also be used to change the favicon when the application starts.
        """
        super().__getattribute__(State.__gui_attr).set_favicon(favicon_path, self)


Review comment (suggestion):
```suggestion
        """Change the favicon for the client of this state.
```

Code after:
        _attrsetter(self, name, val)

    def broadcast(self, name: str, value: t.Any):
        """Update a variable on all clients.

        All connected clients will receive an update of the variable called *name* with the
        provided value, even if it is not shared.

        Arguments:
            name (str): The variable name to update.
            value (Any): The new variable value.
        """
        gui: "Gui" = super().__getattribute__(State.__gui_attr)
        with self._set_context(gui):
            encoded_name = gui._bind_var(name)
            gui._broadcast_all_clients(encoded_name, value)

    def __enter__(self):
        super().__getattribute__(State.__attrs[0]).__enter__()
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        return super().__getattribute__(State.__attrs[0]).__exit__(exc_type, exc_value, traceback)

    def set_favicon(self, favicon_path: t.Union[str, Path]):
        """Change the favicon for the client of this state.

        This function dynamically changes the favicon of Taipy GUI pages for a specific client.
        favicon_path can be an URL (relative or not) or a file path.
        TODO The *favicon* parameter to `(Gui.)run()^` can also be used to change
         the favicon when the application starts.
        """
        super().__getattribute__(State.__gui_attr).set_favicon(favicon_path, self)
Repository: Avaiga/taipy
File: taipy/gui/state.py (Python)

Code before:
    def broadcast(self, name: str, value: t.Any):
        """Update a variable on all clients.

        All connected clients will receive an update of the variable called *name* with the
        provided value, even if it is not shared.

        Arguments:
            name (str): The variable name to update.
            value (Any): The new variable value.
        """
        gui: "Gui" = super().__getattribute__(State.__gui_attr)
        with self._set_context(gui):
            encoded_name = gui._bind_var(name)
            gui._broadcast_all_clients(encoded_name, value)

    def __enter__(self):
        super().__getattribute__(State.__attrs[0]).__enter__()
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        return super().__getattribute__(State.__attrs[0]).__exit__(exc_type, exc_value, traceback)

    def set_favicon(self, favicon_path: t.Union[str, Path]):
        """Change the favicon for a specific client.

        This function dynamically changes the favicon of Taipy GUI pages for a specific client.
        favicon_path can be an URL (relative or not) or a file path.
        The *favicon* parameter to `(Gui.)run()^` can also be used to change the favicon when the application starts.
        """
        super().__getattribute__(State.__gui_attr).set_favicon(favicon_path, self)


Review comment (suggestion):
```suggestion
        This function dynamically changes the favicon of Taipy GUI pages for this client.
```

Code after:
    def broadcast(self, name: str, value: t.Any):
        """Update a variable on all clients.

        All connected clients will receive an update of the variable called *name* with the
        provided value, even if it is not shared.

        Arguments:
            name (str): The variable name to update.
            value (Any): The new variable value.
        """
        gui: "Gui" = super().__getattribute__(State.__gui_attr)
        with self._set_context(gui):
            encoded_name = gui._bind_var(name)
            gui._broadcast_all_clients(encoded_name, value)

    def __enter__(self):
        super().__getattribute__(State.__attrs[0]).__enter__()
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        return super().__getattribute__(State.__attrs[0]).__exit__(exc_type, exc_value, traceback)

    def set_favicon(self, favicon_path: t.Union[str, Path]):
        """Change the favicon for the client of this state.

        This function dynamically changes the favicon of Taipy GUI pages for a specific client.
        favicon_path can be an URL (relative or not) or a file path.
        TODO The *favicon* parameter to `(Gui.)run()^` can also be used to change
         the favicon when the application starts.
        """
        super().__getattribute__(State.__gui_attr).set_favicon(favicon_path, self)
Repository: Avaiga/taipy
File: taipy/core/reason/reason.py (Python)

Code before:

    def __repr__(self) -> str:
        return self._reason

    def __hash__(self) -> int:
        return hash(self._reason)

    def __eq__(self, value: Any) -> bool:
        return isinstance(value, Reason) and value._reason == self._reason


class _DataNodeReasonMixin:
    def __init__(self, datanode_id: str):
        self.datanode_id = datanode_id

    @property
    def datanode(self):
        from ..data._data_manager_factory import _DataManagerFactory

        return _DataManagerFactory._build_manager()._get(self.datanode_id)


class DataNodeEditInProgress(Reason, _DataNodeReasonMixin):
    """
    DataNodeEditInProgress holds the reason a `DataNode^` is being edited,
    which prevents specific actions from being performed.

    Attributes:
        datanode_id (str): The identifier of the `DataNode^`.
    """

    def __init__(self, datanode_id: str):
        Reason.__init__(self, f"DataNode {datanode_id} is being edited")
        _DataNodeReasonMixin.__init__(self, datanode_id)


class DataNodeIsNotWritten(Reason, _DataNodeReasonMixin):
    """
    DataNodeIsNotWritten holds the reason a `DataNode^` is not yet written,
    which prevents specific actions from being performed.

    Attributes:
        datanode_id (str): The identifier of the `DataNode^`.
    """

    def __init__(self, datanode_id: str):
        Reason.__init__(self, f"DataNode {datanode_id} is not written")
        _DataNodeReasonMixin.__init__(self, datanode_id)


class EntityIsNotSubmittableEntity(Reason):

Review comment (suggestion):
```suggestion
    A `DataNode^` is being edited, which prevents specific actions from being performed.
```

Code after:

    def __init__(self, reason: str):
        self._reason = reason

    def __str__(self) -> str:
        return self._reason

    def __repr__(self) -> str:
        return self._reason

    def __hash__(self) -> int:
        return hash(self._reason)

    def __eq__(self, value: Any) -> bool:
        return isinstance(value, Reason) and value._reason == self._reason


class _DataNodeReasonMixin:
    def __init__(self, datanode_id: str):
        self.datanode_id = datanode_id

    @property
    def datanode(self):
        from ..data._data_manager_factory import _DataManagerFactory

        return _DataManagerFactory._build_manager()._get(self.datanode_id)


class DataNodeEditInProgress(Reason, _DataNodeReasonMixin):
    """
    A `DataNode^` is being edited, which prevents specific actions from being performed.

    Attributes:
        datanode_id (str): The identifier of the `DataNode^`.
    """

    def __init__(self, datanode_id: str):
        Reason.__init__(self, f"DataNode {datanode_id} is being edited")
        _DataNodeReasonMixin.__init__(self, datanode_id)


class DataNodeIsNotWritten(Reason, _DataNodeReasonMixin):
    """
    A `DataNode^` has never been written, which prevents specific actions from being performed.

    Attributes:
        datanode_id (str): The identifier of the `DataNode^`.
    """

    def __init__(self, datanode_id: str):
        Reason.__init__(self, f"DataNode {datanode_id} is not written")