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
11 changes: 8 additions & 3 deletions beanprice/price.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,17 @@ def parse_single_source(source: str) -> PriceSource:
Raises:
ValueError: If invalid.
"""
match = re.match(r"([a-zA-Z]+[a-zA-Z0-9\._]+)/(\^?)([a-zA-Z0-9:=_\-\.\(\)]+)$", source)
match = re.match(
r"([a-zA-Z]+[a-zA-Z0-9\._]+)/(\^*)([a-zA-Z0-9:=_\-\.\(\)\^]+)$", source
)
if not match:
raise ValueError('Invalid source name: "{}"'.format(source))
short_module_name, invert, symbol = match.groups()
short_module_name, carets, symbol = match.groups()
module = import_source(short_module_name)
return PriceSource(module, symbol, bool(invert))
invert = len(carets) % 2 == 1
# Every pair of carets is replaced by a single caret in the ticker.
actual_symbol = ("^" * (len(carets) // 2)) + symbol
return PriceSource(module, actual_symbol, invert)


def import_source(module_name: str):
Expand Down
12 changes: 12 additions & 0 deletions beanprice/price_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,18 @@ def test_source_valid(self):
psource = price.parse_single_source("yahoo/CNYUSD=X")
self.assertEqual(PS(yahoo, "CNYUSD=X", False), psource)

psource = price.parse_single_source("yahoo/^CNYUSD=X")
self.assertEqual(PS(yahoo, "CNYUSD=X", True), psource)

psource = price.parse_single_source("yahoo/^^GSPC")
self.assertEqual(PS(yahoo, "^GSPC", False), psource)

psource = price.parse_single_source("yahoo/^^^GSPC")
self.assertEqual(PS(yahoo, "^GSPC", True), psource)

psource = price.parse_single_source("yahoo/TICK^ER")
self.assertEqual(PS(yahoo, "TICK^ER", False), psource)

# Make sure that an invalid name at the tail doesn't succeed.
with self.assertRaises(ValueError):
psource = price.parse_single_source("yahoo/CNYUSD&X")
Expand Down