Skip to content
This repository has been archived by the owner on Nov 15, 2024. It is now read-only.

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Sep 30, 2024
1 parent d0b9384 commit fa04d37
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 57 deletions.
108 changes: 54 additions & 54 deletions examples/lazy_loading.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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": {
Expand All @@ -167,14 +170,14 @@
],
"source": [
"data.__dict__"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
Expand All @@ -187,14 +190,14 @@
],
"source": [
"data.name"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
Expand All @@ -207,14 +210,14 @@
],
"source": [
"data.__dict__"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
Expand All @@ -227,14 +230,14 @@
],
"source": [
"data.age"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
Expand All @@ -247,10 +250,7 @@
],
"source": [
"data.__dict__"
],
"metadata": {
"collapsed": false
}
]
}
],
"metadata": {
Expand Down
11 changes: 8 additions & 3 deletions zninit/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions zninit/descriptor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -140,6 +141,7 @@ def annotation(self):
------
KeyError:
if type checking and the descriptor has no annotation.
"""
try:
annotations_ = self.owner.__annotations__
Expand All @@ -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:
Expand Down

0 comments on commit fa04d37

Please sign in to comment.