Skip to main content

Implement attributes accessors in an easy way

Project description

Documentation Status https://circleci.com/gh/pity7736/nyoibo.svg?style=shield

Nyoibo is an easy way to avoid repetitive code with “private” attributes in Python.

See full documentation

Installation

You can install nyoibo with pip. Nyoibo requires python 3.7 or higer.

pip install nyoibo

What does “nyoibo” mean?

Nyoibo is a mystical staff given to Son Goku by his grandfather Son Gohan.

nyoibo

Usage

Instead of doing this:

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:

from nyoibo import Entity, fields


class Example(Entity):
    _value = fields.StrField()
    _other_value = fields.IntField(immutable=False)
    _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:

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 (coming soon), override __init__ method and so on.

Above example with dataclass decorator:

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 becasue __init__ method has _value, _other_value and _default arguments. Therefore the instantation will be:

example = Example(_value='some value', _other_value=10)

License

Distributed under the terms of the LGPLv3 license.

See license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nyoibo-0.4.0.tar.gz (120.0 kB view details)

Uploaded Source

File details

Details for the file nyoibo-0.4.0.tar.gz.

File metadata

  • Download URL: nyoibo-0.4.0.tar.gz
  • Upload date:
  • Size: 120.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.9

File hashes

Hashes for nyoibo-0.4.0.tar.gz
Algorithm Hash digest
SHA256 2a5c81d63e29410547b262a9c1400dce2982f49bd54879b1e49ea0189e85ede6
MD5 ae968acd22dea9559ec8badcef9e2099
BLAKE2b-256 6453338698ea9a9453682565fb063971ad063a9fe564d00db150ff12bc527a5b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page