jmcarp / betfair.py

A Python wrapper for the Betfair API
MIT License
103 stars 76 forks source link

Stop inflecting on each serializtion #48

Closed petedmarsh closed 9 years ago

petedmarsh commented 9 years ago

The attributes on the models are in snake case to conform to Python conventions, however the keys they map to in JSON response from Betfair are in camel case, e.g.

class Event(BetfairModel):
    ...
    country_code = StringType()
    ...

is serialied to and from JSON like so:

{"countryCode": "GB"}

Previously the names of the attributes were converted to and from camel case on each serilization and deserialization using the inflection libary. This is relatively slow and lots of redundant calls were made as one request could contain multiple attributes with the same name and each one was camelized separately.

The schematics library supports mapping attributes to and from keys with different names, this is done by specifying the serialized_name and deserialize_from parameters of attributes. Specifying these attribute for every existing attribute would be cumersome and error prone. Also, any new model in future would have to make sure to specify them too.

This adds a meta class to BetfairModel which sets the serialized_name and deserialized from attributes (if not already specified) of class attributes. Any BetfairModel attribute now can be specified in snake case but will automatically be mapped to the correct camel case key. This happens once per attribute per class, saving many calls to inflection and speeding up serializating and deserialization.

petedmarsh commented 9 years ago

One thing to consider would just be specifying the serialized_name and deserialize_from parameters on all the model attributes that need it. That would avoid the need for the meta class and still saves the calls to inflection.

jmcarp commented 9 years ago

Much appreciated!