A sober python base library
Project description
A sober python base library for python 2.7 or 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( dict(foo=23) )
print(myobj.foo) # => 23
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
dictionary constructor
myobj = MyObject({ "foo": "23", "bar": "Hello " }) print(isinstance(myobj.foo, int)) # True print(myobj.bar) # "Hello"
set and load_data methods
myobj.setdefault("foo", "Not an int") # Raises exception if foo unset myobj.set("foo", "Not an int") # Raises exception # Converts and trims myobj.load_data({"foo": "23", "bar": "Hello "})
loading from JSON
# Converts and trims myobj = MyObject.newFromJSON('{"foo": "23", "bar": "Hello "}') myobj.fromJSON('{"foo": "23", "bar": "Hello "}')
Not Validated
kwargs constructor
myobj = MyObject(foo="23", bar="Hello ") print(isinstance(myobj.foo, int)) # False print(myobj.bar) # "Hello "
assignment
myobj.foo = "Not an int" # Not an exception! myobj["foo"] = "Not an int" # Not an exception!
update method
myobj.update(foo="Not an int") # Not an exception!
Serialization
We immediately get instantiation and loading from JSON or from vanilla dictionaries:
myobj = MyObject.newFromJSON(
'{"foo":23, "bar":" plugh "}',
verifyclass=False
)
print(myobj.bar) # => "plugh" (spaces stripped)
JSON gets some special treatment, but anything that produces an appropriate dictionary will work for serialization.
myobj = MyObject()
myobj.load_data(yaml.load(open("myobject.yaml")))
By default, import and export try to make sure that the object is a serialization of the correct type of object. Metadata are automatically, injected into the serialization to identify the proper type fo the data. This can be disabled on a per-call basis as seen above with the verifyclass keyword argument, or on a per-class basis by setting some attributes.
This metadata can be encoded in two different ways depending on what you find most convenient for your situation (the “flat” style is the default):
myobj = MyObject(foo=23)
print(myobj.toJSON()) # The default, style="flat"
# => {"__class__": "__mymodule.MyObject__", "foo": 23}
print(myobj.toJSON(style="single-key"))
# => {"__mymodule.MyObject__": {"foo": 23}}
print(myobj.toJSON(includeclass=False))
# => { "foo": 23 }
If you want no munging or class verification at all, set 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" }
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 Distributions
Hashes for amethyst_core-0.6.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 299ccf9853ab08b0e5c7c7b87631557f65e79c840313496b440ddee109ee0123 |
|
MD5 | b08778f4591982d244b12a25a2c9be94 |
|
BLAKE2b-256 | 567de46e78e524d1532d68b3537c44ca81afca45cb3d8f8c94ecbe8199d242bf |
Hashes for amethyst_core-0.6.4-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d03c3498ad0dd94cea28fa86b5606fcdabda95eec032eb1f3fc5ed85bab5e030 |
|
MD5 | 92cce4af54357e7a3598219d772985a1 |
|
BLAKE2b-256 | 2efe3468412a6c544dceb7abb9520e5c5be107b3c26146703fcc820de31fc70a |