jazzband / jsonmodels

jsonmodels is library to make it easier for you to deal with structures that are converted to, or read from JSON.
http://jsonmodels.readthedocs.org/en/latest/
BSD 3-Clause "New" or "Revised" License
334 stars 51 forks source link

Refactor some code for understandability #130

Open micahjsmith opened 5 years ago

micahjsmith commented 5 years ago

Hello, I spent a fair amount of time reading through the code of jsonmodels to understand how it worked. It employs a fair amount of interesting python constructs, like __get__ and __set__ methods of BaseField and a healthy use of metaclasses and class methods.

To help myself understand what was going on, I found myself making small changes to the code for readability and interpretability. This includes

new

for attr_name in dir(cls): field = getattr(cls, attr_name)


- try to simplify logic of field initialization by adding an `_initialized` attribute to `BaseField` and guarding against re-initialization
- make `Base.get_field` a classmethod, which avoids a linear search through the entire set of fields on each access

I thought other people might benefit from these small changes. Hope this is helpful, let me know what you think!
micahjsmith commented 5 years ago

Hi @beregond , any thoughts on this PR?

micahjsmith commented 5 years ago

Hi @beregond any thoughts on this PR?