Skip to main content

SmartObject

SmartObject is a library to easily manipulate object attributes with the API commands, store and synchronize object data.

SmartObject is designed to quickly build IoT applications, but can be used in any other applications, which require combining local and distributed object storages and changing object properties via external API calls, automatically validating incoming data.

Mapped object attributes (called properties) can be automatically validated, saved, loaded, serialized and synchronized with the external services.

SmartObject looks like ORM, but it's different from ORM: object properties can be stored in storages of different type and combined together into a single data object.

SmartObject has built-in storage engines for files (JSON, YAML, MessagePack and CBOR) and databases: RDBMS via SQLAlchemy (can store objects) and Redis (can handle external properties only).

Property values are automatically processed, validated and synchronized with external services if required.

Note: all SmartObject methods are thread-safe (at least they should be :)

Example:

You have a team of people with heart-rate sensors. Data for each person is stored in local JSON files, heartbeat is stored in Redis database. How to implement this with SmartObject? Just a few lines of code:

import smartobject


class Person(smartobject.SmartObject):

    def __init__(self, name):
        self.name = name
        self.load_property_map('person.yml')
        self.apply_property_map()


smartobject.config.storage_dir = 'data'

smartobject.define_storage(smartobject.JSONStorage())
smartobject.define_storage(smartobject.RedisStorage(), 'r1')

people = smartobject.SmartObjectFactory(Person)

# create objects with factory
people.create(name='John')
people.create(name='Jane')

# create object manually
jack = Person('Jack')

# you can set a single prop
people.set_prop('John', 'sex', 'male')
people.set_prop('Jane', 'sex', 'female')
# or multiple props with dict
# heartbeat value is automatically written to Redis
jack.set_prop({'sex': 'male', 'heartbeat': 100})

# print object info (name and sex only)
from pprint import pprint
pprint(people.serialize('Jane', mode='info'))

people.save()
jack.save()

# clear Jack's sex
jack.set_prop('sex', None)
# load it back
jack.load()

# add Jack to factory
people.create(obj=jack)

# heartbeat value is automatically read from Redis
print('Heartbeat of Jack is: {}'.format(people.get('Jack').heartbeat))

The file person.yml is a property map for the Person object. It can be loaded from the external YAML file or specified directly, as Python dict.

The map for the above example looks like:

name:
    pk: true
    serialize: info
sex:
    type: str
    choices:
        - null
        - male
        - female
    store: true
    serialize: info
heartbeat:
    type: float
    external: true
    store: r1

Pretty simple, isn't it? You define a map, SmartObject does the job!

Install

pip3 install smartobject

Documentation

Full documentation is available at https://smartobject.readthedocs.io/

Metadata

Release files for smartobject 0.1.15

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for smartobject 0.1.15
File Size Uploaded
smartobject-0.1.15.tar.gz 15.2 kB Details

Release files / smartobject-0.1.15.tar.gz

Download URL smartobject-0.1.15.tar.gz
Size 15.2 kB
Tags Source
SHA-256 checksum
How to use checksums
54648e1ad140a04e2981ee6f694d3928164ae1816b4518aef20f40af421fa6a3
BLAKE2b-256 checksum
How to use checksums
82af15b38d2bc378defa6239b5648d6598c2647b1124945fdda3d47c43cc29f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.1.15 This release

1 release file

0.1.14

1 release file

0.1.13

1 release file

0.1.12

1 release file

0.1.11

1 release file

0.1.10

1 release file

0.1.9

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page