Skip to main content

Unofficial extension of attrs ment to provided default serialization and deserialization options

Project description

SeriArrts module for automatic serialization and deserialization

SeriArrts is an unofficial extension to attrs library. The module is ment to be used in conjunction with databases or json which may require serialization and deserialization. To allow creation of custom Object-Database mapping DbAttrs supports basic datatype such as ints, uints, vars etc.

Installation

You can install the seriattrs package using pip:

pip install seriattrs

Example

Here's an example of how to use the serialization and deserialization with SeriArrts:

@define
class Bar(DbClass):
    dictionary: dict
    date: datetime
    decimal: Decimal

@define
class Foo(DbClass):
    dictionary: dict
    date: datetime
    decimal: Decimal
    bar: Bar

foo = Foo({}, datetime.now(), Decimal(1), Bar({}, datetime.now(), Decimal(1)))
serialized = foo.get_db_representation()
foo.bar = foo.bar._id
try:
    json.dump(serialized, sys.stdout)
except:
    assert False
deserialized = Foo.from_dict(serialized)
assert deserialized == foo

Here's an example of how to use the serialization and deserialization with DbClassLiteral:

@define
class Bar(DbClassLiteral):
    dictionary: dict
    date: datetime
    decimal: Decimal

@define
class Foo(DbClass):
    dictionary: dict
    date: datetime
    decimal: Decimal
    bar: Bar

foo = Foo({}, datetime.now(), Decimal(1), Bar({}, datetime.now(), Decimal(1)))
serialized = foo.get_db_representation()
try:
    json.dump(serialized, sys.stdout)
except:
    assert False
deserialized = Foo.from_dict(serialized)
assert deserialized == foo

You can make use of db_types the following way

@define
class Foo(DbClass):
    a: int = int8()
    b: int = uint16()
    c: str = varchar(7)
    d: str = text()


class TestFooClass(unittest.TestCase):
    def setUp(self):
        self.foo_instance = Foo(0, 0, '', '')

    def test_attribute_a(self):
        with self.assertRaises(ValueError):
            self.foo_instance.a = -129  # Below int8 range
        with self.assertRaises(ValueError):
            self.foo_instance.a = 128  # Above int8 range

    def test_attribute_b_out_of_range(self):
        with self.assertRaises(ValueError):
            self.foo_instance.b = -1  # Below uint16 range
        with self.assertRaises(ValueError):
            self.foo_instance.b = 65536  # Above uint16 range

    def test_attribute_c_out_of_range(self):
        with self.assertRaises(ValueError):
            self.foo_instance.c = "Too bigg"

    def test_attribute_d_positive(self):
        passed_text = """I'm the Scatman
Ski-bi dibby dib yo da dub dub
Yo da dub dub
Ski-bi dibby dib yo da dub dub
Yo da dub dub
(I'm the Scatman)
Ski-bi dibby dib yo da dub dub
Yo da dub dub
Ski-bi dibby dib yo da dub dub
Yo da dub dub
Ba-da-ba-da-ba-be bop bop bodda bope
Bop ba bodda bope
Be bop ba bodda bope
Bop ba bodda
Ba-da-ba-da-ba-be bop ba bodda bope
Bop ba bodda bope
Be bop ba bodda bope
Bop ba bodda bope"""
        self.foo_instance.d = passed_text
        self.assertEqual(self.foo_instance.d, passed_text)

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

seriattrs-2.2.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

seriattrs-2.2.0-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file seriattrs-2.2.0.tar.gz.

File metadata

  • Download URL: seriattrs-2.2.0.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for seriattrs-2.2.0.tar.gz
Algorithm Hash digest
SHA256 21bf286226fd62df3c09f443191d309175216efaedffccb99edd2237913b8fba
MD5 a71da3c23d1a416b62550fb26c816f8c
BLAKE2b-256 622196e9238be18fd629e6e782b5eda0b1ef848c0c90c11e70f7b044438fbf66

See more details on using hashes here.

File details

Details for the file seriattrs-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: seriattrs-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for seriattrs-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 02d5b0fec5ccec41603938bbb475a538c571125bbb74cd126d1500c160f1fe24
MD5 3632918d3c44509d95a7bdc9301183f0
BLAKE2b-256 de829b47f4a9f7976c7064f0d4f9546a87f2383312c283df758334c8958120f6

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