0

I want to know if there is a way to access the objects created using any Colander Model class using the dot operator.
Example:

class Image(colander.MappingSchema):
    url = colander.SchemaNode(colander.String())
    width = colander.SchemaNode(colander.Int())
    height = colander.SchemaNode(colander.Int())

So, using this model, if I deserialize a json string,

image = Image.deserialize("{'url':'xyz', 'width':10, 'height':12}")

I want to access the model attributes of Image using dot ( . ) operator.

Like,

image.url
image.width
image.height

And these attributes should be available as IDE code completion suggestion once its accessible using dot operator.
The purpose of this is to help clients easily get the model attributes without looking into the model.

timekeeper
  • 698
  • 15
  • 37

1 Answers1

0

I think you might have misunderstood, what deserialize does here. Also, your code example looks wrong to me as deserialise expects a dictionary like object not a string.

However, to answer your initial question, the answer is no you can't use the dot operator. The reason for this is that deserializing an input, actually returns you a dictionary not an object of type Image. See colander's documentation about deserialization

So taking your example and correcting it you would have something like this:

class Image(colander.MappingSchema):
    url = colander.SchemaNode(colander.String())
    width = colander.SchemaNode(colander.Int())
    height = colander.SchemaNode(colander.Int())

image = Image().deserialize({'url':'xyz', 'width':'10', 'height':'12'})

Would give you the following if you type and printed the variable image

>>> type(image)
dict
>>> print(image)
{
    'url': 'xyz',
    'height': 12,
    'width': 10
}

Note the casting of the string numerical values to integers.

Mr-F
  • 871
  • 7
  • 12