amethyst-core
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 exceptionset 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 unsetloading 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):
...
Metadata
Release files for amethyst-core 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| amethyst_core-1.1.0.tar.gz | 37.2 kB | Without provenance | Details for amethyst_core-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| amethyst_core-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for amethyst_core-1.1.0-py3-none-any.whl |
Total release size: 61.4 kB
Release files / amethyst_core-1.1.0.tar.gz
| Download URL | amethyst_core-1.1.0.tar.gz |
|---|---|
| Size | 37.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3836e341a0d0a149d16c411d198597baa0d3663e7ff4547e4cfdb5480bd9ac53
|
|
BLAKE2b-256 checksum How to use checksums |
963646f686d26aeb36f9e63eeb267eecad34218ee90daf1018e7d8bc81923ebd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.5
|
Release files / amethyst_core-1.1.0-py3-none-any.whl
| Download URL | amethyst_core-1.1.0-py3-none-any.whl |
|---|---|
| Size | 24.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
402bcee14d7cb5984fd6c7dc6165f1deaa6e73f0b108b41d0ffa95f2f398dc9f
|
|
BLAKE2b-256 checksum How to use checksums |
df0d79898fc07766e639f127e76a7bc8ff41ffacda20881cd5066f2a3cc3f6ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.5
|
Security for amethyst-core 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.