Clean up unclosed file handles
Close all file handles that are left dangling to avoid warnings such as ``` ResourceWarning: unclosed file <_io.TextIOWrapper name='...' mode='r' encoding='UTF-8'> params = json_load(filename.open('r')) ```
This commit is contained in:
parent
96f99699e0
commit
f7926083ca
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
import io
|
import io
|
||||||
import logging
|
import logging
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
@ -64,10 +63,11 @@ class HyperoptTools():
|
|||||||
'export_time': datetime.now(timezone.utc),
|
'export_time': datetime.now(timezone.utc),
|
||||||
}
|
}
|
||||||
logger.info(f"Dumping parameters to {filename}")
|
logger.info(f"Dumping parameters to {filename}")
|
||||||
rapidjson.dump(final_params, filename.open('w'), indent=2,
|
with filename.open('w') as f:
|
||||||
default=hyperopt_serializer,
|
rapidjson.dump(final_params, f, indent=2,
|
||||||
number_mode=rapidjson.NM_NATIVE | rapidjson.NM_NAN
|
default=hyperopt_serializer,
|
||||||
)
|
number_mode=rapidjson.NM_NATIVE | rapidjson.NM_NAN
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def try_export_params(config: Dict[str, Any], strategy_name: str, params: Dict):
|
def try_export_params(config: Dict[str, Any], strategy_name: str, params: Dict):
|
||||||
|
@ -381,7 +381,8 @@ class HyperStrategyMixin(object):
|
|||||||
if filename.is_file():
|
if filename.is_file():
|
||||||
logger.info(f"Loading parameters from file {filename}")
|
logger.info(f"Loading parameters from file {filename}")
|
||||||
try:
|
try:
|
||||||
params = json_load(filename.open('r'))
|
with filename.open('r') as f:
|
||||||
|
params = json_load(f)
|
||||||
if params.get('strategy_name') != self.__class__.__name__:
|
if params.get('strategy_name') != self.__class__.__name__:
|
||||||
raise OperationalException('Invalid parameter file provided.')
|
raise OperationalException('Invalid parameter file provided.')
|
||||||
return params
|
return params
|
||||||
|
@ -209,7 +209,8 @@ def test_export_params(tmpdir):
|
|||||||
|
|
||||||
assert filename.is_file()
|
assert filename.is_file()
|
||||||
|
|
||||||
content = rapidjson.load(filename.open('r'))
|
with filename.open('r') as f:
|
||||||
|
content = rapidjson.load(f)
|
||||||
assert content['strategy_name'] == 'StrategyTestV2'
|
assert content['strategy_name'] == 'StrategyTestV2'
|
||||||
assert 'params' in content
|
assert 'params' in content
|
||||||
assert "buy" in content["params"]
|
assert "buy" in content["params"]
|
||||||
|
@ -62,8 +62,8 @@ def test_load_strategy(default_conf, result):
|
|||||||
|
|
||||||
|
|
||||||
def test_load_strategy_base64(result, caplog, default_conf):
|
def test_load_strategy_base64(result, caplog, default_conf):
|
||||||
with (Path(__file__).parents[2] / 'freqtrade/templates/sample_strategy.py').open("rb") as file:
|
filepath = Path(__file__).parents[2] / 'freqtrade/templates/sample_strategy.py'
|
||||||
encoded_string = urlsafe_b64encode(file.read()).decode("utf-8")
|
encoded_string = urlsafe_b64encode(filepath.read_bytes()).decode("utf-8")
|
||||||
default_conf.update({'strategy': 'SampleStrategy:{}'.format(encoded_string)})
|
default_conf.update({'strategy': 'SampleStrategy:{}'.format(encoded_string)})
|
||||||
|
|
||||||
strategy = StrategyResolver.load_strategy(default_conf)
|
strategy = StrategyResolver.load_strategy(default_conf)
|
||||||
|
Loading…
Reference in New Issue
Block a user