Skip to main content

Memory efficient Python objects

Project description

https://github.com/nucleic/atom/workflows/Continuous%20Integration/badge.svg https://github.com/nucleic/atom/workflows/Documentation%20building/badge.svg https://codecov.io/gh/nucleic/atom/branch/main/graph/badge.svg Documentation Status Ruff

Atom is a framework for creating memory efficient Python objects with enhanced features such as dynamic initialization, validation, and change notification for object attributes. It provides the default model binding behavior for the Enaml UI framework.

Examples:

from atom.api import Atom, Str, Range, Bool, observe


class Person(Atom):
    """ A simple class representing a person object.

    """
    last_name = Str()

    first_name = Str()

    age = Range(low=0)

    debug = Bool(False)

    @observe('age')
    def debug_print(self, change):
        """ Prints out a debug message whenever the person's age changes.

        """
        if self.debug:
            templ = "{first} {last} is {age} years old."
            s = templ.format(
                first=self.first_name, last=self.last_name, age=self.age,
            )
            print(s)

    def _default_first_name(self):
        return 'John'


john = Person(last_name='Doe', age=42)
john.debug = True
john.age = 43  # prints message
john.age = 'forty three'   # raises TypeError

Starting with atom 0.8.0 atom object can also be defined using type annotations.

from atom.api import Atom, observe

class InventoryItem(Atom):
    """Class for keeping track of an item in inventory."""

    name: str
    unit_price: float
    quantity_on_hand: int = 0

    def total_cost(self) -> float:
        return self.unit_price * self.quantity_on_hand

    @observe("unit_price")
    def check_for_price_reduction(self, change):
        savings = change.get("oldvalue", 0) - change.get("value")
        if savings > 0:
            print(f"Save ${savings} now on {self.name}s!")

>>> w = InventoryItem(name="widget", unit_price=1.99, quantity_on_hand=10)
>>> w.unit_price = 1.00
Save $0.99 now on widgets!

For version information, see the Revision History.

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

atom-0.10.4.tar.gz (169.8 kB view hashes)

Uploaded source

Built Distributions

atom-0.10.4-cp312-cp312-win_amd64.whl (133.4 kB view hashes)

Uploaded cp312

atom-0.10.4-cp312-cp312-win32.whl (120.4 kB view hashes)

Uploaded cp312

atom-0.10.4-cp312-cp312-macosx_11_0_arm64.whl (160.0 kB view hashes)

Uploaded cp312

atom-0.10.4-cp312-cp312-macosx_10_9_x86_64.whl (164.2 kB view hashes)

Uploaded cp312

atom-0.10.4-cp311-cp311-win_amd64.whl (132.5 kB view hashes)

Uploaded cp311

atom-0.10.4-cp311-cp311-win32.whl (119.3 kB view hashes)

Uploaded cp311

atom-0.10.4-cp311-cp311-macosx_11_0_arm64.whl (158.3 kB view hashes)

Uploaded cp311

atom-0.10.4-cp311-cp311-macosx_10_9_x86_64.whl (161.5 kB view hashes)

Uploaded cp311

atom-0.10.4-cp310-cp310-win_amd64.whl (132.6 kB view hashes)

Uploaded cp310

atom-0.10.4-cp310-cp310-win32.whl (119.4 kB view hashes)

Uploaded cp310

atom-0.10.4-cp310-cp310-macosx_11_0_arm64.whl (158.5 kB view hashes)

Uploaded cp310

atom-0.10.4-cp310-cp310-macosx_10_9_x86_64.whl (161.6 kB view hashes)

Uploaded cp310

atom-0.10.4-cp39-cp39-win_amd64.whl (132.8 kB view hashes)

Uploaded cp39

atom-0.10.4-cp39-cp39-win32.whl (119.5 kB view hashes)

Uploaded cp39

atom-0.10.4-cp39-cp39-macosx_11_0_arm64.whl (158.5 kB view hashes)

Uploaded cp39

atom-0.10.4-cp39-cp39-macosx_10_9_x86_64.whl (161.6 kB view hashes)

Uploaded cp39

atom-0.10.4-cp38-cp38-win_amd64.whl (132.6 kB view hashes)

Uploaded cp38

atom-0.10.4-cp38-cp38-win32.whl (119.4 kB view hashes)

Uploaded cp38

atom-0.10.4-cp38-cp38-macosx_11_0_arm64.whl (158.2 kB view hashes)

Uploaded cp38

atom-0.10.4-cp38-cp38-macosx_10_9_x86_64.whl (161.4 kB view hashes)

Uploaded cp38

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