# kabutobashi
[![pytest](https://github.com/gsy0911/kabutobashi/workflows/pytest/badge.svg)](https://github.com/gsy0911/kabutobashi/actions?query=workflow%3Apytest)
[![codecov](https://codecov.io/gh/gsy0911/kabutobashi/branch/main/graph/badge.svg)](https://codecov.io/gh/gsy0911/kabutobashi)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)
[![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)
[![PythonVersion](https://img.shields.io/pypi/pyversions/kabutobashi.svg)](https://pypi.org/project/kabutobashi/)
[![PiPY](https://img.shields.io/pypi/v/kabutobashi.svg)](https://pypi.org/project/kabutobashi/)
[![Documentation Status](https://readthedocs.org/projects/kabutobashi/badge/?version=latest)](https://kabutobashi.readthedocs.io/en/latest/?badge=latest)
# Core Concept
`@block`-decorator and `Flow`-class is important.
`@block` automatically generates input and output functions, allowing you to focus solely on the processing.
`Flow` allows you to focus solely on the process flow and input parameters.
## About `@block`-decorator
simple decorator is like below.
```python
def simple_decorator(func):
def wrap_func() -> str:
res = func()
return f"Hello, {res}"
return wrap_func
@simple_decorator
def world() -> str:
return "world"
world() # => "Hello, world"
```
A `decorator` is something that dynamically generates and adds processes to functions or classes, similar to its name.
First, prepare a function as follows and decorate it with `@block`.
```python
from kabutobashi import block
@block()
class UdfBlock:
term: int = 10
def _process(self):
return {"doubled_term": self.term * 2}
```
The classes above is equivalent to the following class definition.
```python
import pandas as pd
from kabutobashi.domain.entity.blocks import BlockGlue
class UdfBlock:
series: pd.DataFrame = None
params: dict = None
term: int = 10
block_name: str = "udf_block"
def _process(self) -> dict:
return {"doubled_term": self.term * 2}
def process(self) -> BlockGlue:
# _process() method can be Tuple[Optional[dict], Optional[pd.DataFrame]]
res = self._process()
return BlockGlue(params=res, series=None, block_outputs={})
def factory(self, glue: BlockGlue) -> "UdfBlock":
# Omitted. In reality, processes are described.
...
def _factory(self, glue: BlockGlue) -> dict:
# Omitted. In reality, processes are described.
...
def glue(self, glue: BlockGlue) -> BlockGlue:
# Omitted. In reality, processes are described.
...
```
In classes decorated with `@block`, it is not recommended to execute the `__init__()` method. Instead, it is recommended to use the `factory()` class-method.
`factory()` method description.
`process()` method description.
`glue()` method description.
```mermaid
sequenceDiagram
autonumber
participant G as glue()
participant UC as UdfBlock::class
create participant S1 as factory()
UC->>S1: create
create participant S2 as _factory()
UC->>S2: create or defined by user
create participant P1 as process()
UC->>P1: create
create participant P2 as _process()
UC->>P2: create or defined by user
Note over S1: Generate udf_block_instance
G->>+S1: Request
S1->>+S2: Request
Note over S2: User can modify _factory()
S2-->>S2: get params from glue
S2-->>S2: get series from glue
S2-->>-S1: params and series
create participant UI as UdfBlock::instance
S1->>UI: UdfBlock(params, series)
S1->>UI: setattr params to udf_block_instance
S1-->>-G: udf_block_instance
G->>+UI: udf_block_instance.process()
UI->>+P1: process()
Note over P1: execute process()
P1->>P2: Request
Note over P2: execute user defined function
P2-->>P1: params or series
P1-->>-UI: BlockGlue(params, series)
UI-->>-G: block_glue_instance
```
Up to this point, the use of the `@block` decorator with classes such as UdfClass has described, but using the Block class on its own is not intended. Please read the following explanation of the `Flow` class for more details.
## About `Flow`-class
> Blocks are meant to be combined.
Processes always consist of combinations of multiple simple operations. And the only tedious part is aligning their inputs and outputs.
Therefore, in `Flow`-class, it automatically resolves the sequence of those processes for users, as long as you provide the initial values.
## usage
```python
import kabutobashi as kb
# n日前までの営業日の日付リストを取得する関数
target_date = "2020-01-01"
date_list = kb.get_past_n_days(target_date, n=40)
```
## initialize Database
```python
import kabutobashi as kb
kb.KabutobashiDatabase().initialize()
# add data
kb.crawl_info_multiple(code="1375", page="1", database_dir="...")
kb.crawl_info_multiple(code="1375", page="2", database_dir="...")
kb.crawl_info_multiple(code="1375", page="etc...", database_dir="...")
# add data daily
kb.crawl_info(code="1375", database_dir="...")
# analysis and add data
kb.analysis(code="1375", database_dir="...")
```
Raw data
{
"_id": null,
"home_page": "https://github.com/gsy0911/kabutobashi",
"name": "kabutobashi",
"maintainer": null,
"docs_url": null,
"requires_python": "<3.13,>=3.10",
"maintainer_email": null,
"keywords": "stock",
"author": "yoshiki",
"author_email": "yoshiki0911@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/76/91/67713d64c5aa88332d107d3ef48012e1a87eff54e945c37278af4edd55ad/kabutobashi-0.8.7.tar.gz",
"platform": null,
"description": "# kabutobashi\n\n[![pytest](https://github.com/gsy0911/kabutobashi/workflows/pytest/badge.svg)](https://github.com/gsy0911/kabutobashi/actions?query=workflow%3Apytest)\n[![codecov](https://codecov.io/gh/gsy0911/kabutobashi/branch/main/graph/badge.svg)](https://codecov.io/gh/gsy0911/kabutobashi)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)\n[![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)\n\n[![PythonVersion](https://img.shields.io/pypi/pyversions/kabutobashi.svg)](https://pypi.org/project/kabutobashi/)\n[![PiPY](https://img.shields.io/pypi/v/kabutobashi.svg)](https://pypi.org/project/kabutobashi/)\n[![Documentation Status](https://readthedocs.org/projects/kabutobashi/badge/?version=latest)](https://kabutobashi.readthedocs.io/en/latest/?badge=latest)\n\n# Core Concept\n\n`@block`-decorator and `Flow`-class is important.\n`@block` automatically generates input and output functions, allowing you to focus solely on the processing.\n`Flow` allows you to focus solely on the process flow and input parameters.\n\n## About `@block`-decorator\n\nsimple decorator is like below.\n\n```python\ndef simple_decorator(func):\n def wrap_func() -> str:\n res = func()\n return f\"Hello, {res}\"\n return wrap_func\n\n\n@simple_decorator\ndef world() -> str:\n return \"world\"\n\n\nworld() # => \"Hello, world\"\n```\n\nA `decorator` is something that dynamically generates and adds processes to functions or classes, similar to its name.\n\n\nFirst, prepare a function as follows and decorate it with `@block`.\n\n```python\nfrom kabutobashi import block\n\n@block()\nclass UdfBlock:\n term: int = 10\n\n def _process(self):\n return {\"doubled_term\": self.term * 2}\n```\n\nThe classes above is equivalent to the following class definition.\n\n```python\nimport pandas as pd\nfrom kabutobashi.domain.entity.blocks import BlockGlue\n\nclass UdfBlock:\n series: pd.DataFrame = None\n params: dict = None\n term: int = 10\n block_name: str = \"udf_block\"\n\n def _process(self) -> dict:\n return {\"doubled_term\": self.term * 2}\n \n def process(self) -> BlockGlue:\n # _process() method can be Tuple[Optional[dict], Optional[pd.DataFrame]]\n res = self._process()\n return BlockGlue(params=res, series=None, block_outputs={})\n\n def factory(self, glue: BlockGlue) -> \"UdfBlock\":\n # Omitted. In reality, processes are described.\n ...\n\n def _factory(self, glue: BlockGlue) -> dict:\n # Omitted. In reality, processes are described.\n ...\n\n def glue(self, glue: BlockGlue) -> BlockGlue:\n # Omitted. In reality, processes are described.\n ...\n\n```\n\nIn classes decorated with `@block`, it is not recommended to execute the `__init__()` method. Instead, it is recommended to use the `factory()` class-method.\n\n`factory()` method description.\n`process()` method description.\n`glue()` method description.\n\n```mermaid\nsequenceDiagram\n autonumber\n participant G as glue()\n participant UC as UdfBlock::class\n create participant S1 as factory()\n UC->>S1: create\n create participant S2 as _factory()\n UC->>S2: create or defined by user\n create participant P1 as process()\n UC->>P1: create\n create participant P2 as _process()\n UC->>P2: create or defined by user\n Note over S1: Generate udf_block_instance\n G->>+S1: Request\n S1->>+S2: Request\n Note over S2: User can modify _factory()\n S2-->>S2: get params from glue\n S2-->>S2: get series from glue\n S2-->>-S1: params and series\n create participant UI as UdfBlock::instance\n S1->>UI: UdfBlock(params, series)\n S1->>UI: setattr params to udf_block_instance\n S1-->>-G: udf_block_instance\n G->>+UI: udf_block_instance.process()\n UI->>+P1: process()\n Note over P1: execute process()\n P1->>P2: Request\n Note over P2: execute user defined function\n P2-->>P1: params or series\n P1-->>-UI: BlockGlue(params, series)\n UI-->>-G: block_glue_instance\n```\n\n\nUp to this point, the use of the `@block` decorator with classes such as UdfClass has described, but using the Block class on its own is not intended. Please read the following explanation of the `Flow` class for more details.\n\n## About `Flow`-class\n\n> Blocks are meant to be combined.\n\nProcesses always consist of combinations of multiple simple operations. And the only tedious part is aligning their inputs and outputs.\n\nTherefore, in `Flow`-class, it automatically resolves the sequence of those processes for users, as long as you provide the initial values.\n\n## usage\n\n```python\nimport kabutobashi as kb\n\n# n\u65e5\u524d\u307e\u3067\u306e\u55b6\u696d\u65e5\u306e\u65e5\u4ed8\u30ea\u30b9\u30c8\u3092\u53d6\u5f97\u3059\u308b\u95a2\u6570\ntarget_date = \"2020-01-01\"\ndate_list = kb.get_past_n_days(target_date, n=40)\n```\n\n## initialize Database\n\n```python\nimport kabutobashi as kb\nkb.KabutobashiDatabase().initialize()\n\n# add data\nkb.crawl_info_multiple(code=\"1375\", page=\"1\", database_dir=\"...\")\nkb.crawl_info_multiple(code=\"1375\", page=\"2\", database_dir=\"...\")\nkb.crawl_info_multiple(code=\"1375\", page=\"etc...\", database_dir=\"...\")\n\n# add data daily\nkb.crawl_info(code=\"1375\", database_dir=\"...\")\n\n# analysis and add data\nkb.analysis(code=\"1375\", database_dir=\"...\")\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Analyze stock",
"version": "0.8.7",
"project_urls": {
"Homepage": "https://github.com/gsy0911/kabutobashi",
"Repository": "https://github.com/gsy0911/kabutobashi"
},
"split_keywords": [
"stock"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "78440e7006b9436aba56ba6c4ed4246496e7066db871d255b05f43b09a9dbf35",
"md5": "5d2e56277377d157088124fc781ab56f",
"sha256": "976d719b2d57aec1c185119e10249a6358c0cc199a25a39dd6e6eb6fbac738be"
},
"downloads": -1,
"filename": "kabutobashi-0.8.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5d2e56277377d157088124fc781ab56f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<3.13,>=3.10",
"size": 56440,
"upload_time": "2024-08-16T02:08:07",
"upload_time_iso_8601": "2024-08-16T02:08:07.831773Z",
"url": "https://files.pythonhosted.org/packages/78/44/0e7006b9436aba56ba6c4ed4246496e7066db871d255b05f43b09a9dbf35/kabutobashi-0.8.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "769167713d64c5aa88332d107d3ef48012e1a87eff54e945c37278af4edd55ad",
"md5": "10e692040ac391cf5b9b9495dccbadef",
"sha256": "26b1b575addf0e621acb78f6b8f893c7b7745a364ccbdb245bc993206d821f89"
},
"downloads": -1,
"filename": "kabutobashi-0.8.7.tar.gz",
"has_sig": false,
"md5_digest": "10e692040ac391cf5b9b9495dccbadef",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<3.13,>=3.10",
"size": 32742,
"upload_time": "2024-08-16T02:08:09",
"upload_time_iso_8601": "2024-08-16T02:08:09.603052Z",
"url": "https://files.pythonhosted.org/packages/76/91/67713d64c5aa88332d107d3ef48012e1a87eff54e945c37278af4edd55ad/kabutobashi-0.8.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-16 02:08:09",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "gsy0911",
"github_project": "kabutobashi",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "kabutobashi"
}