A sober python base library
Project description
A sober python base library for python 3. (Full Documentation)
A Generic Serializable Object
The primary product of this module is a base python object class designed for easy serialization. JSON serialization and de-serialization come for free for attributes of (most of the) core python object types.
A Basic Class:
from amethyst.core import Object, Attr
class MyObject(Object):
foo = Attr(int)
bar = Attr(isa=str).strip()
myobj = MyObject(foo="23")
print(myobj.foo + 1) # => 24
Validation / Coersion
class MyObject(Object):
amethyst_verifyclass = False # don't check json for class name
foo = Attr(int) # coerce to int
bar = Attr(isa=str).strip() # ensure str then strip whitespace
Validated
constructors
myobj = MyObject({ "foo": "23", "bar": "Hello " }) myobj = MyObject(foo="23", bar="Hello ") print(isinstance(myobj.foo, int)) # True print(myobj.bar) # "Hello"
assignment
myobj["foo"] = "23" # Converts to int myobj["foo"] = "Not an int" # Raises exception myobj.foo = "23" # Converts to int myobj.foo = "Not an int" # Raises exception
set and update methods
myobj.set("foo", value) # Convert to int or raise exception myobj.update(foo=value) # Convert to int or raise exception myobj.setdefault("foo", value) # Convert or raise only if foo unset
loading fresh from dict or json
# Converts and trims myobj.load_data({"foo": "23", "bar": "Hello "}) # Converts and trims myobj = MyObject.newFromJSON('{"foo": "23", "bar": "Hello "}') myobj.fromJSON('{"foo": "23", "bar": "Hello "}')
Not Validated
myobj.direct_set("foo", "Not an int") # DANGER: Not an exception! myobj.direct_update(foo="Not an int") # DANGER: Not an exception!
Serialization
JSON text can be produced and loaded, even for nested objects.
json_string = myobj.toJSON()
myobj2 = MyObject.newFromJSON(json_string)
Other serialization libraries can easily be used as well.
yaml_string = yaml.dump(myobj.deflate_data())
myobj2 = MyObject.inflate_new(yaml.safe_load(yaml_string))
By default the JSON serializer injects type hints to ensure that objects are de-serialized into the correct class:
# print(MyObject(foo=23, bar="plugh").toJSON())
{"__class__": "__mymodule.MyObject__", "foo": 23, "bar": "plugh"}
When building an object from JSON, the constructor will look for these hints and raise a ValueError if the type hint is missing or imported into the wrong class.
# These raise ValueError
MyObject.newFromJSON('{"foo":23, "bar":"plugh"}')
MyObject.newFromJSON('{"__class__": "__mymodule.MyOtherObject__", "foo":23}')
Class verification can be skipped by passing verifyclass=False to the loader.
myobj = MyObject.newFromJSON('{"foo":23, "bar":"plugh"}', verifyclass=False)
If you want no munging or class verification at all, set the class parameters:
class MyObject(Object):
amethyst_includeclass = False
amethyst_verifyclass = False
foo = Attr(int)
bar = Attr(isa=str).strip()
# No extra class info due to modified defaults:
myobj = MyObject.newFromJSON('{"foo":"23", "bar":"plugh"}')
print(myobj.toJSON())
# => { "foo": 23, "bar": "plugh" }
Ecosystem integration
Works with sqlite3.Row objects:
import sqlite3
conn = sqlite3.connect(myfile)
conn.row_factory = sqlite3.Row
for row in conn.execute('SELECT * FROM mytable')
obj = MyObject(row)
...
Works with six.iteritems():
import six
for k, v in six.iteritems(myobj):
...
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
Built Distribution
File details
Details for the file amethyst-core-0.9.0.tar.gz
.
File metadata
- Download URL: amethyst-core-0.9.0.tar.gz
- Upload date:
- Size: 37.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f57086e0c535a120b30c3e81ac0f3816aadc323bf6de5e5e21c0b6ff9a5bcd30 |
|
MD5 | ea48db480891955b8473e029ced59b36 |
|
BLAKE2b-256 | 1f84f7f907096ac7a76be1a68f6b38da11b201a213cddc9963a09f6d41e99025 |
File details
Details for the file amethyst_core-0.9.0-py3-none-any.whl
.
File metadata
- Download URL: amethyst_core-0.9.0-py3-none-any.whl
- Upload date:
- Size: 24.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7584074a8162a02d6b0e1f91bd31f9760c8b37e4e23c8d391481f8c26a4acc22 |
|
MD5 | 66164e741568667c71f2af456f82e1ca |
|
BLAKE2b-256 | 4b4f5d23518985a72a190b95ad8676003913082b5aff475804161a3787125611 |