Attributes

Attribute types used for the model.

The types system provides a mechanism for serializing/un the data to/from JSON structures and for capturing additional information about the model attributes.

class stravalib.attributes.Attribute(type_, resource_states=None, units=None)[source]

Bases: object

Base descriptor class for a Strava model attribute.

property type
marshal(v)[source]

Turn this value into format for wire (JSON).

(By default this will just return the underlying object; subclasses can override for specific behaviors – e.g. date formatting.)

unmarshal(v)[source]

Convert the value from parsed JSON structure to native python representation.

By default this will leave the value as-is since the JSON parsing routines typically convert to native types. The exception may be date strings or other more complex types, where subclasses will override this behavior.

class stravalib.attributes.DateAttribute(resource_states=None)[source]

Bases: stravalib.attributes.Attribute

marshal(v)[source]
Parameters

v (date) – The date object to convert.

Returns

unmarshal(v)[source]

Convert a date in “2012-12-13” format to a datetime.date object.

class stravalib.attributes.TimestampAttribute(resource_states=None, tzinfo=<UTC>)[source]

Bases: stravalib.attributes.Attribute

marshal(v)[source]

Serialize the timestamp to string.

Parameters

v (datetime) – The timestamp.

Returns

The serialized date time.

unmarshal(v)[source]

Convert a timestamp in “2012-12-13T03:43:19Z” format to a datetime.datetime object.

class stravalib.attributes.LatLon(lat, lon)

Bases: tuple

lat

Alias for field number 0

lon

Alias for field number 1

class stravalib.attributes.LocationAttribute(resource_states=None)[source]

Bases: stravalib.attributes.Attribute

marshal(v)[source]

Turn this value into format for wire (JSON).

Parameters

v (LatLon) – The lat/lon.

Returns

Serialized format.

Return type

str

unmarshal(v)[source]
class stravalib.attributes.TimezoneAttribute(resource_states=None)[source]

Bases: stravalib.attributes.Attribute

unmarshal(v)[source]

Convert a timestamp in format “America/Los_Angeles” or “(GMT-08:00) America/Los_Angeles” to a pytz.timestamp object.

marshal(v)[source]

Serialize time zone name.

Parameters

v (tzdata) – The timezone.

Returns

The name of the time zone.

class stravalib.attributes.TimeIntervalAttribute(resource_states=None)[source]

Bases: stravalib.attributes.Attribute

Handles time durations, assumes upstream int value in seconds.

unmarshal(v)[source]

Convert the value from parsed JSON structure to native python representation.

By default this will leave the value as-is since the JSON parsing routines typically convert to native types. The exception may be date strings or other more complex types, where subclasses will override this behavior.

marshal(v)[source]

Serialize native python timedelta object to seconds as int.

Parameters

v (timedelta) – time interval.

Returns

time interval in seconds as int.

class stravalib.attributes.ChoicesAttribute(*args, **kwargs)[source]

Bases: stravalib.attributes.Attribute

Attribute where there are several choices the attribute may take.

Allows conversion from the API value to a more helpful python value.

marshal(v)[source]

Turn this value into API format.

Do a reverse dictionary lookup on choices to find the original value. If there are no keys or too many keys for now we raise a NotImplementedError as marshal is not used anywhere currently. In the future we will want to fail gracefully.

unmarshal(v)[source]

Convert the value from Strava API format to useful python representation.

If the value does not appear in the choices attribute we log an error rather than raising an exception as this may be caused by a change to the API upstream so we want to fail gracefully.

class stravalib.attributes.EntityAttribute(*args, **kwargs)[source]

Bases: stravalib.attributes.Attribute

Attribute for another entity.

property type
marshal(v)[source]

Turn an entity into a dictionary.

Parameters

v (stravalib.model.BaseEntity) – The entity to serialize.

Returns

Dictionary of attributes

Return type

Dict[str, Any]

unmarshal(value, bind_client=None)[source]

Cast the specified value to the entity type.

class stravalib.attributes.EntityCollection(*args, **kwargs)[source]

Bases: stravalib.attributes.EntityAttribute

marshal(values)[source]

Turn a list of entities into a list of dictionaries.

Parameters

values (List[stravalib.model.BaseEntity]) – The entities to serialize.

Returns

List of dictionaries of attributes

Return type

List[Dict[str, Any]]

unmarshal(values, bind_client=None)[source]

Cast the list.