Skip to content

PythonObjectStore

QuantConnect.Storage.PythonObjectStore

PythonObjectStore(
    store: IObjectStore, warn: Callable[[str], Any] = None
)

Bases: ObjectStore

The ObjectStore of Python algorithms and notebooks: adds the save and read methods that take and return Python objects, such as dictionaries and pandas DataFrames

Initializes a new instance of the PythonObjectStore class

Parameters:

Name Type Description Default
store IObjectStore

The IObjectStore instance to wrap

required
warn Callable[[str], Any]

Receives the warnings about sanitized keys, the engine log by default

None

max_size

max_size: int

Gets the maximum storage limit in bytes

max_files

max_files: int

Gets the maximum number of files allowed

error_raised

error_raised: _EventContainer[
    Callable[
        [Object, ObjectStoreErrorRaisedEventArgs], Any
    ],
    Any,
]

Event raised each time there's an error

keys

keys: ICollection[str]

Returns the file paths present in the object store. This is specially useful not to load the object store into memory. An unsupported key saved in this run is listed next to the sanitized name its object is stored under. The store is listed on first use, and Contains finds a key the way contains_key does, without listing it

read_json

read_json(path: str) -> Any

Returns the JSON deserialized object data for the specified path as Python objects

Parameters:

Name Type Description Default
path str

The object path

required

Returns:

Type Description
Any

The deserialized Python object.

save_json

save_json(path: str, obj: Any) -> bool

Saves the given Python dictionary, list, tuple or .NET object in JSON format for the specified path, tolerating nested types the standard json module rejects: datetime/date/time, pandas Timestamp and numpy datetime64 are stored in ISO-8601 format, timedelta and timedelta64 as ISO-8601 durations, Decimal and numpy numbers as numbers, NaN, infinity and NaT as null, .NET objects (Symbol included) as the C# ObjectStore.SaveJson{T} overload writes them (a NaN inside one is Newtonsoft's "NaN" string) and non-string dictionary keys as strings. pandas DataFrames and Series are rejected, use SaveDataframe, and so is any other type that would not round trip. A top-level Python str, int, float or bool is not accepted, use ObjectStore.save_text for text

Parameters:

Name Type Description Default
path str

The object key: english letters, numbers, spaces and the characters '.', '/', '', '', '#', '-', '$' and '=', never '..', e.g. 'folder/trade_log-2024.v2.csv'. Other characters are replaced by ''

required
obj Any

The Python object to be saved

required

Returns:

Type Description
bool

True if the object was saved successfully.

read_dataframe

read_dataframe(path: str) -> DataFrame

Returns the object data for the specified path as a pandas DataFrame, read as JSON if the path has a '.json' extension and as CSV otherwise, so a save_dataframe round trip restores the frame. A CSV index in ISO-8601 format is restored as a time index. A saved Series comes back as a one-column DataFrame, call squeeze() on it to get the Series

Parameters:

Name Type Description Default
path str

The object path

required

Returns:

Type Description
DataFrame

The pandas DataFrame.

save_dataframe

save_dataframe(path: str, data_frame: Any) -> bool

Saves the given pandas DataFrame (or Series) for the specified path, in JSON format if the path has a '.json' extension and as CSV otherwise

Parameters:

Name Type Description Default
path str

The object key: english letters, numbers, spaces and the characters '.', '/', '\', '', '#', '-', '$' and '=', never '..', e.g. 'folder/trade_log-2024.v2.csv'. Other characters are replaced by ''

required
data_frame Any

The pandas DataFrame or Series to be saved

required

Returns:

Type Description
bool

True if the object was saved successfully.

clear

clear() -> None

Will clear the object store state cache. This is useful when the object store is used concurrently by nodes which want to share information

contains_key

contains_key(path: str) -> bool

Determines whether the store contains data for the specified path

Parameters:

Name Type Description Default
path str

The object path

required

Returns:

Type Description
bool

True if the key was found.

delete

delete(path: str) -> bool

Deletes the object data for the specified path

Parameters:

Name Type Description Default
path str

The object path

required

Returns:

Type Description
bool

True if the delete operation was successful.

get_file_path

get_file_path(path: str) -> str

Returns the file path for the specified path

Parameters:

Name Type Description Default
path str

The object path

required

Returns:

Type Description
str

The path for the file.

initialize

initialize(
    user_id: int,
    project_id: int,
    user_token: str,
    controls: Controls,
    algorithm_mode: AlgorithmMode,
) -> None

Initializes the object store

Parameters:

Name Type Description Default
user_id int

The user id

required
project_id int

The project id

required
user_token str

The user token

required
controls Controls

The job controls instance

required
algorithm_mode AlgorithmMode

The algorithm mode

required

read_bytes

read_bytes(path: str) -> List[int]

