# Copyright 2024 The TensorTrade-NG Authors.## 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 Licensefrom__future__importannotationsimporttypingiftyping.TYPE_CHECKING:fromtypingimportOptionalfromtensortrade.envimportTradingEnv
[docs]classSchemeMixin:"""A mixin class that provides managed access to a :class:`TradingEnv` instance. This mixin ensures that any class inheriting from it has a `trading_env` attribute that is properly initialized before use. The `trading_env` attribute is managed through a property to enforce this initialization check. :param _trading_env: A :class:`TradingEnv` instance, defaults to None :type _trading_env: Optional[TradingEnv] """def__init__(self):self._trading_env:Optional[TradingEnv]=None@propertydeftrading_env(self)->TradingEnv:"""Provides access to the :class:`TradingEnv` instance. This property checks whether `_trading_env` has been initialized. If `_trading_env` is `None`, a `ValueError` is raised to prevent the use of an uninitialized :class:`TradingEnv`. :return: The initialized :class:`TradingEnv` instance. :rtype: TradingEnv :raises ValueError: If `_trading_env` is not initialized. """ifself._trading_envisNone:raiseValueError("Trading environment is not initialized. Set trading_env first.")returnself._trading_env@trading_env.setterdeftrading_env(self,value:TradingEnv):"""Sets the :class:`TradingEnv` instance. This setter allows for the initialization of the `_trading_env` attribute. :param value: The `TradingEnv` instance to be assigned to `_trading_env`. :type value: TradingEnv """self._trading_env=value