Skip to content

Commit e179b11

Browse files
committed
FIX: Fix passing map_symbols to Transcoder
1 parent d6c5306 commit e179b11

File tree

3 files changed

+34
-44
lines changed

3 files changed

+34
-44
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 0.22.1 - TBD
4+
5+
#### Bug fixes
6+
- Fixed an issue where `DBNStore.to_csv` and `DBNStore.to_json` were mapping symbols even when `map_symbols` was set to `False`
7+
38
## 0.22.0 - 2023-10-23
49

510
#### Enhancements

databento/common/dbnstore.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
from databento.common.error import BentoError
3737
from databento.common.iterator import chunk
3838
from databento.common.symbology import InstrumentMap
39-
from databento.common.symbology import SymbolInterval
4039
from databento.common.validation import validate_enum
4140
from databento.common.validation import validate_file_write_path
4241
from databento.common.validation import validate_maybe_enum
@@ -812,23 +811,13 @@ def to_csv(
812811
raise ValueError("a schema must be specified for mixed DBN data")
813812
schema = self.schema
814813

815-
record_type = SCHEMA_STRUCT_MAP[schema]
816-
record_iter = filter(lambda r: isinstance(r, record_type), self)
817-
818-
if map_symbols:
819-
self._instrument_map.insert_metadata(self.metadata)
820-
symbol_map = self._instrument_map._data
821-
else:
822-
symbol_map = None
823-
824814
with open(path, "xb") as output:
825815
self._transcode(
826816
output=output,
827-
records_iter=record_iter,
828817
encoding=Encoding.CSV,
829818
pretty_px=pretty_px,
830819
pretty_ts=pretty_ts,
831-
symbol_map=symbol_map,
820+
map_symbols=map_symbols,
832821
compression=compression,
833822
schema=schema,
834823
)
@@ -1025,23 +1014,13 @@ def to_json(
10251014
raise ValueError("a schema must be specified for mixed DBN data")
10261015
schema = self.schema
10271016

1028-
record_type = SCHEMA_STRUCT_MAP[schema]
1029-
record_iter = filter(lambda r: isinstance(r, record_type), self)
1030-
1031-
if map_symbols:
1032-
self._instrument_map.insert_metadata(self.metadata)
1033-
symbol_map = self._instrument_map._data
1034-
else:
1035-
symbol_map = None
1036-
10371017
with open(path, "xb") as output:
10381018
self._transcode(
10391019
output=output,
1040-
records_iter=record_iter,
10411020
encoding=Encoding.JSON,
10421021
pretty_px=pretty_px,
10431022
pretty_ts=pretty_ts,
1044-
symbol_map=symbol_map,
1023+
map_symbols=map_symbols,
10451024
compression=compression,
10461025
schema=schema,
10471026
)
@@ -1114,27 +1093,33 @@ def to_ndarray(
11141093
def _transcode(
11151094
self,
11161095
output: BinaryIO,
1117-
records_iter: Iterator[DBNRecord],
11181096
encoding: Encoding,
11191097
pretty_px: bool,
11201098
pretty_ts: bool,
1121-
symbol_map: dict[int, list[SymbolInterval]] | None,
1099+
map_symbols: bool,
11221100
compression: Compression,
11231101
schema: Schema,
11241102
) -> None:
1103+
if map_symbols:
1104+
self._instrument_map.insert_metadata(self.metadata)
1105+
symbol_map = self._instrument_map._data
1106+
else:
1107+
symbol_map = None
1108+
11251109
transcoder = Transcoder(
11261110
file=output,
11271111
encoding=encoding,
11281112
compression=compression,
11291113
pretty_px=pretty_px,
11301114
pretty_ts=pretty_ts,
1115+
map_symbols=map_symbols,
11311116
has_metadata=True,
11321117
symbol_map=symbol_map, # type: ignore [arg-type]
11331118
schema=schema,
11341119
)
11351120

11361121
transcoder.write(bytes(self.metadata))
1137-
for records in chunk(records_iter, 2**16):
1122+
for records in chunk(self, 2**16):
11381123
for record in records:
11391124
transcoder.write(bytes(record))
11401125
transcoder.flush()

tests/test_historical_bento.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -488,11 +488,11 @@ def test_mbo_to_csv_writes_expected_file_to_disk(
488488
# Assert
489489
written = path.read_text()
490490
expected = (
491-
"ts_recv,ts_event,rtype,publisher_id,instrument_id,action,side,price,size,channel_id,order_id,flags,ts_in_delta,sequence,symbol\n"
492-
"1609160400000704060,1609160400000429831,160,1,5482,C,A,3722750000000,1,0,647784973705,128,22993,1170352,ESH1\n"
493-
"1609160400000711344,1609160400000431665,160,1,5482,C,A,3723000000000,1,0,647784973631,128,19621,1170353,ESH1\n"
494-
"1609160400000728600,1609160400000433051,160,1,5482,C,A,3723250000000,1,0,647784973427,128,16979,1170354,ESH1\n"
495-
"1609160400000740248,1609160400000434353,160,1,5482,C,A,3723500000000,1,0,647784973094,128,17883,1170355,ESH1\n"
491+
"ts_recv,ts_event,rtype,publisher_id,instrument_id,action,side,price,size,channel_id,order_id,flags,ts_in_delta,sequence\n"
492+
"1609160400000704060,1609160400000429831,160,1,5482,C,A,3722750000000,1,0,647784973705,128,22993,1170352\n"
493+
"1609160400000711344,1609160400000431665,160,1,5482,C,A,3723000000000,1,0,647784973631,128,19621,1170353\n"
494+
"1609160400000728600,1609160400000433051,160,1,5482,C,A,3723250000000,1,0,647784973427,128,16979,1170354\n"
495+
"1609160400000740248,1609160400000434353,160,1,5482,C,A,3723500000000,1,0,647784973094,128,17883,1170355\n"
496496
)
497497

498498
assert written == expected
@@ -518,11 +518,11 @@ def test_mbp_1_to_csv_with_no_options_writes_expected_file_to_disk(
518518
# Assert
519519
written = path.read_text()
520520
expected = (
521-
"ts_recv,ts_event,rtype,publisher_id,instrument_id,action,side,depth,price,size,flags,ts_in_delta,sequence,bid_px_00,ask_px_00,bid_sz_00,ask_sz_00,bid_ct_00,ask_ct_00,symbol\n"
522-
"1609160400006136329,1609160400006001487,1,1,5482,A,A,0,3720500000000,1,128,17214,1170362,3720250000000,3720500000000,24,11,15,9,ESH1\n"
523-
"1609160400006246513,1609160400006146661,1,1,5482,A,A,0,3720500000000,1,128,18858,1170364,3720250000000,3720500000000,24,12,15,10,ESH1\n"
524-
"1609160400007159323,1609160400007044577,1,1,5482,A,B,0,3720250000000,2,128,18115,1170365,3720250000000,3720500000000,26,12,16,10,ESH1\n"
525-
"1609160400007260967,1609160400007169135,1,1,5482,C,A,0,3720500000000,1,128,17361,1170366,3720250000000,3720500000000,26,11,16,9,ESH1\n"
521+
"ts_recv,ts_event,rtype,publisher_id,instrument_id,action,side,depth,price,size,flags,ts_in_delta,sequence,bid_px_00,ask_px_00,bid_sz_00,ask_sz_00,bid_ct_00,ask_ct_00\n"
522+
"1609160400006136329,1609160400006001487,1,1,5482,A,A,0,3720500000000,1,128,17214,1170362,3720250000000,3720500000000,24,11,15,9\n"
523+
"1609160400006246513,1609160400006146661,1,1,5482,A,A,0,3720500000000,1,128,18858,1170364,3720250000000,3720500000000,24,12,15,10\n"
524+
"1609160400007159323,1609160400007044577,1,1,5482,A,B,0,3720250000000,2,128,18115,1170365,3720250000000,3720500000000,26,12,16,10\n"
525+
"1609160400007260967,1609160400007169135,1,1,5482,C,A,0,3720500000000,1,128,17361,1170366,3720250000000,3720500000000,26,11,16,9\n"
526526
)
527527

528528
assert written == expected
@@ -578,10 +578,10 @@ def test_mbo_to_json_with_no_options_writes_expected_file_to_disk(
578578
# Assert
579579
written = path.read_text()
580580
assert written == (
581-
'{"ts_recv":"1609160400000704060","hd":{"ts_event":"1609160400000429831","rtype":160,"publisher_id":1,"instrument_id":5482},"action":"C","side":"A","price":"3722750000000","size":1,"channel_id":0,"order_id":"647784973705","flags":128,"ts_in_delta":22993,"sequence":1170352,"symbol":"ESH1"}\n'
582-
'{"ts_recv":"1609160400000711344","hd":{"ts_event":"1609160400000431665","rtype":160,"publisher_id":1,"instrument_id":5482},"action":"C","side":"A","price":"3723000000000","size":1,"channel_id":0,"order_id":"647784973631","flags":128,"ts_in_delta":19621,"sequence":1170353,"symbol":"ESH1"}\n'
583-
'{"ts_recv":"1609160400000728600","hd":{"ts_event":"1609160400000433051","rtype":160,"publisher_id":1,"instrument_id":5482},"action":"C","side":"A","price":"3723250000000","size":1,"channel_id":0,"order_id":"647784973427","flags":128,"ts_in_delta":16979,"sequence":1170354,"symbol":"ESH1"}\n'
584-
'{"ts_recv":"1609160400000740248","hd":{"ts_event":"1609160400000434353","rtype":160,"publisher_id":1,"instrument_id":5482},"action":"C","side":"A","price":"3723500000000","size":1,"channel_id":0,"order_id":"647784973094","flags":128,"ts_in_delta":17883,"sequence":1170355,"symbol":"ESH1"}\n'
581+
'{"ts_recv":"1609160400000704060","hd":{"ts_event":"1609160400000429831","rtype":160,"publisher_id":1,"instrument_id":5482},"action":"C","side":"A","price":"3722750000000","size":1,"channel_id":0,"order_id":"647784973705","flags":128,"ts_in_delta":22993,"sequence":1170352}\n'
582+
'{"ts_recv":"1609160400000711344","hd":{"ts_event":"1609160400000431665","rtype":160,"publisher_id":1,"instrument_id":5482},"action":"C","side":"A","price":"3723000000000","size":1,"channel_id":0,"order_id":"647784973631","flags":128,"ts_in_delta":19621,"sequence":1170353}\n'
583+
'{"ts_recv":"1609160400000728600","hd":{"ts_event":"1609160400000433051","rtype":160,"publisher_id":1,"instrument_id":5482},"action":"C","side":"A","price":"3723250000000","size":1,"channel_id":0,"order_id":"647784973427","flags":128,"ts_in_delta":16979,"sequence":1170354}\n'
584+
'{"ts_recv":"1609160400000740248","hd":{"ts_event":"1609160400000434353","rtype":160,"publisher_id":1,"instrument_id":5482},"action":"C","side":"A","price":"3723500000000","size":1,"channel_id":0,"order_id":"647784973094","flags":128,"ts_in_delta":17883,"sequence":1170355}\n'
585585
)
586586

587587

@@ -632,10 +632,10 @@ def test_mbp_1_to_json_with_no_options_writes_expected_file_to_disk(
632632
# Assert
633633
written = path.read_text()
634634
assert written == (
635-
'{"ts_recv":"1609160400006136329","hd":{"ts_event":"1609160400006001487","rtype":1,"publisher_id":1,"instrument_id":5482},"action":"A","side":"A","depth":0,"price":"3720500000000","size":1,"flags":128,"ts_in_delta":17214,"sequence":1170362,"levels":[{"bid_px":"3720250000000","ask_px":"3720500000000","bid_sz":24,"ask_sz":11,"bid_ct":15,"ask_ct":9}],"symbol":"ESH1"}\n'
636-
'{"ts_recv":"1609160400006246513","hd":{"ts_event":"1609160400006146661","rtype":1,"publisher_id":1,"instrument_id":5482},"action":"A","side":"A","depth":0,"price":"3720500000000","size":1,"flags":128,"ts_in_delta":18858,"sequence":1170364,"levels":[{"bid_px":"3720250000000","ask_px":"3720500000000","bid_sz":24,"ask_sz":12,"bid_ct":15,"ask_ct":10}],"symbol":"ESH1"}\n'
637-
'{"ts_recv":"1609160400007159323","hd":{"ts_event":"1609160400007044577","rtype":1,"publisher_id":1,"instrument_id":5482},"action":"A","side":"B","depth":0,"price":"3720250000000","size":2,"flags":128,"ts_in_delta":18115,"sequence":1170365,"levels":[{"bid_px":"3720250000000","ask_px":"3720500000000","bid_sz":26,"ask_sz":12,"bid_ct":16,"ask_ct":10}],"symbol":"ESH1"}\n'
638-
'{"ts_recv":"1609160400007260967","hd":{"ts_event":"1609160400007169135","rtype":1,"publisher_id":1,"instrument_id":5482},"action":"C","side":"A","depth":0,"price":"3720500000000","size":1,"flags":128,"ts_in_delta":17361,"sequence":1170366,"levels":[{"bid_px":"3720250000000","ask_px":"3720500000000","bid_sz":26,"ask_sz":11,"bid_ct":16,"ask_ct":9}],"symbol":"ESH1"}\n'
635+
'{"ts_recv":"1609160400006136329","hd":{"ts_event":"1609160400006001487","rtype":1,"publisher_id":1,"instrument_id":5482},"action":"A","side":"A","depth":0,"price":"3720500000000","size":1,"flags":128,"ts_in_delta":17214,"sequence":1170362,"levels":[{"bid_px":"3720250000000","ask_px":"3720500000000","bid_sz":24,"ask_sz":11,"bid_ct":15,"ask_ct":9}]}\n'
636+
'{"ts_recv":"1609160400006246513","hd":{"ts_event":"1609160400006146661","rtype":1,"publisher_id":1,"instrument_id":5482},"action":"A","side":"A","depth":0,"price":"3720500000000","size":1,"flags":128,"ts_in_delta":18858,"sequence":1170364,"levels":[{"bid_px":"3720250000000","ask_px":"3720500000000","bid_sz":24,"ask_sz":12,"bid_ct":15,"ask_ct":10}]}\n'
637+
'{"ts_recv":"1609160400007159323","hd":{"ts_event":"1609160400007044577","rtype":1,"publisher_id":1,"instrument_id":5482},"action":"A","side":"B","depth":0,"price":"3720250000000","size":2,"flags":128,"ts_in_delta":18115,"sequence":1170365,"levels":[{"bid_px":"3720250000000","ask_px":"3720500000000","bid_sz":26,"ask_sz":12,"bid_ct":16,"ask_ct":10}]}\n'
638+
'{"ts_recv":"1609160400007260967","hd":{"ts_event":"1609160400007169135","rtype":1,"publisher_id":1,"instrument_id":5482},"action":"C","side":"A","depth":0,"price":"3720500000000","size":1,"flags":128,"ts_in_delta":17361,"sequence":1170366,"levels":[{"bid_px":"3720250000000","ask_px":"3720500000000","bid_sz":26,"ask_sz":11,"bid_ct":16,"ask_ct":9}]}\n'
639639
)
640640

641641

0 commit comments

Comments
 (0)