Skip to content

Commit f4a865e

Browse files
econdb context manager (#6512)
Co-authored-by: Henrique Joaquim <[email protected]>
1 parent b9c866c commit f4a865e

File tree

6 files changed

+52
-25
lines changed

6 files changed

+52
-25
lines changed

openbb_platform/providers/econdb/openbb_econdb/utils/helpers.py

+47-20
Original file line numberDiff line numberDiff line change
@@ -258,18 +258,24 @@
258258
"southeast_asia": ["KH", "ID", "LA", "MY", "PH", "SG", "TH", "VN"],
259259
}
260260

261-
INDICATORS_DESCRIPTIONS = json.load(
262-
(files("openbb_econdb.utils") / "indicators_descriptions.json").open()
263-
)
264-
MULTIPLIERS = json.load((files("openbb_econdb.utils") / "multipliers.json").open())
265-
SCALES = json.load((files("openbb_econdb.utils") / "scales.json").open())
266-
UNITS = json.load((files("openbb_econdb.utils") / "units.json").open())
267-
INDICATOR_COUNTRIES = json.load(
268-
(files("openbb_econdb.utils") / "indicator_countries.json").open()
269-
)
270-
SYMBOL_TO_INDICATOR = json.load(
271-
(files("openbb_econdb.utils") / "symbol_to_indicator.json").open()
272-
)
261+
with (files("openbb_econdb.utils") / "indicators_descriptions.json").open() as f:
262+
INDICATORS_DESCRIPTIONS = json.load(f)
263+
264+
with (files("openbb_econdb.utils") / "multipliers.json").open() as f:
265+
MULTIPLIERS = json.load(f)
266+
267+
with (files("openbb_econdb.utils") / "scales.json").open() as f:
268+
SCALES = json.load(f)
269+
270+
with (files("openbb_econdb.utils") / "units.json").open() as f:
271+
UNITS = json.load(f)
272+
273+
with (files("openbb_econdb.utils") / "symbol_to_indicator.json").open() as f:
274+
SYMBOL_TO_INDICATOR = json.load(f)
275+
276+
with (files("openbb_econdb.utils") / "indicator_countries.json").open() as f:
277+
INDICATOR_COUNTRIES = json.load(f)
278+
273279
HAS_COUNTRIES = {
274280
d: INDICATOR_COUNTRIES.get(d) != ["W00"] for d in INDICATORS_DESCRIPTIONS
275281
}
@@ -622,28 +628,44 @@ def update_symbol_to_indicator() -> None:
622628
"w", # type: ignore
623629
encoding="utf-8",
624630
) as f:
625-
indicators.set_index("short_ticker").sort_index()["symbol_root"].to_json(f)
631+
json_data = json.dumps(
632+
indicators.set_index("short_ticker")
633+
.sort_index()["symbol_root"]
634+
.to_dict()
635+
)
636+
f.write(json_data)
626637

627638
def update_multipliers() -> None:
628639
"""Update the unit multipliers."""
629640
with open( # type: ignore
630641
files("openbb_econdb.utils") / "multipliers.json", "w", encoding="utf-8"
631642
) as f:
632-
indicators.set_index("short_ticker").sort_index()["multiplier"].to_json(f)
643+
json_data = json.dumps(
644+
indicators.set_index("short_ticker")
645+
.sort_index()["multiplier"]
646+
.to_dict()
647+
)
648+
f.write(json_data)
633649

634650
def update_scales() -> None:
635651
"""Update the scales."""
636652
with open( # type: ignore
637653
files("openbb_econdb.utils") / "scales.json", "w", encoding="utf-8"
638654
) as f:
639-
indicators.set_index("short_ticker").sort_index()["scale"].to_json(f)
655+
json_data = json.dumps(
656+
indicators.set_index("short_ticker").sort_index()["scale"].to_dict()
657+
)
658+
f.write(json_data)
640659

641660
def update_units() -> None:
642661
"""Update the units."""
643662
with open( # type: ignore
644663
files("openbb_econdb.utils") / "units.json", "w", encoding="utf-8"
645664
) as f:
646-
indicators.set_index("short_ticker").sort_index()["currency"].to_json(f)
665+
json_data = json.dumps(
666+
indicators.set_index("short_ticker").sort_index()["currency"].to_dict()
667+
)
668+
f.write(json_data)
647669

648670
def update_descriptions() -> None:
649671
"""Update the indicator descriptions."""
@@ -656,7 +678,8 @@ def update_descriptions() -> None:
656678
"w",
657679
encoding="utf-8",
658680
) as f:
659-
json.dump(descriptions_dict, f)
681+
json_data = json.dumps(descriptions_dict)
682+
f.write(json_data)
660683

661684
def update_indicator_countries() -> None:
662685
"""Update the indicator countries."""
@@ -665,9 +688,13 @@ def update_indicator_countries() -> None:
665688
"w",
666689
encoding="utf-8",
667690
) as f:
668-
indicators[indicators["symbol_root"] != "[W00]"].groupby("symbol_root")[
669-
"iso"
670-
].apply(lambda x: x.sort_values().unique().tolist()).to_json(f)
691+
json_data = json.dumps(
692+
indicators[indicators["symbol_root"] != "[W00]"]
693+
.groupby("symbol_root")["iso"]
694+
.apply(lambda x: x.sort_values().unique().tolist())
695+
.to_dict()
696+
)
697+
f.write(json_data)
671698

672699
update_symbol_to_indicator()
673700
update_multipliers()

openbb_platform/providers/econdb/openbb_econdb/utils/indicator_countries.json

+1-1
Large diffs are not rendered by default.

openbb_platform/providers/econdb/openbb_econdb/utils/multipliers.json

+1-1
Large diffs are not rendered by default.

openbb_platform/providers/econdb/openbb_econdb/utils/scales.json

+1-1
Large diffs are not rendered by default.

openbb_platform/providers/econdb/openbb_econdb/utils/symbol_to_indicator.json

+1-1
Large diffs are not rendered by default.

openbb_platform/providers/econdb/openbb_econdb/utils/units.json

+1-1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)