| Name | db-classes JSON |
| Version |
1.0.0
JSON |
| download |
| home_page | https://github.com/Tesla2000/db_attrs |
| Summary | Highly serializable implementation of dataclass ment to work with databases |
| upload_time | 2023-12-03 08:53:43 |
| maintainer | |
| docs_url | None |
| author | Tesla2000 |
| requires_python | >=3.11 |
| license | |
| keywords |
|
| VCS |
 |
| bugtrack_url |
|
| requirements |
No requirements were recorded.
|
| Travis-CI |
No Travis.
|
| coveralls test coverage |
No coveralls.
|
# DbAttrs module for automatic serialization and deserialization
DbAttrs is an unofficial extension to [attrs](https://www.attrs.org/) 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 `db_attrs` package using pip:
```bash
pip install db_attrs
```
### Example
Here's an example of how to use the serialization and deserialization with `DbClass`:
```python
@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`:
```python
@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
```python
@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)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/Tesla2000/db_attrs",
"name": "db-classes",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.11",
"maintainer_email": "",
"keywords": "",
"author": "Tesla2000",
"author_email": "fratajczak124@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/5a/52/aa316389ea014d942fa6ca3b2c13759189ea98be8e1fe33b91b31e5ee121/db-classes-1.0.0.tar.gz",
"platform": null,
"description": "# DbAttrs module for automatic serialization and deserialization\n\nDbAttrs is an unofficial extension to [attrs](https://www.attrs.org/) library. \nThe module is ment to be used in conjunction with databases or json which may require serialization and deserialization.\nTo allow creation of custom Object-Database mapping DbAttrs supports basic datatype such as ints, uints, vars etc.\n\n## Installation\n\nYou can install the `db_attrs` package using pip:\n\n```bash\npip install db_attrs\n```\n\n### Example\n\nHere's an example of how to use the serialization and deserialization with `DbClass`:\n\n```python\n@define\nclass Bar(DbClass):\n dictionary: dict\n date: datetime\n decimal: Decimal\n\n@define\nclass Foo(DbClass):\n dictionary: dict\n date: datetime\n decimal: Decimal\n bar: Bar\n\nfoo = Foo({}, datetime.now(), Decimal(1), Bar({}, datetime.now(), Decimal(1)))\nserialized = foo.get_db_representation()\nfoo.bar = foo.bar._id\ntry:\n json.dump(serialized, sys.stdout)\nexcept:\n assert False\ndeserialized = Foo.from_dict(serialized)\nassert deserialized == foo\n```\n\nHere's an example of how to use the serialization and deserialization with `DbClassLiteral`:\n\n```python\n@define\nclass Bar(DbClassLiteral):\n dictionary: dict\n date: datetime\n decimal: Decimal\n\n@define\nclass Foo(DbClass):\n dictionary: dict\n date: datetime\n decimal: Decimal\n bar: Bar\n\nfoo = Foo({}, datetime.now(), Decimal(1), Bar({}, datetime.now(), Decimal(1)))\nserialized = foo.get_db_representation()\ntry:\n json.dump(serialized, sys.stdout)\nexcept:\n assert False\ndeserialized = Foo.from_dict(serialized)\nassert deserialized == foo\n```\n\nYou can make use of db_types the following way\n\n```python\n@define\nclass Foo(DbClass):\n a: int = int8()\n b: int = uint16()\n c: str = varchar(7)\n d: str = text()\n\n\nclass TestFooClass(unittest.TestCase):\n def setUp(self):\n self.foo_instance = Foo(0, 0, '', '')\n\n def test_attribute_a(self):\n with self.assertRaises(ValueError):\n self.foo_instance.a = -129 # Below int8 range\n with self.assertRaises(ValueError):\n self.foo_instance.a = 128 # Above int8 range\n\n def test_attribute_b_out_of_range(self):\n with self.assertRaises(ValueError):\n self.foo_instance.b = -1 # Below uint16 range\n with self.assertRaises(ValueError):\n self.foo_instance.b = 65536 # Above uint16 range\n\n def test_attribute_c_out_of_range(self):\n with self.assertRaises(ValueError):\n self.foo_instance.c = \"Too bigg\"\n\n def test_attribute_d_positive(self):\n passed_text = \"\"\"I'm the Scatman\nSki-bi dibby dib yo da dub dub\nYo da dub dub\nSki-bi dibby dib yo da dub dub\nYo da dub dub\n(I'm the Scatman)\nSki-bi dibby dib yo da dub dub\nYo da dub dub\nSki-bi dibby dib yo da dub dub\nYo da dub dub\nBa-da-ba-da-ba-be bop bop bodda bope\nBop ba bodda bope\nBe bop ba bodda bope\nBop ba bodda\nBa-da-ba-da-ba-be bop ba bodda bope\nBop ba bodda bope\nBe bop ba bodda bope\nBop ba bodda bope\"\"\"\n self.foo_instance.d = passed_text\n self.assertEqual(self.foo_instance.d, passed_text)\n```\n",
"bugtrack_url": null,
"license": "",
"summary": "Highly serializable implementation of dataclass ment to work with databases",
"version": "1.0.0",
"project_urls": {
"Homepage": "https://github.com/Tesla2000/db_attrs"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4e212ba1776a17979dcd7d000b7f59c74613c89f41693783116a5296e7622fa4",
"md5": "f0621b22c2aba387c5b1970a2b3ef76f",
"sha256": "a6972f1e36348c2cb4635ab5e8e6a9035d24629aaf5a25e245d7daafe91ca30b"
},
"downloads": -1,
"filename": "db_classes-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f0621b22c2aba387c5b1970a2b3ef76f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.11",
"size": 11073,
"upload_time": "2023-12-03T08:53:41",
"upload_time_iso_8601": "2023-12-03T08:53:41.285687Z",
"url": "https://files.pythonhosted.org/packages/4e/21/2ba1776a17979dcd7d000b7f59c74613c89f41693783116a5296e7622fa4/db_classes-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "5a52aa316389ea014d942fa6ca3b2c13759189ea98be8e1fe33b91b31e5ee121",
"md5": "cacb2297ba89f67ce08e3b1385789550",
"sha256": "2f37a7f895491c787e8387064416c9aaadb941be594cbf3038c52a8c2e31fafd"
},
"downloads": -1,
"filename": "db-classes-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "cacb2297ba89f67ce08e3b1385789550",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11",
"size": 7791,
"upload_time": "2023-12-03T08:53:43",
"upload_time_iso_8601": "2023-12-03T08:53:43.044777Z",
"url": "https://files.pythonhosted.org/packages/5a/52/aa316389ea014d942fa6ca3b2c13759189ea98be8e1fe33b91b31e5ee121/db-classes-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-03 08:53:43",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Tesla2000",
"github_project": "db_attrs",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "db-classes"
}