Returns the object data for the specified path

Parameters:

Name Type Description Default
path str

The object path

required

Returns:

Type Description
List[int]

A byte array containing the data.

save_bytes

save_bytes(path: str, contents: List[int]) -> bool

Saves the object data for the specified path

Parameters:

Name Type Description Default
path str

The object key: english letters, numbers, spaces and the characters '.', '/', '\', '', '#', '-', '$' and '=', never '..', e.g. 'folder/trade_log-2024.v2.csv'. Other characters are replaced by ''

required
contents List[int]

The object data

required

Returns:

Type Description
bool

True if the save operation was successful.

read_xml

read_xml(
    path: str, encoding: Encoding = None
) -> QuantConnect_Storage_ObjectStore_ReadXml_T

Returns the XML deserialized object data for the specified path

Parameters:

Name Type Description Default
path str

The object path

required
encoding Encoding

The string encoding used

None

Returns:

Type Description
QuantConnect_Storage_ObjectStore_ReadXml_T

An object containing the data.

save

save(path: str) -> bool
save(
    path: str, text: str, encoding: Encoding = None
) -> bool

Signature descriptions:

  • Saves the data from a local file path associated with the specified path

  • Saves the object data in text format for the specified path

Parameters:

Name Type Description Default
path str

The object key: english letters, numbers, spaces and the characters '.', '/', '', '', '#', '-', '$' and '=', never '..', e.g. 'folder/trade_log-2024.v2.csv'. Other characters are replaced by ''

required
text Optional[str]

The string object to be saved

None
encoding Optional[Encoding]

The string encoding used, Encoding.UTF8 by default

None

Returns:

Type Description
bool

True if the object was saved successfully.

save_xml

save_xml(
    path: str,
    obj: QuantConnect_Storage_ObjectStore_SaveXml_T,
    encoding: Encoding = None,
) -> bool

Saves the object data in XML format for the specified path

Parameters:

Name Type Description Default
path str

The object key: english letters, numbers, spaces and the characters '.', '/', '', '', '#', '-', '$' and '=', never '..', e.g. 'folder/trade_log-2024.v2.csv'. Other characters are replaced by ''

required
obj QuantConnect_Storage_ObjectStore_SaveXml_T

The object to be saved

required
encoding Encoding

The string encoding used

None

Returns:

Type Description
bool

True if the object was saved successfully.

__iter__

__iter__() -> Iterator[KeyValuePair[str, List[int]]]

dispose

dispose() -> None

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

get_enumerator

get_enumerator() -> (
    IEnumerator[KeyValuePair[str, List[int]]]
)

Returns an enumerator that iterates through the collection.

Returns:

Type Description
IEnumerator[KeyValuePair[str, List[int]]]

A System.Collections.Generic.IEnumerator`1 that can be used to iterate through the collection.

read

read(path: str, encoding: Encoding = None) -> str

Returns the string object data for the specified path

Parameters:

Name Type Description Default
path str

The object path

required
encoding Encoding

The string encoding used

None

Returns:

Type Description
str

A string containing the data.

read_string

read_string(path: str, encoding: Encoding = None) -> str

Returns the string object data for the specified path

Parameters:

Name Type Description Default
path str

The object path

required
encoding Encoding

The string encoding used

None

Returns:

Type Description
str

A string containing the data.

read_text

read_text(path: str, encoding: Encoding = None) -> str

Returns the object data for the specified path as a string. Alias of read(string, Encoding)

Parameters:

Name Type Description Default
path str

The object path

required
encoding Encoding

The string encoding used, Encoding.UTF8 by default

None

Returns:

Type Description
str

A string containing the data.

save_string

save_string(
    path: str, text: str, encoding: Encoding = None
) -> bool

Saves the object data in text format for the specified path

Parameters:

Name Type Description Default
path str

The object key: english letters, numbers, spaces and the characters '.', '/', '\', '', '#', '-', '$' and '=', never '..', e.g. 'folder/trade_log-2024.v2.csv'. Other characters are replaced by ''

required
text str

The string object to be saved

required
encoding Encoding

The string encoding used

None

Returns:

Type Description
bool

True if the object was saved successfully.

save_text

save_text(
    path: str, text: str, encoding: Encoding = None
) -> bool

Saves the object data in text format for the specified path. Alias of save(string, string, Encoding)

Parameters:

Name Type Description Default
path str

The object key: english letters, numbers, spaces and the characters '.', '/', '\', '', '#', '-', '$' and '=', never '..', e.g. 'folder/trade_log-2024.v2.csv'. Other characters are replaced by ''

required
text str

The string object to be saved

required
encoding Encoding

The string encoding used, Encoding.UTF8 by default

None

Returns:

Type Description
bool

True if the object was saved successfully.