|
4700 | 4700 | "text/markdown": [
|
4701 | 4701 | "---\n",
|
4702 | 4702 | "\n",
|
4703 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L829){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 4703 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L861){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
4704 | 4704 | "\n",
|
4705 | 4705 | "#### fastuple\n",
|
4706 | 4706 | "\n",
|
|
4711 | 4711 | "text/plain": [
|
4712 | 4712 | "---\n",
|
4713 | 4713 | "\n",
|
4714 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L829){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 4714 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L861){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
4715 | 4715 | "\n",
|
4716 | 4716 | "#### fastuple\n",
|
4717 | 4717 | "\n",
|
|
4785 | 4785 | "text/markdown": [
|
4786 | 4786 | "---\n",
|
4787 | 4787 | "\n",
|
4788 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L848){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 4788 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L880){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
4789 | 4789 | "\n",
|
4790 | 4790 | "##### fastuple.add\n",
|
4791 | 4791 | "\n",
|
|
4796 | 4796 | "text/plain": [
|
4797 | 4797 | "---\n",
|
4798 | 4798 | "\n",
|
4799 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L848){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 4799 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L880){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
4800 | 4800 | "\n",
|
4801 | 4801 | "##### fastuple.add\n",
|
4802 | 4802 | "\n",
|
|
4835 | 4835 | "text/markdown": [
|
4836 | 4836 | "---\n",
|
4837 | 4837 | "\n",
|
4838 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L844){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 4838 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L876){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
4839 | 4839 | "\n",
|
4840 | 4840 | "##### fastuple.mul\n",
|
4841 | 4841 | "\n",
|
|
4846 | 4846 | "text/plain": [
|
4847 | 4847 | "---\n",
|
4848 | 4848 | "\n",
|
4849 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L844){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 4849 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L876){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
4850 | 4850 | "\n",
|
4851 | 4851 | "##### fastuple.mul\n",
|
4852 | 4852 | "\n",
|
|
4996 | 4996 | "text/markdown": [
|
4997 | 4997 | "---\n",
|
4998 | 4998 | "\n",
|
4999 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L875){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 4999 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L907){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
5000 | 5000 | "\n",
|
5001 | 5001 | "### bind\n",
|
5002 | 5002 | "\n",
|
|
5007 | 5007 | "text/plain": [
|
5008 | 5008 | "---\n",
|
5009 | 5009 | "\n",
|
5010 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L875){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 5010 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L907){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
5011 | 5011 | "\n",
|
5012 | 5012 | "### bind\n",
|
5013 | 5013 | "\n",
|
|
5645 | 5645 | "outputs": [],
|
5646 | 5646 | "source": [
|
5647 | 5647 | "#|export\n",
|
5648 |
| - "def patch_to(cls, as_prop=False, cls_method=False):\n", |
| 5648 | + "def patch_to(cls, as_prop=False, cls_method=False, set_prop=False):\n", |
5649 | 5649 | " \"Decorator: add `f` to `cls`\"\n",
|
5650 | 5650 | " if not isinstance(cls, (tuple,list)): cls=(cls,)\n",
|
5651 | 5651 | " def _inner(f):\n",
|
|
5657 | 5657 | " nf.__qualname__ = f\"{c_.__name__}.{nm}\"\n",
|
5658 | 5658 | " if cls_method: setattr(c_, nm, _clsmethod(nf))\n",
|
5659 | 5659 | " else:\n",
|
5660 |
| - " if as_prop: setattr(c_, nm, property(nf))\n", |
| 5660 | + " if set_prop: setattr(c_, nm, getattr(c_, nm).setter(nf))\n", |
| 5661 | + " elif as_prop: setattr(c_, nm, property(nf))\n", |
5661 | 5662 | " else:\n",
|
5662 | 5663 | " onm = '_orig_'+nm\n",
|
5663 | 5664 | " if hasattr(c_, nm) and not hasattr(c_, onm): setattr(c_, onm, getattr(c_, nm))\n",
|
|
5753 | 5754 | "test_eq(t.add_ten, 14)"
|
5754 | 5755 | ]
|
5755 | 5756 | },
|
| 5757 | + { |
| 5758 | + "cell_type": "markdown", |
| 5759 | + "metadata": {}, |
| 5760 | + "source": [ |
| 5761 | + "Once you have a property, you can assign a setter with `set_prop=True`:" |
| 5762 | + ] |
| 5763 | + }, |
| 5764 | + { |
| 5765 | + "cell_type": "code", |
| 5766 | + "execution_count": null, |
| 5767 | + "metadata": {}, |
| 5768 | + "outputs": [], |
| 5769 | + "source": [ |
| 5770 | + "class _T2():\n", |
| 5771 | + " def __init__(self, val): self._val = val\n", |
| 5772 | + "\n", |
| 5773 | + "@patch_to(_T2, as_prop=True)\n", |
| 5774 | + "def val(self): return self._val\n", |
| 5775 | + "\n", |
| 5776 | + "t = _T2(2)\n", |
| 5777 | + "test_eq(t.val, 2)\n", |
| 5778 | + "\n", |
| 5779 | + "@patch_to(_T2, set_prop=True)\n", |
| 5780 | + "def val(self, val): self._val = val\n", |
| 5781 | + "\n", |
| 5782 | + "t.val = 3\n", |
| 5783 | + "test_eq(t.val, 3)" |
| 5784 | + ] |
| 5785 | + }, |
5756 | 5786 | {
|
5757 | 5787 | "cell_type": "markdown",
|
5758 | 5788 | "metadata": {},
|
|
5785 | 5815 | "outputs": [],
|
5786 | 5816 | "source": [
|
5787 | 5817 | "#|export\n",
|
5788 |
| - "def patch(f=None, *, as_prop=False, cls_method=False):\n", |
| 5818 | + "def patch(f=None, *, as_prop=False, cls_method=False, set_prop=False):\n", |
5789 | 5819 | " \"Decorator: add `f` to the first parameter's class (based on f's type annotations)\"\n",
|
5790 |
| - " if f is None: return partial(patch, as_prop=as_prop, cls_method=cls_method)\n", |
| 5820 | + " if f is None: return partial(patch, as_prop=as_prop, cls_method=cls_method, set_prop=set_prop)\n", |
5791 | 5821 | " ann,glb,loc = get_annotations_ex(f)\n",
|
5792 | 5822 | " cls = union2tuple(eval_type(ann.pop('cls') if cls_method else next(iter(ann.values())), glb, loc))\n",
|
5793 |
| - " return patch_to(cls, as_prop=as_prop, cls_method=cls_method)(f)" |
| 5823 | + " return patch_to(cls, as_prop=as_prop, cls_method=cls_method, set_prop=set_prop)(f)" |
5794 | 5824 | ]
|
5795 | 5825 | },
|
5796 | 5826 | {
|
|
5847 | 5877 | "cell_type": "markdown",
|
5848 | 5878 | "metadata": {},
|
5849 | 5879 | "source": [
|
5850 |
| - "Just like `patch_to` decorator you can use `as_prop` and `cls_method` parameters with `patch` decorator:" |
| 5880 | + "Just like `patch_to` decorator you can use `as_prop`, `set_prop`, and `cls_method` parameters with `patch` decorator:" |
5851 | 5881 | ]
|
5852 | 5882 | },
|
5853 | 5883 | {
|
|
5863 | 5893 | "test_eq(t.add_ten, 14)"
|
5864 | 5894 | ]
|
5865 | 5895 | },
|
| 5896 | + { |
| 5897 | + "cell_type": "code", |
| 5898 | + "execution_count": null, |
| 5899 | + "metadata": {}, |
| 5900 | + "outputs": [], |
| 5901 | + "source": [ |
| 5902 | + "class _T2():\n", |
| 5903 | + " def __init__(self, val): self._val = val\n", |
| 5904 | + "\n", |
| 5905 | + "@patch(as_prop=True)\n", |
| 5906 | + "def val(self:_T2): return self._val\n", |
| 5907 | + "\n", |
| 5908 | + "t = _T2(2)\n", |
| 5909 | + "test_eq(t.val, 2)\n", |
| 5910 | + "\n", |
| 5911 | + "@patch(set_prop=True)\n", |
| 5912 | + "def val(self:_T2, val): self._val = val\n", |
| 5913 | + "\n", |
| 5914 | + "t.val = 3\n", |
| 5915 | + "test_eq(t.val, 3)" |
| 5916 | + ] |
| 5917 | + }, |
5866 | 5918 | {
|
5867 | 5919 | "cell_type": "code",
|
5868 | 5920 | "execution_count": null,
|
|
5972 | 6024 | "text/markdown": [
|
5973 | 6025 | "---\n",
|
5974 | 6026 | "\n",
|
5975 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L1047){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 6027 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1080){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
5976 | 6028 | "\n",
|
5977 | 6029 | "#### ImportEnum\n",
|
5978 | 6030 | "\n",
|
5979 |
| - "> ImportEnum (new_class_name, names, module=None, qualname=None, type=None,\n", |
| 6031 | + "> ImportEnum (value, names=None, module=None, qualname=None, type=None,\n", |
5980 | 6032 | "> start=1, boundary=None)\n",
|
5981 | 6033 | "\n",
|
5982 | 6034 | "*An `Enum` that can have its values imported*"
|
5983 | 6035 | ],
|
5984 | 6036 | "text/plain": [
|
5985 | 6037 | "---\n",
|
5986 | 6038 | "\n",
|
5987 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L1047){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 6039 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1080){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
5988 | 6040 | "\n",
|
5989 | 6041 | "#### ImportEnum\n",
|
5990 | 6042 | "\n",
|
5991 |
| - "> ImportEnum (new_class_name, names, module=None, qualname=None, type=None,\n", |
| 6043 | + "> ImportEnum (value, names=None, module=None, qualname=None, type=None,\n", |
5992 | 6044 | "> start=1, boundary=None)\n",
|
5993 | 6045 | "\n",
|
5994 | 6046 | "*An `Enum` that can have its values imported*"
|
|
6037 | 6089 | "text/markdown": [
|
6038 | 6090 | "---\n",
|
6039 | 6091 | "\n",
|
6040 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L1055){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 6092 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1088){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
6041 | 6093 | "\n",
|
6042 | 6094 | "#### StrEnum\n",
|
6043 | 6095 | "\n",
|
6044 |
| - "> StrEnum (new_class_name, names, module=None, qualname=None, type=None,\n", |
| 6096 | + "> StrEnum (value, names=None, module=None, qualname=None, type=None,\n", |
6045 | 6097 | "> start=1, boundary=None)\n",
|
6046 | 6098 | "\n",
|
6047 | 6099 | "*An `ImportEnum` that behaves like a `str`*"
|
6048 | 6100 | ],
|
6049 | 6101 | "text/plain": [
|
6050 | 6102 | "---\n",
|
6051 | 6103 | "\n",
|
6052 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L1055){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 6104 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1088){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
6053 | 6105 | "\n",
|
6054 | 6106 | "#### StrEnum\n",
|
6055 | 6107 | "\n",
|
6056 |
| - "> StrEnum (new_class_name, names, module=None, qualname=None, type=None,\n", |
| 6108 | + "> StrEnum (value, names=None, module=None, qualname=None, type=None,\n", |
6057 | 6109 | "> start=1, boundary=None)\n",
|
6058 | 6110 | "\n",
|
6059 | 6111 | "*An `ImportEnum` that behaves like a `str`*"
|
|
6102 | 6154 | "text/markdown": [
|
6103 | 6155 | "---\n",
|
6104 | 6156 | "\n",
|
6105 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L1065){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 6157 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1098){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
6106 | 6158 | "\n",
|
6107 | 6159 | "#### ValEnum\n",
|
6108 | 6160 | "\n",
|
6109 |
| - "> ValEnum (new_class_name, names, module=None, qualname=None, type=None,\n", |
| 6161 | + "> ValEnum (value, names=None, module=None, qualname=None, type=None,\n", |
6110 | 6162 | "> start=1, boundary=None)\n",
|
6111 | 6163 | "\n",
|
6112 | 6164 | "*An `ImportEnum` that stringifies using values*"
|
6113 | 6165 | ],
|
6114 | 6166 | "text/plain": [
|
6115 | 6167 | "---\n",
|
6116 | 6168 | "\n",
|
6117 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L1065){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 6169 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1098){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
6118 | 6170 | "\n",
|
6119 | 6171 | "#### ValEnum\n",
|
6120 | 6172 | "\n",
|
6121 |
| - "> ValEnum (new_class_name, names, module=None, qualname=None, type=None,\n", |
| 6173 | + "> ValEnum (value, names=None, module=None, qualname=None, type=None,\n", |
6122 | 6174 | "> start=1, boundary=None)\n",
|
6123 | 6175 | "\n",
|
6124 | 6176 | "*An `ImportEnum` that stringifies using values*"
|
|
6191 | 6243 | "text/markdown": [
|
6192 | 6244 | "---\n",
|
6193 | 6245 | "\n",
|
6194 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L1070){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 6246 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1103){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
6195 | 6247 | "\n",
|
6196 | 6248 | "#### Stateful\n",
|
6197 | 6249 | "\n",
|
|
6202 | 6254 | "text/plain": [
|
6203 | 6255 | "---\n",
|
6204 | 6256 | "\n",
|
6205 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L1070){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 6257 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1103){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
6206 | 6258 | "\n",
|
6207 | 6259 | "#### Stateful\n",
|
6208 | 6260 | "\n",
|
|
6353 | 6405 | "text/markdown": [
|
6354 | 6406 | "---\n",
|
6355 | 6407 | "\n",
|
6356 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L1107){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 6408 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1140){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
6357 | 6409 | "\n",
|
6358 | 6410 | "#### PrettyString\n",
|
6359 | 6411 | "\n",
|
|
6364 | 6416 | "text/plain": [
|
6365 | 6417 | "---\n",
|
6366 | 6418 | "\n",
|
6367 |
| - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/basics.py#L1107){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 6419 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1140){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
6368 | 6420 | "\n",
|
6369 | 6421 | "#### PrettyString\n",
|
6370 | 6422 | "\n",
|
|
6489 | 6541 | {
|
6490 | 6542 | "data": {
|
6491 | 6543 | "text/plain": [
|
6492 |
| - "10" |
| 6544 | + "22" |
6493 | 6545 | ]
|
6494 | 6546 | },
|
6495 | 6547 | "execution_count": null,
|
|
0 commit comments