genomoncology / related

Nested Object Models in Python with dictionary, YAML, and JSON transformation support
MIT License
198 stars 15 forks source link

@property attributes are missing in serialized output #21

Open GeorgeLubaretsi opened 5 years ago

GeorgeLubaretsi commented 5 years ago

Example:

import related

@related.immutable
class Person(object):
    first_name = related.StringField()
    last_name = related.StringField()

    @property
    def full_name(self):
        return '{0} {1}'.format(self.first_name, self.last_name)

person = Person(first_name='John', last_name='Doe')

print(related.to_dict(person))
# OrderedDict([('first_name', 'John'), ('last_name', 'Doe')])

I think property attributes should be included in a serialized output. Perhaps iterating over the attributes and getting all the values of property instances should be the easiest approach?

imaurer commented 5 years ago

Interesting idea. But automatically putting all properties into the JSON format will probably have some unintended consequences. It might be better if there was something like this:

    @related.property
    def full_name(self):
        return '{0} {1}'.format(self.first_name, self.last_name)

thoughts?