jams.JObject

class jams.JObject(**kwargs)[source]

Bases: object

Dict-like object for JSON Serialization.

This object behaves like a dictionary to allow init-level attribute names, seamless JSON-serialization, and double-star style unpacking (** obj).

By setting the type attribute to a defined schema entry, only the fields allowed by the schema are permitted as attributes.

__init__(self, **kwargs)[source]

Construct a new JObject

Parameters:
kwargs

Each keyword argument becomes an attribute with the specified value

Examples

>>> J = jams.JObject(foo=5)
>>> J.foo
5
>>> dict(J)
{'foo': 5}

Attributes

type The type (class name) of a derived JObject type

Methods

__init__(self, \*\*kwargs) Construct a new JObject
dumps(self, \*\*kwargs) Serialize the JObject to a string.
keys(self) Return a list of the attributes of the object.
loads(string) De-serialize a JObject
search(self, \*\*kwargs) Query this object (and its descendants).
update(self, \*\*kwargs) Update the attributes of a JObject.
validate(self[, strict]) Validate a JObject against its schema
dumps(self, **kwargs)[source]

Serialize the JObject to a string.

Parameters:
kwargs

Keyword arguments to json.dumps

Returns:
object_str : str

Serialized JObject

See also

json.dumps
loads

Examples

>>> J = jams.JObject(foo=5, bar='baz')
>>> J.dumps()
'{"foo": 5, "bar": "baz"}'
keys(self)[source]

Return a list of the attributes of the object.

Returns:
keys : list

The attributes of the object

Examples

>>> J = jams.JObject(foo=5, bar='baz')
>>> J.keys()
['foo', 'bar']
classmethod loads(string)[source]

De-serialize a JObject

Parameters:
string : str

A serialized (JSON string) JObject

Returns:
J : JObject

The input string reconstructed as a JObject

See also

json.loads
dumps

Examples

>>> J = jams.JObject(foo=5, bar='baz')
>>> J.dumps()
'{"foo": 5, "bar": "baz"}'
>>> jams.JObject.loads(J.dumps())
<JObject foo, bar>
search(self, **kwargs)[source]

Query this object (and its descendants).

Parameters:
kwargs

Each (key, value) pair encodes a search field in key and a target value in value.

key must be a string, and should correspond to a property in the JAMS object hierarchy, e.g., ‘Annotation.namespace` or email

value must be either an object (tested for equality), a string describing a search pattern (regular expression), or a lambda function which evaluates to True if the candidate object matches the search criteria and False otherwise.

Returns:
match : bool

True if any of the search keys match the specified value, False otherwise, or if the search keys do not exist within the object.

Examples

>>> J = jams.JObject(foo=5, needle='quick brown fox')
>>> J.search(needle='.*brown.*')
True
>>> J.search(needle='.*orange.*')
False
>>> J.search(badger='.*brown.*')
False
>>> J.search(foo=5)
True
>>> J.search(foo=10)
False
>>> J.search(foo=lambda x: x < 10)
True
>>> J.search(foo=lambda x: x > 10)
False
type

The type (class name) of a derived JObject type

update(self, **kwargs)[source]

Update the attributes of a JObject.

Parameters:
kwargs

Keyword arguments of the form attribute=new_value

Examples

>>> J = jams.JObject(foo=5)
>>> J.dumps()
'{"foo": 5}'
>>> J.update(bar='baz')
>>> J.dumps()
'{"foo": 5, "bar": "baz"}'
validate(self, strict=True)[source]

Validate a JObject against its schema

Parameters:
strict : bool

Enforce strict schema validation

Returns:
valid : bool

True if the jam validates False if not, and strict==False

Raises:
SchemaError

If strict==True and jam fails validation