polars-fxmacrodata 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- polars_fxmacrodata/__init__.py +6 -0
- polars_fxmacrodata/io.py +127 -0
- polars_fxmacrodata/py.typed +0 -0
- polars_fxmacrodata-0.1.0.dist-info/METADATA +132 -0
- polars_fxmacrodata-0.1.0.dist-info/RECORD +8 -0
- polars_fxmacrodata-0.1.0.dist-info/WHEEL +5 -0
- polars_fxmacrodata-0.1.0.dist-info/licenses/LICENSE +21 -0
- polars_fxmacrodata-0.1.0.dist-info/top_level.txt +1 -0
polars_fxmacrodata/io.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"""Read public FXMacroData operations through the Polars scan interface."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
import json
|
|
7
|
+
from typing import Any, Iterator, Mapping
|
|
8
|
+
|
|
9
|
+
import polars as pl
|
|
10
|
+
from polars.io.plugins import register_io_source
|
|
11
|
+
|
|
12
|
+
from fxmacrodata_platform_core import COLUMNS, operations, read, validate_request
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def source_schema() -> pl.Schema:
|
|
16
|
+
"""Return the lossless, additive schema without fetching remote data."""
|
|
17
|
+
return pl.Schema({name: pl.Float64 if name == "value" else pl.String for name in COLUMNS})
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def operation_catalog() -> pl.DataFrame:
|
|
21
|
+
"""Discover every supported REST operation and packaged MCP tool locally."""
|
|
22
|
+
rows = [
|
|
23
|
+
{
|
|
24
|
+
"name": operation.name,
|
|
25
|
+
"description": operation.description,
|
|
26
|
+
"method": operation.method,
|
|
27
|
+
"path": operation.path,
|
|
28
|
+
"input_schema_json": json.dumps(operation.input_schema, sort_keys=True),
|
|
29
|
+
}
|
|
30
|
+
for operation in operations()
|
|
31
|
+
]
|
|
32
|
+
return pl.DataFrame(rows, schema={name: pl.String for name in (
|
|
33
|
+
"name", "description", "method", "path", "input_schema_json"
|
|
34
|
+
)})
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass(frozen=True)
|
|
38
|
+
class _IOSource:
|
|
39
|
+
operation: str
|
|
40
|
+
arguments_json: str
|
|
41
|
+
api_key_env: str | None
|
|
42
|
+
complete_history: bool
|
|
43
|
+
max_pages: int
|
|
44
|
+
|
|
45
|
+
def __call__(
|
|
46
|
+
self,
|
|
47
|
+
with_columns: list[str] | None,
|
|
48
|
+
predicate: pl.Expr | None,
|
|
49
|
+
n_rows: int | None,
|
|
50
|
+
batch_size: int | None,
|
|
51
|
+
) -> Iterator[pl.DataFrame]:
|
|
52
|
+
schema = source_schema()
|
|
53
|
+
columns = list(schema) if with_columns is None else with_columns
|
|
54
|
+
if n_rows == 0:
|
|
55
|
+
yield pl.DataFrame(schema=schema).select(columns)
|
|
56
|
+
return
|
|
57
|
+
batch = read(
|
|
58
|
+
self.operation,
|
|
59
|
+
json.loads(self.arguments_json),
|
|
60
|
+
api_key_env=self.api_key_env,
|
|
61
|
+
complete_history=self.complete_history,
|
|
62
|
+
max_pages=self.max_pages,
|
|
63
|
+
)
|
|
64
|
+
rows = iter(batch.rows())
|
|
65
|
+
size = batch_size if batch_size is not None and batch_size > 0 else 10_000
|
|
66
|
+
remaining = n_rows
|
|
67
|
+
emitted = False
|
|
68
|
+
while remaining is None or remaining > 0:
|
|
69
|
+
chunk = []
|
|
70
|
+
for _ in range(size):
|
|
71
|
+
try:
|
|
72
|
+
chunk.append(next(rows))
|
|
73
|
+
except StopIteration:
|
|
74
|
+
break
|
|
75
|
+
if not chunk:
|
|
76
|
+
break
|
|
77
|
+
frame = pl.DataFrame(chunk, schema=schema, strict=True)
|
|
78
|
+
# A pushed predicate may refer to a column excluded by the projection.
|
|
79
|
+
if predicate is not None:
|
|
80
|
+
frame = frame.filter(predicate)
|
|
81
|
+
if remaining is not None:
|
|
82
|
+
frame = frame.head(remaining)
|
|
83
|
+
remaining -= frame.height
|
|
84
|
+
frame = frame.select(columns)
|
|
85
|
+
if frame.height:
|
|
86
|
+
emitted = True
|
|
87
|
+
yield frame
|
|
88
|
+
if not emitted:
|
|
89
|
+
yield pl.DataFrame(schema=schema).select(columns)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def scan_fxmacrodata(
|
|
93
|
+
operation: str,
|
|
94
|
+
arguments: Mapping[str, Any] | None = None,
|
|
95
|
+
*,
|
|
96
|
+
api_key_env: str | None = None,
|
|
97
|
+
complete_history: bool = True,
|
|
98
|
+
max_pages: int = 10_000,
|
|
99
|
+
) -> pl.LazyFrame:
|
|
100
|
+
"""Create a lazy scan; credentials are resolved only when it is collected.
|
|
101
|
+
|
|
102
|
+
``api_key_env`` is an environment variable name, never an API key. Omitting
|
|
103
|
+
it explicitly uses anonymous access. Original records and response envelopes
|
|
104
|
+
remain available in ``record_json`` and ``response_json``.
|
|
105
|
+
"""
|
|
106
|
+
if not isinstance(operation, str) or operation not in {item.name for item in operations()}:
|
|
107
|
+
raise ValueError("Unknown operation; inspect operation_catalog()['name']")
|
|
108
|
+
if not isinstance(max_pages, int) or isinstance(max_pages, bool) or max_pages < 1:
|
|
109
|
+
raise ValueError("max_pages must be a positive integer")
|
|
110
|
+
if api_key_env is not None and (
|
|
111
|
+
not isinstance(api_key_env, str)
|
|
112
|
+
or not api_key_env.isidentifier()
|
|
113
|
+
or not api_key_env.isascii()
|
|
114
|
+
):
|
|
115
|
+
raise ValueError("api_key_env must be an environment variable name")
|
|
116
|
+
if arguments is not None and not isinstance(arguments, Mapping):
|
|
117
|
+
raise ValueError("arguments must be an object")
|
|
118
|
+
if not isinstance(complete_history, bool):
|
|
119
|
+
raise ValueError("complete_history must be a boolean")
|
|
120
|
+
source = _IOSource(
|
|
121
|
+
operation,
|
|
122
|
+
json.dumps(validate_request(operation, dict(arguments) if arguments is not None else None), allow_nan=False, sort_keys=True),
|
|
123
|
+
api_key_env,
|
|
124
|
+
complete_history,
|
|
125
|
+
max_pages,
|
|
126
|
+
)
|
|
127
|
+
return register_io_source(io_source=source, schema=source_schema())
|
|
File without changes
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: polars-fxmacrodata
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Lazy Polars IO source for FXMacroData macroeconomic and market data
|
|
5
|
+
Author-email: FXMacroData <info@fxmacrodata.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 FXMacroData
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://fxmacrodata.com/?utm_source=pypi&utm_medium=referral&utm_campaign=polars-fxmacrodata&utm_content=package
|
|
29
|
+
Project-URL: Documentation, https://fxmacrodata.com/documentation/reference?utm_source=pypi&utm_medium=referral&utm_campaign=polars-fxmacrodata&utm_content=docs
|
|
30
|
+
Requires-Python: >=3.10
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
License-File: LICENSE
|
|
33
|
+
Requires-Dist: polars==1.38.1
|
|
34
|
+
Requires-Dist: fxmacrodata-platform-core==0.1.0
|
|
35
|
+
Dynamic: license-file
|
|
36
|
+
|
|
37
|
+
# FXMacroData for Polars
|
|
38
|
+
|
|
39
|
+
Query FXMacroData as a native Polars lazy IO source. Combine macroeconomic releases,
|
|
40
|
+
FX, commodities, positioning and research data with your own datasets using the
|
|
41
|
+
same expressions, joins and sinks as other Polars scans.
|
|
42
|
+
|
|
43
|
+
[Explore FXMacroData](https://fxmacrodata.com/?utm_source=github&utm_medium=referral&utm_campaign=polars-fxmacrodata&utm_content=readme)
|
|
44
|
+
|
|
45
|
+
## Install
|
|
46
|
+
|
|
47
|
+
Install the `polars-fxmacrodata` wheel together with its
|
|
48
|
+
`fxmacrodata-platform-core` and `fxmacrodata-public-client` dependencies. The Polars IO
|
|
49
|
+
plugin API is unstable, so this package pins the supported Polars release.
|
|
50
|
+
|
|
51
|
+
From the supplied packages directory:
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
python -m pip install ./fxmacrodata-public-client ./fxmacrodata-platform-core ./polars-fxmacrodata
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
import polars as pl
|
|
59
|
+
from polars_fxmacrodata import operation_catalog, scan_fxmacrodata
|
|
60
|
+
|
|
61
|
+
# Local discovery includes operation arguments and requires no remote request.
|
|
62
|
+
print(operation_catalog().select("name", "description"))
|
|
63
|
+
print(operation_catalog().filter(pl.col("name") == "indicator_history"))
|
|
64
|
+
|
|
65
|
+
history = scan_fxmacrodata(
|
|
66
|
+
"indicator_history",
|
|
67
|
+
{"currency": "USD", "indicator": "policy_rate"},
|
|
68
|
+
api_key_env="FXMD_API_KEY",
|
|
69
|
+
)
|
|
70
|
+
result = (
|
|
71
|
+
history.filter(pl.col("value").is_not_null())
|
|
72
|
+
.select("date", "value", "record_json")
|
|
73
|
+
.collect()
|
|
74
|
+
)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Set `FXMD_API_KEY` using your environment or secret manager. Pass its name through
|
|
78
|
+
`api_key_env`; the source resolves its value when executing the query. Omitting
|
|
79
|
+
`api_key_env` explicitly selects anonymous access. Credentials do not belong in
|
|
80
|
+
arguments, notebooks, saved query plans or source control.
|
|
81
|
+
|
|
82
|
+
`scan_fxmacrodata` builds a plan without making a request. `collect`, a sink, or
|
|
83
|
+
another executing operation performs the read. Historical operations follow all
|
|
84
|
+
pages by default; `complete_history=False` requests the supplied page only.
|
|
85
|
+
`max_pages` defaults to 10,000 and raises on an incomplete history instead of
|
|
86
|
+
silently truncating it. Normal API access and licence terms apply.
|
|
87
|
+
|
|
88
|
+
## Add release context to portfolio positions
|
|
89
|
+
|
|
90
|
+
Suppose `positions.parquet` contains `currency` and `position_usd` columns:
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
calendar = scan_fxmacrodata(
|
|
94
|
+
"release_calendar", {"currency": "USD"}, api_key_env="FXMD_API_KEY"
|
|
95
|
+
)
|
|
96
|
+
positions = pl.scan_parquet("positions.parquet")
|
|
97
|
+
exposure_by_event = (
|
|
98
|
+
calendar.filter(pl.col("announcement_datetime").is_not_null())
|
|
99
|
+
.select("currency", "indicator", "announcement_datetime", "record_json")
|
|
100
|
+
.join(positions, on="currency", how="inner")
|
|
101
|
+
.collect()
|
|
102
|
+
)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Calendar times retain the API's original strings. Unknown publication times stay
|
|
106
|
+
null. An observation's period date is not substituted for its release time.
|
|
107
|
+
|
|
108
|
+
## Data and query semantics
|
|
109
|
+
|
|
110
|
+
The fixed schema contains `operation`, `source_url`, `record_id`, `currency`,
|
|
111
|
+
`indicator`, `date`, `announcement_datetime`, `value`, `value_text`, `record_json`
|
|
112
|
+
and `response_json`. `value` is nullable Float64; other columns are nullable
|
|
113
|
+
String. `record_json` preserves the original record and `response_json` preserves
|
|
114
|
+
the complete response envelope, including metadata and revision information.
|
|
115
|
+
Empty results retain an envelope row with a null record. Inspect `source_schema()`
|
|
116
|
+
without fetching data.
|
|
117
|
+
|
|
118
|
+
The scanner applies pushed predicates before column projection and honours the
|
|
119
|
+
engine's row limit and batch-size hint. Predicates run in Polars and are not
|
|
120
|
+
translated into different API filters. Supply documented operation arguments to
|
|
121
|
+
reduce the remote dataset. The complete response is fetched before it is emitted
|
|
122
|
+
in dataframe batches; a final `head()` limits the result, not the remote history
|
|
123
|
+
request. Collecting the same plan again performs a fresh read.
|
|
124
|
+
|
|
125
|
+
All public REST operations and packaged MCP tools are exposed through the
|
|
126
|
+
operation catalog. MCP tool names have an `mcp_` prefix. Authentication and
|
|
127
|
+
endpoint-specific access requirements remain those of the selected operation.
|
|
128
|
+
|
|
129
|
+
## Development
|
|
130
|
+
|
|
131
|
+
Run the offline source tests with `python -m pytest tests`. They exercise native
|
|
132
|
+
Polars collection with fixture responses and never call the live service.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
polars_fxmacrodata/__init__.py,sha256=6efTQP5oH1BABm9I4MxsVXZFLZgH9YQIQdJki0dxen0,205
|
|
2
|
+
polars_fxmacrodata/io.py,sha256=5W1un_yJ8WfHQxEsUCTWQbYkcMB2E58dC2FTiHUhuS0,4781
|
|
3
|
+
polars_fxmacrodata/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
polars_fxmacrodata-0.1.0.dist-info/licenses/LICENSE,sha256=mLVV_vNUMbtObjBLP4s10Tc8K4m_5sbNJ96E169Td84,1089
|
|
5
|
+
polars_fxmacrodata-0.1.0.dist-info/METADATA,sha256=2mGgGPOgfs9G9X63hRZh9URBfdFFinM81Clp1r-d-6g,6156
|
|
6
|
+
polars_fxmacrodata-0.1.0.dist-info/WHEEL,sha256=16fc_9Rp854SLcdhaDEjr0u65p0e3FhZxihNgcnCE98,91
|
|
7
|
+
polars_fxmacrodata-0.1.0.dist-info/top_level.txt,sha256=R-t9IQtKzdwpoJJBfsfXr5GLEkIGlD8iSsikjAM0Wxc,19
|
|
8
|
+
polars_fxmacrodata-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 FXMacroData
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
polars_fxmacrodata
|