mimird 0.1.0__tar.gz
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.
- mimird-0.1.0/PKG-INFO +43 -0
- mimird-0.1.0/README.md +34 -0
- mimird-0.1.0/pyproject.toml +22 -0
- mimird-0.1.0/pyproject.toml.orig +21 -0
- mimird-0.1.0/src/mimird/__init__.py +68 -0
- mimird-0.1.0/src/mimird/calls.py +62 -0
mimird-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: mimird
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: The mimird module Mimird scripts import: instruments, history and providers, answered by the platform.
|
|
5
|
+
Author: RamonAvocado
|
|
6
|
+
Author-email: RamonAvocado <ramonet.miret@gmail.com>
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
|
|
10
|
+
# mimird
|
|
11
|
+
|
|
12
|
+
The `mimird` module that Mimird strategy scripts import. It's plain Python with no
|
|
13
|
+
dependencies, so the same wheel runs on Mimird's workers and in the browser (Pyodide).
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
import mimird
|
|
17
|
+
|
|
18
|
+
gold = mimird.ticker("GLD") # an instrument
|
|
19
|
+
apple = mimird.ticker("AAPL", exchange="XNAS") # pick the listing when there are several
|
|
20
|
+
days = mimird.history_days() # the run's history, capped by your plan
|
|
21
|
+
fred = mimird.provider("fred") # your connected provider, with your saved settings
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Pass arguments as plain strings: Mimird reads them from the code (`mimird.calls(code)`)
|
|
25
|
+
before the run, to know which data to load and to draw them in the editor.
|
|
26
|
+
|
|
27
|
+
## Hosts
|
|
28
|
+
|
|
29
|
+
Whatever runs the script plugs in where answers come from:
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
mimird.use(source) # source has .ticker(symbol, exchange), .history_days(), .provider(id)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Without a source, every call raises an error that says to run the script in Mimird.
|
|
36
|
+
|
|
37
|
+
## Develop
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
uv run pytest
|
|
41
|
+
uv build # dist/mimird-<version>-py3-none-any.whl
|
|
42
|
+
uv publish
|
|
43
|
+
```
|
mimird-0.1.0/README.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# mimird
|
|
2
|
+
|
|
3
|
+
The `mimird` module that Mimird strategy scripts import. It's plain Python with no
|
|
4
|
+
dependencies, so the same wheel runs on Mimird's workers and in the browser (Pyodide).
|
|
5
|
+
|
|
6
|
+
```python
|
|
7
|
+
import mimird
|
|
8
|
+
|
|
9
|
+
gold = mimird.ticker("GLD") # an instrument
|
|
10
|
+
apple = mimird.ticker("AAPL", exchange="XNAS") # pick the listing when there are several
|
|
11
|
+
days = mimird.history_days() # the run's history, capped by your plan
|
|
12
|
+
fred = mimird.provider("fred") # your connected provider, with your saved settings
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Pass arguments as plain strings: Mimird reads them from the code (`mimird.calls(code)`)
|
|
16
|
+
before the run, to know which data to load and to draw them in the editor.
|
|
17
|
+
|
|
18
|
+
## Hosts
|
|
19
|
+
|
|
20
|
+
Whatever runs the script plugs in where answers come from:
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
mimird.use(source) # source has .ticker(symbol, exchange), .history_days(), .provider(id)
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Without a source, every call raises an error that says to run the script in Mimird.
|
|
27
|
+
|
|
28
|
+
## Develop
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
uv run pytest
|
|
32
|
+
uv build # dist/mimird-<version>-py3-none-any.whl
|
|
33
|
+
uv publish
|
|
34
|
+
```
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "mimird"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "The mimird module Mimird scripts import: instruments, history and providers, answered by the platform."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
|
+
dependencies = []
|
|
8
|
+
|
|
9
|
+
[[project.authors]]
|
|
10
|
+
name = "RamonAvocado"
|
|
11
|
+
email = "ramonet.miret@gmail.com"
|
|
12
|
+
|
|
13
|
+
[dependency-groups]
|
|
14
|
+
dev = [
|
|
15
|
+
"mypy>=2.4.1",
|
|
16
|
+
"pytest>=8.2",
|
|
17
|
+
"ruff>=0.17.5",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[build-system]
|
|
21
|
+
requires = ["uv_build>=0.12.7,<0.13.0"]
|
|
22
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "mimird"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "The mimird module Mimird scripts import: instruments, history and providers, answered by the platform."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "RamonAvocado", email = "ramonet.miret@gmail.com" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.11"
|
|
10
|
+
dependencies = []
|
|
11
|
+
|
|
12
|
+
[dependency-groups]
|
|
13
|
+
dev = [
|
|
14
|
+
"mypy>=2.4.1",
|
|
15
|
+
"pytest>=8.2",
|
|
16
|
+
"ruff>=0.17.5",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[build-system]
|
|
20
|
+
requires = ["uv_build>=0.12.7,<0.13.0"]
|
|
21
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""What a Mimird script asks the platform for: instruments, the run's history, providers.
|
|
2
|
+
|
|
3
|
+
Plain Python with no dependencies, so the same wheel runs on Mimird's workers and in
|
|
4
|
+
the browser (Pyodide). The host plugs in where the data comes from with :func:`use`;
|
|
5
|
+
a script only calls the functions below::
|
|
6
|
+
|
|
7
|
+
import mimird
|
|
8
|
+
|
|
9
|
+
gold = mimird.ticker("GLD")
|
|
10
|
+
days = mimird.history_days()
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from typing import Any, Protocol
|
|
16
|
+
|
|
17
|
+
from .calls import Call, calls
|
|
18
|
+
|
|
19
|
+
__all__ = ["Call", "Source", "calls", "history_days", "provider", "ticker", "use"]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Source(Protocol):
|
|
23
|
+
"""What the host (Mimird's runner, the browser worker) answers a script with."""
|
|
24
|
+
|
|
25
|
+
def ticker(self, symbol: str, exchange: str | None) -> Any: ...
|
|
26
|
+
|
|
27
|
+
def history_days(self) -> int: ...
|
|
28
|
+
|
|
29
|
+
def provider(self, provider_id: str) -> Any: ...
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
# ponytail: one source per process, fine for prefork workers and Pyodide;
|
|
33
|
+
# a ContextVar if runs ever share a process across threads.
|
|
34
|
+
_source: Source | None = None
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def use(source: Source | None) -> Source | None:
|
|
38
|
+
"""Plug in ``source`` for the calls that follow; returns the one it replaces."""
|
|
39
|
+
global _source
|
|
40
|
+
previous, _source = _source, source
|
|
41
|
+
return previous
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _current() -> Source:
|
|
45
|
+
if _source is None:
|
|
46
|
+
raise RuntimeError(
|
|
47
|
+
"mimird has no data source here: run this script in Mimird's Lab "
|
|
48
|
+
"(a host plugs one in with mimird.use)."
|
|
49
|
+
)
|
|
50
|
+
return _source
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def ticker(symbol: str, exchange: str | None = None) -> Any:
|
|
54
|
+
"""The instrument ``symbol`` (on ``exchange``, a MIC like ``"XNAS"``, when it's
|
|
55
|
+
listed on several). Pass both as plain strings: Mimird reads them from the code
|
|
56
|
+
before the run to know which data to load."""
|
|
57
|
+
return _current().ticker(symbol, exchange)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def history_days() -> int:
|
|
61
|
+
"""How many days of history this run may see (your Lab setting, capped by your plan)."""
|
|
62
|
+
return _current().history_days()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def provider(provider_id: str) -> Any:
|
|
66
|
+
"""Your connection to the data provider ``provider_id`` (e.g. ``"fred"``), built with
|
|
67
|
+
the settings you saved in Mimird. Pass the id as a plain string."""
|
|
68
|
+
return _current().provider(provider_id)
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""Find a script's ``mimird.*`` calls without running it."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ast
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
#: The functions a script calls on ``mimird`` that the host reads ahead of the run.
|
|
10
|
+
NAMES = frozenset({"ticker", "history_days", "provider"})
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass(frozen=True)
|
|
14
|
+
class Call:
|
|
15
|
+
"""One ``mimird.<name>(...)`` call. ``literal`` is False when any argument isn't a
|
|
16
|
+
plain constant (a variable, an f-string): its value is only known at run time."""
|
|
17
|
+
|
|
18
|
+
name: str
|
|
19
|
+
args: tuple[Any, ...]
|
|
20
|
+
kwargs: dict[str, Any] = field(hash=False)
|
|
21
|
+
line: int
|
|
22
|
+
col: int
|
|
23
|
+
literal: bool
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def calls(code: str) -> list[Call]:
|
|
27
|
+
"""Every ``mimird`` call in ``code``, in source order. Follows ``import mimird as m``
|
|
28
|
+
and ``from mimird import ticker as t``. Raises ``SyntaxError`` for invalid code."""
|
|
29
|
+
tree = ast.parse(code)
|
|
30
|
+
modules: set[str] = set() # names bound to the mimird module
|
|
31
|
+
functions: dict[str, str] = {} # local name -> mimird function
|
|
32
|
+
for node in ast.walk(tree):
|
|
33
|
+
if isinstance(node, ast.Import):
|
|
34
|
+
modules |= {a.asname or a.name for a in node.names if a.name == "mimird"}
|
|
35
|
+
elif isinstance(node, ast.ImportFrom) and node.module == "mimird":
|
|
36
|
+
functions |= {a.asname or a.name: a.name for a in node.names if a.name in NAMES}
|
|
37
|
+
|
|
38
|
+
found = []
|
|
39
|
+
for node in ast.walk(tree):
|
|
40
|
+
if not isinstance(node, ast.Call):
|
|
41
|
+
continue
|
|
42
|
+
f = node.func
|
|
43
|
+
if isinstance(f, ast.Attribute) and isinstance(f.value, ast.Name) and f.value.id in modules:
|
|
44
|
+
name = f.attr
|
|
45
|
+
elif isinstance(f, ast.Name) and f.id in functions:
|
|
46
|
+
name = functions[f.id]
|
|
47
|
+
else:
|
|
48
|
+
continue
|
|
49
|
+
if name not in NAMES:
|
|
50
|
+
continue
|
|
51
|
+
args, kwargs, literal = [], {}, True
|
|
52
|
+
for value, key in [(a, None) for a in node.args] + [(k.value, k.arg) for k in node.keywords]:
|
|
53
|
+
try:
|
|
54
|
+
v = ast.literal_eval(value)
|
|
55
|
+
except ValueError:
|
|
56
|
+
v, literal = None, False
|
|
57
|
+
if key is None:
|
|
58
|
+
args.append(v)
|
|
59
|
+
else:
|
|
60
|
+
kwargs[key] = v
|
|
61
|
+
found.append(Call(name, tuple(args), kwargs, node.lineno, node.col_offset, literal))
|
|
62
|
+
return sorted(found, key=lambda c: (c.line, c.col))
|