autoinit


Nameautoinit JSON
Version 1.1.1 PyPI version JSON
download
home_page
SummaryPython decorator for automatic initialization instance attributes
upload_time2023-03-24 17:41:24
maintainer
docs_urlNone
author
requires_python>=2.7
licenseThe MIT License (MIT) ===================== Copyright (c) 2020-2023, Constantine Kosmachevski Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords __init__ construcor oop sugar
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![Tests Pypy](https://github.com/oversider-kosma/autoinit/actions/workflows/pylint.yml/badge.svg?branch=master)
![Tests CPython](https://github.com/oversider-kosma/autoinit/actions/workflows/test_cpython.yml/badge.svg?branch=master)
![Tests Pypy](https://github.com/oversider-kosma/autoinit/actions/workflows/test_pypy.yml/badge.svg?branch=master)

# autoinit
> Python decorator for automatic initialization instance attributes

### What
```python3
import autoinit  # same as `from autoinit import autoinit`

@autoinit
class X:
    def __init__(self, a, b, c, d:int, e=99.99, f='some_default_value'):
	    print("__init__ do some another things")

x = X(42, 100, 500, None)
#  Output: "__init__ do some another things"

print(x.__dict__)
# Output: {'a': 42, 'b': 100, 'c': 500, 'd': None, 'e': 99.99, 'f': 'some_default_value'}
```

### How
$ ```pip install autoinit```
### Where
Tested in:
* CPython: 2.7, 3.5-3.11
* Pypy: 2.7, 3.5-3.9
* Jython: 2.7


...but with a high probability will work with other implementations as well.

### Why
A lot of elementary assignments inside `__init__` are a fairly frequent and rather dull case.

```python3
class FiveDimensionRecord:
    def __init__(self, x:int, y:int, z:int, u:int,
                 v:int, dt:typing.Optional[datetime]=None, description:str=''):
        self.x = x
        self.y = y
        self.z = z
        self.u = u
        self.v = v
        self.dt = dt or datetime.now()
        self.description = description
```

Dataclasses do not make it much more fun, mainly because you still cannot declare attributes in one line
```python3
@dataclass
class FiveDimensionRecord:
    x: int
    y: int
    z: int
    u: int
    v: int
    dt: 'typing.Any' = None
    description: str = ''

    def __post_init__(self):
        self.dt = self.dt or datetime.now()
```

With `autoinit` it looks much more compact and minimalistic

```python3
class FiveDimensionRecord:
    @autoinit
    def __init__(self, x:int, y:int, z:int,
                 u:int, v:int, dt=None, description:str=''):
        self.dt = self.dt or datetime.now()
```

### Options
* `@autoinit(exclude='attr')` or `@autoinit(exclude=['attr1', 'attr2]')`: skip specified attributes. Default: `[]`

* `@autoinit(no_warn=True)`: do not throw warning if decorator applied to non-`__init__` method. Default: `False`.

* `@autoinit(reverse=True)`: invert the order of actions - first call the wrapped method (which is usually `__init__`), and then do assignment. Default: `False`.

The decorator itself can be equally applied to both the `__init__` method and the entire class.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "autoinit",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=2.7",
    "maintainer_email": "",
    "keywords": "__init__,construcor,OOP,sugar",
    "author": "",
    "author_email": "Constantine Kosmachevski <oversider.kosma@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/40/f6/2803316df6781430897abb8016ea1db9762d8e267ca0cb56d756efd5761b/autoinit-1.1.1.tar.gz",
    "platform": null,
    "description": "![Tests Pypy](https://github.com/oversider-kosma/autoinit/actions/workflows/pylint.yml/badge.svg?branch=master)\n![Tests CPython](https://github.com/oversider-kosma/autoinit/actions/workflows/test_cpython.yml/badge.svg?branch=master)\n![Tests Pypy](https://github.com/oversider-kosma/autoinit/actions/workflows/test_pypy.yml/badge.svg?branch=master)\n\n# autoinit\n> Python decorator for automatic initialization instance attributes\n\n### What\n```python3\nimport autoinit  # same as `from autoinit import autoinit`\n\n@autoinit\nclass X:\n    def __init__(self, a, b, c, d:int, e=99.99, f='some_default_value'):\n\t    print(\"__init__ do some another things\")\n\nx = X(42, 100, 500, None)\n#  Output: \"__init__ do some another things\"\n\nprint(x.__dict__)\n# Output: {'a': 42, 'b': 100, 'c': 500, 'd': None, 'e': 99.99, 'f': 'some_default_value'}\n```\n\n### How\n$ ```pip install autoinit```\n### Where\nTested in:\n* CPython: 2.7, 3.5-3.11\n* Pypy: 2.7, 3.5-3.9\n* Jython: 2.7\n\n\n...but with a high probability will work with other implementations as well.\n\n### Why\nA lot of elementary assignments inside `__init__` are a fairly frequent and rather dull case.\n\n```python3\nclass FiveDimensionRecord:\n    def __init__(self, x:int, y:int, z:int, u:int,\n                 v:int, dt:typing.Optional[datetime]=None, description:str=''):\n        self.x = x\n        self.y = y\n        self.z = z\n        self.u = u\n        self.v = v\n        self.dt = dt or datetime.now()\n        self.description = description\n```\n\nDataclasses do not make it much more fun, mainly because you still cannot declare attributes in one line\n```python3\n@dataclass\nclass FiveDimensionRecord:\n    x: int\n    y: int\n    z: int\n    u: int\n    v: int\n    dt: 'typing.Any' = None\n    description: str = ''\n\n    def __post_init__(self):\n        self.dt = self.dt or datetime.now()\n```\n\nWith `autoinit` it looks much more compact and minimalistic\n\n```python3\nclass FiveDimensionRecord:\n    @autoinit\n    def __init__(self, x:int, y:int, z:int,\n                 u:int, v:int, dt=None, description:str=''):\n        self.dt = self.dt or datetime.now()\n```\n\n### Options\n* `@autoinit(exclude='attr')` or `@autoinit(exclude=['attr1', 'attr2]')`: skip specified attributes. Default: `[]`\n\n* `@autoinit(no_warn=True)`: do not throw warning if decorator applied to non-`__init__` method. Default: `False`.\n\n* `@autoinit(reverse=True)`: invert the order of actions - first call the wrapped method (which is usually `__init__`), and then do assignment. Default: `False`.\n\nThe decorator itself can be equally applied to both the `__init__` method and the entire class.\n",
    "bugtrack_url": null,
    "license": "The MIT License (MIT) =====================  Copyright (c) 2020-2023, Constantine Kosmachevski  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Python decorator for automatic initialization instance attributes",
    "version": "1.1.1",
    "split_keywords": [
        "__init__",
        "construcor",
        "oop",
        "sugar"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "50627647028c77e67c2fb75c803dc5a4490707932379e2297abb485075ab6abc",
                "md5": "55cffe969d52241e4326014d6ca675d5",
                "sha256": "3bd9d74330517a1796f16eb45ce9e5efe3a746030d3bdf78f46b64b58c150493"
            },
            "downloads": -1,
            "filename": "autoinit-1.1.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "55cffe969d52241e4326014d6ca675d5",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=2.7",
            "size": 5505,
            "upload_time": "2023-03-24T17:41:23",
            "upload_time_iso_8601": "2023-03-24T17:41:23.456236Z",
            "url": "https://files.pythonhosted.org/packages/50/62/7647028c77e67c2fb75c803dc5a4490707932379e2297abb485075ab6abc/autoinit-1.1.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "40f62803316df6781430897abb8016ea1db9762d8e267ca0cb56d756efd5761b",
                "md5": "ea00f7e88929578a86735f48ccff52b0",
                "sha256": "0c276ca9d5a7962416bc5b8d23e5acb9afad9880c5ec130c2cf4139b2b2f5836"
            },
            "downloads": -1,
            "filename": "autoinit-1.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "ea00f7e88929578a86735f48ccff52b0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=2.7",
            "size": 6320,
            "upload_time": "2023-03-24T17:41:24",
            "upload_time_iso_8601": "2023-03-24T17:41:24.934882Z",
            "url": "https://files.pythonhosted.org/packages/40/f6/2803316df6781430897abb8016ea1db9762d8e267ca0cb56d756efd5761b/autoinit-1.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-24 17:41:24",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "autoinit"
}
        
Elapsed time: 0.04720s