diff --git a/examples/lazy_loading.ipynb b/examples/lazy_loading.ipynb index ce08b53..1446856 100644 --- a/examples/lazy_loading.ipynb +++ b/examples/lazy_loading.ipynb @@ -15,18 +15,21 @@ }, { "cell_type": "markdown", + "metadata": { + "collapsed": false + }, "source": [ "# Define a custom Descriptor which lazy-loads data from File\n", "The following example is a minimal version of a lazy-loading implementation.\n", "It is purely for illustration purpose. Lazy Loading is not a feature of `ZnInit` but can be relatively easily implemented with it." - ], - "metadata": { - "collapsed": false - } + ] }, { "cell_type": "code", "execution_count": 2, + "metadata": { + "collapsed": false + }, "outputs": [], "source": [ "class LazyData:\n", @@ -51,7 +54,7 @@ " self.data_file.write_text(json.dumps(data))\n", "\n", " def __get__(self, instance, owner=None):\n", - " \"\"\"reading the class attribute\"\"\"\n", + " \"\"\"Reading the class attribute\"\"\"\n", " if instance is None:\n", " return self\n", " value = instance.__dict__.get(self.name, self.default)\n", @@ -61,51 +64,51 @@ " return instance.__dict__.get(self.name, self.default)\n", "\n", " def __set__(self, instance, value):\n", - " \"\"\"writing the class attribute\"\"\"\n", + " \"\"\"Writing the class attribute\"\"\"\n", " if value is LazyData:\n", " return\n", " self._instance = instance\n", " instance.__dict__[self.name] = value\n", " self.save_to_file(value)" - ], - "metadata": { - "collapsed": false - } + ] }, { "cell_type": "markdown", - "source": [ - "Write a new `ZnInit` class, which defines two lazy descriptors. We have to disable `use_repr` because it will read the data from file automatically." - ], "metadata": { "collapsed": false - } + }, + "source": [ + "Write a new `ZnInit` class, which defines two lazy descriptors. We have to disable `use_repr` because it will read the data from file automatically." + ] }, { "cell_type": "code", "execution_count": 3, + "metadata": { + "collapsed": false + }, "outputs": [], "source": [ "class SaveToFileData(ZnInit):\n", " name: str = LazyDataDescriptor(LazyData, use_repr=False)\n", " age: int = LazyDataDescriptor(LazyData, use_repr=False)" - ], - "metadata": { - "collapsed": false - } + ] }, { "cell_type": "markdown", - "source": [ - "Instantiating a new instance will save the data into a `data.json` file." - ], "metadata": { "collapsed": false - } + }, + "source": [ + "Instantiating a new instance will save the data into a `data.json` file." + ] }, { "cell_type": "code", "execution_count": 4, + "metadata": { + "collapsed": false + }, "outputs": [ { "name": "stdout", @@ -127,34 +130,34 @@ "data = SaveToFileData(name=\"Fabian\", age=27)\n", "print(data.__dict__)\n", "data" - ], - "metadata": { - "collapsed": false - } + ] }, { "cell_type": "markdown", - "source": [ - "When we create a new instance of the class the `__dict__` will be empty. But when reading the attributes it will be filled by the data from `data.json`." - ], "metadata": { "collapsed": false - } + }, + "source": [ + "When we create a new instance of the class the `__dict__` will be empty. But when reading the attributes it will be filled by the data from `data.json`." + ] }, { "cell_type": "code", "execution_count": 5, + "metadata": { + "collapsed": false + }, "outputs": [], "source": [ "data = SaveToFileData()" - ], - "metadata": { - "collapsed": false - } + ] }, { "cell_type": "code", "execution_count": 6, + "metadata": { + "collapsed": false + }, "outputs": [ { "data": { @@ -167,14 +170,14 @@ ], "source": [ "data.__dict__" - ], - "metadata": { - "collapsed": false - } + ] }, { "cell_type": "code", "execution_count": 7, + "metadata": { + "collapsed": false + }, "outputs": [ { "data": { @@ -187,14 +190,14 @@ ], "source": [ "data.name" - ], - "metadata": { - "collapsed": false - } + ] }, { "cell_type": "code", "execution_count": 8, + "metadata": { + "collapsed": false + }, "outputs": [ { "data": { @@ -207,14 +210,14 @@ ], "source": [ "data.__dict__" - ], - "metadata": { - "collapsed": false - } + ] }, { "cell_type": "code", "execution_count": 9, + "metadata": { + "collapsed": false + }, "outputs": [ { "data": { @@ -227,14 +230,14 @@ ], "source": [ "data.age" - ], - "metadata": { - "collapsed": false - } + ] }, { "cell_type": "code", "execution_count": 10, + "metadata": { + "collapsed": false + }, "outputs": [ { "data": { @@ -247,10 +250,7 @@ ], "source": [ "data.__dict__" - ], - "metadata": { - "collapsed": false - } + ] } ], "metadata": { diff --git a/zninit/core/__init__.py b/zninit/core/__init__.py index 73218d4..e41acaf 100644 --- a/zninit/core/__init__.py +++ b/zninit/core/__init__.py @@ -75,6 +75,7 @@ def _handle_args(args, kwargs, kwarg_names, cls_name): TypeError if more positional arguments are given than keyword arguments if a keyword argument is given multiple times + """ if len(args) > len(kwarg_names): raise TypeError( @@ -111,6 +112,7 @@ def get_auto_init( # noqa: C901 typically this is Node.__init__ allow_args: bool allow args in the __init__. Otherwise only kwargs are allowed + """ kwargs_no_default = [] if kwargs_no_default is None else kwargs_no_default kwargs_with_default = {} if kwargs_with_default is None else kwargs_with_default @@ -143,9 +145,9 @@ def auto_init(self, *args, **kwargs): # noqa: C901 if kwarg_name not in priority_kwargs: required_keys.append(kwarg_name) - init_kwargs.update({ - name: kwargs.pop(name, value) for name, value in kwargs_with_default.items() - }) + init_kwargs.update( + {name: kwargs.pop(name, value) for name, value in kwargs_with_default.items()} + ) super_init(self, **kwargs) # call the super_init explicitly instead of super # must call the super_init first e.g. it is required to set the node_name @@ -238,6 +240,7 @@ def _get_auto_init_signature(cls) -> typing.Tuple[list, dict, list]: kwargs_with_default: dict a dict of {name: default} that will be converted to kwargs signature_params: inspect.Parameter + """ signature_params = [] cls_annotations = cls.__annotations__ # pylint: disable=no-member @@ -277,6 +280,7 @@ class ZnInit: # pylint: disable=R0903 A list of kwargs that should be prioritized in the __init__. These kwargs will be set in the given order before the other args / kwargs are set. + """ init_descriptors: typing.List[Descriptor] = [Descriptor] @@ -305,6 +309,7 @@ def __init__(self): Raises ------ TypeError: ZnInit.__init__() got an unexpected keyword argument ... + """ def __init_subclass__(cls, allow_args: bool = True, **kwargs): diff --git a/zninit/descriptor/__init__.py b/zninit/descriptor/__init__.py index 0e5a22c..990aebb 100644 --- a/zninit/descriptor/__init__.py +++ b/zninit/descriptor/__init__.py @@ -86,6 +86,7 @@ def __init__( on_setattr: Callable, default=None A callable that is run whenever an attribute is set via 'class.myattr = value' or 'setattr(class, "mattr", value)'. + """ self._default = default self._owner = owner @@ -140,6 +141,7 @@ def annotation(self): ------ KeyError: if type checking and the descriptor has no annotation. + """ try: annotations_ = self.owner.__annotations__ @@ -164,6 +166,7 @@ def __get__(self, instance, owner=None): Raises ------ AttributeError: if the value is not in the instance.__dict__ or in self.default + """ self._instance = instance if instance is None: