kabukit 0.1.1__tar.gz → 0.2.1__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.
Files changed (40) hide show
  1. kabukit-0.2.1/LICENSE +21 -0
  2. kabukit-0.2.1/PKG-INFO +67 -0
  3. kabukit-0.2.1/README.md +18 -0
  4. {kabukit-0.1.1 → kabukit-0.2.1}/pyproject.toml +31 -21
  5. kabukit-0.2.1/src/kabukit/__init__.py +7 -0
  6. kabukit-0.2.1/src/kabukit/analysis/visualization.py +57 -0
  7. kabukit-0.2.1/src/kabukit/cli/__init__.py +0 -0
  8. {kabukit-0.1.1 → kabukit-0.2.1}/src/kabukit/cli/auth.py +2 -2
  9. kabukit-0.2.1/src/kabukit/core/__init__.py +0 -0
  10. kabukit-0.2.1/src/kabukit/core/base.py +45 -0
  11. kabukit-0.2.1/src/kabukit/core/client.py +25 -0
  12. kabukit-0.2.1/src/kabukit/core/info.py +12 -0
  13. kabukit-0.2.1/src/kabukit/core/prices.py +30 -0
  14. kabukit-0.2.1/src/kabukit/core/statements.py +7 -0
  15. kabukit-0.2.1/src/kabukit/edinet/__init__.py +3 -0
  16. {kabukit-0.1.1 → kabukit-0.2.1}/src/kabukit/edinet/client.py +14 -12
  17. kabukit-0.2.1/src/kabukit/edinet/concurrent.py +153 -0
  18. kabukit-0.2.1/src/kabukit/edinet/doc.py +32 -0
  19. kabukit-0.2.1/src/kabukit/jquants/__init__.py +3 -0
  20. {kabukit-0.1.1 → kabukit-0.2.1}/src/kabukit/jquants/client.py +63 -101
  21. kabukit-0.1.1/src/kabukit/jquants/stream.py → kabukit-0.2.1/src/kabukit/jquants/concurrent.py +12 -43
  22. {kabukit-0.1.1 → kabukit-0.2.1}/src/kabukit/jquants/info.py +9 -1
  23. kabukit-0.2.1/src/kabukit/jquants/prices.py +29 -0
  24. {kabukit-0.1.1 → kabukit-0.2.1}/src/kabukit/jquants/schema.py +19 -8
  25. {kabukit-0.1.1 → kabukit-0.2.1}/src/kabukit/jquants/statements.py +35 -2
  26. kabukit-0.2.1/src/kabukit/py.typed +0 -0
  27. kabukit-0.2.1/src/kabukit/utils/__init__.py +0 -0
  28. kabukit-0.2.1/src/kabukit/utils/concurrent.py +148 -0
  29. kabukit-0.1.1/PKG-INFO +0 -30
  30. kabukit-0.1.1/README.md +0 -10
  31. kabukit-0.1.1/src/kabukit/__init__.py +0 -4
  32. kabukit-0.1.1/src/kabukit/concurrent.py +0 -40
  33. kabukit-0.1.1/src/kabukit/jquants/__init__.py +0 -3
  34. {kabukit-0.1.1/src/kabukit/cli → kabukit-0.2.1/src/kabukit/analysis}/__init__.py +0 -0
  35. /kabukit-0.1.1/src/kabukit/edinet/__init__.py → /kabukit-0.2.1/src/kabukit/analysis/indicators.py +0 -0
  36. /kabukit-0.1.1/src/kabukit/jquants/prices.py → /kabukit-0.2.1/src/kabukit/analysis/preprocess.py +0 -0
  37. /kabukit-0.1.1/src/kabukit/py.typed → /kabukit-0.2.1/src/kabukit/analysis/screener.py +0 -0
  38. {kabukit-0.1.1 → kabukit-0.2.1}/src/kabukit/cli/app.py +0 -0
  39. {kabukit-0.1.1/src/kabukit → kabukit-0.2.1/src/kabukit/utils}/config.py +0 -0
  40. {kabukit-0.1.1/src/kabukit → kabukit-0.2.1/src/kabukit/utils}/params.py +0 -0
