66import logging
77from collections .abc import Generator
88from collections .abc import Iterator
9+ from collections .abc import Mapping
910from io import BytesIO
1011from os import PathLike
1112from pathlib import Path
12- from typing import (
13- IO ,
14- TYPE_CHECKING ,
15- Any ,
16- BinaryIO ,
17- Callable ,
18- Final ,
19- Literal ,
20- Protocol ,
21- overload ,
22- )
13+ from typing import IO
14+ from typing import TYPE_CHECKING
15+ from typing import Any
16+ from typing import BinaryIO
17+ from typing import Callable
18+ from typing import Final
19+ from typing import Literal
20+ from typing import Protocol
21+ from typing import TextIO
22+ from typing import overload
2323
2424import databento_dbn
2525import numpy as np
4949from databento .common .symbology import InstrumentMap
5050from databento .common .types import DBNRecord
5151from databento .common .types import Default
52+ from databento .common .types import MappingIntervalDict
5253from databento .common .validation import validate_enum
5354from databento .common .validation import validate_file_write_path
5455from databento .common .validation import validate_maybe_enum
@@ -108,20 +109,16 @@ class DataSource(abc.ABC):
108109 Abstract base class for backing DBNStore instances with data.
109110 """
110111
111- def __init__ (self , source : object ) -> None :
112- ...
112+ def __init__ (self , source : object ) -> None : ...
113113
114114 @property
115- def name (self ) -> str :
116- ...
115+ def name (self ) -> str : ...
117116
118117 @property
119- def nbytes (self ) -> int :
120- ...
118+ def nbytes (self ) -> int : ...
121119
122120 @property
123- def reader (self ) -> IO [bytes ]:
124- ...
121+ def reader (self ) -> IO [bytes ]: ...
125122
126123
127124class FileDataSource (DataSource ):
@@ -371,6 +368,7 @@ def __init__(self, data_source: DataSource) -> None:
371368 # Read metadata
372369 self ._metadata : Metadata = Metadata .decode (
373370 metadata_bytes .getvalue (),
371+ upgrade_policy = VersionUpgradePolicy .AS_IS ,
374372 )
375373
376374 self ._instrument_map = InstrumentMap ()
@@ -384,10 +382,7 @@ def __iter__(self) -> Generator[DBNRecord, None, None]:
384382 raw = reader .read (DBNStore .DBN_READ_SIZE )
385383 if raw :
386384 decoder .write (raw )
387- try :
388- records = decoder .decode ()
389- except ValueError :
390- continue
385+ records = decoder .decode ()
391386 for record in records :
392387 if isinstance (record , databento_dbn .Metadata ):
393388 continue
@@ -475,7 +470,7 @@ def nbytes(self) -> int:
475470 return self ._data_source .nbytes
476471
477472 @property
478- def mappings (self ) -> dict [str , list [dict [ str , Any ] ]]:
473+ def mappings (self ) -> dict [str , list [MappingIntervalDict ]]:
479474 """
480475 Return the symbology mappings for the data.
481476
@@ -675,6 +670,27 @@ def from_bytes(cls, data: BytesIO | bytes | IO[bytes]) -> DBNStore:
675670 """
676671 return cls (MemoryDataSource (data ))
677672
673+ def insert_symbology_json (
674+ self ,
675+ json_data : str | Mapping [str , Any ] | TextIO ,
676+ clear_existing : bool = True ,
677+ ) -> None :
678+ """
679+ Insert the given JSON data obtained from the `symbology.resolve`
680+ endpoint or a `symbology.json` file.
681+
682+ Parameters
683+ ----------
684+ json_data : str | Mapping[str, Any] | TextIO
685+ The JSON data to insert.
686+ clear_existing : bool, default True
687+ If existing symbology data should be cleared from the internal mappings.
688+
689+ """
690+ if clear_existing :
691+ self ._instrument_map .clear ()
692+ self ._instrument_map .insert_json (json_data )
693+
678694 def replay (self , callback : Callable [[Any ], None ]) -> None :
679695 """
680696 Replay data by passing records sequentially to the given callback.
@@ -834,8 +850,7 @@ def to_df(
834850 schema : Schema | str | None = ...,
835851 tz : pytz .BaseTzInfo | str = ...,
836852 count : None = ...,
837- ) -> pd .DataFrame :
838- ...
853+ ) -> pd .DataFrame : ...
839854
840855 @overload
841856 def to_df (
@@ -846,16 +861,17 @@ def to_df(
846861 schema : Schema | str | None = ...,
847862 tz : pytz .BaseTzInfo | str = ...,
848863 count : int = ...,
849- ) -> DataFrameIterator :
850- ...
864+ ) -> DataFrameIterator : ...
851865
852866 def to_df (
853867 self ,
854868 price_type : Literal ["fixed" , "float" , "decimal" ] = "float" ,
855869 pretty_ts : bool = True ,
856870 map_symbols : bool = True ,
857871 schema : Schema | str | None = None ,
858- tz : pytz .BaseTzInfo | str | Default [pytz .BaseTzInfo ] = Default [pytz .BaseTzInfo ](pytz .UTC ),
872+ tz : pytz .BaseTzInfo | str | Default [pytz .BaseTzInfo ] = Default [pytz .BaseTzInfo ](
873+ pytz .UTC ,
874+ ),
859875 count : int | None = None ,
860876 ) -> pd .DataFrame | DataFrameIterator :
861877 """
@@ -903,7 +919,9 @@ def to_df(
903919 if isinstance (tz , Default ):
904920 tz = tz .value # consume default
905921 elif not pretty_ts :
906- raise ValueError ("A timezone was specified when `pretty_ts` is `False`. Did you mean to set `pretty_ts=True`?" )
922+ raise ValueError (
923+ "A timezone was specified when `pretty_ts` is `False`. Did you mean to set `pretty_ts=True`?" ,
924+ )
907925
908926 if not isinstance (tz , pytz .BaseTzInfo ):
909927 tz = pytz .timezone (tz )
@@ -1096,16 +1114,14 @@ def to_ndarray( # type: ignore [misc]
10961114 self ,
10971115 schema : Schema | str | None = ...,
10981116 count : None = ...,
1099- ) -> np .ndarray [Any , Any ]:
1100- ...
1117+ ) -> np .ndarray [Any , Any ]: ...
11011118
11021119 @overload
11031120 def to_ndarray (
11041121 self ,
11051122 schema : Schema | str | None = ...,
11061123 count : int = ...,
1107- ) -> NDArrayIterator :
1108- ...
1124+ ) -> NDArrayIterator : ...
11091125
11101126 def to_ndarray (
11111127 self ,
@@ -1208,7 +1224,7 @@ def _transcode(
12081224 pretty_ts = pretty_ts ,
12091225 has_metadata = True ,
12101226 map_symbols = map_symbols ,
1211- symbol_interval_map = symbol_map ,
1227+ symbol_interval_map = symbol_map , # type: ignore [arg-type]
12121228 schema = schema ,
12131229 )
12141230
@@ -1242,12 +1258,10 @@ def _schema_struct_map(self) -> dict[Schema, type[DBNRecord]]:
12421258
12431259class NDArrayIterator (Protocol ):
12441260 @abc .abstractmethod
1245- def __iter__ (self ) -> NDArrayIterator :
1246- ...
1261+ def __iter__ (self ) -> NDArrayIterator : ...
12471262
12481263 @abc .abstractmethod
1249- def __next__ (self ) -> np .ndarray [Any , Any ]:
1250- ...
1264+ def __next__ (self ) -> np .ndarray [Any , Any ]: ...
12511265
12521266
12531267class NDArrayStreamIterator (NDArrayIterator ):
0 commit comments