Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion qlib/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,10 @@ def fname_to_code(fname: str):

prefix = "_qlib_"
if fname.startswith(prefix):
fname = fname.lstrip(prefix)
# NOTE: use slicing rather than ``lstrip(prefix)``; ``str.lstrip`` strips
# any leading characters contained in ``prefix`` (e.g. it would turn
# "_qlib_lpt1" into "pt1"), not the prefix as a whole.
fname = fname[len(prefix) :]
return fname


Expand Down
17 changes: 17 additions & 0 deletions tests/misc/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from qlib.constant import REG_CN, REG_US, REG_TW
from qlib.utils.time import cal_sam_minute as cal_sam_minute_new, get_min_cal, CN_TIME, US_TIME, TW_TIME
from qlib.utils.data import guess_horizon
from qlib.utils import code_to_fname, fname_to_code

REG_MAP = {REG_CN: CN_TIME, REG_US: US_TIME, REG_TW: TW_TIME}

Expand Down Expand Up @@ -127,6 +128,22 @@ def test_guess_horizon(self):
result = guess_horizon(label)
assert result == 5


class FileNameUtils(TestCase):
def test_fname_code_round_trip(self):
# code_to_fname only prefixes reserved Windows device names; fname_to_code
# must strip the whole "_qlib_" prefix, not individual characters.
# exists_qlib_data() lowercases the directory name before converting back,
# and "lpt*" begins with characters that are also in the prefix.
for code in ["CON", "PRN", "AUX", "NUL", "COM1", "LPT1", "LPT9"]:
fname = code_to_fname(code)
self.assertEqual(fname_to_code(fname.lower()), code.lower())

# a name whose body consists only of prefix characters must survive
self.assertEqual(fname_to_code("_qlib_lll"), "lll")
# plain codes pass through unchanged
self.assertEqual(fname_to_code("AAPL"), "AAPL")

label = ["Ref($close, -1) / Ref($close, -1) - 1"]
result = guess_horizon(label)
assert result == 1
Expand Down