kabukit-0.2.1/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Daizu
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.
kabukit-0.2.1/PKG-INFO ADDED
@@ -0,0 +1,67 @@
1
+ Metadata-Version: 2.3
2
+ Name: kabukit
3
+ Version: 0.2.1
4
+ Summary: A Python toolkit for Japanese financial market data, supporting J-Quants and EDINET APIs.
5
+ Author: daizutabi
6
+ Author-email: daizutabi <daizutabi@gmail.com>
7
+ License: MIT License
8
+
9
+ Copyright (c) 2025 Daizu
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+ Classifier: Development Status :: 4 - Beta
29
+ Classifier: Programming Language :: Python
30
+ Classifier: Programming Language :: Python :: 3.12
31
+ Classifier: Programming Language :: Python :: 3.13
32
+ Requires-Dist: altair>=5
33
+ Requires-Dist: async-typer>=0.1
34
+ Requires-Dist: holidays>=0.81
35
+ Requires-Dist: httpx>=0.28.1
36
+ Requires-Dist: marimo[lsp]>=0.16
37
+ Requires-Dist: platformdirs>=4
38
+ Requires-Dist: polars>=1
39
+ Requires-Dist: python-dotenv>=1
40
+ Requires-Dist: typer>=0.19
41
+ Requires-Dist: vegafusion-python-embed>=1.6
42
+ Requires-Dist: vegafusion>=2
43
+ Requires-Dist: vl-convert-python>=1.8
44
+ Requires-Python: >=3.12
45
+ Project-URL: Documentation, https://daizutabi.github.io/kabukit/
46
+ Project-URL: Issues, https://github.com/daizutabi/kabukit/issues
47
+ Project-URL: Source, https://github.com/daizutabi/kabukit
48
+ Description-Content-Type: text/markdown
49
+
50
+ # kabukit
51
+
52
+ A Python toolkit for Japanese financial market data, supporting J-Quants and EDINET APIs.
53
+
54
+ [![PyPI Version][pypi-v-image]][pypi-v-link]
55
+ [![Python Version][python-v-image]][python-v-link]
56
+
57
+ ## Installation
58
+
59
+ ```bash
60
+ pip install kabukit
61
+ ```
62
+
63
+ <!-- Badges -->
64
+ [pypi-v-image]: https://img.shields.io/pypi/v/kabukit.svg
65
+ [pypi-v-link]: https://pypi.org/project/kabukit/
66
+ [python-v-image]: https://img.shields.io/pypi/pyversions/kabukit.svg
67
+ [python-v-link]: https://pypi.org/project/kabukit
@@ -0,0 +1,18 @@
1
+ # kabukit
2
+
3
+ A Python toolkit for Japanese financial market data, supporting J-Quants and EDINET APIs.
4
+
5
+ [![PyPI Version][pypi-v-image]][pypi-v-link]
6
+ [![Python Version][python-v-image]][python-v-link]
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ pip install kabukit
12
+ ```
13
+
14
+ <!-- Badges -->
15
+ [pypi-v-image]: https://img.shields.io/pypi/v/kabukit.svg
16
+ [pypi-v-link]: https://pypi.org/project/kabukit/
17
+ [python-v-image]: https://img.shields.io/pypi/pyversions/kabukit.svg
18
+ [python-v-link]: https://pypi.org/project/kabukit
@@ -4,45 +4,54 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "kabukit"
7
- version = "0.1.1"
7
+ version = "0.2.1"
8
8
  description = "A Python toolkit for Japanese financial market data, supporting J-Quants and EDINET APIs."
9
9
  readme = "README.md"
10
- authors = [{ name = "Daizu", email = "daizutabi@gmail.com" }]
11
- requires-python = ">=3.13"
10
+ license = { file = "LICENSE" }
11
+ authors = [{ name = "daizutabi", email = "daizutabi@gmail.com" }]
12
+ classifiers = [
13
+ "Development Status :: 4 - Beta",
14
+ "Programming Language :: Python",
15
+ "Programming Language :: Python :: 3.12",
16
+ "Programming Language :: Python :: 3.13",
17
+ ]
18
+ requires-python = ">=3.12"
12
19
  dependencies = [
13
- "altair>=5.5.0",
14
- "async-typer>=0.1.10",
20
+ "altair>=5",
21
+ "async-typer>=0.1",
22
+ "holidays>=0.81",
15
23
  "httpx>=0.28.1",
16
- "marimo>=0.15.3",
17
- "platformdirs>=4.4.0",
18
- "polars>=1.33.1",
19
- "python-dotenv>=1.1.1",
20
- "typer>=0.17.4",
21
- "vegafusion-python-embed>=1.6.9",
22
- "vegafusion>=2.0.2",
23
- "vl-convert-python>=1.8.0",
24
+ "marimo[lsp]>=0.16",
25
+ "platformdirs>=4",
26
+ "polars>=1",
27
+ "python-dotenv>=1",
28
+ "typer>=0.19",
29
+ "vegafusion-python-embed>=1.6",
30
+ "vegafusion>=2",
31
+ "vl-convert-python>=1.8",
24
32
  ]
25
33
 
26
34
  [project.scripts]
27
35
  kabu = "kabukit.cli.app:app"
28
36
 
37
+ [project.urls]
38
+ Documentation = "https://daizutabi.github.io/kabukit/"
39
+ Source = "https://github.com/daizutabi/kabukit"
40
+ Issues = "https://github.com/daizutabi/kabukit/issues"
41
+
29
42
  [dependency-groups]
30
43
  dev = [
31
44
  "basedpyright>=1.31.4",
32
- "numpy>=2.3.3", # polars 1.33 type hinting workaround,
33
- "polars>=1.33.1",
45
+ "numpy>=2.3.3", # polars 1.33 type hinting workaround,
34
46
  "pytest-asyncio>=1.2.0",
35
47
  "pytest-clarity>=1.0.1",
36
48
  "pytest-cov>=7.0.0",
37
49
  "pytest-randomly>=4.0.1",
38
50
  "pytest-xdist>=3.8.0",
39
- "python-lsp-server>=1.13.1",
40
51
  "rich>=14.1.0",
41
- "ruff>=0.13.0",
42
52
  "tqdm>=4.67.1",
43
- "websockets>=15.0.1",
44
53
  ]
45
- docs = ["mkapi>=4.4", "mkdocs-material"]
54
+ docs = ["mkapi>=4.4", "mkdocs-marimo", "mkdocs-material"]
46
55
 
47
56
  [tool.pytest.ini_options]
48
57
  addopts = ["--cov=kabukit", "--cov-report=lcov:lcov.info", "--doctest-modules"]
@@ -54,7 +63,7 @@ skip_covered = true
54
63
 
55
64
  [tool.ruff]
56
65
  line-length = 88
57
- target-version = "py313"
66
+ target-version = "py312"
58
67
  include = ["src", "tests"]
59
68
 
60
69
  [tool.ruff.lint]
@@ -77,8 +86,9 @@ ignore = [
77
86
  ]
78
87
 
79
88
  [tool.ruff.lint.per-file-ignores]
80
- "tests/*" = ["ANN", "FBT", "S101"]
89
+ "tests/*" = ["ANN", "FBT", "S101", "S607"]
81
90
  "schema.py" = ["E501"]
91
+ "notebooks/*" = ["F704", "PLE1142"]
82
92
 
83
93
  [tool.ruff.format]
84
94
  exclude = ["schema.py"]
@@ -0,0 +1,7 @@
1
+ from .core.info import Info
2
+ from .core.prices import Prices
3
+ from .core.statements import Statements
4
+ from .edinet.client import EdinetClient
5
+ from .jquants.client import JQuantsClient
6
+
7
+ __all__ = ["EdinetClient", "Info", "JQuantsClient", "Prices", "Statements"]
@@ -0,0 +1,57 @@
1
+ """チャート作成のためのモジュール"""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import TYPE_CHECKING, Literal
6
+
7
+ import altair as alt
8
+
9
+ if TYPE_CHECKING:
10
+ from kabukit.core.prices import Prices
11
+
12
+
13
+ def plot_prices(
14
+ prices: Prices,
15
+ kind: Literal["candlestick"] = "candlestick",
16
+ ) -> alt.VConcatChart:
17
+ if kind == "candlestick":
18
+ chart_price = plot_prices_candlestick(prices)
19
+ chart_price_volume = plot_prices_volume(prices)
20
+ return alt.vconcat(chart_price, chart_price_volume)
21
+
22
+ raise NotImplementedError # pyright: ignore[reportUnreachable]
23
+
24
+
25
+ def plot_prices_candlestick(prices: Prices) -> alt.LayerChart:
26
+ rule = alt.Chart(prices.data, mark="rule").encode(y="Low:Q", y2="High:Q")
27
+ bar = alt.Chart(prices.data, mark="bar").encode(y="Open:Q", y2="Close:Q")
28
+
29
+ color_condition = alt.condition(
30
+ "datum.Open < datum.Close",
31
+ alt.value("#ff3030"),
32
+ alt.value("#3030ff"),
33
+ )
34
+
35
+ return alt.layer(rule, bar, height=200).encode(
36
+ x=alt.X("Date:T", axis=alt.Axis(title="日付", format="%Y-%m-%d")),
37
+ y=alt.Y(title="株価", scale=alt.Scale(zero=False)),
38
+ color=color_condition,
39
+ tooltip=[
40
+ alt.Tooltip("Date:T", title="日付"),
41
+ alt.Tooltip("Open:Q", title="始値"),
42
+ alt.Tooltip("High:Q", title="高値"),
43
+ alt.Tooltip("Low:Q", title="安値"),
44
+ alt.Tooltip("Close:Q", title="終値"),
45
+ ],
46
+ )
47
+
48
+
49
+ def plot_prices_volume(prices: Prices) -> alt.Chart:
50
+ return alt.Chart(prices.data, mark="bar", height=50).encode(
51
+ x=alt.X("Date:T", axis=alt.Axis(title="日付", format="%Y-%m-%d")),
52
+ y=alt.Y("Volume:Q", title="出来高"),
53
+ tooltip=[
54
+ alt.Tooltip("Date:T", title="日付"),
55
+ alt.Tooltip("Volume:Q", title="出来高"),
56
+ ],
57
+ )
File without changes
@@ -50,7 +50,7 @@ async def jquants_alias(mailaddress: Mailaddress, password: Password) -> None:
50
50
 
51
51
  def auth_edinet(api_key: str) -> None:
52
52
  """EDINET APIのAPIキーを設定ファイルに保存します。"""
53
- from kabukit.config import set_key
53
+ from kabukit.utils.config import set_key
54
54
 
55
55
  set_key("EDINET_API_KEY", api_key)
56
56
  typer.echo("EDINETのAPIキーを保存しました。")
@@ -75,7 +75,7 @@ def show() -> None:
75
75
  """設定ファイルに保存したトークン・APIキーを表示します。"""
76
76
  from dotenv import dotenv_values
77
77
 
78
- from kabukit.config import get_dotenv_path
78
+ from kabukit.utils.config import get_dotenv_path
79
79
 
80
80
  path = get_dotenv_path()
81
81
  typer.echo(f"Configuration file: {path}")
File without changes
@@ -0,0 +1,45 @@
1
+ from __future__ import annotations
2
+
3
+ import datetime
4
+ from pathlib import Path
5
+ from typing import TYPE_CHECKING
6
+
7
+ import polars as pl
8
+ from platformdirs import user_cache_dir
9
+
10
+ if TYPE_CHECKING:
11
+ from typing import Self
12
+
13
+ from polars import DataFrame
14
+
15
+
16
+ class Base:
17
+ data: DataFrame
18
+
19
+ def __init__(self, data: DataFrame) -> None:
20
+ self.data = data
21
+
22
+ @classmethod
23
+ def data_dir(cls) -> Path:
24
+ clsname = cls.__name__.lower()
25
+ return Path(user_cache_dir("kabukit")) / clsname
26
+
27
+ def write(self) -> Path:
28
+ data_dir = self.data_dir()
29
+ data_dir.mkdir(parents=True, exist_ok=True)
30
+ path = datetime.datetime.today().strftime("%Y%m%d") # noqa: DTZ002
31
+ filename = data_dir / f"{path}.parquet"
32
+ self.data.write_parquet(filename)
33
+ return filename
34
+
35
+ @classmethod
36
+ def read(cls, path: str | None = None) -> Self:
37
+ data_dir = cls.data_dir()
38
+
39
+ if path is None:
40
+ filename = sorted(data_dir.glob("*.parquet"))[-1]
41
+ else:
42
+ filename = data_dir / path
43
+
44
+ data = pl.read_parquet(filename)
45
+ return cls(data)
@@ -0,0 +1,25 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ from httpx import AsyncClient
6
+
7
+ if TYPE_CHECKING:
8
+ from typing import Self
9
+
10
+
11
+ class Client:
12
+ client: AsyncClient
13
+
14
+ def __init__(self, base_url: str = "") -> None:
15
+ self.client = AsyncClient(base_url=base_url)
16
+
17
+ async def aclose(self) -> None:
18
+ """HTTPクライアントを閉じる。"""
19
+ await self.client.aclose()
20
+
21
+ async def __aenter__(self) -> Self:
22
+ return self
23
+
24
+ async def __aexit__(self, exc_type, exc_val, exc_tb) -> None: # pyright: ignore[reportMissingParameterType, reportUnknownParameterType] # noqa: ANN001
25
+ await self.aclose()
@@ -0,0 +1,12 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ from .base import Base
6
+
7
+ if TYPE_CHECKING:
8
+ from polars import DataFrame
9
+
10
+
11
+ class Info(Base):
12
+ pass
@@ -0,0 +1,30 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ import polars as pl
6
+
7
+ from .base import Base
8
+
9
+ if TYPE_CHECKING:
10
+ from datetime import timedelta
11
+ from typing import Self
12
+
13
+ from polars import Expr
14
+
15
+
16
+ class Prices(Base):
17
+ def truncate(self, every: str | timedelta | Expr) -> Self:
18
+ data = (
19
+ self.data.group_by(pl.col("Date").dt.truncate(every), "Code")
20
+ .agg(
21
+ pl.col("Open").drop_nulls().first(),
22
+ pl.col("High").max(),
23
+ pl.col("Low").min(),
24
+ pl.col("Close").drop_nulls().last(),
25
+ pl.col("Volume").sum(),
26
+ pl.col("TurnoverValue").sum(),
27
+ )
28
+ .sort("Code", "Date")
29
+ )
30
+ return self.__class__(data)
@@ -0,0 +1,7 @@
1
+ from __future__ import annotations
2
+
3
+ from .base import Base
4
+
5
+
6
+ class Statements(Base):
7
+ pass
@@ -0,0 +1,3 @@
1
+ from .concurrent import fetch, fetch_list
2
+
3
+ __all__ = ["fetch", "fetch_list"]
@@ -7,11 +7,13 @@ from enum import StrEnum
7
7
  from typing import TYPE_CHECKING
8
8
 
9
9
  import polars as pl
10
- from httpx import AsyncClient
11
10
  from polars import DataFrame
12
11
 
13
- from kabukit.config import load_dotenv
14
- from kabukit.params import get_params
12
+ from kabukit.core.client import Client
13
+ from kabukit.utils.config import load_dotenv
14
+ from kabukit.utils.params import get_params
15
+
16
+ from .doc import clean_csv, clean_list
15
17
 
16
18
  if TYPE_CHECKING:
17
19
  import datetime
@@ -29,11 +31,9 @@ class AuthKey(StrEnum):
29
31
  API_KEY = "EDINET_API_KEY"
30
32
 
31
33
 
32
- class EdinetClient:
33
- client: AsyncClient
34
-
34
+ class EdinetClient(Client):
35
35
  def __init__(self, api_key: str | None = None) -> None:
36
- self.client = AsyncClient(base_url=BASE_URL)
36
+ super().__init__(BASE_URL)
37
37
  self.set_api_key(api_key)
38
38
 
39
39
  def set_api_key(self, api_key: str | None = None) -> None:
@@ -44,9 +44,6 @@ class EdinetClient:
44
44
  if api_key:
45
45
  self.client.params = {"Subscription-Key": api_key}
46
46
 
47
- async def aclose(self) -> None:
48
- await self.client.aclose()
49
-
50
47
  async def get(self, url: str, params: QueryParamTypes) -> Response:
51
48
  resp = await self.client.get(url, params=params)
52
49
  resp.raise_for_status()
@@ -71,7 +68,12 @@ class EdinetClient:
71
68
  if "results" not in data:
72
69
  return DataFrame()
73
70
 
74
- return DataFrame(data["results"], infer_schema_length=None)
71
+ df = DataFrame(data["results"], infer_schema_length=None)
72
+
73
+ if df.is_empty():
74
+ return df
75
+
76
+ return clean_list(df, date)
75
77
 
76
78
  async def get_document(self, doc_id: str, doc_type: int) -> Response:
77
79
  params = get_params(type=doc_type)
@@ -105,7 +107,7 @@ class EdinetClient:
105
107
  f.read(),
106
108
  separator="\t",
107
109
  encoding="utf-16-le",
108
- )
110
+ ).pipe(clean_csv, doc_id)
109
111
 
110
112
  msg = "CSV is not available."
111
113
  raise ValueError(msg)
@@ -0,0 +1,153 @@
1
+ from __future__ import annotations
2
+
3
+ import datetime
4
+ from typing import TYPE_CHECKING
5
+
6
+ from kabukit.utils import concurrent
7
+
8
+ from .client import EdinetClient
9
+
10
+ if TYPE_CHECKING:
11
+ from collections.abc import Iterable
12
+
13
+ from polars import DataFrame
14
+
15
+ from kabukit.utils.concurrent import Callback, Progress
16
+
17
+
18
+ def get_dates(days: int | None = None, years: int | None = None) -> list[datetime.date]:
19
+ """過去days日またはyears年の日付リストを返す。
20
+
21
+ Args:
22
+ days (int | None): 過去days日の日付リストを取得する。
23
+ years (int | None): 過去years年の日付リストを取得する。
24
+ daysが指定されている場合は無視される。
25
+ """
26
+ end_date = datetime.date.today() # noqa: DTZ011
27
+
28
+ if days is not None:
29
+ start_date = end_date - datetime.timedelta(days=days)
30
+ elif years is not None:
31
+ start_date = end_date.replace(year=end_date.year - years)
32
+ else:
33
+ msg = "daysまたはyearsのいずれかを指定してください。"
34
+ raise ValueError(msg)
35
+
36
+ return [
37
+ start_date + datetime.timedelta(days=i)
38
+ for i in range(1, (end_date - start_date).days + 1)
39
+ ]
40
+
41
+
42
+ async def fetch(
43
+ resource: str,
44
+ args: Iterable[str | datetime.date],
45
+ /,
46
+ max_concurrency: int | None = None,
47
+ progress: Progress | None = None,
48
+ callback: Callback | None = None,
49
+ ) -> DataFrame:
50
+ """引数に対応する各種データを取得し、単一のDataFrameにまとめて返す。
51
+
52
+ Args:
53
+ resource (str): 取得するデータの種類。EdinetClientのメソッド名から"get_"を
54
+ 除いたものを指定する。
55
+ args (Iterable[str | datetime.date]): 取得対象の引数のリスト。
56
+ max_concurrency (int | None, optional): 同時に実行するリクエストの最大数。
57
+ 指定しないときはデフォルト値が使用される。
58
+ progress (Progress | None, optional): 進捗表示のための関数。
59
+ tqdm, marimoなどのライブラリを使用できる。
60
+ 指定しないときは進捗表示は行われない。
61
+ callback (Callback | None, optional): 各DataFrameに対して適用する
62
+ コールバック関数。指定しないときはそのままのDataFrameが使用される。
63
+
64
+ Returns:
65
+ DataFrame:
66
+ すべての銘柄の財務情報を含む単一のDataFrame。
67
+ """
68
+ return await concurrent.fetch(
69
+ EdinetClient,
70
+ resource,
71
+ args,
72
+ max_concurrency=max_concurrency,
73
+ progress=progress,
74
+ callback=callback,
75
+ )
76
+
77
+
78
+ async def fetch_list(
79
+ days: int | None = None,
80
+ years: int | None = None,
81
+ limit: int | None = None,
82
+ max_concurrency: int | None = None,
83
+ progress: Progress | None = None,
84
+ callback: Callback | None = None,
85
+ ) -> DataFrame:
86
+ """過去days日またはyears年の文書一覧を取得し、単一のDataFrameにまとめて返す。
87
+
88
+ Args:
89
+ days (int | None): 過去days日の日付リストを取得する。
90
+ years (int | None): 過去years年の日付リストを取得する。
91
+ daysが指定されている場合は無視される。
92
+ max_concurrency (int | None, optional): 同時に実行するリクエストの最大数。
93
+ 指定しないときはデフォルト値が使用される。
94
+ progress (Progress | None, optional): 進捗表示のための関数。
95
+ tqdm, marimoなどのライブラリを使用できる。
96
+ 指定しないときは進捗表示は行われない。
97
+ callback (Callback | None, optional): 各DataFrameに対して適用する
98
+ コールバック関数。指定しないときはそのままのDataFrameが使用される。
99
+
100
+ Returns:
101
+ DataFrame:
102
+ 文書一覧を含む単一のDataFrame。
103
+ """
104
+ dates = get_dates(days=days, years=years)
105
+
106
+ if limit is not None:
107
+ dates = dates[:limit]
108
+
109
+ return await fetch(
110
+ "list",
111
+ dates,
112
+ max_concurrency=max_concurrency,
113
+ progress=progress,
114
+ callback=callback,
115
+ )
116
+
117
+
118
+ async def fetch_csv(
119
+ doc_ids: Iterable[str],
120
+ /,
121
+ limit: int | None = None,
122
+ max_concurrency: int | None = None,
123
+ progress: Progress | None = None,
124
+ callback: Callback | None = None,
125
+ ) -> DataFrame:
126
+ """文書をCSV形式で取得し、単一のDataFrameにまとめて返す。
127
+
128
+ Args:
129
+ doc_ids (Iterable[str]): 取得対象の文書IDのリスト。
130
+ max_concurrency (int | None, optional): 同時に実行するリクエストの最大数。
131
+ 指定しないときはデフォルト値が使用される。
132
+ progress (Progress | None, optional): 進捗表示のための関数。
133
+ tqdm, marimoなどのライブラリを使用できる。
134
+ 指定しないときは進捗表示は行われない。
135
+ callback (Callback | None, optional): 各DataFrameに対して適用する
136
+ コールバック関数。指定しないときはそのままのDataFrameが使用される。
137
+
138
+ Returns:
139
+ DataFrame:
140
+ 文書含む単一のDataFrame。
141
+ """
142
+ doc_ids = list(doc_ids)
143
+
144
+ if limit is not None:
145
+ doc_ids = doc_ids[:limit]
146
+
147
+ return await fetch(
148
+ "csv",
149
+ doc_ids,
150
+ max_concurrency=max_concurrency,
151
+ progress=progress,
152
+ callback=callback,
153
+ )
@@ -0,0 +1,32 @@
1
+ from __future__ import annotations
2
+
3
+ import datetime
4
+ from typing import TYPE_CHECKING
5
+
6
+ import polars as pl
7
+
8
+ if TYPE_CHECKING:
9
+ from polars import DataFrame
10
+
11
+
12
+ def clean_list(df: DataFrame, date: str | datetime.date) -> DataFrame:
13
+ if isinstance(date, str):
14
+ date = datetime.datetime.strptime(date, "%Y-%m-%d").date() # noqa: DTZ007
15
+
16
+ return df.with_columns(
17
+ pl.lit(date).alias("Date"),
18
+ pl.col("submitDateTime").str.to_datetime("%Y-%m-%d %H:%M", strict=False),
19
+ pl.col("^period.+$").str.to_date("%Y-%m-%d", strict=False),
20
+ pl.col("^.+Flag$").cast(pl.Int8).cast(pl.Boolean),
21
+ pl.col("^.+Code$").cast(pl.String),
22
+ pl.col("opeDateTime")
23
+ .cast(pl.String)
24
+ .str.to_datetime("%Y-%m-%d %H:%M", strict=False),
25
+ ).select("Date", pl.exclude("Date"))
26
+
27
+
28
+ def clean_csv(df: DataFrame, doc_id: str) -> DataFrame:
29
+ return df.select(
30
+ pl.lit(doc_id).alias("docID"),
31
+ pl.all(),
32
+ )
@@ -0,0 +1,3 @@
1
+ from .concurrent import fetch, fetch_all
2
+
3
+ __all__ = ["fetch", "fetch_all"]