-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
9,120 additions
and
7,828 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"id": "6c7eafeb028e7924", | ||
"metadata": { | ||
"collapsed": true, | ||
"ExecuteTime": { | ||
"end_time": "2024-06-08T20:24:43.262799Z", | ||
"start_time": "2024-06-08T20:24:43.241174Z" | ||
} | ||
}, | ||
"source": [ | ||
"from inspect import Signature, Parameter\n", | ||
"\n", | ||
"\n", | ||
"def auto_label(self):\n", | ||
" cname = self.__class__.__qualname__\n", | ||
" signature = Signature.from_callable(self.__init__)\n", | ||
" args, keyword_only = [], False\n", | ||
"\n", | ||
" for p in signature.parameters.values():\n", | ||
" v = getattr(self, p.name, p.default)\n", | ||
"\n", | ||
" if p.kind in (Parameter.VAR_POSITIONAL, Parameter.VAR_KEYWORD):\n", | ||
" raise ValueError(f\"Unsupported parameter type {p.kind}\")\n", | ||
"\n", | ||
" if p.kind == Parameter.KEYWORD_ONLY:\n", | ||
" keyword_only = True\n", | ||
" elif isinstance(p.default, (type(None), str, bool)):\n", | ||
" keyword_only = True\n", | ||
"\n", | ||
" if v == p.default:\n", | ||
" # skip argument if not equal to default\n", | ||
" if keyword_only or not isinstance(v, (int, float)):\n", | ||
" keyword_only = True\n", | ||
" continue\n", | ||
"\n", | ||
" if keyword_only:\n", | ||
" args.append(f\"{p.name}={v!r}\")\n", | ||
" else:\n", | ||
" args.append(f\"{v!r}\")\n", | ||
"\n", | ||
" args = \", \".join(args)\n", | ||
"\n", | ||
" return f\"{cname}({args})\"\n", | ||
"\n", | ||
"\n", | ||
"class Indicator:\n", | ||
" \"\"\"Implements a basic __repr__ based on __init__ signature\"\"\"\n", | ||
"\n", | ||
" def __init_subclass__(cls, **kwargs):\n", | ||
" super().__init_subclass__(**kwargs)\n", | ||
" cls.__repr__ = auto_label\n", | ||
"\n", | ||
"\n" | ||
], | ||
"outputs": [], | ||
"execution_count": 19 | ||
}, | ||
{ | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-06-08T20:24:43.889003Z", | ||
"start_time": "2024-06-08T20:24:43.871067Z" | ||
} | ||
}, | ||
"cell_type": "code", | ||
"source": [ | ||
"from dataclasses import dataclass, field\n", | ||
"\n", | ||
"@dataclass(frozen=True)\n", | ||
"class EMA(Indicator):\n", | ||
" period: int = 20\n", | ||
" adjust : bool = False\n", | ||
" item: str = field(default=None)\n", | ||
"\n", | ||
" def __call__(self, prices):\n", | ||
" print(self.__class__.__name__, self.period, self.item)\n", | ||
" return prices\n", | ||
"\n", | ||
"\n", | ||
"for i in EMA(20), EMA(adjust=True), EMA(20, item=\"close\"):\n", | ||
" print(i)\n", | ||
"\n" | ||
], | ||
"id": "378856a846836d89", | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"EMA(20)\n", | ||
"EMA(20, adjust=True)\n", | ||
"EMA(20, item='close')\n" | ||
] | ||
} | ||
], | ||
"execution_count": 20 | ||
}, | ||
{ | ||
"metadata": {}, | ||
"cell_type": "code", | ||
"outputs": [], | ||
"execution_count": null, | ||
"source": "", | ||
"id": "3cce5ce0a9e1eec4" | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 2 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython2", | ||
"version": "2.7.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.