Nyoibo
======
.. image:: https://readthedocs.org/projects/nyoibo/badge/?version=latest
:target: https://nyoibo.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status
.. image:: https://circleci.com/gh/pity7736/nyoibo.svg?style=shield
:target: https://circleci.com/gh/pity7736/nyoibo
Nyoibo is an easy way to avoid repetitive code with "private" attributes in
Python.
`See full documentation <https://nyoibo.readthedocs.io/en/latest/>`_
Installation
------------
You can install nyoibo with pip. Nyoibo requires python 3.9 or higher.
``pip install nyoibo``
What does "nyoibo" mean?
------------------------
Nyoibo is a mystical staff given to Son Goku by his grandfather Son Gohan.
.. image:: ./nyoibo.png
:width: 300px
:height: 300px
:alt: nyoibo
Usage
-----
Instead of doing this:
.. code-block:: python
class Example:
def __init__(self, value=None, other_value=None, default='hello')
self._value = value
self._other_value = other_value
self._default = default
def get_value(self):
return self._value
value = property(get_value)
def get_other_value(self):
return self._other_value
def set_other_value(self, value):
self._other_value = value
other_value = property(get_other_value, set_other_value)
def do_something(self):
return f'{self._default} world'
You can do this:
.. code-block:: python
from nyoibo import Entity, fields
class Example(Entity):
_value = fields.StrField()
_other_value = fields.IntField(mutable=True)
_default = fields.StrField(private=True, default_value='hello')
def do_something(self):
return f'{self._default} world'
In both cases you could use this code like this:
.. code-block:: python
example = Example(value='some value', other_value=10)
assert example.value == 'some value'
assert example.get_value() == 'some value'
assert example.get_other_value() == 10
example.other_value = 15
assert example.get_other_value() == 15
assert example.do_something() == 'hello world'
Why not use dataclass decorator?
--------------------------------
``@dataclass`` decorator helps to avoid to write the ``__init__`` method but if you
want to use this approach (information hiding and encapsulation), you need to
write getters and setters anyway. Furthermore, with ``nyoibo`` you get extra
features like casting to right value (due to static typing), validations, override ``__init__`` method and so on.
Above example with ``dataclass`` decorator:
.. code-block:: python
from dataclasses import dataclass
@dataclass
class Example:
_value: str
_other_value: int
_default: str = 'hello'
def get_value(self):
return self._value
value = property(get_value)
def get_other_value(self):
return self._other_value
def set_other_value(self, value):
self._other_value = value
other_value = property(get_other_value, set_other_value)
def do_something(self):
return f'{self._default} world'
Even this code doesn't work because ``__init__`` method has ``_value``,
``_other_value`` and ``_default`` arguments. Therefore the instantiation will be:
.. code-block:: python
example = Example(_value='some value', _other_value=10)
License
-------
Distributed under the terms of the LGPLv3 license.
See `license <https://github.com/pity7736/nyoibo/blob/master/LICENSE>`_.
Raw data
{
"_id": null,
"home_page": "https://github.com/pity7736/nyoibo",
"name": "nyoibo",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": "",
"keywords": "accessors private",
"author": "Juli\u00e1n Cort\u00e9s",
"author_email": "julian.cortes77@protonmail.com",
"download_url": "https://files.pythonhosted.org/packages/c9/50/1983591f2988df8fa66e93e5f62d7bd486db006466ceef0ea63560a98e6d/nyoibo-0.6.0.tar.gz",
"platform": null,
"description": "Nyoibo\n======\n\n.. image:: https://readthedocs.org/projects/nyoibo/badge/?version=latest\n :target: https://nyoibo.readthedocs.io/en/latest/?badge=latest\n :alt: Documentation Status\n\n.. image:: https://circleci.com/gh/pity7736/nyoibo.svg?style=shield\n :target: https://circleci.com/gh/pity7736/nyoibo\n\nNyoibo is an easy way to avoid repetitive code with \"private\" attributes in\nPython.\n\n`See full documentation <https://nyoibo.readthedocs.io/en/latest/>`_\n\n\nInstallation\n------------\n\nYou can install nyoibo with pip. Nyoibo requires python 3.9 or higher.\n\n``pip install nyoibo``\n\n\nWhat does \"nyoibo\" mean?\n------------------------\n\nNyoibo is a mystical staff given to Son Goku by his grandfather Son Gohan.\n\n.. image:: ./nyoibo.png\n :width: 300px\n :height: 300px\n :alt: nyoibo\n\n\nUsage\n-----\n\nInstead of doing this:\n\n.. code-block:: python\n\n class Example:\n\n def __init__(self, value=None, other_value=None, default='hello')\n self._value = value\n self._other_value = other_value\n self._default = default\n\n def get_value(self):\n return self._value\n\n value = property(get_value)\n\n def get_other_value(self):\n return self._other_value\n\n def set_other_value(self, value):\n self._other_value = value\n\n other_value = property(get_other_value, set_other_value)\n\n def do_something(self):\n return f'{self._default} world'\n\n\nYou can do this:\n\n.. code-block:: python\n\n from nyoibo import Entity, fields\n\n\n class Example(Entity):\n _value = fields.StrField()\n _other_value = fields.IntField(mutable=True)\n _default = fields.StrField(private=True, default_value='hello')\n\n def do_something(self):\n return f'{self._default} world'\n\nIn both cases you could use this code like this:\n\n.. code-block:: python\n\n example = Example(value='some value', other_value=10)\n\n assert example.value == 'some value'\n assert example.get_value() == 'some value'\n assert example.get_other_value() == 10\n example.other_value = 15\n assert example.get_other_value() == 15\n assert example.do_something() == 'hello world'\n\n\nWhy not use dataclass decorator?\n--------------------------------\n\n``@dataclass`` decorator helps to avoid to write the ``__init__`` method but if you\nwant to use this approach (information hiding and encapsulation), you need to\nwrite getters and setters anyway. Furthermore, with ``nyoibo`` you get extra\nfeatures like casting to right value (due to static typing), validations, override ``__init__`` method and so on.\n\nAbove example with ``dataclass`` decorator:\n\n.. code-block:: python\n\n from dataclasses import dataclass\n\n\n @dataclass\n class Example:\n _value: str\n _other_value: int\n _default: str = 'hello'\n\n def get_value(self):\n return self._value\n\n value = property(get_value)\n\n def get_other_value(self):\n return self._other_value\n\n def set_other_value(self, value):\n self._other_value = value\n\n other_value = property(get_other_value, set_other_value)\n\n def do_something(self):\n return f'{self._default} world'\n\nEven this code doesn't work because ``__init__`` method has ``_value``,\n``_other_value`` and ``_default`` arguments. Therefore the instantiation will be:\n\n.. code-block:: python\n\n example = Example(_value='some value', _other_value=10)\n\n\n\nLicense\n-------\n\nDistributed under the terms of the LGPLv3 license.\n\nSee `license <https://github.com/pity7736/nyoibo/blob/master/LICENSE>`_.\n",
"bugtrack_url": null,
"license": "",
"summary": "Implement attributes accessors in an easy way",
"version": "0.6.0",
"project_urls": {
"Homepage": "https://github.com/pity7736/nyoibo"
},
"split_keywords": [
"accessors",
"private"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c9501983591f2988df8fa66e93e5f62d7bd486db006466ceef0ea63560a98e6d",
"md5": "47944dce5088b9fa47911140dadf2afb",
"sha256": "197a3773ecd0face4642779a00a664a308305b62b044fef0560d1c7c29bed9f0"
},
"downloads": -1,
"filename": "nyoibo-0.6.0.tar.gz",
"has_sig": false,
"md5_digest": "47944dce5088b9fa47911140dadf2afb",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 142232,
"upload_time": "2023-11-02T01:18:28",
"upload_time_iso_8601": "2023-11-02T01:18:28.885538Z",
"url": "https://files.pythonhosted.org/packages/c9/50/1983591f2988df8fa66e93e5f62d7bd486db006466ceef0ea63560a98e6d/nyoibo-0.6.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-02 01:18:28",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "pity7736",
"github_project": "nyoibo",
"travis_ci": false,
"coveralls": true,
"github_actions": false,
"circle": true,
"lcname": "nyoibo"
}